numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
single_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
31
32namespace num_prob_collect::opt {
33
39public:
41 using variable_type = double;
43 using value_type = double;
44
57
63 void evaluate_on(double variable) noexcept {
64 value_ = ((optimal_variables_ - variable).square() * coefficients_ +
66 .minCoeff();
67 }
68
74 [[nodiscard]] auto value() const noexcept -> const double& {
75 return value_;
76 }
77
83 [[nodiscard]] auto optimal_variables() const noexcept
84 -> const Eigen::ArrayXd& {
85 return optimal_variables_;
86 }
87
93 [[nodiscard]] auto optimal_values() const noexcept
94 -> const Eigen::ArrayXd& {
95 return optimal_values_;
96 }
97
103 [[nodiscard]] auto coefficients() const noexcept -> const Eigen::ArrayXd& {
104 return coefficients_;
105 }
106
107private:
109 Eigen::ArrayXd optimal_variables_;
110
112 Eigen::ArrayXd optimal_values_;
113
115 Eigen::ArrayXd coefficients_;
116
118 double value_{0.0};
119};
120
125public:
127 static constexpr double min_variable = -10.0;
128
130 static constexpr double max_variable = 10.0;
131
133 static constexpr double global_optimal_value = 0.0;
134
136 static constexpr double min_non_global_optimal_value = 1.0;
137
139 static constexpr double max_non_global_optimal_value = 5.0;
140
143
151 : num_local_optima_(num_local_optima) {
153 num_local_optima >= 2, "num_local_optima must be at least 2.");
154 }
155
161 [[nodiscard]] auto operator()() noexcept
163 Eigen::ArrayXd optimal_variables;
164 optimal_variables.resize(num_local_optima_);
165 std::generate(optimal_variables.begin(), optimal_variables.end(),
166 [this] { return optimal_variable_dist_(generator_); });
167
168 Eigen::ArrayXd optimal_values;
169 optimal_values.resize(num_local_optima_);
170 optimal_values[0] = global_optimal_value;
171 std::generate(optimal_values.begin() + 1, optimal_values.end(),
172 [this] { return non_global_optimal_value_dist_(generator_); });
173
174 Eigen::ArrayXd coefficients;
175 coefficients.resize(num_local_optima_);
176 std::generate(coefficients.begin(), coefficients.end(),
177 [this] { return coefficients_dist_(generator_); });
178
180 std::move(optimal_variables), std::move(optimal_values),
181 std::move(coefficients)};
182 }
183
184private:
186 std::mt19937
187 generator_{}; // NOLINT(cert-msc32-c,cert-msc51-cpp): For reproducibility.
188
190 std::uniform_real_distribution<double> optimal_variable_dist_{
192
196
198 std::uniform_real_distribution<double> coefficients_dist_{
199 0.5, 3.0}; // NOLINT
200
203};
204
205} // namespace num_prob_collect::opt
Class to generate random single_variate_multi_optima_function objects.
static constexpr num_collect::index_type default_num_local_optima
Default value of the number of local optima.
std::uniform_real_distribution< double > coefficients_dist_
Distribution of coefficients.
std::uniform_real_distribution< double > non_global_optimal_value_dist_
Distribution of non-global optimal values.
auto operator()() noexcept -> single_variate_multi_optima_function
Generate a function.
std::uniform_real_distribution< double > optimal_variable_dist_
Distribution of optimal variables.
random_single_variate_multi_optima_function_generator(num_collect::index_type num_local_optima=default_num_local_optima)
Constructor.
Class of functions of single-variate optimization problems with multiple local optima.
single_variate_multi_optima_function(Eigen::ArrayXd optimal_variables, Eigen::ArrayXd optimal_values, Eigen::ArrayXd coefficients)
Constructor.
void evaluate_on(double variable) noexcept
Evaluate function value on variable.
auto optimal_variables() const noexcept -> const Eigen::ArrayXd &
Get the optimal variables.
auto coefficients() const noexcept -> const Eigen::ArrayXd &
Get the coefficients for optima.
auto optimal_values() const noexcept -> const Eigen::ArrayXd &
Get the optimal values.
auto value() const noexcept -> const double &
Get function value.
Definition of exceptions.
Definition of index_type type.
Definition of macros for logging.
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.
Definition of NUM_COLLECT_PRECONDITION macro.
#define NUM_COLLECT_PRECONDITION(CONDITION,...)
Check whether a precondition is satisfied and throw an exception if not.