numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
random_quadratic_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 <random>
23
24namespace num_prob_collect::opt {
25
31public:
33 using variable_type = double;
35 using value_type = double;
36
45
51 void evaluate_on(double variable) noexcept {
52 const double diff = variable - optimal_variable_;
53 value_ = coeff_ * diff * diff;
54 }
55
61 [[nodiscard]] auto value() const noexcept -> const double& {
62 return value_;
63 }
64
70 [[nodiscard]] auto coeff() const noexcept -> double { return coeff_; }
71
77 [[nodiscard]] auto optimal_variable() const noexcept -> double {
78 return optimal_variable_;
79 }
80
81private:
83 double coeff_;
84
87
89 double value_{0.0};
90};
91
96public:
98 static constexpr double min_variable = -10.0;
99
101 static constexpr double max_variable = 10.0;
102
107
113 [[nodiscard]] auto operator()() noexcept -> random_quadratic_function {
114 const double coeff = coeff_dist_(generator_);
115 const double optimal_variable = optimal_variable_dist_(generator_);
116 return random_quadratic_function(coeff, optimal_variable);
117 }
118
119private:
121 std::mt19937
122 generator_{}; // NOLINT(cert-msc32-c,cert-msc51-cpp): For reproducibility.
123
125 std::uniform_real_distribution<double> coeff_dist_{0.5, 3.0}; // NOLINT
126
128 std::uniform_real_distribution<double> optimal_variable_dist_{
130};
131
132} // namespace num_prob_collect::opt
std::uniform_real_distribution< double > coeff_dist_
Distribution of coefficients.
random_quadratic_function_generator() noexcept=default
Constructor.
static constexpr double max_variable
Maximum optimal variable.
static constexpr double min_variable
Minimum optimal variable.
std::uniform_real_distribution< double > optimal_variable_dist_
Distribution of optimal variables.
Class of quadratic functions with random coefficients and optimal variables.
random_quadratic_function(double coeff, double optimal_variable) noexcept
Constructor.
auto optimal_variable() const noexcept -> double
Get the optimal variable.
auto coeff() const noexcept -> double
Get the coefficient.
void evaluate_on(double variable) noexcept
Evaluate function value on variable.
auto value() const noexcept -> const double &
Get function value.
Namespace of optimization problems.
Definition namespaces.h:25