From 8d47ee0f4b6c4697ae94203a3d4e928ee1f80335 Mon Sep 17 00:00:00 2001 From: Nick Thompson Date: Mon, 12 Feb 2024 14:24:44 -0800 Subject: [PATCH] Include tests for optimization --- include/boost/math/optimization/cma_es.hpp | 8 +++---- .../boost/math/optimization/detail/common.hpp | 4 +++- .../optimization/differential_evolution.hpp | 6 ----- include/boost/math/optimization/jso.hpp | 3 --- .../boost/math/optimization/random_search.hpp | 4 ---- test/Jamfile.v2 | 3 +++ .../differential_evolution_incl_test.cpp | 22 +++++++++++++++++++ test/compile_test/jso_incl_test.cpp | 22 +++++++++++++++++++ test/compile_test/random_search_incl_test.cpp | 22 +++++++++++++++++++ 9 files changed, 75 insertions(+), 19 deletions(-) create mode 100644 test/compile_test/differential_evolution_incl_test.cpp create mode 100644 test/compile_test/jso_incl_test.cpp create mode 100644 test/compile_test/random_search_incl_test.cpp diff --git a/include/boost/math/optimization/cma_es.hpp b/include/boost/math/optimization/cma_es.hpp index 9470c60a7..42b901a19 100644 --- a/include/boost/math/optimization/cma_es.hpp +++ b/include/boost/math/optimization/cma_es.hpp @@ -6,7 +6,6 @@ */ #ifndef BOOST_MATH_OPTIMIZATION_CMA_ES_HPP #define BOOST_MATH_OPTIMIZATION_CMA_ES_HPP -#include #include #include #include @@ -14,7 +13,6 @@ #include #include #include -#include #include #include #include @@ -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(x.size()) == n); + BOOST_MATH_ASSERT(static_cast(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}_{w} Eigen::Vector 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 C_inv_sqrt_y_avg = B*inv_D_B_transpose_y; diff --git a/include/boost/math/optimization/detail/common.hpp b/include/boost/math/optimization/detail/common.hpp index f98fbd62b..05124026e 100644 --- a/include/boost/math/optimization/detail/common.hpp +++ b/include/boost/math/optimization/detail/common.hpp @@ -6,12 +6,13 @@ */ #ifndef BOOST_MATH_OPTIMIZATION_DETAIL_COMMON_HPP #define BOOST_MATH_OPTIMIZATION_DETAIL_COMMON_HPP -#include +#include // for std::sort #include #include #include #include #include +#include // for std::false_type namespace boost::math::optimization::detail { @@ -114,6 +115,7 @@ std::vector random_initial_population(ArgumentContainer const template 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) { diff --git a/include/boost/math/optimization/differential_evolution.hpp b/include/boost/math/optimization/differential_evolution.hpp index 6298f3bf9..1c34d053d 100644 --- a/include/boost/math/optimization/differential_evolution.hpp +++ b/include/boost/math/optimization/differential_evolution.hpp @@ -6,21 +6,15 @@ */ #ifndef BOOST_MATH_OPTIMIZATION_DIFFERENTIAL_EVOLUTION_HPP #define BOOST_MATH_OPTIMIZATION_DIFFERENTIAL_EVOLUTION_HPP -#include -#include #include #include -#include #include -#include #include -#include #include #include #include #include #include -#include #include #include diff --git a/include/boost/math/optimization/jso.hpp b/include/boost/math/optimization/jso.hpp index e48d93836..25bc5e8fc 100644 --- a/include/boost/math/optimization/jso.hpp +++ b/include/boost/math/optimization/jso.hpp @@ -6,14 +6,11 @@ */ #ifndef BOOST_MATH_OPTIMIZATION_JSO_HPP #define BOOST_MATH_OPTIMIZATION_JSO_HPP -#include -#include #include #include #include #include #include -#include #include #include #include diff --git a/include/boost/math/optimization/random_search.hpp b/include/boost/math/optimization/random_search.hpp index b419c2406..4ecd4ce36 100644 --- a/include/boost/math/optimization/random_search.hpp +++ b/include/boost/math/optimization/random_search.hpp @@ -6,18 +6,14 @@ */ #ifndef BOOST_MATH_OPTIMIZATION_RANDOM_SEARCH_HPP #define BOOST_MATH_OPTIMIZATION_RANDOM_SEARCH_HPP -#include -#include #include #include #include -#include #include #include #include #include #include -#include #include #include #include diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index bb33ba7a0..f616a1397 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -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 : : 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 : diff --git a/test/compile_test/differential_evolution_incl_test.cpp b/test/compile_test/differential_evolution_incl_test.cpp new file mode 100644 index 000000000..9347f3074 --- /dev/null +++ b/test/compile_test/differential_evolution_incl_test.cpp @@ -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 +// +// 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 const & v) { return v[0]*v[0]; }; + boost::math::optimization::differential_evolution_parameters> params; + std::mt19937_64 gen(12345); + auto v = boost::math::optimization::differential_evolution(f, params, gen); +} diff --git a/test/compile_test/jso_incl_test.cpp b/test/compile_test/jso_incl_test.cpp new file mode 100644 index 000000000..2e1d91113 --- /dev/null +++ b/test/compile_test/jso_incl_test.cpp @@ -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 +// +// 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 const & v) { return v[0]*v[0]; }; + boost::math::optimization::jso_parameters> params; + std::mt19937_64 gen(12345); + auto v = boost::math::optimization::jso(f, params, gen); +} diff --git a/test/compile_test/random_search_incl_test.cpp b/test/compile_test/random_search_incl_test.cpp new file mode 100644 index 000000000..ddffd733c --- /dev/null +++ b/test/compile_test/random_search_incl_test.cpp @@ -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 +// +// 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 const & v) { return v[0]*v[0]; }; + boost::math::optimization::random_search_parameters> params; + std::mt19937_64 gen(12345); + auto v = boost::math::optimization::random_search(f, params, gen); +}