| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "sabirov_s_monte_carlo_seq/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <cmath> | ||
| 4 | #include <cstddef> | ||
| 5 | #include <random> | ||
| 6 | #include <vector> | ||
| 7 | |||
| 8 | #include "sabirov_s_monte_carlo_seq/common/include/common.hpp" | ||
| 9 | |||
| 10 | namespace sabirov_s_monte_carlo_seq { | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | double EvalLinear(const std::vector<double> &point) { | ||
| 15 | double s = 0.0; | ||
| 16 |
2/2✓ Branch 0 taken 880000 times.
✓ Branch 1 taken 480000 times.
|
1360000 | for (double x : point) { |
| 17 | 880000 | s += x; | |
| 18 | } | ||
| 19 | return s; | ||
| 20 | } | ||
| 21 | |||
| 22 | double EvalSumCubes(const std::vector<double> &point) { | ||
| 23 | double s = 0.0; | ||
| 24 |
2/2✓ Branch 0 taken 880000 times.
✓ Branch 1 taken 480000 times.
|
1360000 | for (double x : point) { |
| 25 | 880000 | s += x * x * x; | |
| 26 | } | ||
| 27 | return s; | ||
| 28 | } | ||
| 29 | |||
| 30 | double EvalCosProduct(const std::vector<double> &point) { | ||
| 31 | double p = 1.0; | ||
| 32 |
2/2✓ Branch 0 taken 880000 times.
✓ Branch 1 taken 480000 times.
|
1360000 | for (double x : point) { |
| 33 | 880000 | p *= std::cos(x); | |
| 34 | } | ||
| 35 | return p; | ||
| 36 | } | ||
| 37 | |||
| 38 | double EvalExpNeg(const std::vector<double> &point) { | ||
| 39 | double s = 0.0; | ||
| 40 |
2/2✓ Branch 0 taken 880000 times.
✓ Branch 1 taken 480000 times.
|
1360000 | for (double x : point) { |
| 41 | 880000 | s += x; | |
| 42 | } | ||
| 43 | 480000 | return std::exp(-s); | |
| 44 | } | ||
| 45 | |||
| 46 | double EvalMixedPoly(const std::vector<double> &point) { | ||
| 47 | double s = 0.0; | ||
| 48 |
2/2✓ Branch 0 taken 2480000 times.
✓ Branch 1 taken 880000 times.
|
3360000 | for (double x : point) { |
| 49 | 2480000 | s += (x * x) + x; | |
| 50 | } | ||
| 51 | return s; | ||
| 52 | } | ||
| 53 | |||
| 54 | double EvalSinSum(const std::vector<double> &point) { | ||
| 55 | double s = 0.0; | ||
| 56 |
2/2✓ Branch 0 taken 80000 times.
✓ Branch 1 taken 80000 times.
|
160000 | for (double x : point) { |
| 57 | 80000 | s += std::sin(x); | |
| 58 | } | ||
| 59 | return s; | ||
| 60 | } | ||
| 61 | |||
| 62 | double EvalSqrtSum(const std::vector<double> &point) { | ||
| 63 | double s = 0.0; | ||
| 64 |
2/2✓ Branch 0 taken 80000 times.
✓ Branch 1 taken 80000 times.
|
160000 | for (double x : point) { |
| 65 | 80000 | s += std::sqrt(x); | |
| 66 | } | ||
| 67 | return s; | ||
| 68 | } | ||
| 69 | |||
| 70 | double EvalQuarticSum(const std::vector<double> &point) { | ||
| 71 | double s = 0.0; | ||
| 72 |
2/2✓ Branch 0 taken 800000 times.
✓ Branch 1 taken 400000 times.
|
1200000 | for (double x : point) { |
| 73 | 800000 | s += x * x * x * x; | |
| 74 | } | ||
| 75 | return s; | ||
| 76 | } | ||
| 77 | |||
| 78 | 3360000 | double EvaluateAt(FuncType func_type, const std::vector<double> &point) { | |
| 79 |
8/9✓ Branch 0 taken 480000 times.
✓ Branch 1 taken 480000 times.
✓ Branch 2 taken 480000 times.
✓ Branch 3 taken 480000 times.
✓ Branch 4 taken 880000 times.
✓ Branch 5 taken 80000 times.
✓ Branch 6 taken 80000 times.
✓ Branch 7 taken 400000 times.
✗ Branch 8 not taken.
|
3360000 | switch (func_type) { |
| 80 | case FuncType::kLinear: | ||
| 81 | return EvalLinear(point); | ||
| 82 | case FuncType::kSumCubes: | ||
| 83 | return EvalSumCubes(point); | ||
| 84 | case FuncType::kCosProduct: | ||
| 85 | return EvalCosProduct(point); | ||
| 86 | case FuncType::kExpNeg: | ||
| 87 | 480000 | return EvalExpNeg(point); | |
| 88 | case FuncType::kMixedPoly: | ||
| 89 | return EvalMixedPoly(point); | ||
| 90 | case FuncType::kSinSum: | ||
| 91 | return EvalSinSum(point); | ||
| 92 | case FuncType::kSqrtSum: | ||
| 93 | return EvalSqrtSum(point); | ||
| 94 | case FuncType::kQuarticSum: | ||
| 95 | return EvalQuarticSum(point); | ||
| 96 | default: | ||
| 97 | return 0.0; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | } // namespace | ||
| 102 | |||
| 103 |
1/2✓ Branch 1 taken 104 times.
✗ Branch 2 not taken.
|
104 | SabirovSMonteCarloSEQ::SabirovSMonteCarloSEQ(const InType &in) { |
| 104 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 105 |
1/2✓ Branch 1 taken 104 times.
✗ Branch 2 not taken.
|
104 | GetInput() = in; |
| 106 | 104 | GetOutput() = 0.0; | |
| 107 | 104 | } | |
| 108 | |||
| 109 |
1/2✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
|
104 | bool SabirovSMonteCarloSEQ::ValidationImpl() { |
| 110 | const auto &in = GetInput(); | ||
| 111 |
2/4✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 104 times.
|
104 | if (in.lower.size() != in.upper.size() || in.lower.empty()) { |
| 112 | return false; | ||
| 113 | } | ||
| 114 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
|
104 | if (in.num_samples <= 0) { |
| 115 | return false; | ||
| 116 | } | ||
| 117 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 104 times.
|
264 | for (size_t i = 0; i < in.lower.size(); ++i) { |
| 118 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 160 times.
|
160 | if (in.lower[i] >= in.upper[i]) { |
| 119 | return false; | ||
| 120 | } | ||
| 121 | } | ||
| 122 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 104 times.
|
104 | if (in.func_type < FuncType::kLinear || in.func_type > FuncType::kQuarticSum) { |
| 123 | return false; | ||
| 124 | } | ||
| 125 | constexpr size_t kMaxDimensions = 10; | ||
| 126 | 104 | return in.lower.size() <= kMaxDimensions; | |
| 127 | } | ||
| 128 | |||
| 129 | 104 | bool SabirovSMonteCarloSEQ::PreProcessingImpl() { | |
| 130 | const auto &in = GetInput(); | ||
| 131 | 104 | lower_ = in.lower; | |
| 132 | 104 | upper_ = in.upper; | |
| 133 | 104 | num_samples_ = in.num_samples; | |
| 134 | 104 | func_type_ = in.func_type; | |
| 135 | 104 | GetOutput() = 0.0; | |
| 136 | 104 | return true; | |
| 137 | } | ||
| 138 | |||
| 139 | 104 | bool SabirovSMonteCarloSEQ::RunImpl() { | |
| 140 | 104 | auto dims = static_cast<int>(lower_.size()); | |
| 141 | |||
| 142 | 104 | std::random_device rd; | |
| 143 | 104 | std::mt19937 gen(rd()); | |
| 144 | 104 | std::vector<std::uniform_real_distribution<double>> dists; | |
| 145 |
1/2✓ Branch 1 taken 104 times.
✗ Branch 2 not taken.
|
104 | dists.reserve(dims); |
| 146 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 104 times.
|
264 | for (int i = 0; i < dims; ++i) { |
| 147 |
1/2✓ Branch 1 taken 160 times.
✗ Branch 2 not taken.
|
160 | dists.emplace_back(lower_[i], upper_[i]); |
| 148 | } | ||
| 149 | |||
| 150 | double volume = 1.0; | ||
| 151 |
2/2✓ Branch 0 taken 160 times.
✓ Branch 1 taken 104 times.
|
264 | for (int i = 0; i < dims; ++i) { |
| 152 | 160 | volume *= (upper_[i] - lower_[i]); | |
| 153 | } | ||
| 154 | |||
| 155 |
1/4✓ Branch 1 taken 104 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
104 | std::vector<double> point(dims); |
| 156 | double sum = 0.0; | ||
| 157 |
2/2✓ Branch 0 taken 3360000 times.
✓ Branch 1 taken 104 times.
|
3360104 | for (int i = 0; i < num_samples_; ++i) { |
| 158 |
2/2✓ Branch 0 taken 6960000 times.
✓ Branch 1 taken 3360000 times.
|
10320000 | for (int j = 0; j < dims; ++j) { |
| 159 | 6960000 | point[j] = dists[j](gen); | |
| 160 | } | ||
| 161 | 3360000 | sum += EvaluateAt(func_type_, point); | |
| 162 | } | ||
| 163 | |||
| 164 |
1/2✓ Branch 0 taken 104 times.
✗ Branch 1 not taken.
|
104 | GetOutput() = volume * sum / static_cast<double>(num_samples_); |
| 165 | 104 | return true; | |
| 166 | } | ||
| 167 | |||
| 168 | 104 | bool SabirovSMonteCarloSEQ::PostProcessingImpl() { | |
| 169 | 104 | return true; | |
| 170 | } | ||
| 171 | |||
| 172 | } // namespace sabirov_s_monte_carlo_seq | ||
| 173 |