numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
conjugate_gradient_optimizer.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
29
30namespace num_collect::opt {
31
34 logging::log_tag_view("num_collect::opt::conjugate_gradient_optimizer");
35
48template <
50 concepts::line_searcher LineSearcher =
53 : public descent_method_base<
54 conjugate_gradient_optimizer<ObjectiveFunction, LineSearcher>,
55 LineSearcher> {
56public:
58 using this_type =
60
63
65 using typename base_type::variable_type;
66
68 using variable_scalar_type = typename variable_type::Scalar;
69
78
86 using typename base_type::value_type;
87
93 void init(const variable_type& init_variable) {
94 base_type::init(init_variable);
96 }
97
101 [[nodiscard]] auto calc_direction() -> variable_type {
103 variable_scalar_type prev_coeff =
104 gradient().dot(gradient() - prev_grad_) /
105 prev_grad_.squaredNorm();
106 if (prev_coeff < static_cast<variable_scalar_type>(0)) {
107 prev_coeff = static_cast<variable_scalar_type>(0);
108 }
109 direction_ = -gradient() + prev_coeff * direction_;
111 } else {
112 direction_ = -gradient();
115 }
116 return direction_;
117 }
118
124 const {
125 iteration_logger.template append<index_type>(
126 "Iter.", &base_type::iterations);
127 iteration_logger.template append<index_type>(
128 "Eval.", &base_type::evaluations);
129 iteration_logger.template append<value_type>(
130 "Value", &base_type::opt_value);
131 iteration_logger.template append<value_type>(
132 "Grad.", &base_type::gradient_norm);
133 }
134
135private:
138
141
144};
145
146} // namespace num_collect::opt
Definition of backtracking_line_searcher class.
Class of tags of logs without memory management.
Class to perform backtracking line search.
Class of conjugate gradient method for optimization.
typename variable_type::Scalar variable_scalar_type
Type of scalars in variables.
void configure_iteration_logger(logging::iterations::iteration_logger< this_type > &iteration_logger) const
Configure an iteration logger.
bool has_first_iteration_done_
Whether the first iteration has been done.
auto gradient() const -> const variable_type &
Get gradient for current optimal variable.
typename objective_function_type::variable_type variable_type
Type of variables.
auto calc_direction() -> variable_type
Calculate search direction.
void init(const variable_type &init_variable)
Initialize.
typename line_searcher_type::objective_function_type objective_function_type
Type of the objective function.
conjugate_gradient_optimizer(const objective_function_type &obj_fun=objective_function_type())
Constructor.
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.
typename objective_function_type::variable_type variable_type
Type of variables.
void init(const variable_type &init_variable)
Initialize.
auto evaluations() const noexcept -> index_type
Get the number of function evaluations.
typename line_searcher_type::objective_function_type objective_function_type
Type of the objective function.
auto gradient_norm() const -> value_type
Calculate norm of gradient.
auto opt_value() const -> const value_type &
Get current optimal value.
auto line_searcher() -> line_searcher_type &
Access object to perform line search.
Concept of objects to perform line search in optimization.
Concept of multi-variate first-order differentiable objective functions in optimization.
Definition of descent_method_base class.
Definition of index_type type.
Definition of iteration_logger class.
Definition of line_searcher concept.
Definition of log_tag_view class.
Definition of multi_variate_differentiable_objective_function concept.
Namespace of optimization algorithms.
constexpr auto conjugate_gradient_optimizer_tag
Tag of conjugate_gradient_optimizer.