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_SCALAR_ROOT_FINDER_BRACKTING_HH
14#define OPTIMIST_SCALAR_ROOT_FINDER_BRACKTING_HH
15
17
18namespace Optimist
19{
20 namespace ScalarRootFinder
21 {
22
23 /*\
24 | ____ _ _ _
25 | | __ ) _ __ __ _ ___| | _____| |_(_)_ __ __ _
26 | | _ \| '__/ _` |/ __| |/ / _ \ __| | '_ \ / _` |
27 | | |_) | | | (_| | (__| < __/ |_| | | | | (_| |
28 | |____/|_| \__,_|\___|_|\_\___|\__|_|_| |_|\__, |
29 | |___/
30 \*/
31
39 template <typename Real, typename DerivedSolver>
40 class Bracketing : public ScalarRootFinder<Real, DerivedSolver>
41 {
42 public:
43 static constexpr bool requires_function{DerivedSolver::requires_function};
44 static constexpr bool requires_first_derivative{DerivedSolver::requires_first_derivative};
45 static constexpr bool requires_second_derivative{DerivedSolver::requires_second_derivative};
46
48
49 // Function types
50 using FunctionWrapper = typename ScalarRootFinder<Real, DerivedSolver>::FunctionWrapper;
51 using FirstDerivativeWrapper = typename ScalarRootFinder<Real, DerivedSolver>::FirstDerivativeWrapper;
53
54 protected:
55 Real m_tolerance_bracketing{100*EPSILON};
56 Real m_mu{0.5};
57 Real m_interval_shink{0.025};
58
59 Real m_a{0.0}, m_fa{0.0};
60 Real m_b{0.0}, m_fb{0.0};
61 Real m_c{0.0}, m_fc{0.0};
62 Real m_d{0.0}, m_fd{0.0};
63 Real m_e{0.0}, m_fe{0.0};
64
65 public:
70
75 std::string name_impl() const
76 {
77 return static_cast<const DerivedSolver *>(this)->name_impl();
78 }
79
85 void tolerance_bracketing(Real t_tolerance) {this->m_tolerance_bracketing = t_tolerance;}
86
94 bool solve_impl(FunctionWrapper function, Real /*x_ini*/, Real & x_sol)
95 {
96 // Setup internal variables
97 this->reset();
98
99 // Print header
100 if (this->m_verbose) {this->header();}
101
102 // Initialize variables
103 this->m_a = this->m_lower_bound; this->evaluate_function(function, this->m_a, this->m_fa);
104 this->m_b = this->m_upper_bound; this->evaluate_function(function, this->m_b, this->m_fb);
105
106 // Check if the solution exists
107 if (this->m_fa*this->m_fb > 0.0) {return false;}
108 else {x_sol = this->find_root(function);}
109
110 // Print bottom
111 if (this->m_verbose) {this->bottom();}
112
113 // Convergence data
114 return this->m_converged;
115 }
116
124 {
125 return static_cast<DerivedSolver *>(this)->find_root_impl(function);
126 }
127
128 }; // class Bracketing
129
130 } // namespace ScalarRootFinder
131
132} // namespace Optimist
133
134#endif // OPTIMIST_SCALAR_ROOT_FINDER_BRACKTING_HH
#define OPTIMIST_BASIC_CONSTANTS(Real)
Definition Optimist.hh:70
Bracketing()
Definition Bracketing.hh:69
Real m_fb
Definition Bracketing.hh:60
void tolerance_bracketing(Real t_tolerance)
Definition Bracketing.hh:85
Real m_fd
Definition Bracketing.hh:62
static constexpr bool requires_second_derivative
Definition Bracketing.hh:45
Real m_fc
Definition Bracketing.hh:61
Real m_c
Definition Bracketing.hh:61
typename ScalarRootFinder< Real, DerivedSolver >::SecondDerivativeWrapper SecondDerivativeWrapper
Definition Bracketing.hh:52
Real m_a
Definition Bracketing.hh:59
Real find_root(FunctionWrapper function)
Definition Bracketing.hh:123
Real m_interval_shink
Definition Bracketing.hh:57
static constexpr bool requires_function
Definition Bracketing.hh:43
Real m_tolerance_bracketing
Definition Bracketing.hh:55
bool solve_impl(FunctionWrapper function, Real, Real &x_sol)
Definition Bracketing.hh:94
Real m_b
Definition Bracketing.hh:60
static constexpr bool requires_first_derivative
Definition Bracketing.hh:44
std::string name_impl() const
Definition Bracketing.hh:75
typename ScalarRootFinder< Real, DerivedSolver >::FunctionWrapper FunctionWrapper
Definition Bracketing.hh:50
Real m_mu
Definition Bracketing.hh:56
Real m_fa
Definition Bracketing.hh:59
typename ScalarRootFinder< Real, DerivedSolver >::FirstDerivativeWrapper FirstDerivativeWrapper
Definition Bracketing.hh:51
Real m_e
Definition Bracketing.hh:63
Real m_d
Definition Bracketing.hh:62
Real m_fe
Definition Bracketing.hh:63
ScalarRootFinder()
Definition ScalarRootFinder.hh:66
bool m_converged
Definition Solver.hh:99
InputType m_upper_bound
Definition Solver.hh:72
void header()
Definition Solver.hh:799
void reset()
Definition Solver.hh:693
InputType m_lower_bound
Definition Solver.hh:71
void bottom()
Definition Solver.hh:829
void evaluate_function(FunctionWrapper function, const InputType &x, OutputType &out)
Definition Solver.hh:710
Namespace for the Optimist library.
Definition Optimist.hh:87