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, Mattia Piazza and Enrico Bertolazzi. *
3 * *
4 * The Optimist project is distributed under the BSD 2-Clause License. *
5 * *
6 * Davide Stocco Mattia Piazza Enrico Bertolazzi *
7 * University of Trento University of Trento University of Trento *
8 * davide.stocco@unitn.it mattia.piazza@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{
20 namespace RootFinder
21 {
22
23 /*\
24 | ____ _
25 | | __ ) _ __ ___ _ _ __| | ___ _ __
26 | | _ \| '__/ _ \| | | |/ _` |/ _ \ '_ \
27 | | |_) | | | (_) | |_| | (_| | __/ | | |
28 | |____/|_| \___/ \__, |\__,_|\___|_| |_|
29 | |___/
30 \*/
31
40 template <typename Real, Integer N>
41 class Broyden : public QuasiNewton<Real, N, Broyden<Real, N>>
42 {
43 public:
44 static constexpr bool requires_function{true};
45 static constexpr bool requires_first_derivative{true};
46 static constexpr bool requires_second_derivative{false};
47
49
50 using Method = enum class Method : Integer {GOOD = 0, BAD = 1, COMBINED = 2};
56
57 private:
58 Method m_method{Method::COMBINED};
59
60 public:
65
70 std::string name_impl() const
71 {
72 std::ostringstream os;
73 os << "Broyden";
74 if (this->m_method == Method::GOOD) {
75 os << "Good";
76 } else if (this->m_method == Method::BAD) {
77 os << "Bad";
78 } else if (this->m_method == Method::COMBINED) {
79 os << "Combined";
80 }
81 return os.str();
82 }
83
88 Method method() const {return this->m_method;}
89
94 void method(Method t_method) {this->m_method = t_method;}
95
99 void enable_good_method() {this->m_method = Method::GOOD;}
100
104 void enable_bad_method() {this->m_method = Method::BAD;}
105
109 void enable_combined_method() {this->m_method = Method::COMBINED;}
110
115 void set_method(Method t_method) {this->m_method = t_method;}
116
128 Vector const & delta_x_old, Vector const & delta_function_old, Matrix const & jacobian_old,
129 Vector const & delta_x_new, Vector const & delta_function_new, Vector const & /*function_new*/,
130 Matrix & jacobian_new
131 ) {
132 Vector tmp_1(jacobian_old * delta_function_new);
133 Real tmp_2{delta_function_new.squaredNorm()};
134 // Selection criteria: |(dx_new'*dx_old) / (dx_new'*J_old*dF_new)| < |(dF_new'*dF_old) / (dF_new'*dF_new)|
135 if (this->m_method == Method::COMBINED || this->m_method == Method::GOOD || this->iterations() < Integer(2) ||
136 std::abs(delta_x_new.transpose() * delta_x_old) / std::abs(delta_x_new.transpose() * tmp_1)
137 < std::abs(delta_function_new.transpose() * delta_function_old) / tmp_2) {
138 // Broyden's Good solver: J_new = J_old - (J_old*dF_new-dx_new) / (C'*dF_new)*C', where C = J_old'*dx_new;
139 Vector C_g(jacobian_old.transpose() * delta_x_new);
140 jacobian_new = jacobian_old - (tmp_1 - delta_x_new) / (C_g.transpose() * delta_function_new) * C_g.transpose();
141 } else {
142 // Broyden's Bad solver: J_new = J_old - (J_old*dF_old-dx_new) / (C'*dF_old)*C', where C = dF_old;
143 jacobian_new = jacobian_old - (tmp_1 - delta_x_new) / tmp_2 * delta_function_old.transpose();
144 }
145 }
146
147 }; // class Broyden
148
149 } // namespace RootFinder
150
151} // namespace Optimist
152
153#endif // OPTIMIST_ROOTFINDER_BROYDEN_HH
#define OPTIMIST_BASIC_CONSTANTS(Real)
Definition Optimist.hh:70
static constexpr bool requires_function
Definition Broyden.hh:44
void method(Method t_method)
Definition Broyden.hh:94
Broyden()
Definition Broyden.hh:64
static constexpr bool requires_first_derivative
Definition Broyden.hh:45
typename QuasiNewton< Real, N, Broyden< Real, N > >::Matrix Matrix
Definition Broyden.hh:52
void enable_good_method()
Definition Broyden.hh:99
void enable_bad_method()
Definition Broyden.hh:104
static constexpr bool requires_second_derivative
Definition Broyden.hh:46
Method m_method
Definition Broyden.hh:58
std::string name_impl() const
Definition Broyden.hh:70
void enable_combined_method()
Definition Broyden.hh:109
Method method() const
Definition Broyden.hh:88
void update_impl(Vector const &delta_x_old, Vector const &delta_function_old, Matrix const &jacobian_old, Vector const &delta_x_new, Vector const &delta_function_new, Vector const &, Matrix &jacobian_new)
Definition Broyden.hh:127
enum class Method :Integer {GOOD=0, BAD=1, COMBINED=2} Method
Definition Broyden.hh:50
typename QuasiNewton< Real, N, Broyden< Real, N > >::JacobianWrapper JacobianWrapper
Definition Broyden.hh:54
void set_method(Method t_method)
Definition Broyden.hh:115
typename QuasiNewton< Real, N, Broyden< Real, N > >::Vector Vector
Definition Broyden.hh:51
typename QuasiNewton< Real, N, Broyden< Real, N > >::FunctionWrapper FunctionWrapper
Definition Broyden.hh:53
bool solve(FunctionWrapper function, Vector const &x_ini, Vector &x_sol)
Definition RootFinder.hh:164
Integer iterations() const
Definition Solver.hh:314
Namespace for the Optimist library.
Definition Optimist.hh:87
OPTIMIST_DEFAULT_INTEGER_TYPE Integer
The Integer type as used for the API.
Definition Optimist.hh:95