numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
bfgs_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
22#include <Eigen/Cholesky>
23#include <Eigen/Core>
24
33
34namespace num_collect::opt {
35
37constexpr auto bfgs_optimizer_tag =
38 logging::log_tag_view("num_collect::opt::bfgs_optimizer");
39
51template <
53 concepts::line_searcher LineSearcher =
56 Eigen::MatrixX<typename ObjectiveFunction::value_type>,
57 typename ObjectiveFunction::variable_type>
58 HessianSolver =
59 Eigen::LLT<Eigen::MatrixX<typename ObjectiveFunction::value_type>>>
61 : public descent_method_base<
62 bfgs_optimizer<ObjectiveFunction, LineSearcher, HessianSolver>,
63 LineSearcher> {
64public:
66 using this_type =
68
71
73 using typename base_type::variable_type;
74
76 using variable_scalar_type = typename variable_type::Scalar;
77
79 using hessian_solver_type = HessianSolver;
80
82 using hessian_type = typename hessian_solver_type::MatrixType;
83
92
100 using typename base_type::value_type;
101
107 void init(const variable_type& init_variable) {
108 base_type::init(init_variable);
109 const auto dimensions = init_variable.size();
110 approx_hessian_ = hessian_type::Identity(dimensions, dimensions);
112 }
113
117 [[nodiscard]] auto calc_direction() -> variable_type {
123 (diff_grad_ * diff_grad_.transpose()) /
124 (diff_grad_.dot(diff_var_)) -
125 (hessian_var_ * hessian_var_.transpose()) /
126 (diff_var_.dot(hessian_var_));
127 }
131
132 solver_.compute(approx_hessian_);
133 return -solver_.solve(prev_grad_);
134 }
135
141 const {
142 iteration_logger.template append<index_type>(
143 "Iter.", &base_type::iterations);
144 iteration_logger.template append<index_type>(
145 "Eval.", &base_type::evaluations);
146 iteration_logger.template append<value_type>(
147 "Value", &base_type::opt_value);
148 iteration_logger.template append<value_type>(
149 "Grad.", &base_type::gradient_norm);
150 }
151
152private:
155
158
161
164
167
170
173
176};
177
178} // 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 quasi-Newton method with Broyden-Fletcher-Goldfarb-Shanno (BFGS) formula.
typename variable_type::Scalar variable_scalar_type
Type of scalars in variables.
auto gradient() const -> const variable_type &
Get gradient for current optimal variable.
auto opt_variable() const -> const variable_type &
Get current optimal variable.
variable_type prev_grad_
Previous gradient.
variable_type diff_var_
Difference of variable.
HessianSolver hessian_solver_type
Type of solvers of linear equation of Hessian.
auto calc_direction() -> variable_type
Calculate search direction.
hessian_type approx_hessian_
Approximate Hessian.
typename objective_function_type::variable_type variable_type
Type of variables.
typename hessian_solver_type::MatrixType hessian_type
Type of Hessian.
variable_type diff_grad_
Difference of gradient.
void configure_iteration_logger(logging::iterations::iteration_logger< this_type > &iteration_logger) const
Configure an iteration logger.
void init(const variable_type &init_variable)
Initialize.
variable_type prev_var_
Previous variable.
typename line_searcher_type::objective_function_type objective_function_type
Type of the objective function.
hessian_solver_type solver_
Solver of linear equation of Hessian.
bool has_first_iteration_done_
Whether the first iteration has been done.
bfgs_optimizer(const objective_function_type &obj_fun=objective_function_type())
Constructor.
variable_type hessian_var_
approx_hessian_ * diff_var_
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 Eigen's solvers of linear equations.
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 eigen_solver_of concept.
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 bfgs_optimizer_tag
Tag of bfgs_optimizer.