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 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_BRACKTING_HH
14#define OPTIMIST_ROOTFINDER_BRACKTING_HH
15
17
18namespace Optimist {
19 namespace RootFinder {
20
21 /*\
22 | ____ _ _ _
23 | | __ ) _ __ __ _ ___| | _____| |_(_)_ __ __ _
24 | | _ \| '__/ _` |/ __| |/ / _ \ __| | '_ \ / _` |
25 | | |_) | | | (_| | (__| < __/ |_| | | | | (_| |
26 | |____/|_| \__,_|\___|_|\_\___|\__|_|_| |_|\__, |
27 | |___/
28 \*/
29
37 template <typename Scalar, typename DerivedSolver>
38 class Bracketing : public RootFinder<Scalar, DerivedSolver> {
39 public:
41
42 protected:
44 100 * EPSILON};
45 Scalar m_mu{0.5};
47
48 Scalar m_a{0.0}, m_fa{0.0};
49 Scalar m_b{0.0}, m_fb{0.0};
50 Scalar m_c{0.0}, m_fc{0.0};
51 Scalar m_d{0.0}, m_fd{0.0};
52 Scalar m_e{0.0}, m_fe{0.0};
53
54 public:
59
64 constexpr std::string name_impl() const {
65 return static_cast<const DerivedSolver *>(this)->name_impl();
66 }
67
76 void tolerance_bracketing(Scalar t_tolerance) {
77 this->m_tolerance_bracketing = t_tolerance;
78 }
79
89 template <typename FunctionLambda>
90 bool solve_impl(FunctionLambda &&function,
91 Scalar /*x_ini*/,
92 Scalar &x_sol) {
93#define CMD "Optimist::RootFinder::Bracketing::solve_impl(...): "
94
95 // Setup internal variables
96 bool success;
97 this->reset_counters();
98
99 // Print header
100 if (this->m_verbose) {
101 this->header();
102 }
103
104 // Initialize variables
105 this->m_a = this->m_lower_bound;
106 success =
107 this->evaluate_function(std::forward<FunctionLambda>(function),
108 this->m_a,
109 this->m_fa);
110 OPTIMIST_ASSERT(success,
111 CMD "function evaluation failed at the lower bound.");
112
113 this->m_b = this->m_upper_bound;
114 success =
115 this->evaluate_function(std::forward<FunctionLambda>(function),
116 this->m_b,
117 this->m_fb);
118 OPTIMIST_ASSERT(success,
119 CMD "function evaluation failed at the upper bound.");
120
121 // Check if the solution exists
122 if (this->m_fa * this->m_fb > 0) {
123 return false;
124 } else {
125 x_sol = this->find_root(std::forward<FunctionLambda>(function));
126 }
127
128 // Print bottom
129 if (this->m_verbose) {
130 this->bottom();
131 }
132
133 // Convergence data
134 return this->m_converged;
135
136#undef CMD
137 }
138
147 template <typename FunctionLambda>
148 Scalar find_root(FunctionLambda &&function) {
149 return static_cast<DerivedSolver *>(this)->find_root_impl(
150 std::forward<FunctionLambda>(function));
151 }
152
153 }; // class Bracketing
154
155 } // namespace RootFinder
156
157} // namespace Optimist
158
159#endif // OPTIMIST_ROOTFINDER_BRACKTING_HH
#define OPTIMIST_BASIC_CONSTANTS(Scalar)
Definition Optimist.hh:69
#define OPTIMIST_ASSERT(COND, MSG)
Definition Optimist.hh:47
#define CMD
Scalar m_mu
Definition Bracketing.hh:45
Scalar m_fa
Definition Bracketing.hh:48
Scalar find_root(FunctionLambda &&function)
Definition Bracketing.hh:148
Scalar m_fd
Definition Bracketing.hh:51
Scalar m_c
Definition Bracketing.hh:50
Scalar m_e
Definition Bracketing.hh:52
void tolerance_bracketing(Scalar t_tolerance)
Definition Bracketing.hh:76
Scalar m_a
Definition Bracketing.hh:48
Scalar m_b
Definition Bracketing.hh:49
Scalar m_fc
Definition Bracketing.hh:50
Bracketing()
Definition Bracketing.hh:58
bool solve_impl(FunctionLambda &&function, Scalar, Scalar &x_sol)
Definition Bracketing.hh:90
Scalar m_d
Definition Bracketing.hh:51
Scalar m_fe
Definition Bracketing.hh:52
Scalar m_interval_shink
Definition Bracketing.hh:46
Scalar m_fb
Definition Bracketing.hh:49
constexpr std::string name_impl() const
Definition Bracketing.hh:64
Scalar m_tolerance_bracketing
Definition Bracketing.hh:43
typename TypeTrait< Scalar >::Scalar Scalar
Definition RootFinder.hh:53
bool evaluate_function(FunctionLambda &&function, const Scalar &x, Scalar &out)
Definition SolverBase.hh:1040
void header()
Definition SolverBase.hh:1168
Scalar m_lower_bound
Definition SolverBase.hh:101
void reset_counters()
Definition SolverBase.hh:1022
Scalar m_upper_bound
Definition SolverBase.hh:102
bool m_converged
Definition SolverBase.hh:136
void bottom()
Definition SolverBase.hh:1204
Namespace for the Optimist library.
Definition Optimist.hh:89