2
0
mirror of https://github.com/boostorg/math.git synced 2026-01-19 04:22:09 +00:00

Include tests for optimization

This commit is contained in:
Nick Thompson
2024-02-12 14:24:44 -08:00
committed by Nick
parent b7a62c4a29
commit 8d47ee0f4b
9 changed files with 75 additions and 19 deletions

View File

@@ -6,7 +6,6 @@
*/
#ifndef BOOST_MATH_OPTIMIZATION_CMA_ES_HPP
#define BOOST_MATH_OPTIMIZATION_CMA_ES_HPP
#include <algorithm>
#include <atomic>
#include <cmath>
#include <iostream>
@@ -14,7 +13,6 @@
#include <random>
#include <sstream>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <vector>
#include <boost/math/optimization/detail/common.hpp>
@@ -273,8 +271,8 @@ ArgumentContainer cma_es(
for (size_t k = 0; k < params.population_size; ++k) {
auto & y = ys[k];
auto & x = xs[k];
BOOST_MATH_ASSERT(x.size() == n);
BOOST_MATH_ASSERT(y.size() == n);
BOOST_MATH_ASSERT(static_cast<size_t>(x.size()) == n);
BOOST_MATH_ASSERT(static_cast<size_t>(y.size()) == n);
size_t resample_counter = 0;
do {
// equation (39) of Figure 6:
@@ -339,7 +337,7 @@ ArgumentContainer cma_es(
}
// Equation (43), Figure 6: Start with C^{-1/2}<y>_{w}
Eigen::Vector<DimensionlessReal, Eigen::Dynamic> inv_D_B_transpose_y = B.transpose()*weighted_avg_y;
for (size_t j = 0; j < inv_D_B_transpose_y.size(); ++j) {
for (long j = 0; j < inv_D_B_transpose_y.size(); ++j) {
inv_D_B_transpose_y[j] /= D[j];
}
Eigen::Vector<DimensionlessReal, Eigen::Dynamic> C_inv_sqrt_y_avg = B*inv_D_B_transpose_y;

View File

@@ -6,12 +6,13 @@
*/
#ifndef BOOST_MATH_OPTIMIZATION_DETAIL_COMMON_HPP
#define BOOST_MATH_OPTIMIZATION_DETAIL_COMMON_HPP
#include <algorithm>
#include <algorithm> // for std::sort
#include <cmath>
#include <limits>
#include <sstream>
#include <stdexcept>
#include <random>
#include <type_traits> // for std::false_type
namespace boost::math::optimization::detail {
@@ -114,6 +115,7 @@ std::vector<ArgumentContainer> random_initial_population(ArgumentContainer const
template <typename ArgumentContainer>
void validate_initial_guess(ArgumentContainer const &initial_guess, ArgumentContainer const &lower_bounds,
ArgumentContainer const &upper_bounds) {
using std::isfinite;
std::ostringstream oss;
auto const dimension = lower_bounds.size();
if (initial_guess.size() != dimension) {

View File

@@ -6,21 +6,15 @@
*/
#ifndef BOOST_MATH_OPTIMIZATION_DIFFERENTIAL_EVOLUTION_HPP
#define BOOST_MATH_OPTIMIZATION_DIFFERENTIAL_EVOLUTION_HPP
#include <algorithm>
#include <array>
#include <atomic>
#include <boost/math/optimization/detail/common.hpp>
#include <chrono>
#include <cmath>
#include <future>
#include <limits>
#include <list>
#include <mutex>
#include <random>
#include <sstream>
#include <stdexcept>
#include <thread>
#include <type_traits>
#include <utility>
#include <vector>

View File

@@ -6,14 +6,11 @@
*/
#ifndef BOOST_MATH_OPTIMIZATION_JSO_HPP
#define BOOST_MATH_OPTIMIZATION_JSO_HPP
#include <algorithm>
#include <array>
#include <atomic>
#include <boost/math/optimization/detail/common.hpp>
#include <cmath>
#include <iostream>
#include <limits>
#include <list>
#include <random>
#include <sstream>
#include <stdexcept>

View File

@@ -6,18 +6,14 @@
*/
#ifndef BOOST_MATH_OPTIMIZATION_RANDOM_SEARCH_HPP
#define BOOST_MATH_OPTIMIZATION_RANDOM_SEARCH_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>

View File

@@ -1064,6 +1064,9 @@ test-suite interpolators :
[ run jso_test.cpp : : : [ requires cxx17_if_constexpr cxx17_std_apply ] ]
[ run random_search_test.cpp : : : [ requires cxx17_if_constexpr cxx17_std_apply ] ]
[ run cma_es_test.cpp : : : [ requires cxx17_if_constexpr cxx17_std_apply ] [ check-target-builds ../../multiprecision/config//has_eigen : : <build>no ] ]
[ compile compile_test/random_search_incl_test.cpp : [ requires cxx17_if_constexpr cxx17_std_apply ] ]
[ compile compile_test/differential_evolution_incl_test.cpp : [ requires cxx17_if_constexpr cxx17_std_apply ] ]
[ compile compile_test/jso_incl_test.cpp : [ requires cxx17_if_constexpr cxx17_std_apply ] ]
;
test-suite quadrature :

View File

@@ -0,0 +1,22 @@
// 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)
//
// Basic sanity check that header
// #includes all the files that it needs to.
//
#include <boost/math/optimization/differential_evolution.hpp>
//
// Note this header includes no other headers, this is
// important if this test is to be meaningful:
//
#include "test_compile_result.hpp"
void compile_and_link_test()
{
auto f = [](std::vector<double> const & v) { return v[0]*v[0]; };
boost::math::optimization::differential_evolution_parameters<std::vector<double>> params;
std::mt19937_64 gen(12345);
auto v = boost::math::optimization::differential_evolution(f, params, gen);
}

View File

@@ -0,0 +1,22 @@
// 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)
//
// Basic sanity check that header
// #includes all the files that it needs to.
//
#include <boost/math/optimization/jso.hpp>
//
// Note this header includes no other headers, this is
// important if this test is to be meaningful:
//
#include "test_compile_result.hpp"
void compile_and_link_test()
{
auto f = [](std::vector<double> const & v) { return v[0]*v[0]; };
boost::math::optimization::jso_parameters<std::vector<double>> params;
std::mt19937_64 gen(12345);
auto v = boost::math::optimization::jso(f, params, gen);
}

View File

@@ -0,0 +1,22 @@
// 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)
//
// Basic sanity check that header
// #includes all the files that it needs to.
//
#include <boost/math/optimization/random_search.hpp>
//
// Note this header includes no other headers, this is
// important if this test is to be meaningful:
//
#include "test_compile_result.hpp"
void compile_and_link_test()
{
auto f = [](std::vector<double> const & v) { return v[0]*v[0]; };
boost::math::optimization::random_search_parameters<std::vector<double>> params;
std::mt19937_64 gen(12345);
auto v = boost::math::optimization::random_search(f, params, gen);
}