| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <functional> | ||
| 6 | #include <limits> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | namespace levonychev_i_multistep_2d_optimization { | ||
| 10 | |||
| 11 | struct Point { | ||
| 12 | double x = 0.0; | ||
| 13 | double y = 0.0; | ||
| 14 | double value = std::numeric_limits<double>::max(); | ||
| 15 | |||
| 16 | 102 | Point() = default; | |
| 17 | 99900 | Point(double x_val, double y_val, double func_value) : x(x_val), y(y_val), value(func_value) {} | |
| 18 | }; | ||
| 19 | |||
| 20 | struct SearchRegion { | ||
| 21 | double x_min = 0.0; | ||
| 22 | double x_max = 0.0; | ||
| 23 | double y_min = 0.0; | ||
| 24 | double y_max = 0.0; | ||
| 25 | |||
| 26 | SearchRegion() = default; | ||
| 27 | SearchRegion(double x_mn, double x_mx, double y_mn, double y_mx) | ||
| 28 |
1/2✓ Branch 1 taken 288 times.
✗ Branch 2 not taken.
|
312 | : x_min(x_mn), x_max(x_mx), y_min(y_mn), y_max(y_mx) {} |
| 29 | }; | ||
| 30 | |||
| 31 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
|
30 | inline Point LocalOptimization(const std::function<double(double, double)> &func, double x_start, double y_start, |
| 32 | double x_min, double x_max, double y_min, double y_max, int max_iterations = 100) { | ||
| 33 | const double epsilon = 1e-6; | ||
| 34 | const double step_size = 0.01; | ||
| 35 | |||
| 36 | double x = x_start; | ||
| 37 | double y = y_start; | ||
| 38 | 30 | double current_value = func(x, y); | |
| 39 | |||
| 40 |
1/2✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
|
34 | for (int iter = 0; iter < max_iterations; ++iter) { |
| 41 | const double h = 1e-5; | ||
| 42 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 34 times.
|
68 | double grad_x = (func(x + h, y) - func(x - h, y)) / (2.0 * h); |
| 43 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 34 times.
|
68 | double grad_y = (func(x, y + h) - func(x, y - h)) / (2.0 * h); |
| 44 | |||
| 45 | 34 | double new_x = x - (step_size * grad_x); | |
| 46 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
|
34 | double new_y = y - (step_size * grad_y); |
| 47 | |||
| 48 | 34 | new_x = std::max(x_min, std::min(x_max, new_x)); | |
| 49 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 34 times.
|
34 | new_y = std::max(y_min, std::min(y_max, new_y)); |
| 50 | |||
| 51 | 34 | double new_value = func(new_x, new_y); | |
| 52 | |||
| 53 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 30 times.
|
34 | if (std::abs(new_value - current_value) < epsilon) { |
| 54 | break; | ||
| 55 | } | ||
| 56 | |||
| 57 | x = new_x; | ||
| 58 | y = new_y; | ||
| 59 | current_value = new_value; | ||
| 60 | } | ||
| 61 | |||
| 62 | 30 | return {x, y, current_value}; | |
| 63 | } | ||
| 64 | |||
| 65 | 120 | inline void SearchInRegion(std::vector<Point> &grid_points, const std::function<double(double, double)> &func, | |
| 66 | const SearchRegion ®ion, int grid_size, int size) { | ||
| 67 | 120 | auto grid_size_double = static_cast<double>(grid_size); | |
| 68 | 120 | auto size_double = static_cast<double>(size); | |
| 69 | |||
| 70 | 120 | double step_x = (region.x_max - region.x_min) / (grid_size_double / size_double - 1.0); | |
| 71 | 120 | double step_y = (region.y_max - region.y_min) / (grid_size_double - 1.0); | |
| 72 | |||
| 73 |
2/2✓ Branch 0 taken 2970 times.
✓ Branch 1 taken 120 times.
|
3090 | for (int i = 0; i < grid_size / size; ++i) { |
| 74 |
2/2✓ Branch 0 taken 99900 times.
✓ Branch 1 taken 2970 times.
|
102870 | for (int j = 0; j < grid_size; ++j) { |
| 75 | 99900 | double x = region.x_min + (i * step_x); | |
| 76 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 99900 times.
|
99900 | double y = region.y_min + (j * step_y); |
| 77 | |||
| 78 | 99900 | double value = func(x, y); | |
| 79 | 99900 | grid_points.emplace_back(x, y, value); | |
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 |
16/22✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 418956 times.
✓ Branch 5 taken 158541 times.
✓ Branch 6 taken 253787 times.
✓ Branch 7 taken 158541 times.
✓ Branch 8 taken 4935 times.
✓ Branch 9 taken 5315 times.
✓ Branch 10 taken 2365 times.
✓ Branch 11 taken 2570 times.
✓ Branch 12 taken 2494 times.
✓ Branch 13 taken 2821 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 215 times.
✓ Branch 17 taken 1585 times.
✓ Branch 18 taken 3782 times.
✓ Branch 19 taken 1585 times.
✓ Branch 20 taken 245367 times.
✓ Branch 21 taken 97980 times.
|
1350589 | std::ranges::sort(grid_points, [](const Point &a, const Point &b) { return a.value < b.value; }); |
| 84 | 120 | } | |
| 85 | |||
| 86 | } // namespace levonychev_i_multistep_2d_optimization | ||
| 87 |