numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
descent_method_base.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
26
27namespace num_collect::opt {
28
35template <typename Derived, concepts::line_searcher LineSearcher>
36class descent_method_base : public optimizer_base<Derived> {
37public:
39 using line_searcher_type = LineSearcher;
40
43 typename line_searcher_type::objective_function_type;
44
46 using variable_type = typename objective_function_type::variable_type;
47
49 using value_type = typename objective_function_type::value_type;
50
56 void init(const variable_type& init_variable) {
57 line_searcher_.init(init_variable);
58 iterations_ = 0;
59 }
60
66 [[nodiscard]] auto calc_direction() -> variable_type {
67 return derived().calc_direction();
68 }
69
73 void iterate() {
76 }
77
81 [[nodiscard]] auto is_stop_criteria_satisfied() const -> bool {
83 }
84
90 [[nodiscard]] auto line_searcher() -> line_searcher_type& {
91 return line_searcher_;
92 }
93
99 [[nodiscard]] auto line_searcher() const -> const line_searcher_type& {
100 return line_searcher_;
101 }
102
106 [[nodiscard]] auto opt_variable() const -> const variable_type& {
107 return line_searcher().opt_variable();
108 }
109
113 [[nodiscard]] auto opt_value() const -> const value_type& {
114 return line_searcher().opt_value();
115 }
116
120 [[nodiscard]] auto iterations() const noexcept -> index_type {
121 return iterations_;
122 }
123
127 [[nodiscard]] auto evaluations() const noexcept -> index_type {
128 return line_searcher().evaluations();
129 }
130
136 [[nodiscard]] auto gradient() const -> const variable_type& {
137 return line_searcher().gradient();
138 }
139
145 [[nodiscard]] auto gradient_norm() const -> value_type {
146 return gradient().norm();
147 }
148
155 auto tol_gradient_norm(const value_type& value) -> Derived& {
156 tol_grad_norm_ = value;
157 return derived();
158 }
159
160protected:
161 // use derived() function
162 using optimizer_base<Derived>::derived;
163
173
174private:
177
180
182 static inline const auto default_tol_grad_norm =
183 static_cast<value_type>(1e-3);
184
187};
188
189} // namespace num_collect::opt
auto derived() noexcept -> Derived &
Access derived object.
Class of tags of logs without memory management.
Base class of implementations of descent methods for optimization.
typename objective_function_type::value_type value_type
Type of function values.
auto gradient() const -> const variable_type &
Get gradient for current optimal variable.
auto opt_variable() const -> const variable_type &
Get current optimal variable.
auto iterations() const noexcept -> index_type
Get the number of iterations.
auto is_stop_criteria_satisfied() const -> bool
Determine if stopping criteria of the algorithm are satisfied.
auto tol_gradient_norm(const value_type &value) -> Derived &
Set tolerance of norm of gradient.
descent_method_base(logging::log_tag_view tag, const objective_function_type &obj_fun=objective_function_type())
Constructor.
value_type tol_grad_norm_
Tolerance of norm of gradient.
auto calc_direction() -> variable_type
Calculate search direction.
typename objective_function_type::variable_type variable_type
Type of variables.
line_searcher_type line_searcher_
Object to perform line search.
void init(const variable_type &init_variable)
Initialize.
auto evaluations() const noexcept -> index_type
Get the number of function evaluations.
void iterate()
Iterate the algorithm once.
index_type iterations_
Number of iterations.
typename line_searcher_type::objective_function_type objective_function_type
Type of the objective function.
static const auto default_tol_grad_norm
Default tolerance of norm of gradient.
auto gradient_norm() const -> value_type
Calculate norm of gradient.
LineSearcher line_searcher_type
Type of class to perform line search.
auto line_searcher() const -> const line_searcher_type &
Access object to perform line search.
auto opt_value() const -> const value_type &
Get current optimal value.
auto line_searcher() -> line_searcher_type &
Access object to perform line search.
Base class of implementations of optimization algorithms.
Definition of index_type type.
Definition of line_searcher concept.
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.
Definition of optimizer_base class.