| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "gonozov_l_global_search/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <algorithm> | ||
| 4 | #include <cmath> | ||
| 5 | #include <functional> | ||
| 6 | #include <limits> | ||
| 7 | #include <tuple> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | #include "gonozov_l_global_search/common/include/common.hpp" | ||
| 11 | |||
| 12 | namespace gonozov_l_global_search { | ||
| 13 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | GonozovLGlobalSearchSEQ::GonozovLGlobalSearchSEQ(const InType &in) { |
| 14 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 15 | GetInput() = in; | ||
| 16 | 32 | GetOutput() = 0.0; | |
| 17 | 32 | } | |
| 18 | |||
| 19 | 32 | bool GonozovLGlobalSearchSEQ::ValidationImpl() { | |
| 20 |
2/4✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 32 times.
|
32 | return (std::get<1>(GetInput()) > 1.0) && (std::get<2>(GetInput()) < std::get<3>(GetInput())) && |
| 21 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | (std::get<4>(GetInput()) > 0); |
| 22 | } | ||
| 23 | |||
| 24 | 32 | bool GonozovLGlobalSearchSEQ::PreProcessingImpl() { | |
| 25 | 32 | return true; | |
| 26 | } | ||
| 27 | |||
| 28 | namespace { | ||
| 29 | |||
| 30 | 2944 | double CountingM(int t, double &highm, const std::vector<double> &test_sequence, const auto &function) { | |
| 31 |
2/2✓ Branch 0 taken 32 times.
✓ Branch 1 taken 2912 times.
|
2944 | if (highm == -std::numeric_limits<double>::infinity()) { |
| 32 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 32 times.
|
96 | return std::abs((function(test_sequence[1]) - function(test_sequence[0])) / (test_sequence[1] - test_sequence[0])); |
| 33 | } | ||
| 34 |
3/6✗ Branch 0 not taken.
✓ Branch 1 taken 2912 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2912 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2912 times.
|
8736 | double highm1 = std::abs(function(test_sequence.back()) - function(test_sequence[t - 1])) / |
| 35 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2912 times.
|
2912 | (test_sequence.back() - test_sequence[t - 1]); |
| 36 | |||
| 37 | double highm2 = | ||
| 38 |
4/6✗ Branch 0 not taken.
✓ Branch 1 taken 2912 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2912 times.
✓ Branch 4 taken 144 times.
✓ Branch 5 taken 2768 times.
|
8736 | std::abs(function(test_sequence[t]) - function(test_sequence.back())) / (test_sequence[t] - test_sequence.back()); |
| 39 | |||
| 40 | 2912 | double high = std::max(highm, highm1); | |
| 41 | return std::max(high, highm2); | ||
| 42 | } | ||
| 43 | |||
| 44 | 2944 | int Countingt(double m, std::vector<double> &test_sequence, const auto &function) { | |
| 45 | int t = 1; | ||
| 46 | double value_rt = -std::numeric_limits<double>::infinity(); | ||
| 47 |
2/2✓ Branch 0 taken 276176 times.
✓ Branch 1 taken 2944 times.
|
279120 | for (unsigned int i = 1; i < test_sequence.size(); i++) { |
| 48 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 276176 times.
|
276176 | double sub_x = (test_sequence[i] - test_sequence[i - 1]); |
| 49 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 276176 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 276176 times.
|
552352 | double sub_z = (function(test_sequence[i]) - function(test_sequence[i - 1])); |
| 50 |
2/4✗ Branch 0 not taken.
✓ Branch 1 taken 276176 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 276176 times.
|
552352 | double sum_z = (function(test_sequence[i]) + function(test_sequence[i - 1])); |
| 51 | 276176 | double inter_value_rt = (m * sub_x) + (sub_z * sub_z / m / sub_x) - (2 * sum_z); | |
| 52 |
2/2✓ Branch 0 taken 23600 times.
✓ Branch 1 taken 252576 times.
|
276176 | if (inter_value_rt > value_rt) { |
| 53 | value_rt = inter_value_rt; | ||
| 54 | 23600 | t = static_cast<int>(i); | |
| 55 | } | ||
| 56 | } | ||
| 57 | 2944 | return t; | |
| 58 | } | ||
| 59 | |||
| 60 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | void InizialzationStartParameters(std::vector<double> &test_sequence, const std::function<double(double)> &function, |
| 61 | double &global_min_x, double &global_min_value, double a, double b) { | ||
| 62 | test_sequence.push_back(a); | ||
| 63 | test_sequence.push_back(b); | ||
| 64 |
3/4✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 16 times.
|
64 | if (function(b) < global_min_value) { |
| 65 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
|
16 | global_min_x = b; |
| 66 | 16 | global_min_value = function(b); | |
| 67 | } | ||
| 68 | 32 | } | |
| 69 | void FormNewtestSequence(std::vector<double> &test_sequence) { | ||
| 70 | 2944 | std::ranges::sort(test_sequence.begin(), test_sequence.end()); | |
| 71 | } | ||
| 72 | |||
| 73 | void FormNewParameters(double &m, double &highm, double r, int t, const std::vector<double> &test_sequence, | ||
| 74 | const std::function<double(double)> &function) { | ||
| 75 | 5888 | highm = CountingM(t, highm, test_sequence, function); | |
| 76 |
2/2✓ Branch 0 taken 2928 times.
✓ Branch 1 taken 16 times.
|
2944 | m = (highm == 0.0) ? 1.0 : r * highm; |
| 77 | } | ||
| 78 | |||
| 79 | 2944 | void CountingNewCoordinateContinueIteration(bool &continue_iteration, std::vector<double> &test_sequence, int &t, | |
| 80 | const std::function<double(double)> &function, double &global_min_x, | ||
| 81 | double &global_min_value, double m, double epsilon) { | ||
| 82 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2944 times.
|
2944 | double new_elem_sequence = (0.5 * (test_sequence[t] + test_sequence[t - 1])) - |
| 83 |
3/6✗ Branch 0 not taken.
✓ Branch 1 taken 2944 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2944 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 2944 times.
|
8832 | ((0.5 / m) * (function(test_sequence[t]) - function(test_sequence[t - 1]))); |
| 84 |
2/2✓ Branch 0 taken 104 times.
✓ Branch 1 taken 2840 times.
|
2944 | if (function(new_elem_sequence) < global_min_value) { |
| 85 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
|
104 | global_min_x = new_elem_sequence; |
| 86 | 104 | global_min_value = function(new_elem_sequence); | |
| 87 | } | ||
| 88 | test_sequence.push_back(new_elem_sequence); | ||
| 89 | |||
| 90 | 2944 | continue_iteration = abs(test_sequence[t] - test_sequence[t - 1]) > epsilon; | |
| 91 | 2944 | } | |
| 92 | |||
| 93 | } // namespace | ||
| 94 | |||
| 95 | 32 | bool GonozovLGlobalSearchSEQ::RunImpl() { | |
| 96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | std::vector<double> test_sequence; |
| 97 | const std::function<double(double)> &function = std::get<0>(GetInput()); | ||
| 98 | 32 | double r = std::get<1>(GetInput()); | |
| 99 | 32 | double a = std::get<2>(GetInput()); | |
| 100 | 32 | double b = std::get<3>(GetInput()); | |
| 101 | 32 | double epsilon = std::get<4>(GetInput()); | |
| 102 | |||
| 103 | 32 | int t = 1; | |
| 104 | 32 | double highm = -std::numeric_limits<double>::infinity(); | |
| 105 | |||
| 106 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
|
32 | double global_min_x = a; |
| 107 | 32 | double global_min_value = function(a); | |
| 108 | |||
| 109 |
1/2✓ Branch 1 taken 32 times.
✗ Branch 2 not taken.
|
32 | InizialzationStartParameters(test_sequence, function, global_min_x, global_min_value, a, b); |
| 110 | 32 | bool continue_iteration = true; | |
| 111 |
2/2✓ Branch 0 taken 2944 times.
✓ Branch 1 taken 32 times.
|
2976 | while (continue_iteration) { |
| 112 | FormNewtestSequence(test_sequence); | ||
| 113 | |||
| 114 | double m = 0.0; | ||
| 115 |
1/2✓ Branch 1 taken 2944 times.
✗ Branch 2 not taken.
|
2944 | FormNewParameters(m, highm, r, t, test_sequence, function); |
| 116 | |||
| 117 |
1/2✓ Branch 1 taken 2944 times.
✗ Branch 2 not taken.
|
2944 | t = Countingt(m, test_sequence, function); |
| 118 | |||
| 119 |
1/2✓ Branch 1 taken 2944 times.
✗ Branch 2 not taken.
|
2944 | CountingNewCoordinateContinueIteration(continue_iteration, test_sequence, t, function, global_min_x, |
| 120 | global_min_value, m, epsilon); | ||
| 121 | } | ||
| 122 | |||
| 123 |
1/2✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
|
32 | GetOutput() = global_min_x; |
| 124 | |||
| 125 | 32 | return true; | |
| 126 | } | ||
| 127 | |||
| 128 | 32 | bool GonozovLGlobalSearchSEQ::PostProcessingImpl() { | |
| 129 | 32 | return true; | |
| 130 | } | ||
| 131 | |||
| 132 | } // namespace gonozov_l_global_search | ||
| 133 |