| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "levonychev_i_multistep_2d_optimization/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <limits> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "levonychev_i_multistep_2d_optimization/common/include/common.hpp" | ||
| 9 | #include "levonychev_i_multistep_2d_optimization/common/include/optimization_common.hpp" | ||
| 10 | |||
| 11 | namespace levonychev_i_multistep_2d_optimization { | ||
| 12 | |||
| 13 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | LevonychevIMultistep2dOptimizationSEQ::LevonychevIMultistep2dOptimizationSEQ(const InType &in) { |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | GetInput() = in; |
| 16 | 24 | GetOutput() = OptimizationResult(); | |
| 17 | 24 | } | |
| 18 | |||
| 19 | 24 | bool LevonychevIMultistep2dOptimizationSEQ::ValidationImpl() { | |
| 20 | const auto ¶ms = GetInput(); | ||
| 21 | |||
| 22 |
2/4✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
|
24 | bool is_correct_ranges = (params.x_min < params.x_max) && (params.y_min < params.y_max); |
| 23 | bool isnt_correct_func = !params.func; | ||
| 24 |
3/6✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 24 times.
|
24 | bool is_correct_params = (params.num_steps > 0) && (params.grid_size_step1 > 0) && (params.candidates_per_step > 0); |
| 25 | |||
| 26 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | return is_correct_ranges && !isnt_correct_func && is_correct_params; |
| 27 | } | ||
| 28 | |||
| 29 | 24 | bool LevonychevIMultistep2dOptimizationSEQ::PreProcessingImpl() { | |
| 30 | 24 | GetOutput() = OptimizationResult(); | |
| 31 | 24 | return true; | |
| 32 | } | ||
| 33 | |||
| 34 | 24 | bool LevonychevIMultistep2dOptimizationSEQ::RunImpl() { | |
| 35 | const auto ¶ms = GetInput(); | ||
| 36 | auto &result = GetOutput(); | ||
| 37 | |||
| 38 | 24 | SearchRegion current_region(params.x_min, params.x_max, params.y_min, params.y_max); | |
| 39 | |||
| 40 |
2/2✓ Branch 0 taken 72 times.
✓ Branch 1 taken 24 times.
|
96 | for (int step = 0; step < params.num_steps; ++step) { |
| 41 | 72 | int grid_size = params.grid_size_step1 * (1 << step); | |
| 42 | |||
| 43 | 72 | std::vector<Point> local_points; | |
| 44 | 72 | size_t total_points = static_cast<size_t>(grid_size) * static_cast<size_t>(grid_size); | |
| 45 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
72 | local_points.reserve(total_points); |
| 46 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
72 | SearchInRegion(local_points, params.func, current_region, grid_size, 1); |
| 47 | |||
| 48 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
72 | std::vector<Point> local_candidates; |
| 49 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
72 | int num_local_candidates = std::min(params.candidates_per_step, static_cast<int>(local_points.size())); |
| 50 | 72 | local_candidates.assign(local_points.begin(), local_points.begin() + num_local_candidates); | |
| 51 | |||
| 52 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
72 | std::vector<Point> all_candidates = local_candidates; |
| 53 | |||
| 54 | 72 | std::vector<Point> valid_candidates; | |
| 55 |
2/2✓ Branch 0 taken 288 times.
✓ Branch 1 taken 72 times.
|
360 | for (const auto &cand : all_candidates) { |
| 56 |
1/2✓ Branch 0 taken 288 times.
✗ Branch 1 not taken.
|
288 | if (cand.value < std::numeric_limits<double>::max()) { |
| 57 |
1/2✓ Branch 1 taken 288 times.
✗ Branch 2 not taken.
|
288 | valid_candidates.push_back(cand); |
| 58 | } | ||
| 59 | } | ||
| 60 |
2/22✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 17 taken 216 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 216 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
|
432 | std::ranges::sort(valid_candidates, [](const Point &a, const Point &b) { return a.value < b.value; }); |
| 61 | |||
| 62 |
1/2✓ Branch 1 taken 72 times.
✗ Branch 2 not taken.
|
72 | int num_global = std::min(params.candidates_per_step, static_cast<int>(valid_candidates.size())); |
| 63 | 72 | all_candidates.assign(valid_candidates.begin(), valid_candidates.begin() + num_global); | |
| 64 | |||
| 65 | 72 | std::vector<SearchRegion> new_regions; | |
| 66 |
2/2✓ Branch 0 taken 288 times.
✓ Branch 1 taken 72 times.
|
360 | for (const auto &cand : all_candidates) { |
| 67 | 288 | double margin_x = (params.x_max - params.x_min) * 0.05 / (1 << step); | |
| 68 | 288 | double margin_y = (params.y_max - params.y_min) * 0.05 / (1 << step); | |
| 69 | |||
| 70 | 288 | SearchRegion new_region(std::max(params.x_min, cand.x - margin_x), std::min(params.x_max, cand.x + margin_x), | |
| 71 |
1/2✓ Branch 1 taken 288 times.
✗ Branch 2 not taken.
|
288 | std::max(params.y_min, cand.y - margin_y), std::min(params.y_max, cand.y + margin_y)); |
| 72 |
1/2✓ Branch 1 taken 288 times.
✗ Branch 2 not taken.
|
288 | new_regions.push_back(new_region); |
| 73 | } | ||
| 74 |
1/2✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
|
72 | if (!new_regions.empty()) { |
| 75 | 72 | current_region = new_regions[0]; | |
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | int power_of_2 = 1; | ||
| 80 |
2/2✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
|
72 | for (int i = 0; i < params.num_steps - 1; ++i) { |
| 81 | 48 | power_of_2 *= 2; | |
| 82 | } | ||
| 83 | 24 | int final_grid_size = params.grid_size_step1 * power_of_2; | |
| 84 | 24 | std::vector<Point> local_points; | |
| 85 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | local_points.reserve(static_cast<size_t>(final_grid_size) * static_cast<size_t>(final_grid_size)); |
| 86 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | SearchInRegion(local_points, params.func, current_region, final_grid_size, 1); |
| 87 | |||
| 88 | Point best_point; | ||
| 89 | 24 | best_point = local_points[0]; | |
| 90 |
1/2✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 | if (params.use_local_optimization) { |
| 91 | 24 | best_point = LocalOptimization(params.func, best_point.x, best_point.y, params.x_min, params.x_max, params.y_min, | |
| 92 |
1/2✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
|
24 | params.y_max); |
| 93 | } | ||
| 94 | |||
| 95 | 24 | result.x_min = best_point.x; | |
| 96 | 24 | result.y_min = best_point.y; | |
| 97 | 24 | result.value = best_point.value; | |
| 98 | |||
| 99 | 24 | return true; | |
| 100 | } | ||
| 101 | |||
| 102 | 24 | bool LevonychevIMultistep2dOptimizationSEQ::PostProcessingImpl() { | |
| 103 | const auto ¶ms = GetInput(); | ||
| 104 | auto &result = GetOutput(); | ||
| 105 | |||
| 106 |
3/6✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 24 times.
✗ Branch 5 not taken.
|
24 | return result.x_min >= params.x_min && result.x_min <= params.x_max && result.y_min >= params.y_min && |
| 107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 | result.y_min <= params.y_max; |
| 108 | } | ||
| 109 | |||
| 110 | } // namespace levonychev_i_multistep_2d_optimization | ||
| 111 |