Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
Bracketing.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_BRACKTING_HH
14#define OPTIMIST_ROOTFINDER_BRACKTING_HH
15
17
18namespace Optimist
19{
20 namespace RootFinder
21 {
22
23 /*\
24 | ____ _ _ _
25 | | __ ) _ __ __ _ ___| | _____| |_(_)_ __ __ _
26 | | _ \| '__/ _` |/ __| |/ / _ \ __| | '_ \ / _` |
27 | | |_) | | | (_| | (__| < __/ |_| | | | | (_| |
28 | |____/|_| \__,_|\___|_|\_\___|\__|_|_| |_|\__, |
29 | |___/
30 \*/
31
39 template <typename Real, typename DerivedSolver>
40 class Bracketing : public RootFinder<Real, 1, DerivedSolver>
41 {
42 public:
44
45 protected:
46 Real m_tolerance_bracketing{100*EPSILON};
47 Real m_mu{0.5};
48 Real m_interval_shink{0.025};
49
50 Real m_a{0.0}, m_fa{0.0};
51 Real m_b{0.0}, m_fb{0.0};
52 Real m_c{0.0}, m_fc{0.0};
53 Real m_d{0.0}, m_fd{0.0};
54 Real m_e{0.0}, m_fe{0.0};
55
56 public:
61
66 std::string name_impl() const
67 {
68 return static_cast<const DerivedSolver *>(this)->name_impl();
69 }
70
76 void tolerance_bracketing(Real t_tolerance) {this->m_tolerance_bracketing = t_tolerance;}
77
86 template <typename FunctionLambda>
87 bool solve_impl(FunctionLambda && function, Real /*x_ini*/, Real & x_sol)
88 {
89 #define CMD "Optimist::ScalarRootfinder::Bracketing::solve_impl(...): "
90
91 // Setup internal variables
92 bool success;
93 this->reset();
94
95 // Print header
96 if (this->m_verbose) {this->header();}
97
98 // Initialize variables
99 this->m_a = this->m_lower_bound;
100 success = this->evaluate_function(std::forward<FunctionLambda>(function), this->m_a, this->m_fa);
102 CMD "function evaluation failed at the lower bound.");
103
104 this->m_b = this->m_upper_bound;
105 success = this->evaluate_function(std::forward<FunctionLambda>(function), this->m_b, this->m_fb);
107 CMD "function evaluation failed at the upper bound.");
108
109 // Check if the solution exists
110 if (this->m_fa*this->m_fb > 0.0) {return false;}
111 else {x_sol = this->find_root(std::forward<FunctionLambda>(function));}
112
113 // Print bottom
114 if (this->m_verbose) {this->bottom();}
115
116 // Convergence data
117 return this->m_converged;
118
119 #undef CMD
120 }
121
129 template <typename FunctionLambda>
130 Real find_root(FunctionLambda && function)
131 {
132 return static_cast<DerivedSolver *>(this)->find_root_impl(std::forward<FunctionLambda>(function));
133 }
134
135 }; // class Bracketing
136
137 } // namespace RootFinder
138
139} // namespace Optimist
140
141#endif // OPTIMIST_ROOTFINDER_BRACKTING_HH
#define OPTIMIST_ASSERT_WARNING(COND, MSG)
Definition Optimist.hh:61
#define OPTIMIST_BASIC_CONSTANTS(Real)
Definition Optimist.hh:71
#define CMD
Real m_b
Definition Bracketing.hh:51
Real m_e
Definition Bracketing.hh:54
Real m_fd
Definition Bracketing.hh:53
Real m_d
Definition Bracketing.hh:53
bool solve_impl(FunctionLambda &&function, Real, Real &x_sol)
Definition Bracketing.hh:87
std::string name_impl() const
Definition Bracketing.hh:66
Real m_a
Definition Bracketing.hh:50
Bracketing()
Definition Bracketing.hh:60
Real m_mu
Definition Bracketing.hh:47
Real m_fe
Definition Bracketing.hh:54
void tolerance_bracketing(Real t_tolerance)
Definition Bracketing.hh:76
Real m_interval_shink
Definition Bracketing.hh:48
Real m_fa
Definition Bracketing.hh:50
Real m_tolerance_bracketing
Definition Bracketing.hh:46
Real find_root(FunctionLambda &&function)
Definition Bracketing.hh:130
Real m_fc
Definition Bracketing.hh:52
Real m_fb
Definition Bracketing.hh:51
Real m_c
Definition Bracketing.hh:52
void header()
Definition SolverBase.hh:870
void reset()
Definition SolverBase.hh:748
bool m_verbose
Definition SolverBase.hh:92
bool m_converged
Definition SolverBase.hh:98
InputType m_upper_bound
Definition SolverBase.hh:71
void bottom()
Definition SolverBase.hh:900
bool evaluate_function(FunctionLambda &&function, InputType const &x, OutputType &out)
Definition SolverBase.hh:768
InputType m_lower_bound
Definition SolverBase.hh:70
Namespace for the Optimist library.
Definition Optimist.hh:88