numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
golden_section_search.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 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 <cmath>
23
29
30namespace num_collect::opt {
31
34 logging::log_tag_view("num_collect::opt::golden_section_search");
35
41template <concepts::single_variate_objective_function ObjectiveFunction>
43 : public optimizer_base<golden_section_search<ObjectiveFunction>> {
44public:
47
49 using objective_function_type = ObjectiveFunction;
50
52 using variable_type = typename objective_function_type::variable_type;
53
55 using value_type = typename objective_function_type::value_type;
56
67
74 obj_fun_ = obj_fun;
75 }
76
83 void init(variable_type end1, variable_type end2) {
84 end1_ = end1;
85 end2_ = end2;
86
88 obj_fun_.evaluate_on(mid1_);
89 value1_ = obj_fun_.value();
90
91 iterations_ = 0;
92 }
93
101 tol_section_len_ = value;
102 return *this;
103 }
104
108 void iterate() {
109 const variable_type mid2 = end2_ + (end1_ - end2_) * mid_point_ratio;
110 obj_fun_.evaluate_on(mid2);
111 const value_type value2 = obj_fun_.value();
112 if (value1_ < value2) {
113 // (end1_, mid1_, end2_) = (mid2, mid1_, end1_)
114 end2_ = end1_;
115 end1_ = mid2;
116 } else {
117 // (end1_, mid1_, end2_) = (mid1_, mid2, end2_)
118 end1_ = mid1_;
119 mid1_ = mid2;
120 value1_ = value2;
121 }
122 ++iterations_;
123 }
124
128 [[nodiscard]] auto is_stop_criteria_satisfied() const -> bool {
129 return section_len() < tol_section_len_;
130 }
131
137 const {
138 iteration_logger.template append<index_type>(
139 "Iter.", &this_type::iterations);
140 iteration_logger.template append<index_type>(
141 "Eval.", &this_type::evaluations);
142 iteration_logger.template append<value_type>(
143 "Value", &this_type::opt_value);
144 }
145
149 [[nodiscard]] auto opt_variable() const -> const variable_type& {
150 return mid1_;
151 }
152
156 [[nodiscard]] auto opt_value() const -> const value_type& {
157 return value1_;
158 }
159
163 [[nodiscard]] auto iterations() const noexcept -> index_type {
164 return iterations_;
165 }
166
170 [[nodiscard]] auto evaluations() const noexcept -> index_type {
171 return iterations_ + 1;
172 }
173
179 [[nodiscard]] auto section_len() const -> variable_type {
180 using std::abs;
181 return abs(end1_ - end2_);
182 }
183
185 static inline const auto mid_point_ratio =
186 static_cast<variable_type>((3.0 - std::sqrt(5.0)) / 2.0);
187
189 static inline const auto default_tol_section_len =
190 static_cast<variable_type>(1e-3);
191
192private:
195
198
201
204
207
210
213};
214
215} // namespace num_collect::opt
Class of tags of logs without memory management.
Class of golden section search method.
typename objective_function_type::variable_type variable_type
Type of variables.
auto opt_variable() const -> const variable_type &
Get current optimal variable.
static const auto default_tol_section_len
Default tolerance of length of section between end points.
index_type iterations_
Number of iterations.
ObjectiveFunction objective_function_type
Type of the objective function.
value_type value1_
Function value on end1_.
variable_type tol_section_len_
Tolerance of length of section between end points.
auto tol_section_len(variable_type value) -> golden_section_search &
Set the tolerance of length of section between end points.
auto section_len() const -> variable_type
Get length of section between end points.
objective_function_type obj_fun_
Objective function.
static const auto mid_point_ratio
Ratio of middle point.
auto is_stop_criteria_satisfied() const -> bool
Determine if stopping criteria of the algorithm are satisfied.
typename objective_function_type::value_type value_type
Type of function values.
auto opt_value() const -> const value_type &
Get current optimal value.
auto evaluations() const noexcept -> index_type
Get the number of function evaluations.
void configure_iteration_logger(logging::iterations::iteration_logger< this_type > &iteration_logger) const
Configure an iteration logger.
void change_objective_function(const objective_function_type &obj_fun)
Change the objective function.
golden_section_search(const objective_function_type &obj_fun=objective_function_type())
Constructor.
void iterate()
Iterate the algorithm once.
void init(variable_type end1, variable_type end2)
Initialize the algorithm.
auto iterations() const noexcept -> index_type
Get the number of iterations.
variable_type mid1_
Middle point near to end1_.
Base class of implementations of optimization algorithms.
Definition of index_type type.
Definition of iteration_logger class.
Definition of log_tag_view class.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
Namespace of optimization algorithms.
constexpr auto golden_section_search_tag
Tag of golden_section_search.
Definition of optimizer_base class.
Definition of single_variate_objective_function concept.