numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
multi_variate_multi_optima_function.h
Go to the documentation of this file.
1/*
2 * Copyright 2023 MusicScience37 (Kenta Kabashima)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
20#pragma once
21
22#include <algorithm>
23#include <random>
24
25#include <Eigen/Core>
26
29
30namespace num_prob_collect::opt {
31
37public:
39 using variable_type = Eigen::VectorXd;
40
42 using value_type = double;
43
45 using hessian_type = Eigen::MatrixXd;
46
59
65 void evaluate_on(const Eigen::VectorXd& variable) noexcept {
66 value_ = ((optimal_variables_.rowwise() - variable.transpose().array())
67 .square()
68 .rowwise()
69 .sum() *
72 .minCoeff();
73 }
74
80 [[nodiscard]] auto value() const noexcept -> const double& {
81 return value_;
82 }
83
89 [[nodiscard]] auto optimal_variables() const noexcept
90 -> const Eigen::ArrayXXd& {
91 return optimal_variables_;
92 }
93
99 [[nodiscard]] auto optimal_values() const noexcept
100 -> const Eigen::ArrayXd& {
101 return optimal_values_;
102 }
103
109 [[nodiscard]] auto coefficients() const noexcept -> const Eigen::ArrayXd& {
110 return coefficients_;
111 }
112
113private:
115 Eigen::ArrayXXd optimal_variables_;
116
118 Eigen::ArrayXd optimal_values_;
119
121 Eigen::ArrayXd coefficients_;
122
124 double value_{0.0};
125};
126
131public:
133 static constexpr double min_variable = -10.0;
134
136 static constexpr double max_variable = 10.0;
137
139 static constexpr double global_optimal_value = 0.0;
140
142 static constexpr double min_non_global_optimal_value = 1.0;
143
145 static constexpr double max_non_global_optimal_value = 5.0;
146
149
160
166 [[nodiscard]] auto operator()() noexcept
168 Eigen::ArrayXXd optimal_variables;
169 optimal_variables.resize(num_local_optima_, num_variables_);
170 auto reshaped_optimal_variables = optimal_variables.reshaped();
171 std::generate(reshaped_optimal_variables.begin(),
172 reshaped_optimal_variables.end(),
173 [this] { return optimal_variable_dist_(generator_); });
174
175 Eigen::ArrayXd optimal_values;
176 optimal_values.resize(num_local_optima_);
177 optimal_values[0] = global_optimal_value;
178 std::generate(optimal_values.begin() + 1, optimal_values.end(),
179 [this] { return non_global_optimal_value_dist_(generator_); });
180
181 Eigen::ArrayXd coefficients;
182 coefficients.resize(num_local_optima_);
183 std::generate(coefficients.begin(), coefficients.end(),
184 [this] { return coefficients_dist_(generator_); });
185
186 return multi_variate_multi_optima_function{std::move(optimal_variables),
187 std::move(optimal_values), std::move(coefficients)};
188 }
189
190private:
192 std::mt19937
193 generator_{}; // NOLINT(cert-msc32-c,cert-msc51-cpp): For reproducibility.
194
196 std::uniform_real_distribution<double> optimal_variable_dist_{
198
202
204 std::uniform_real_distribution<double> coefficients_dist_{
205 0.5, 3.0}; // NOLINT
206
209
212};
213
222public:
224 static constexpr double min_variable = -10.0;
225
227 static constexpr double max_variable = 10.0;
228
230 static constexpr double global_optimal_value = 0.0;
231
233 static constexpr double min_non_global_optimal_value = 1.0;
234
236 static constexpr double max_non_global_optimal_value = 5.0;
237
240
251
257 [[nodiscard]] auto operator()() noexcept
259 Eigen::ArrayXXd optimal_variables;
260 optimal_variables.resize(num_local_optima_, num_variables_);
261 auto reshaped_optimal_variables = optimal_variables.reshaped();
262 std::generate(reshaped_optimal_variables.begin(),
263 reshaped_optimal_variables.end(),
264 [this] { return optimal_variable_dist_(generator_); });
265
266 Eigen::ArrayXd optimal_values;
267 optimal_values.resize(num_local_optima_);
268 optimal_values[0] = global_optimal_value;
269 std::generate(optimal_values.begin() + 1, optimal_values.end(),
270 [this] { return non_global_optimal_value_dist_(generator_); });
271
272 Eigen::ArrayXd coefficients;
273 coefficients.resize(num_local_optima_);
274 coefficients[0] = 10.0; // NOLINT
275 std::generate(coefficients.begin() + 1, coefficients.end(),
276 [this] { return coefficients_dist_(generator_); });
277
278 return multi_variate_multi_optima_function{std::move(optimal_variables),
279 std::move(optimal_values), std::move(coefficients)};
280 }
281
282private:
284 std::mt19937
285 generator_{}; // NOLINT(cert-msc32-c,cert-msc51-cpp): For reproducibility.
286
288 std::uniform_real_distribution<double> optimal_variable_dist_{
290
294
296 std::uniform_real_distribution<double> coefficients_dist_{
297 0.5, 3.0}; // NOLINT
298
301
304};
305
306} // namespace num_prob_collect::opt
Class of functions of multi-variate optimization problems with multiple local optima.
auto optimal_values() const noexcept -> const Eigen::ArrayXd &
Get the optimal values.
multi_variate_multi_optima_function(Eigen::ArrayXXd optimal_variables, Eigen::ArrayXd optimal_values, Eigen::ArrayXd coefficients)
Constructor.
auto optimal_variables() const noexcept -> const Eigen::ArrayXXd &
Get the optimal variables.
void evaluate_on(const Eigen::VectorXd &variable) noexcept
Evaluate function value on variable.
auto value() const noexcept -> const double &
Get function value.
auto coefficients() const noexcept -> const Eigen::ArrayXd &
Get the coefficients for optima.
Class to generate difficult version of random multi_variate_multi_optima_function objects.
auto operator()() noexcept -> multi_variate_multi_optima_function
Generate a random quadratic function.
std::uniform_real_distribution< double > non_global_optimal_value_dist_
Distribution of non-global optimal values.
std::uniform_real_distribution< double > coefficients_dist_
Distribution of coefficients.
random_multi_variate_difficult_multi_optima_function_generator(num_collect::index_type num_variables, num_collect::index_type num_local_optima=default_num_local_optima)
Constructor.
static constexpr num_collect::index_type default_num_local_optima
Default value of the number of local optima.
std::uniform_real_distribution< double > optimal_variable_dist_
Distribution of optimal variables.
Class to generate random multi_variate_multi_optima_function objects.
auto operator()() noexcept -> multi_variate_multi_optima_function
Generate a random quadratic function.
std::uniform_real_distribution< double > non_global_optimal_value_dist_
Distribution of non-global optimal values.
random_multi_variate_multi_optima_function_generator(num_collect::index_type num_variables, num_collect::index_type num_local_optima=default_num_local_optima)
Constructor.
std::uniform_real_distribution< double > coefficients_dist_
Distribution of coefficients.
std::uniform_real_distribution< double > optimal_variable_dist_
Distribution of optimal variables.
static constexpr num_collect::index_type default_num_local_optima
Default value of the number of local optima.
Definition of exceptions.
Definition of index_type type.
Namespace of Eigen library.
Definition variable.h:416
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
Namespace of optimization problems.
Definition namespaces.h:25
STL namespace.