Skip to content

Commit

Permalink
Include tests for optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
NAThompson committed Feb 12, 2024
1 parent b7a62c4 commit 1c1d90b
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 19 deletions.
8 changes: 3 additions & 5 deletions include/boost/math/optimization/cma_es.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
*/
#ifndef BOOST_MATH_OPTIMIZATION_CMA_ES_HPP
#define BOOST_MATH_OPTIMIZATION_CMA_ES_HPP
#include <algorithm>
#include <atomic>
#include <cmath>
#include <iostream>
#include <limits>
#include <random>
#include <sstream>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <vector>
#include <boost/math/optimization/detail/common.hpp>
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion include/boost/math/optimization/detail/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 0 additions & 6 deletions include/boost/math/optimization/differential_evolution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down
3 changes: 0 additions & 3 deletions include/boost/math/optimization/jso.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 0 additions & 4 deletions include/boost/math/optimization/random_search.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 4 additions & 0 deletions test/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,10 @@ 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 ] ]
[ compile compile_test/cma_es_incl_test.cpp : [ requires cxx17_if_constexpr cxx17_std_apply ] [ check-target-builds ../../multiprecision/config//has_eigen : : <build>no ] ]
;

test-suite quadrature :
Expand Down
22 changes: 22 additions & 0 deletions test/compile_test/cma_es_incl_test.cpp
Original file line number Diff line number Diff line change
@@ -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/cma_es.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::cma_es_parameters<std::vector<double>> params;
std::mt19937_64 gen(12345);
auto v = boost::math::optimization::cma_es(f, params, gen);
}
22 changes: 22 additions & 0 deletions test/compile_test/differential_evolution_incl_test.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
22 changes: 22 additions & 0 deletions test/compile_test/jso_incl_test.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
22 changes: 22 additions & 0 deletions test/compile_test/random_search_incl_test.cpp
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 1c1d90b

Please sign in to comment.