File: jso_test.cpp

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (158 lines) | stat: -rw-r--r-- 5,906 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
 * 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)
 */

#include "math_unit_test.hpp"
#include "test_functions_for_optimization.hpp"
#include <boost/math/optimization/jso.hpp>
#include <random>
#include <limits>

using boost::math::optimization::jso;
using boost::math::optimization::jso_parameters;
using boost::math::optimization::detail::weighted_lehmer_mean;

void test_weighted_lehmer_mean() {
  size_t n = 50;
  std::vector<double> weights(n, 1.0);
  std::vector<double> values(n, 2.5);
  // Technically, this is not a fully general weighted Lehmer mean,
  // but just a weighted contraharmonic mean.
  // So we have a few more invariants available to us:
  CHECK_ULP_CLOSE(2.5, weighted_lehmer_mean(values, weights), n);
  std::mt19937_64 gen(12345);
  std::uniform_real_distribution<double> unif(std::numeric_limits<double>::epsilon(),1);
  for (size_t i = 0; i < n; ++i) {
    weights[i] = unif(gen);
    values[i] = unif(gen);
  }
  auto mean = weighted_lehmer_mean(values, weights);
  CHECK_LE(mean, 1.0);
  CHECK_LE(std::numeric_limits<double>::epsilon(), mean);
}

template <class Real> void test_ackley() {
  std::cout << "Testing jSO on Ackley function . . .\n";
  using ArgType = std::array<Real, 2>;
  auto jso_params = jso_parameters<ArgType>();
  jso_params.lower_bounds = {-5, -5};
  jso_params.upper_bounds = {5, 5};

  std::mt19937_64 gen(12345);
  auto local_minima = jso(ackley<Real>, jso_params, gen);
  CHECK_LE(std::abs(local_minima[0]), 10 * std::numeric_limits<Real>::epsilon());
  CHECK_LE(std::abs(local_minima[1]), 10 * std::numeric_limits<Real>::epsilon());

  // Does it work with a lambda?
  auto ack = [](std::array<Real, 2> const &x) { return ackley<Real>(x); };
  local_minima = jso(ack, jso_params, gen);
  CHECK_LE(std::abs(local_minima[0]), 10 * std::numeric_limits<Real>::epsilon());
  CHECK_LE(std::abs(local_minima[1]), 10 * std::numeric_limits<Real>::epsilon());

  // Test that if an intial guess is the exact solution, the returned solution is the exact solution:
  std::array<Real, 2> initial_guess{0, 0};
  jso_params.initial_guess = &initial_guess;
  local_minima = jso(ack, jso_params, gen);

  using std::fabs;
  CHECK_LE(fabs(local_minima[0]), 128 * boost::math::tools::epsilon<Real>());
  CHECK_LE(fabs(local_minima[1]), 128 * boost::math::tools::epsilon<Real>());
}

template <class Real> void test_rosenbrock_saddle() {
  std::cout << "Testing jSO on Rosenbrock saddle . . .\n";
  using ArgType = std::array<Real, 2>;
  jso_parameters<ArgType> jso_params { };
  jso_params.lower_bounds = {0.5, 0.5};
  jso_params.upper_bounds = {2.048, 2.048};
  std::mt19937_64 gen(234568);
  auto local_minima = jso(rosenbrock_saddle<Real>, jso_params, gen);

  CHECK_ABSOLUTE_ERROR(Real(1), local_minima[0], 10 * std::numeric_limits<Real>::epsilon());
  CHECK_ABSOLUTE_ERROR(Real(1), local_minima[1], 10 * std::numeric_limits<Real>::epsilon());

  // Does cancellation work?
  std::atomic<bool> cancel = true;
  gen.seed(12345);
  local_minima =
      jso(rosenbrock_saddle<Real>, jso_params, gen, std::numeric_limits<Real>::quiet_NaN(), &cancel);
  CHECK_GE(std::abs(local_minima[0] - Real(1)), std::sqrt(std::numeric_limits<Real>::epsilon()));
}



template <class Real> void test_rastrigin() {
  std::cout << "Testing jSO on Rastrigin function (global minimum = (0,0,...,0))\n";
  using ArgType = std::vector<Real>;
  auto jso_params = jso_parameters<ArgType>();
  jso_params.lower_bounds.resize(3, static_cast<Real>(-5.12));
  jso_params.upper_bounds.resize(3, static_cast<Real>(5.12));
  jso_params.initial_population_size = 5000;
  jso_params.max_function_evaluations = 1000000;
  std::mt19937_64 gen(34567);

  // By definition, the value of the function which a target value is provided must be <= target_value.
  Real target_value = 1e-3;
  auto local_minima = jso(rastrigin<Real>, jso_params, gen, target_value);
  CHECK_LE(rastrigin(local_minima), target_value);
}

// Tests NaN return types and return type != input type:
void test_sphere() {
  std::cout << "Testing jSO on sphere . . .\n";
  using ArgType = std::vector<float>;
  auto jso_params = jso_parameters<ArgType>();
  jso_params.lower_bounds.resize(8, -1);
  jso_params.upper_bounds.resize(8, 1);
  std::mt19937_64 gen(56789);
  auto local_minima = jso(sphere, jso_params, gen);
  for (auto x : local_minima) {
    CHECK_ABSOLUTE_ERROR(0.0f, x, 2e-4f);
  }
}

template<typename Real>
void test_three_hump_camel() {
  std::cout << "Testing jSO on three hump camel . . .\n";
  using ArgType = std::array<Real, 2>;
  auto jso_params = jso_parameters<ArgType>();
  jso_params.lower_bounds[0] = -5.0;
  jso_params.lower_bounds[1] = -5.0;
  jso_params.upper_bounds[0] = 5.0;
  jso_params.upper_bounds[1] = 5.0;
  std::mt19937_64 gen(56789);
  auto local_minima = jso(three_hump_camel<Real>, jso_params, gen);
  for (auto x : local_minima) {
    CHECK_ABSOLUTE_ERROR(0.0f, x, 2e-4f);
  }
}

template<typename Real>
void test_beale() {
  std::cout << "Testing jSO on the Beale function . . .\n";
  using ArgType = std::array<Real, 2>;
  auto jso_params = jso_parameters<ArgType>();
  jso_params.lower_bounds[0] = -5.0;
  jso_params.lower_bounds[1] = -5.0;
  jso_params.upper_bounds[0]= 5.0;
  jso_params.upper_bounds[1]= 5.0;
  std::mt19937_64 gen(56789);
  auto local_minima = jso(beale<Real>, jso_params, gen);
  CHECK_ABSOLUTE_ERROR(Real(3), local_minima[0], Real(2e-4));
  CHECK_ABSOLUTE_ERROR(Real(1)/Real(2), local_minima[1], Real(2e-4));
}

int main() {
  test_ackley<float>();
  test_ackley<double>();
  test_rosenbrock_saddle<double>();
  test_rastrigin<double>();
  test_three_hump_camel<float>();
  test_beale<double>();
  test_sphere();
  test_weighted_lehmer_mean();
  return boost::math::test::report_errors();
}