2
0
mirror of https://github.com/boostorg/math.git synced 2026-02-27 05:02:32 +00:00

Units compatibility for optimization

This commit is contained in:
Nick Thompson
2024-02-10 12:36:12 -08:00
committed by Nick
parent 91f3a22169
commit f8da4ba2be
10 changed files with 187 additions and 78 deletions

View File

@@ -146,6 +146,18 @@ void test_beale() {
CHECK_ABSOLUTE_ERROR(Real(1)/Real(2), local_minima[1], Real(0.1));
}
#if BOOST_MATH_TEST_UNITS_COMPATIBILITY
void test_dimensioned_sphere() {
std::cout << "Testing CMA-ES on dimensioned sphere . . .\n";
using ArgType = std::vector<quantity<length>>;
auto params = cma_es_parameters<ArgType>();
params.lower_bounds.resize(4, -1.0*meter);
params.upper_bounds.resize(4, 1*meter);
std::mt19937_64 gen(56789);
auto local_minima = cma_es(dimensioned_sphere, params, gen);
}
#endif
int main() {
#if (defined(__clang__) || defined(_MSC_VER))
test_ackley<float>();
@@ -154,6 +166,9 @@ int main() {
test_rastrigin<double>();
test_three_hump_camel<float>();
test_beale<double>();
#endif
#if BOOST_MATH_TEST_UNITS_COMPATIBILITY
test_dimensioned_sphere();
#endif
test_sphere();
return boost::math::test::report_errors();

View File

@@ -188,6 +188,19 @@ void test_beale() {
CHECK_ABSOLUTE_ERROR(Real(1)/Real(2), local_minima[1], Real(2e-4));
}
#if BOOST_MATH_TEST_UNITS_COMPATIBILITY
void test_dimensioned_sphere() {
std::cout << "Testing differential evolution on dimensioned sphere . . .\n";
using ArgType = std::vector<quantity<length>>;
auto params = differential_evolution_parameters<ArgType>();
params.lower_bounds.resize(4, -1.0*meter);
params.upper_bounds.resize(4, 1*meter);
params.threads = 2;
std::mt19937_64 gen(56789);
auto local_minima = differential_evolution(dimensioned_sphere, params, gen);
}
#endif
int main() {
#if defined(__clang__) || defined(_MSC_VER)
@@ -197,6 +210,9 @@ int main() {
test_rastrigin<float>();
test_three_hump_camel<float>();
test_beale<double>();
#endif
#if BOOST_MATH_TEST_UNITS_COMPATIBILITY
test_dimensioned_sphere();
#endif
test_sphere();
test_parameter_checks();

View File

@@ -143,6 +143,19 @@ void test_beale() {
CHECK_ABSOLUTE_ERROR(Real(1)/Real(2), local_minima[1], Real(2e-4));
}
#if BOOST_MATH_TEST_UNITS_COMPATIBILITY
void test_dimensioned_sphere() {
std::cout << "Testing jso on dimensioned sphere . . .\n";
using ArgType = std::vector<quantity<length>>;
auto params = jso_parameters<ArgType>();
params.lower_bounds.resize(4, -1.0*meter);
params.upper_bounds.resize(4, 1*meter);
params.threads = 2;
std::mt19937_64 gen(56789);
auto local_minima = jso(dimensioned_sphere, params, gen);
}
#endif
int main() {
#if defined(__clang__) || defined(_MSC_VER)
test_ackley<float>();
@@ -151,6 +164,9 @@ int main() {
test_rastrigin<double>();
test_three_hump_camel<float>();
test_beale<double>();
#endif
#if BOOST_MATH_TEST_UNITS_COMPATIBILITY
test_dimensioned_sphere();
#endif
test_sphere();
test_weighted_lehmer_mean();

View File

@@ -10,7 +10,6 @@
#include <boost/math/optimization/random_search.hpp>
#include <random>
#include <limits>
using boost::math::optimization::random_search;
using boost::math::optimization::random_search_parameters;
@@ -149,6 +148,21 @@ void test_beale() {
CHECK_ABSOLUTE_ERROR(Real(1)/Real(2), local_minima[1], Real(0.1));
}
#if BOOST_MATH_TEST_UNITS_COMPATIBILITY
void test_dimensioned_sphere() {
std::cout << "Testing random search on dimensioned sphere . . .\n";
using ArgType = std::vector<quantity<length>>;
auto rs_params = random_search_parameters<ArgType>();
rs_params.lower_bounds.resize(4, -1.0*meter);
rs_params.upper_bounds.resize(4, 1*meter);
rs_params.max_function_calls = 100000;
rs_params.threads = 2;
std::mt19937_64 gen(56789);
auto local_minima = random_search(dimensioned_sphere, rs_params, gen);
}
#endif
int main() {
#if defined(__clang__) || defined(_MSC_VER)
test_ackley<float>();
@@ -157,6 +171,9 @@ int main() {
test_rastrigin<double>();
test_three_hump_camel<float>();
test_beale<double>();
#endif
#if BOOST_MATH_TEST_UNITS_COMPATIBILITY
test_dimensioned_sphere();
#endif
test_sphere();
return boost::math::test::report_errors();

View File

@@ -6,7 +6,37 @@
*/
#ifndef TEST_FUNCTIONS_FOR_OPTIMIZATION_HPP
#define TEST_FUNCTIONS_FOR_OPTIMIZATION_HPP
#include <array>
#include <vector>
#include <boost/math/constants/constants.hpp>
#if __has_include(<boost/units/systems/si/length.hpp>)
// This is the only system boost.units still works on.
// I imagine this will start to fail at some point,
// and we'll have to remove this test as well.
#if defined(__APPLE__)
#define BOOST_MATH_TEST_UNITS_COMPATIBILITY 1
#include <boost/units/systems/si/length.hpp>
#include <boost/units/systems/si/area.hpp>
#include <boost/units/cmath.hpp>
#include <boost/units/quantity.hpp>
#include <boost/units/systems/si/io.hpp>
using namespace boost::units;
using namespace boost::units::si;
// This *should* return an area, but see: https://github.com/boostorg/units/issues/58
// This sadly prevents std::atomic<quantity<area>>.
// Nonetheless, we *do* get some information making the argument type dimensioned,
// even if it would be better to get the full information:
double dimensioned_sphere(std::vector<quantity<length>> const & v) {
quantity<area> r(0.0*meters*meters);
for (auto const & x : v) {
r += (x * x);
}
quantity<area> scale(1.0*meters*meters);
return static_cast<double>(r/scale);
}
#endif
#endif
// Taken from: https://en.wikipedia.org/wiki/Test_functions_for_optimization
template <typename Real> Real ackley(std::array<Real, 2> const &v) {