Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
Broyden.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
2 * Copyright (c) 2025, Davide Stocco and Enrico Bertolazzi. *
3 * *
4 * The Optimist project is distributed under the BSD 2-Clause License. *
5 * *
6 * Davide Stocco Enrico Bertolazzi *
7 * University of Trento University of Trento *
8 * davide.stocco@unitn.it enrico.bertolazzi@unitn.it *
9\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
10
11#pragma once
12
13#ifndef OPTIMIST_ROOTFINDER_BROYDEN_HH
14#define OPTIMIST_ROOTFINDER_BROYDEN_HH
15
17
18namespace Optimist {
19 namespace RootFinder {
20
21 /*\
22 | ____ _
23 | | __ ) _ __ ___ _ _ __| | ___ _ __
24 | | _ \| '__/ _ \| | | |/ _` |/ _ \ '_ \
25 | | |_) | | | (_) | |_| | (_| | __/ | | |
26 | |____/|_| \___/ \__, |\__,_|\___|_| |_|
27 | |___/
28 \*/
29
37 template <typename Vector>
38 requires TypeTrait<Vector>::IsEigen &&
39 (!TypeTrait<Vector>::IsFixed || TypeTrait<Vector>::Dimension > 0)
40 class Broyden : public QuasiNewton<Vector, Broyden<Vector>> {
41 public:
42 static constexpr bool RequiresFunction{true};
43 static constexpr bool RequiresFirstDerivative{true};
44 static constexpr bool RequiresSecondDerivative{false};
45
49
51
52
55 using Method = enum class Method : Integer {
56 GOOD = 0,
57 BAD = 1,
58 COMBINED = 2
59 };
60
61 private:
62 Method m_method{Method::COMBINED};
63
64 public:
69
74 constexpr std::string name_impl() const {
75 return "Broyden";
76 }
77
82 Method method() const {
83 return this->m_method;
84 }
85
90 void method(Method t_method) {
91 this->m_method = t_method;
92 }
93
98 this->m_method = Method::GOOD;
99 }
100
105 this->m_method = Method::BAD;
106 }
107
112 this->m_method = Method::COMBINED;
113 }
114
119 void set_method(Method t_method) {
120 this->m_method = t_method;
121 }
122
133 void update_impl(const Vector &delta_x_old,
134 const Vector &delta_function_old,
135 const FirstDerivative &jacobian_old,
136 const Vector &delta_x_new,
137 const Vector &delta_function_new,
138 const Vector & /*function_new*/,
139 FirstDerivative &jacobian_new) {
140 Vector tmp_1(jacobian_old * delta_function_new);
141 Scalar tmp_2{delta_function_new.squaredNorm()};
142 // Selection criteria: |(dx_new'*dx_old) / (dx_new'*J_old*dF_new)| <
143 // |(dF_new'*dF_old) / (dF_new'*dF_new)|
144 if (this->m_method == Method::COMBINED ||
145 this->m_method == Method::GOOD || this->iterations() < Integer(2) ||
146 std::abs(delta_x_new.dot(delta_x_old)) /
147 std::abs(delta_x_new.dot(tmp_1)) <
148 std::abs(delta_function_new.dot(delta_function_old)) / tmp_2) {
149 // Broyden's Good solver: J_new = J_old - (J_old*dF_new-dx_new) /
150 // (C'*dF_new)*C', where C = J_old'*dx_new;
151 Vector C_g(jacobian_old.transpose() * delta_x_new);
152 jacobian_new = jacobian_old - (tmp_1 - delta_x_new) /
153 (C_g.dot(delta_function_new)) *
154 C_g.transpose();
155 } else {
156 // Broyden's Bad solver: J_new = J_old - (J_old*dF_old-dx_new) /
157 // (C'*dF_old)*C', where C = dF_old;
158 jacobian_new = jacobian_old - (tmp_1 - delta_x_new) / tmp_2 *
159 delta_function_old.transpose();
160 }
161 }
162
163 }; // class Broyden
164
165 } // namespace RootFinder
166
167} // namespace Optimist
168
169#endif // OPTIMIST_ROOTFINDER_BROYDEN_HH
#define OPTIMIST_BASIC_CONSTANTS(Scalar)
Definition Optimist.hh:69
void enable_good_method()
Definition Broyden.hh:97
Method method() const
Definition Broyden.hh:82
static constexpr bool RequiresFunction
Definition Broyden.hh:42
void enable_combined_method()
Definition Broyden.hh:111
TypeTrait< Vector > VectorTrait
Definition Broyden.hh:46
constexpr std::string name_impl() const
Definition Broyden.hh:74
static constexpr bool RequiresSecondDerivative
Definition Broyden.hh:44
enum class Method :Integer { GOOD=0, BAD=1, COMBINED=2 } Method
Definition Broyden.hh:55
void enable_bad_method()
Definition Broyden.hh:104
void update_impl(const Vector &delta_x_old, const Vector &delta_function_old, const FirstDerivative &jacobian_old, const Vector &delta_x_new, const Vector &delta_function_new, const Vector &, FirstDerivative &jacobian_new)
Definition Broyden.hh:133
Broyden()
Definition Broyden.hh:68
void set_method(Method t_method)
Definition Broyden.hh:119
typename TypeTrait< Vector >::Scalar Scalar
Definition Broyden.hh:47
Method m_method
Definition Broyden.hh:62
static constexpr bool RequiresFirstDerivative
Definition Broyden.hh:43
void method(Method t_method)
Definition Broyden.hh:90
std::conditional_t< InputTrait::IsEigen||OutputTrait::IsEigen, std::conditional_t< InputTrait::IsSparse||OutputTrait::IsSparse, Eigen::SparseMatrix< Scalar >, Eigen::Matrix< Scalar, OutputTrait::Dimension, InputTrait::Dimension > >, Scalar > FirstDerivative
Definition SolverBase.hh:80
Integer iterations() const
Definition SolverBase.hh:440
Namespace for the Optimist library.
Definition Optimist.hh:89
OPTIMIST_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Optimist.hh:97
Definition Optimist.hh:113