| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "shilin_n_monte_carlo_integration/seq/include/ops_seq.hpp" | ||
| 2 | |||
| 3 | #include <cstddef> | ||
| 4 | #include <vector> | ||
| 5 | |||
| 6 | #include "shilin_n_monte_carlo_integration/common/include/common.hpp" | ||
| 7 | |||
| 8 | namespace shilin_n_monte_carlo_integration { | ||
| 9 | |||
| 10 |
1/2✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
|
64 | ShilinNMonteCarloIntegrationSEQ::ShilinNMonteCarloIntegrationSEQ(const InType &in) { |
| 11 | SetTypeOfTask(GetStaticTypeOfTask()); | ||
| 12 | GetInput() = in; | ||
| 13 | 64 | GetOutput() = 0.0; | |
| 14 | 64 | } | |
| 15 | |||
| 16 |
1/2✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
|
64 | bool ShilinNMonteCarloIntegrationSEQ::ValidationImpl() { |
| 17 | const auto &[lower, upper, n, func_type] = GetInput(); | ||
| 18 |
2/4✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 64 times.
|
64 | if (lower.size() != upper.size() || lower.empty()) { |
| 19 | return false; | ||
| 20 | } | ||
| 21 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | if (n <= 0) { |
| 22 | return false; | ||
| 23 | } | ||
| 24 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 64 times.
|
192 | for (size_t i = 0; i < lower.size(); ++i) { |
| 25 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 128 times.
|
128 | if (lower[i] >= upper[i]) { |
| 26 | return false; | ||
| 27 | } | ||
| 28 | } | ||
| 29 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
|
64 | if (func_type < FuncType::kConstant || func_type > FuncType::kSinProduct) { |
| 30 | return false; | ||
| 31 | } | ||
| 32 | constexpr size_t kMaxDimensions = 10; | ||
| 33 | 64 | return lower.size() <= kMaxDimensions; | |
| 34 | } | ||
| 35 | |||
| 36 | 64 | bool ShilinNMonteCarloIntegrationSEQ::PreProcessingImpl() { | |
| 37 | const auto &[lower, upper, n, func_type] = GetInput(); | ||
| 38 | 64 | lower_bounds_ = lower; | |
| 39 | 64 | upper_bounds_ = upper; | |
| 40 | 64 | num_points_ = n; | |
| 41 | 64 | func_type_ = func_type; | |
| 42 | 64 | return true; | |
| 43 | } | ||
| 44 | |||
| 45 | 64 | bool ShilinNMonteCarloIntegrationSEQ::RunImpl() { | |
| 46 | 64 | auto dimensions = static_cast<int>(lower_bounds_.size()); | |
| 47 | |||
| 48 | // Quasi-random Kronecker sequence: alpha_d = fractional part of sqrt(prime_d) | ||
| 49 | const std::vector<double> alpha = { | ||
| 50 | 0.41421356237309504, // frac(sqrt(2)) | ||
| 51 | 0.73205080756887729, // frac(sqrt(3)) | ||
| 52 | 0.23606797749978969, // frac(sqrt(5)) | ||
| 53 | 0.64575131106459059, // frac(sqrt(7)) | ||
| 54 | 0.31662479035539984, // frac(sqrt(11)) | ||
| 55 | 0.60555127546398929, // frac(sqrt(13)) | ||
| 56 | 0.12310562561766059, // frac(sqrt(17)) | ||
| 57 | 0.35889894354067355, // frac(sqrt(19)) | ||
| 58 | 0.79583152331271838, // frac(sqrt(23)) | ||
| 59 | 0.38516480713450403 // frac(sqrt(29)) | ||
| 60 | 64 | }; | |
| 61 | |||
| 62 |
1/4✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
64 | std::vector<double> current(dimensions, 0.5); |
| 63 |
1/4✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
64 | std::vector<double> point(dimensions); |
| 64 | double sum = 0.0; | ||
| 65 | |||
| 66 |
2/2✓ Branch 0 taken 3360000 times.
✓ Branch 1 taken 64 times.
|
3360064 | for (int i = 0; i < num_points_; ++i) { |
| 67 |
2/2✓ Branch 0 taken 8160000 times.
✓ Branch 1 taken 3360000 times.
|
11520000 | for (int di = 0; di < dimensions; ++di) { |
| 68 |
2/2✓ Branch 0 taken 4112048 times.
✓ Branch 1 taken 4047952 times.
|
8160000 | current[di] += alpha[di]; |
| 69 |
2/2✓ Branch 0 taken 4112048 times.
✓ Branch 1 taken 4047952 times.
|
8160000 | if (current[di] >= 1.0) { |
| 70 | 4112048 | current[di] -= 1.0; | |
| 71 | } | ||
| 72 | 8160000 | point[di] = lower_bounds_[di] + ((upper_bounds_[di] - lower_bounds_[di]) * current[di]); | |
| 73 | } | ||
| 74 | 3360000 | sum += IntegrandFunction::Evaluate(func_type_, point); | |
| 75 | } | ||
| 76 | |||
| 77 | double volume = 1.0; | ||
| 78 |
2/2✓ Branch 0 taken 128 times.
✓ Branch 1 taken 64 times.
|
192 | for (int di = 0; di < dimensions; ++di) { |
| 79 | 128 | volume *= (upper_bounds_[di] - lower_bounds_[di]); | |
| 80 | } | ||
| 81 | |||
| 82 |
1/2✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
|
64 | GetOutput() = volume * sum / static_cast<double>(num_points_); |
| 83 | 64 | return true; | |
| 84 | } | ||
| 85 | |||
| 86 | 64 | bool ShilinNMonteCarloIntegrationSEQ::PostProcessingImpl() { | |
| 87 | 64 | return true; | |
| 88 | } | ||
| 89 | |||
| 90 | } // namespace shilin_n_monte_carlo_integration | ||
| 91 |