| 86 | // ---------------------------------------------------------------------------------------- |
| 87 | |
| 88 | void test_upper_bound_candidate_selection() |
| 89 | /*! |
| 90 | ensures |
| 91 | - Checks that candidate selection agrees with exhaustive upper-bound |
| 92 | evaluation of the same candidate pool, including ties. |
| 93 | !*/ |
| 94 | { |
| 95 | for (long dims : {1, 3, 8}) |
| 96 | for (int integer_mode : {0, 1, 2}) |
| 97 | for (double noise : {0.0, 0.01}) |
| 98 | { |
| 99 | print_spinner(); |
| 100 | matrix<double,0,1> lower(dims), upper(dims); |
| 101 | lower = -10; |
| 102 | upper = 10; |
| 103 | std::vector<bool> integers(dims); |
| 104 | for (long j = 0; j < dims; ++j) |
| 105 | integers[j] = integer_mode == 2 || (integer_mode == 1 && j%2 == 0); |
| 106 | function_spec spec(lower, upper, integers); |
| 107 | |
| 108 | dlib::rand data_rng; |
| 109 | std::vector<function_evaluation> samples; |
| 110 | for (long i = 0; i < 32; ++i) |
| 111 | { |
| 112 | matrix<double,0,1> x(dims); |
| 113 | for (long j = 0; j < dims; ++j) |
| 114 | { |
| 115 | x(j) = data_rng.get_double_in_range(-1, 1); |
| 116 | if (integers[j]) |
| 117 | x(j) = std::round(x(j)); |
| 118 | } |
| 119 | samples.emplace_back(x, std::sin(sum(x)) + data_rng.get_random_double()/10); |
| 120 | } |
| 121 | upper_bound_function bound(samples, noise); |
| 122 | double best_observed = -std::numeric_limits<double>::infinity(); |
| 123 | for (const auto& sample : samples) |
| 124 | best_observed = std::max(best_observed, sample.y); |
| 125 | |
| 126 | for (size_t count : {1, 32, 5000}) |
| 127 | for (time_t seed : {17, 43, 101}) |
| 128 | { |
| 129 | global_function_search opt({spec}, {samples}, noise); |
| 130 | opt.set_seed(seed); |
| 131 | opt.set_pure_random_search_probability(0); |
| 132 | opt.set_solver_epsilon(std::numeric_limits<double>::max()); |
| 133 | opt.set_monte_carlo_upper_bound_sample_num(count); |
| 134 | |
| 135 | dlib::rand candidate_rng; |
| 136 | candidate_rng.set_seed(cast_to_string(seed)); |
| 137 | candidate_rng.get_random_double(); // The choice to use the upper bound. |
| 138 | auto draw_candidate = [&]() |
| 139 | { |
| 140 | matrix<double,0,1> x(dims); |
| 141 | for (long j = 0; j < dims; ++j) |
| 142 | { |
| 143 | x(j) = candidate_rng.get_double_in_range(lower(j), upper(j)); |
| 144 | if (integers[j]) |
| 145 | x(j) = std::round(x(j)); |
no test coverage detected