| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "krykov_e_multistep_sad/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | #include "krykov_e_multistep_sad/common/include/common.hpp" | ||
| 8 | |||
| 9 | namespace krykov_e_multistep_sad { | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | |||
| 13 | constexpr double kEps = 1e-4; | ||
| 14 | constexpr int kMaxIter = 1000; | ||
| 15 | |||
| 16 | 41552 | double EvaluateCenter(const Function2D &f, Region &r) { | |
| 17 | 41552 | const double xc = 0.5 * (r.x_min + r.x_max); | |
| 18 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 41552 times.
|
41552 | const double yc = 0.5 * (r.y_min + r.y_max); |
| 19 | 41552 | r.value = f(xc, yc); | |
| 20 | 41552 | return r.value; | |
| 21 | } | ||
| 22 | |||
| 23 | Region SplitRegionX(const Region &r, double xm) { | ||
| 24 | 1024 | return Region{.x_min = r.x_min, .x_max = xm, .y_min = r.y_min, .y_max = r.y_max, .value = 0.0}; | |
| 25 | } | ||
| 26 | |||
| 27 | Region SplitRegionXRight(const Region &r, double xm) { | ||
| 28 | 1024 | return Region{.x_min = xm, .x_max = r.x_max, .y_min = r.y_min, .y_max = r.y_max, .value = 0.0}; | |
| 29 | } | ||
| 30 | |||
| 31 | Region SplitRegionY(const Region &r, double ym) { | ||
| 32 | 1064 | return Region{.x_min = r.x_min, .x_max = r.x_max, .y_min = r.y_min, .y_max = ym, .value = 0.0}; | |
| 33 | } | ||
| 34 | |||
| 35 | Region SplitRegionYTop(const Region &r, double ym) { | ||
| 36 | 1064 | return Region{.x_min = r.x_min, .x_max = r.x_max, .y_min = ym, .y_max = r.y_max, .value = 0.0}; | |
| 37 | } | ||
| 38 | |||
| 39 | } // namespace | ||
| 40 | |||
| 41 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | KrykovEMultistepSADSEQ::KrykovEMultistepSADSEQ(const InType &in) { |
| 42 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 43 | GetInput() = in; | ||
| 44 | 64 | } | |
| 45 | |||
| 46 |
1/2✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
|
64 | bool KrykovEMultistepSADSEQ::ValidationImpl() { |
| 47 | const auto &[f, x1, x2, y1, y2] = GetInput(); | ||
| 48 |
3/6✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 64 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 64 times.
|
64 | return static_cast<bool>(f) && x1 < x2 && y1 < y2; |
| 49 | } | ||
| 50 | |||
| 51 | 64 | bool KrykovEMultistepSADSEQ::PreProcessingImpl() { | |
| 52 | 64 | return true; | |
| 53 | } | ||
| 54 | |||
| 55 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | bool KrykovEMultistepSADSEQ::RunImpl() { |
| 56 | const auto &[f, x_min, x_max, y_min, y_max] = GetInput(); | ||
| 57 | |||
| 58 | 64 | std::vector<Region> regions; | |
| 59 |
2/4✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 64 times.
✗ Branch 5 not taken.
|
64 | regions.push_back(Region{.x_min = x_min, .x_max = x_max, .y_min = y_min, .y_max = y_max, .value = 0.0}); |
| 60 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | EvaluateCenter(f, regions.front()); |
| 61 | |||
| 62 |
1/2✓ Branch 0 taken 2152 times.
✗ Branch 1 not taken.
|
2152 | for (int iter = 0; iter < kMaxIter; ++iter) { |
| 63 |
2/2✓ Branch 0 taken 37312 times.
✓ Branch 1 taken 2152 times.
|
39464 | for (auto &r : regions) { |
| 64 |
1/2✓ Branch 1 taken 37312 times.
✗ Branch 2 not taken.
|
37312 | EvaluateCenter(f, r); |
| 65 | } | ||
| 66 | |||
| 67 | auto best_it = std::ranges::min_element(regions, {}, &Region::value); | ||
| 68 |
2/2✓ Branch 0 taken 648 times.
✓ Branch 1 taken 1504 times.
|
2152 | Region best = *best_it; |
| 69 | regions.erase(best_it); | ||
| 70 | |||
| 71 | 2152 | double dx = best.x_max - best.x_min; | |
| 72 | 2152 | double dy = best.y_max - best.y_min; | |
| 73 | |||
| 74 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 2088 times.
|
2152 | if (std::max(dx, dy) < kEps) { |
| 75 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | regions.push_back(best); |
| 76 | 64 | break; | |
| 77 | } | ||
| 78 | |||
| 79 |
2/2✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 1064 times.
|
2088 | if (dx >= dy) { |
| 80 | 1024 | double xm = 0.5 * (best.x_min + best.x_max); | |
| 81 | Region r1 = SplitRegionX(best, xm); | ||
| 82 | Region r2 = SplitRegionXRight(best, xm); | ||
| 83 |
1/2✓ Branch 1 taken 1024 times.
✗ Branch 2 not taken.
|
1024 | EvaluateCenter(f, r1); |
| 84 |
1/2✓ Branch 1 taken 1024 times.
✗ Branch 2 not taken.
|
1024 | EvaluateCenter(f, r2); |
| 85 |
1/2✓ Branch 1 taken 1024 times.
✗ Branch 2 not taken.
|
1024 | regions.push_back(r1); |
| 86 |
1/2✓ Branch 1 taken 1024 times.
✗ Branch 2 not taken.
|
1024 | regions.push_back(r2); |
| 87 | } else { | ||
| 88 | 1064 | double ym = 0.5 * (best.y_min + best.y_max); | |
| 89 | Region r1 = SplitRegionY(best, ym); | ||
| 90 | Region r2 = SplitRegionYTop(best, ym); | ||
| 91 |
1/2✓ Branch 1 taken 1064 times.
✗ Branch 2 not taken.
|
1064 | EvaluateCenter(f, r1); |
| 92 |
1/2✓ Branch 1 taken 1064 times.
✗ Branch 2 not taken.
|
1064 | EvaluateCenter(f, r2); |
| 93 |
1/2✓ Branch 1 taken 1064 times.
✗ Branch 2 not taken.
|
1064 | regions.push_back(r1); |
| 94 |
1/2✓ Branch 1 taken 1064 times.
✗ Branch 2 not taken.
|
1064 | regions.push_back(r2); |
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | const auto &best = *std::ranges::min_element(regions, {}, &Region::value); | ||
| 99 | 64 | double x = 0.5 * (best.x_min + best.x_max); | |
| 100 | 64 | double y = 0.5 * (best.y_min + best.y_max); | |
| 101 |
2/6✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 64 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
|
128 | GetOutput() = {x, y, best.value}; |
| 102 | |||
| 103 | 64 | return true; | |
| 104 | } | ||
| 105 | |||
| 106 | 64 | bool KrykovEMultistepSADSEQ::PostProcessingImpl() { | |
| 107 | 64 | return true; | |
| 108 | } | ||
| 109 | |||
| 110 | } // namespace krykov_e_multistep_sad | ||
| 111 |