-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fccad84
commit 544e33f
Showing
6 changed files
with
538 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
[/ | ||
Copyright (c) 2024 Nick Thompson | ||
Use, modification and distribution are subject to the | ||
Boost Software License, Version 1.0. (See accompanying file | ||
LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
] | ||
|
||
[section:cma_es Evolution Strategy with Covariance Matrix Adaptation] | ||
|
||
[heading Synopsis] | ||
|
||
`` | ||
#include <boost/math/optimization/cma_es.hpp> | ||
|
||
namespace boost::math::optimization { | ||
|
||
template <typename ArgumentContainer> struct cma_es_parameters { | ||
using Real = typename ArgumentContainer::value_type; | ||
ArgumentContainer lower_bounds; | ||
ArgumentContainer upper_bounds; | ||
Real std_dev = std::numeric_limits<Real>::quiet_NaN(); | ||
Real initial_population_size = 0; | ||
ArgumentContainer const * initial_guess = nullptr; | ||
}; | ||
|
||
template <typename ArgumentContainer, class Func, class URBG> | ||
ArgumentContainer cma_es( | ||
const Func cost_function, | ||
random_search_parameters<ArgumentContainer> const ¶ms, | ||
URBG &gen, | ||
std::invoke_result_t<Func, ArgumentContainer> target_value = std::numeric_limits<std::invoke_result_t<Func, ArgumentContainer>>::quiet_NaN(), | ||
std::atomic<bool> *cancellation = nullptr, | ||
std::atomic<std::invoke_result_t<Func, ArgumentContainer>> *current_minimum_cost = nullptr, | ||
std::vector<std::pair<ArgumentContainer, std::invoke_result_t<Func, ArgumentContainer>>> *queries = nullptr); | ||
|
||
} // namespaces | ||
`` | ||
|
||
The `cma_es` function searches for a global minimum of a function. | ||
|
||
|
||
[heading Parameters] | ||
|
||
`lower_bounds`: A container representing the lower bounds of the optimization space along each dimension. The `.size()` of the bounds should return the dimension of the problem. | ||
`upper_bounds`: A container representing the upper bounds of the optimization space along each dimension. It should have the same size of `lower_bounds`, and each element should be >= the corresponding element of `lower_bounds`. | ||
`initial_guess`: An optional guess for where we should start looking for solutions. This is provided for consistency with other optimization functions-it's not particularly useful for this function. | ||
|
||
[heading The function] | ||
|
||
`` | ||
template <typename ArgumentContainer, class Func, class URBG> | ||
ArgumentContainer cma_es(const Func cost_function, | ||
random_search_parameters<ArgumentContainer> const ¶ms, | ||
URBG &gen, | ||
std::invoke_result_t<Func, ArgumentContainer> value_to_reach = std::numeric_limits<std::invoke_result_t<Func, ArgumentContainer>>::quiet_NaN(), | ||
std::atomic<bool> *cancellation = nullptr, | ||
std::atomic<std::invoke_result_t<Func, ArgumentContainer>> *current_minimum_cost = nullptr, | ||
std::vector<std::pair<ArgumentContainer, std::invoke_result_t<Func, ArgumentContainer>>> *queries = nullptr) | ||
`` | ||
|
||
Parameters: | ||
|
||
`cost_function`: The cost function to be minimized. | ||
`params`: The parameters to the algorithm as described above. | ||
`gen`: A uniform random bit generator, like `std::mt19937_64`. | ||
`value_to_reach`: An optional value that, if reached, stops the optimization. This is the most robust way to terminate the calculation, but in most cases the optimal value of the cost function is unknown. If it is, use it! Physical considerations can often be used to find optimal values for cost functions. | ||
`cancellation`: An optional atomic boolean to allow the user to stop the computation and gracefully return the best result found up to that point. N.B.: Cancellation is not immediate; the in-progress generation finishes. | ||
`current_minimum_cost`: An optional atomic variable to store the current minimum cost during optimization. This allows developers to (e.g.) plot the progress of the minimization over time and in conjunction with the `cancellation` argument allow halting the computation when the progress stagnates. | ||
`queries`: An optional vector to store intermediate results during optimization. This is useful for debugging and perhaps volume rendering of the objective function after the calculation is complete. | ||
|
||
Returns: | ||
|
||
The argument vector corresponding to the minimum cost found by the optimization. | ||
|
||
[h4:examples Examples] | ||
|
||
An example exhibiting graceful cancellation and progress observability can be studied in [@../../example/cma_es_example.cpp cma_es_example.cpp]. | ||
|
||
[h4:references References] | ||
|
||
http://www.cmap.polytechnique.fr/~nikolaus.hansen/gecco2013-CMA-ES-tutorial.pdf | ||
|
||
[endsect] [/section:cma_es] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* | ||
* Copyright Nick Thompson, 2024 | ||
* Use, modification and distribution are subject to the | ||
* Boost Software License, Version 1.0. (See accompanying file | ||
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
*/ | ||
#ifndef BOOST_MATH_OPTIMIZATION_CMA_ES_HPP | ||
#define BOOST_MATH_OPTIMIZATION_CMA_ES_HPP | ||
#include <algorithm> | ||
#include <array> | ||
#include <atomic> | ||
#include <cmath> | ||
#include <limits> | ||
#include <list> | ||
#include <mutex> | ||
#include <random> | ||
#include <sstream> | ||
#include <stdexcept> | ||
#include <thread> | ||
#include <type_traits> | ||
#include <utility> | ||
#include <vector> | ||
#include <boost/math/optimization/detail/common.hpp> | ||
#if __has_include(<Eigen/Dense>) | ||
#include <boost/math/optimization/detail/multivariate_normal_distribution.hpp> | ||
#include <Eigen/Dense> | ||
#else | ||
#error "CMA-ES requires Eigen." | ||
#endif | ||
|
||
// Follows the notation in: | ||
// http://www.cmap.polytechnique.fr/~nikolaus.hansen/gecco2013-CMA-ES-tutorial.pdf | ||
|
||
namespace boost::math::optimization { | ||
|
||
template <typename ArgumentContainer> struct cma_es_parameters { | ||
using Real = typename ArgumentContainer::value_type; | ||
ArgumentContainer lower_bounds; | ||
ArgumentContainer upper_bounds; | ||
size_t max_function_calls = 10000*std::thread::hardware_concurrency(); | ||
ArgumentContainer const *initial_guess = nullptr; | ||
Real step_length = std::numeric_limits<Real>::quiet_NaN(); | ||
size_t initial_population_size = 0; | ||
unsigned threads = std::thread::hardware_concurrency(); | ||
}; | ||
|
||
template <typename ArgumentContainer> | ||
void validate_cma_es_parameters(cma_es_parameters<ArgumentContainer> const ¶ms) { | ||
using std::isfinite; | ||
using std::isnan; | ||
std::ostringstream oss; | ||
detail::validate_bounds(params.lower_bounds, params.upper_bounds); | ||
if (params.initial_guess) { | ||
detail::validate_initial_guess(*params.initial_guess, params.lower_bounds, params.upper_bounds); | ||
} | ||
if (params.threads == 0) { | ||
oss << __FILE__ << ":" << __LINE__ << ":" << __func__; | ||
oss << ": There must be at least one thread."; | ||
throw std::invalid_argument(oss.str()); | ||
} | ||
} | ||
|
||
template <typename ArgumentContainer, class Func, class URBG> | ||
ArgumentContainer cma_es( | ||
const Func cost_function, | ||
cma_es_parameters<ArgumentContainer> const ¶ms, | ||
URBG &gen, | ||
std::invoke_result_t<Func, ArgumentContainer> target_value = std::numeric_limits<std::invoke_result_t<Func, ArgumentContainer>>::quiet_NaN(), | ||
std::atomic<bool> *cancellation = nullptr, | ||
std::atomic<std::invoke_result_t<Func, ArgumentContainer>> *current_minimum_cost = nullptr, | ||
std::vector<std::pair<ArgumentContainer, std::invoke_result_t<Func, ArgumentContainer>>> *queries = nullptr) | ||
{ | ||
using Real = typename ArgumentContainer::value_type; | ||
using ResultType = std::invoke_result_t<Func, ArgumentContainer>; | ||
using std::isnan; | ||
using std::uniform_real_distribution; | ||
validate_cma_es_parameters(params); | ||
const size_t dimension = params.lower_bounds.size(); | ||
std::atomic<bool> target_attained = false; | ||
std::atomic<ResultType> lowest_cost = std::numeric_limits<ResultType>::infinity(); | ||
ArgumentContainer best_vector; | ||
if constexpr (detail::has_resize_v<ArgumentContainer>) { | ||
best_vector.resize(dimension, std::numeric_limits<Real>::quiet_NaN()); | ||
} | ||
// http://www.cmap.polytechnique.fr/~nikolaus.hansen/gecco2013-CMA-ES-tutorial.pdf, slide 16: | ||
Real c_c = Real(4)/dimension; | ||
Real c_sigma = Real(4)/dimension; | ||
Real c_1 = Real(2)/(dimension*dimension); | ||
std::vector<Real> weights(params.initial_population_size, std::numeric_limits<Real>::quiet_NaN()); | ||
// mu:= number of parents, lambda := number of offspring. | ||
auto C = Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic>::Identity(dimension, dimension); | ||
if (params.initial_guess) { | ||
auto initial_cost = cost_function(*params.initial_guess); | ||
if (!isnan(initial_cost)) { | ||
best_vector = *params.initial_guess; | ||
if (current_minimum_cost) { | ||
*current_minimum_cost = initial_cost; | ||
} | ||
} | ||
} | ||
std::mutex mt; | ||
std::vector<std::thread> thread_pool; | ||
std::atomic<size_t> function_calls = 0; | ||
for (unsigned j = 0; j < params.threads; ++j) { | ||
auto seed = gen(); | ||
thread_pool.emplace_back([&, seed]() { | ||
URBG g(seed); | ||
ArgumentContainer trial_vector; | ||
// This vector is empty unless the user requests the queries be stored: | ||
std::vector<std::pair<ArgumentContainer, std::invoke_result_t<Func, ArgumentContainer>>> local_queries; | ||
if constexpr (detail::has_resize_v<ArgumentContainer>) { | ||
trial_vector.resize(dimension, std::numeric_limits<Real>::quiet_NaN()); | ||
} | ||
while (function_calls < params.max_function_calls) { | ||
if (cancellation && *cancellation) { | ||
break; | ||
} | ||
if (target_attained) { | ||
break; | ||
} | ||
// Fill trial vector: | ||
uniform_real_distribution<Real> unif01(Real(0), Real(1)); | ||
for (size_t i = 0; i < dimension; ++i) { | ||
trial_vector[i] = params.lower_bounds[i] + (params.upper_bounds[i] - params.lower_bounds[i])*unif01(g); | ||
} | ||
ResultType trial_cost = cost_function(trial_vector); | ||
++function_calls; | ||
if (isnan(trial_cost)) { | ||
continue; | ||
} | ||
if (trial_cost < lowest_cost) { | ||
lowest_cost = trial_cost; | ||
if (current_minimum_cost) { | ||
*current_minimum_cost = trial_cost; | ||
} | ||
// We expect to need to acquire this lock with decreasing frequency | ||
// as the computation proceeds: | ||
std::scoped_lock lock(mt); | ||
best_vector = trial_vector; | ||
} | ||
if (queries) { | ||
local_queries.push_back(std::make_pair(trial_vector, trial_cost)); | ||
} | ||
if (!isnan(target_value) && trial_cost <= target_value) { | ||
target_attained = true; | ||
} | ||
} | ||
if (queries) { | ||
std::scoped_lock lock(mt); | ||
queries->insert(queries->begin(), local_queries.begin(), local_queries.end()); | ||
} | ||
}); | ||
} | ||
for (auto &thread : thread_pool) { | ||
thread.join(); | ||
} | ||
return best_vector; | ||
} | ||
|
||
} // namespace boost::math::optimization | ||
#endif |
96 changes: 96 additions & 0 deletions
96
include/boost/math/optimization/detail/multivariate_normal_distribution.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright Nick Thompson, 2024 | ||
* Use, modification and distribution are subject to the | ||
* Boost Software License, Version 1.0. (See accompanying file | ||
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
*/ | ||
#ifndef BOOST_MATH_OPTIMIZATION_DETAIL_MULTIVARIATE_NORMAL_HPP | ||
#define BOOST_MATH_OPTIMIZATION_DETAIL_MULTIVARIATE_NORMAL_HPP | ||
#include <algorithm> | ||
#include <cmath> | ||
#include <limits> | ||
#include <sstream> | ||
#include <stdexcept> | ||
#include <random> | ||
#include <boost/math/optimization/detail/common.hpp> | ||
#if __has_include(<Eigen/Dense>) | ||
#include <Eigen/Dense> | ||
#else | ||
#error "The Eigen library is required for the successful operation of this class" | ||
#endif | ||
|
||
namespace boost::math::optimization::detail { | ||
|
||
// This is super useful functionality, but nonetheless it must be shunted off into a dark corner of the library | ||
// because even today there is no standard matrix class and no standard was to do a Cholesky decomposition. | ||
// Hence a more public place in the library just puts users in dependency hell. | ||
template<class RandomAccessContainer> | ||
class multivariate_normal_distribution { | ||
public: | ||
using Real = typename RandomAccessContainer::value_type; | ||
multivariate_normal_distribution(RandomAccessContainer const & mean, Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic> const & covariance_matrix) : m_{mean} { | ||
if (covariance_matrix.rows() != covariance_matrix.cols()) { | ||
std::ostringstream oss; | ||
oss << __FILE__ << ":" << __LINE__ << ":" << __func__; | ||
oss << ": The covariance matrix must be square, but received a (" << covariance_matrix.rows() << ", " << covariance_matrix.cols() << ") matrix."; | ||
throw std::domain_error(oss.str()); | ||
} | ||
if (mean.size() != covariance_matrix.cols()) { | ||
std::ostringstream oss; | ||
oss << __FILE__ << ":" << __LINE__ << ":" << __func__; | ||
oss << ": The mean has dimension " << mean.size() << " but the covariance matrix has " << covariance_matrix.cols() << " columns."; | ||
throw std::domain_error(oss.str()); | ||
} | ||
Eigen::LLT<Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic> > llt(covariance_matrix); | ||
if(llt.info() == Eigen::NumericalIssue) { | ||
std::ostringstream oss; | ||
oss << __FILE__ << ":" << __LINE__ << ":" << __func__; | ||
oss << ": The covariance matrix is not positive definite. We probably need to use Eigen::LDLT instead."; | ||
throw std::domain_error(oss.str()); | ||
} | ||
L_ = llt.matrixL(); | ||
} | ||
|
||
template<class URNG> | ||
RandomAccessContainer operator()(URNG& g) const { | ||
RandomAccessContainer x; | ||
if constexpr (detail::has_resize_v<RandomAccessContainer>) { | ||
x.resize(m_.size()); | ||
} | ||
(*this)(x, g); | ||
return x; | ||
} | ||
|
||
template<class URNG> | ||
void operator()(RandomAccessContainer& x, URNG& g) const { | ||
using std::normal_distribution; | ||
if (x.size() != m_.size()) { | ||
std::ostringstream oss; | ||
oss << __FILE__ << ":" << __LINE__ << ":" << __func__; | ||
oss << ": Must provide a vector of the same length as the mean."; | ||
throw std::domain_error(oss.str()); | ||
} | ||
|
||
auto dis = normal_distribution<Real>(0, 1); | ||
/// First generate standard normal random vector: | ||
Eigen::Vector<Real, Eigen::Dynamic> u; | ||
u.resize(x.size()); | ||
for (size_t i = 0; i < x.size(); ++i) { | ||
u[i] = dis(g); | ||
} | ||
// Transform it with L: | ||
u = L_*u; | ||
for (size_t i = 0; i < x.size(); ++i) { | ||
x[i] = u[i] + m_[i]; | ||
} | ||
} | ||
|
||
|
||
|
||
private: | ||
RandomAccessContainer m_; | ||
Eigen::Matrix<Real, Eigen::Dynamic, Eigen::Dynamic> L_; | ||
}; | ||
|
||
} // namespace boost::math::optimization::detail | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.