Optimist  0.0.0
A C++ library for optimization
Loading...
Searching...
No Matches
Optimizer.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_OPTIMIZER_HH
14#define OPTIMIST_OPTIMIZER_HH
15
17
18namespace Optimist {
19
23 namespace Optimizer {
24
25 /*\
26 | ___ _ _ _
27 | / _ \ _ __ | |_(_)_ __ ___ (_)_______ _ __
28 | | | | | '_ \| __| | '_ ` _ \| |_ / _ \ '__|
29 | | |_| | |_) | |_| | | | | | | |/ / __/ |
30 | \___/| .__/ \__|_|_| |_| |_|_/___\___|_|
31 | |_|
32 \*/
33
42 template <typename T, typename DerivedSolver>
46 : public SolverBase<T, typename TypeTrait<T>::Scalar, DerivedSolver> {
47 public:
48 friend class SolverBase<T, typename TypeTrait<T>::Scalar, DerivedSolver>;
49
50 static constexpr bool IsRootFinder{false};
51 static constexpr bool IsOptimizer{true};
52
53 // Input and output types
54 using Scalar = typename TypeTrait<T>::Scalar;
55 using Input = T;
56 using Output = Scalar;
57
58 // Derivative types
61
63
64
68
73 constexpr std::string name() const {
74 return static_cast<const DerivedSolver *>(this)->name_impl();
75 }
76
84
92
98 void max_gradient_evaluations(const Integer t_gradient_evaluations) {
99 this->max_first_derivative_evaluations(t_gradient_evaluations);
100 }
101
107 return this->second_derivative_evaluations();
108 }
109
117
123 void max_hessian_evaluations(const Integer t_hessian_evaluations) {
124 this->max_second_derivative_evaluations(t_hessian_evaluations);
125 }
126
127 protected:
136 template <typename GradientLambda>
137 bool evaluate_gradient(GradientLambda &&gradient,
138 const Input &x,
139 FirstDerivative &out) {
140 return this->evaluate_first_derivative(
141 std::forward<GradientLambda>(gradient),
142 x,
143 out);
144 }
145
154 template <typename HessianLambda>
155 bool evaluate_hessian(HessianLambda &&hessian,
156 const Input &x,
157 SecondDerivative &out) {
158 return this->evaluate_second_derivative(
159 std::forward<HessianLambda>(hessian),
160 x,
161 out);
162 }
163
173 template <typename FunctionLambda>
174 bool solve(FunctionLambda &&function, const Input &x_ini, Output &x_sol) {
175#define CMD "Optimist::Optimizer::solve(...): "
176
177 static_assert(DerivedSolver::RequiresFunction,
178 CMD "the solver requires the function.");
179 return static_cast<DerivedSolver *>(this)->solve_impl(
180 std::forward<FunctionLambda>(function),
181 x_ini,
182 x_sol);
183
184#undef CMD
185 }
186
197 template <typename FunctionLambda, typename GradientLambda>
198 bool solve(FunctionLambda &&function,
199 GradientLambda &&gradient,
200 const Input &x_ini,
201 Output &x_sol) {
202#define CMD "Optimist::Optimizer::solve(...): "
203
204 static_assert(DerivedSolver::RequiresFunction,
205 CMD "the solver requires the function.");
206 static_assert(DerivedSolver::RequiresFirstDerivative,
207 CMD "the solver requires the first derivative.");
208 return static_cast<DerivedSolver *>(this)->solve_impl(
209 std::forward<FunctionLambda>(function),
210 std::forward<GradientLambda>(gradient),
211 x_ini,
212 x_sol);
213
214#undef CMD
215 }
216
230 template <typename FunctionLambda,
231 typename GradientLambda,
232 typename HessianLambda>
233 bool solve(FunctionLambda &&function,
234 GradientLambda &&gradient,
235 HessianLambda &&hessian,
236 const Input &x_ini,
237 Output &x_sol) {
238#define CMD "Optimist::Optimizer::solve(...): "
239
240 static_assert(DerivedSolver::RequiresFunction,
241 CMD "the solver requires the function.");
242 static_assert(DerivedSolver::RequiresFirstDerivative,
243 CMD "the solver requires the first derivative.");
244 static_assert(DerivedSolver::RequiresSecondDerivative,
245 CMD "the solver requires the second derivative.");
246 return static_cast<DerivedSolver *>(this)->solve_impl(
247 std::forward<FunctionLambda>(function),
248 std::forward<GradientLambda>(gradient),
249 std::forward<HessianLambda>(hessian),
250 x_ini,
251 x_sol);
252
253#undef CMD
254 }
255
256 }; // class Optimizer
257
258 } // namespace Optimizer
259
260} // namespace Optimist
261
262#endif // OPTIMIST_OPTIMIZER_HH
#define OPTIMIST_BASIC_CONSTANTS(Scalar)
Definition Optimist.hh:69
#define CMD
Class container for the multi-dimensional optimizer.
Definition Optimizer.hh:46
static constexpr bool IsRootFinder
Definition Optimizer.hh:50
typename TypeTrait< T >::Scalar Scalar
Definition Optimizer.hh:54
Integer max_gradient_evaluations() const
Definition Optimizer.hh:89
Integer hessian_evaluations() const
Definition Optimizer.hh:106
Integer max_hessian_evaluations() const
Definition Optimizer.hh:114
bool solve(FunctionLambda &&function, const Input &x_ini, Output &x_sol)
Definition Optimizer.hh:174
void max_gradient_evaluations(const Integer t_gradient_evaluations)
Definition Optimizer.hh:98
Scalar Output
Definition Optimizer.hh:56
constexpr std::string name() const
Definition Optimizer.hh:73
void max_hessian_evaluations(const Integer t_hessian_evaluations)
Definition Optimizer.hh:123
bool evaluate_hessian(HessianLambda &&hessian, const Input &x, SecondDerivative &out)
Definition Optimizer.hh:155
T Input
Definition Optimizer.hh:55
Optimizer()
Definition Optimizer.hh:67
Integer gradient_evaluations() const
Definition Optimizer.hh:81
bool solve(FunctionLambda &&function, GradientLambda &&gradient, const Input &x_ini, Output &x_sol)
Definition Optimizer.hh:198
bool evaluate_gradient(GradientLambda &&gradient, const Input &x, FirstDerivative &out)
Definition Optimizer.hh:137
bool solve(FunctionLambda &&function, GradientLambda &&gradient, HessianLambda &&hessian, const Input &x_ini, Output &x_sol)
Definition Optimizer.hh:233
static constexpr bool IsOptimizer
Definition Optimizer.hh:51
Integer first_derivative_evaluations() const
Definition SolverBase.hh:379
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
bool evaluate_first_derivative(FirstDerivativeLambda &&function, const T &x, FirstDerivative &out)
Definition SolverBase.hh:1061
bool evaluate_second_derivative(SecondDerivativeLambda &&function, const T &x, SecondDerivative &out)
Definition SolverBase.hh:1082
Integer max_first_derivative_evaluations() const
Definition SolverBase.hh:387
Integer second_derivative_evaluations() const
Definition SolverBase.hh:409
Integer max_second_derivative_evaluations() const
Definition SolverBase.hh:417
std::conditional_t< InputTrait::IsEigen||OutputTrait::IsEigen, std::conditional_t< InputTrait::IsSparse||OutputTrait::IsSparse, std::vector< Eigen::SparseMatrix< Scalar > >, std::vector< Eigen::Matrix< Scalar, OutputTrait::Dimension, InputTrait::Dimension > > >, Scalar > SecondDerivative
Definition SolverBase.hh:88
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