| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "khruev_a_global_opt/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <cstddef> | ||
| 6 | |||
| 7 | #include "khruev_a_global_opt/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace khruev_a_global_opt { | ||
| 10 | |||
| 11 | 32 | KhruevAGlobalOptSEQ::KhruevAGlobalOptSEQ(const InType &in) { | |
| 12 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 13 | 32 | GetInput() = in; | |
| 14 | 32 | } | |
| 15 | |||
| 16 | 32 | bool KhruevAGlobalOptSEQ::ValidationImpl() { | |
| 17 |
3/6✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 32 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 32 times.
|
32 | return GetInput().max_iter > 0 && GetInput().epsilon > 0 && GetInput().r > 1.0; |
| 18 | } | ||
| 19 | |||
| 20 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | bool KhruevAGlobalOptSEQ::PreProcessingImpl() { |
| 21 | trials_.clear(); | ||
| 22 | 32 | return true; | |
| 23 | } | ||
| 24 | |||
| 25 | 3232 | double KhruevAGlobalOptSEQ::CalculateFunction(double t) { | |
| 26 | 3232 | double u = 0.0; | |
| 27 | 3232 | double v = 0.0; | |
| 28 | 3232 | D2xy(t, u, v); // Преобразование Гильберта 1D -> 2D | |
| 29 | |||
| 30 | // Масштабирование из [0,1] в реальную область [ax, bx] | ||
| 31 | 3232 | double real_x = GetInput().ax + (u * (GetInput().bx - GetInput().ax)); | |
| 32 | 3232 | double real_y = GetInput().ay + (v * (GetInput().by - GetInput().ay)); | |
| 33 | |||
| 34 | 3232 | return TargetFunction(GetInput().func_id, real_x, real_y); | |
| 35 | } | ||
| 36 | |||
| 37 | 3232 | void KhruevAGlobalOptSEQ::AddTrial(double t, double z) { | |
| 38 | 3232 | Trial tr{}; | |
| 39 | 3232 | tr.x = t; | |
| 40 | 3232 | tr.z = z; | |
| 41 | 3232 | trials_.push_back(tr); | |
| 42 | // Сортировка по координате x (1) | ||
| 43 |
17/22✓ Branch 0 taken 1368 times.
✓ Branch 1 taken 25352 times.
✓ Branch 2 taken 34216 times.
✓ Branch 3 taken 33424 times.
✓ Branch 4 taken 119120 times.
✓ Branch 5 taken 37216 times.
✓ Branch 6 taken 612432 times.
✓ Branch 7 taken 37216 times.
✓ Branch 8 taken 20936 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 4656 times.
✓ Branch 11 taken 16280 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✓ Branch 16 taken 3712 times.
✓ Branch 17 taken 40928 times.
✓ Branch 18 taken 37264 times.
✓ Branch 19 taken 40928 times.
✓ Branch 20 taken 86264 times.
✓ Branch 21 taken 116960 times.
|
1247336 | std::ranges::sort(trials_, [](const Trial &a, const Trial &b) { return a.x < b.x; }); |
| 44 | 3232 | } | |
| 45 | |||
| 46 | 3168 | double KhruevAGlobalOptSEQ::ComputeM() { | |
| 47 | 3168 | double max_slope = 0.0; | |
| 48 |
2/2✓ Branch 0 taken 158400 times.
✓ Branch 1 taken 3168 times.
|
161568 | for (size_t i = 1; i < trials_.size(); ++i) { |
| 49 | 158400 | double dx = trials_[i].x - trials_[i - 1].x; | |
| 50 |
1/2✓ Branch 0 taken 158400 times.
✗ Branch 1 not taken.
|
158400 | double dz = std::abs(trials_[i].z - trials_[i - 1].z); |
| 51 |
1/2✓ Branch 0 taken 158400 times.
✗ Branch 1 not taken.
|
158400 | if (dx > 1e-15) { |
| 52 | 158400 | max_slope = std::max(max_slope, dz / dx); | |
| 53 | } | ||
| 54 | } | ||
| 55 |
1/2✓ Branch 0 taken 3168 times.
✗ Branch 1 not taken.
|
3168 | return (max_slope > 0) ? GetInput().r * max_slope : 1.0; |
| 56 | } | ||
| 57 | |||
| 58 | 3168 | int KhruevAGlobalOptSEQ::FindBestInterval(double m) const { | |
| 59 | double max_r = -1e18; | ||
| 60 | int best_interval = -1; | ||
| 61 | |||
| 62 |
2/2✓ Branch 0 taken 158400 times.
✓ Branch 1 taken 3168 times.
|
161568 | for (size_t i = 1; i < trials_.size(); ++i) { |
| 63 | 158400 | double dx = trials_[i].x - trials_[i - 1].x; | |
| 64 | 158400 | double z_r = trials_[i].z; | |
| 65 | 158400 | double z_l = trials_[i - 1].z; | |
| 66 | |||
| 67 | // формула (5) | ||
| 68 | 158400 | double r = (m * dx) + (((z_r - z_l) * (z_r - z_l)) / (m * dx)) - (2.0 * (z_r + z_l)); | |
| 69 | |||
| 70 |
2/2✓ Branch 0 taken 17448 times.
✓ Branch 1 taken 140952 times.
|
158400 | if (r > max_r) { |
| 71 | max_r = r; | ||
| 72 | 17448 | best_interval = static_cast<int>(i); | |
| 73 | } | ||
| 74 | } | ||
| 75 | 3168 | return best_interval; | |
| 76 | } | ||
| 77 | |||
| 78 | 3168 | double KhruevAGlobalOptSEQ::GenerateNewX(int best_interval, double m) const { | |
| 79 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3168 times.
|
3168 | double x_r = trials_[best_interval].x; |
| 80 | 3168 | double x_l = trials_[best_interval - 1].x; | |
| 81 | 3168 | double z_r = trials_[best_interval].z; | |
| 82 | 3168 | double z_l = trials_[best_interval - 1].z; | |
| 83 | |||
| 84 | 3168 | double new_x = (0.5 * (x_r + x_l)) - ((z_r - z_l) / (2.0 * m)); | |
| 85 | |||
| 86 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3168 times.
|
3168 | if (new_x < x_l) { |
| 87 | ✗ | new_x = x_l + 1e-9; | |
| 88 | } | ||
| 89 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3168 times.
|
3168 | if (new_x > x_r) { |
| 90 | ✗ | new_x = x_r - 1e-9; | |
| 91 | } | ||
| 92 | 3168 | return new_x; | |
| 93 | } | ||
| 94 | |||
| 95 | 32 | bool KhruevAGlobalOptSEQ::RunImpl() { | |
| 96 | 32 | AddTrial(0.0, CalculateFunction(0.0)); | |
| 97 | 32 | AddTrial(1.0, CalculateFunction(1.0)); | |
| 98 | |||
| 99 | int k = 1; | ||
| 100 | |||
| 101 |
2/2✓ Branch 0 taken 3168 times.
✓ Branch 1 taken 32 times.
|
3200 | for (k = 1; k < GetInput().max_iter; ++k) { |
| 102 | 3168 | double m = ComputeM(); | |
| 103 | |||
| 104 | 3168 | int best_interval = FindBestInterval(m); | |
| 105 | |||
| 106 |
1/2✓ Branch 0 taken 3168 times.
✗ Branch 1 not taken.
|
3168 | if (best_interval == -1) { |
| 107 | break; | ||
| 108 | } | ||
| 109 | |||
| 110 |
1/2✓ Branch 0 taken 3168 times.
✗ Branch 1 not taken.
|
3168 | double dx_best = trials_[best_interval].x - trials_[best_interval - 1].x; |
| 111 |
1/2✓ Branch 0 taken 3168 times.
✗ Branch 1 not taken.
|
3168 | if (dx_best < GetInput().epsilon) { |
| 112 | break; | ||
| 113 | } | ||
| 114 | |||
| 115 | 3168 | double new_x = GenerateNewX(best_interval, m); | |
| 116 | |||
| 117 | 3168 | AddTrial(new_x, CalculateFunction(new_x)); | |
| 118 | } | ||
| 119 | |||
| 120 | // Поиск минимума среди всех точек | ||
| 121 | double min_z = 1e18; | ||
| 122 | double best_t = 0; | ||
| 123 |
2/2✓ Branch 0 taken 3232 times.
✓ Branch 1 taken 32 times.
|
3264 | for (const auto &t : trials_) { |
| 124 |
2/2✓ Branch 0 taken 256 times.
✓ Branch 1 taken 2976 times.
|
3232 | if (t.z < min_z) { |
| 125 | min_z = t.z; | ||
| 126 | 256 | best_t = t.x; | |
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | // Восстанавливаем 2D координаты для ответа | ||
| 131 | 32 | double u = 0.0; | |
| 132 | 32 | double v = 0.0; | |
| 133 | 32 | D2xy(best_t, u, v); | |
| 134 | 32 | result_.x = GetInput().ax + (u * (GetInput().bx - GetInput().ax)); | |
| 135 | 32 | result_.y = GetInput().ay + (v * (GetInput().by - GetInput().ay)); | |
| 136 | 32 | result_.value = min_z; | |
| 137 | 32 | result_.iter_count = k; | |
| 138 | |||
| 139 | 32 | GetOutput() = result_; | |
| 140 | 32 | return true; | |
| 141 | } | ||
| 142 | |||
| 143 | 32 | bool KhruevAGlobalOptSEQ::PostProcessingImpl() { | |
| 144 | 32 | return true; | |
| 145 | } | ||
| 146 | |||
| 147 | } // namespace khruev_a_global_opt | ||
| 148 |