|
| 1 | +/* |
| 2 | + * Copyright Nick Thompson, 2024 |
| 3 | + * Use, modification and distribution are subject to the |
| 4 | + * Boost Software License, Version 1.0. (See accompanying file |
| 5 | + * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | + */ |
| 7 | +#ifndef BOOST_MATH_OPTIMIZATION_RANDOM_SEARCH_HPP |
| 8 | +#define BOOST_MATH_OPTIMIZATION_RANDOM_SEARCH_HPP |
| 9 | +#include <algorithm> |
| 10 | +#include <array> |
| 11 | +#include <atomic> |
| 12 | +#include <cmath> |
| 13 | +#include <limits> |
| 14 | +#include <list> |
| 15 | +#include <mutex> |
| 16 | +#include <random> |
| 17 | +#include <sstream> |
| 18 | +#include <stdexcept> |
| 19 | +#include <thread> |
| 20 | +#include <type_traits> |
| 21 | +#include <utility> |
| 22 | +#include <vector> |
| 23 | +#include <boost/math/optimization/detail/common.hpp> |
| 24 | + |
| 25 | +namespace boost::math::optimization { |
| 26 | + |
| 27 | +template <typename ArgumentContainer> struct random_search_parameters { |
| 28 | + using Real = typename ArgumentContainer::value_type; |
| 29 | + ArgumentContainer lower_bounds; |
| 30 | + ArgumentContainer upper_bounds; |
| 31 | + size_t max_function_calls = 10000*std::thread::hardware_concurrency(); |
| 32 | + ArgumentContainer const *initial_guess = nullptr; |
| 33 | + unsigned threads = std::thread::hardware_concurrency(); |
| 34 | +}; |
| 35 | + |
| 36 | +template <typename ArgumentContainer> |
| 37 | +void validate_random_search_parameters(random_search_parameters<ArgumentContainer> const ¶ms) { |
| 38 | + using std::isfinite; |
| 39 | + using std::isnan; |
| 40 | + std::ostringstream oss; |
| 41 | + detail::validate_bounds(params.lower_bounds, params.upper_bounds); |
| 42 | + if (params.initial_guess) { |
| 43 | + detail::validate_initial_guess(*params.initial_guess, params.lower_bounds, params.upper_bounds); |
| 44 | + } |
| 45 | + if (params.threads == 0) { |
| 46 | + oss << __FILE__ << ":" << __LINE__ << ":" << __func__; |
| 47 | + oss << ": There must be at least one thread."; |
| 48 | + throw std::invalid_argument(oss.str()); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +template <typename ArgumentContainer, class Func, class URBG> |
| 53 | +ArgumentContainer random_search( |
| 54 | + const Func cost_function, |
| 55 | + random_search_parameters<ArgumentContainer> const ¶ms, |
| 56 | + URBG &gen, |
| 57 | + std::invoke_result_t<Func, ArgumentContainer> target_value = std::numeric_limits<std::invoke_result_t<Func, ArgumentContainer>>::quiet_NaN(), |
| 58 | + std::atomic<bool> *cancellation = nullptr, |
| 59 | + std::atomic<std::invoke_result_t<Func, ArgumentContainer>> *current_minimum_cost = nullptr, |
| 60 | + std::vector<std::pair<ArgumentContainer, std::invoke_result_t<Func, ArgumentContainer>>> *queries = nullptr) |
| 61 | + { |
| 62 | + using Real = typename ArgumentContainer::value_type; |
| 63 | + using ResultType = std::invoke_result_t<Func, ArgumentContainer>; |
| 64 | + using std::isnan; |
| 65 | + using std::uniform_real_distribution; |
| 66 | + validate_random_search_parameters(params); |
| 67 | + const size_t dimension = params.lower_bounds.size(); |
| 68 | + std::atomic<bool> target_attained = false; |
| 69 | + // Unfortunately, the "minimum_cost" variable can either be passed |
| 70 | + // (for observability) or not (if the user doesn't care). |
| 71 | + // That makes this a bit awkward . . . |
| 72 | + std::atomic<ResultType> lowest_cost = std::numeric_limits<ResultType>::infinity(); |
| 73 | + |
| 74 | + ArgumentContainer best_vector; |
| 75 | + if constexpr (detail::has_resize_v<ArgumentContainer>) { |
| 76 | + best_vector.resize(dimension, std::numeric_limits<Real>::quiet_NaN()); |
| 77 | + } |
| 78 | + if (params.initial_guess) { |
| 79 | + auto initial_cost = cost_function(*params.initial_guess); |
| 80 | + if (!isnan(initial_cost)) { |
| 81 | + lowest_cost = initial_cost; |
| 82 | + best_vector = *params.initial_guess; |
| 83 | + if (current_minimum_cost) { |
| 84 | + *current_minimum_cost = initial_cost; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + std::mutex mt; |
| 89 | + std::vector<std::thread> thread_pool; |
| 90 | + std::atomic<size_t> function_calls = 0; |
| 91 | + for (unsigned j = 0; j < params.threads; ++j) { |
| 92 | + auto seed = gen(); |
| 93 | + thread_pool.emplace_back([&, seed]() { |
| 94 | + URBG g(seed); |
| 95 | + ArgumentContainer trial_vector; |
| 96 | + // This vector is empty unless the user requests the queries be stored: |
| 97 | + std::vector<std::pair<ArgumentContainer, std::invoke_result_t<Func, ArgumentContainer>>> local_queries; |
| 98 | + if constexpr (detail::has_resize_v<ArgumentContainer>) { |
| 99 | + trial_vector.resize(dimension, std::numeric_limits<Real>::quiet_NaN()); |
| 100 | + } |
| 101 | + while (function_calls < params.max_function_calls) { |
| 102 | + if (cancellation && *cancellation) { |
| 103 | + break; |
| 104 | + } |
| 105 | + if (target_attained) { |
| 106 | + break; |
| 107 | + } |
| 108 | + // Fill trial vector: |
| 109 | + uniform_real_distribution<Real> unif01(Real(0), Real(1)); |
| 110 | + for (size_t i = 0; i < dimension; ++i) { |
| 111 | + trial_vector[i] = params.lower_bounds[i] + (params.upper_bounds[i] - params.lower_bounds[i])*unif01(g); |
| 112 | + } |
| 113 | + ResultType trial_cost = cost_function(trial_vector); |
| 114 | + ++function_calls; |
| 115 | + if (isnan(trial_cost)) { |
| 116 | + continue; |
| 117 | + } |
| 118 | + if (trial_cost < lowest_cost) { |
| 119 | + lowest_cost = trial_cost; |
| 120 | + if (current_minimum_cost) { |
| 121 | + *current_minimum_cost = trial_cost; |
| 122 | + } |
| 123 | + // We expect to need to acquire this lock with decreasing frequency |
| 124 | + // as the computation proceeds: |
| 125 | + std::scoped_lock lock(mt); |
| 126 | + best_vector = trial_vector; |
| 127 | + } |
| 128 | + if (queries) { |
| 129 | + local_queries.push_back(std::make_pair(trial_vector, trial_cost)); |
| 130 | + } |
| 131 | + if (!isnan(target_value) && trial_cost <= target_value) { |
| 132 | + target_attained = true; |
| 133 | + } |
| 134 | + } |
| 135 | + if (queries) { |
| 136 | + std::scoped_lock lock(mt); |
| 137 | + queries->insert(queries->begin(), local_queries.begin(), local_queries.end()); |
| 138 | + } |
| 139 | + }); |
| 140 | + } |
| 141 | + for (auto &thread : thread_pool) { |
| 142 | + thread.join(); |
| 143 | + } |
| 144 | + return best_vector; |
| 145 | +} |
| 146 | + |
| 147 | +} // namespace boost::math::optimization |
| 148 | +#endif |
0 commit comments