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, 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_OPTIMIZER_HH
14#define OPTIMIST_OPTIMIZER_HH
15
17
18namespace Optimist
19{
20
24 namespace Optimizer
25 {
26
27 /*\
28 | ___ _ _ _
29 | / _ \ _ __ | |_(_)_ __ ___ (_)_______ _ __
30 | | | | | '_ \| __| | '_ ` _ \| |_ / _ \ '__|
31 | | |_| | |_) | |_| | | | | | | |/ / __/ |
32 | \___/| .__/ \__|_|_| |_| |_|_/___\___|_|
33 | |_|
34 \*/
35
45 template <typename Real, Integer N, typename DerivedSolver, bool ForceEigen = false>
46 class Optimizer : public SolverBase<Real, N, 1, DerivedSolver, ForceEigen>
47 {
48 public:
49 // Fancy static assertions (just for fun, don't take it too seriously)
50 static_assert(N != static_cast<Integer>(0),
51 "Have you ever heard of a zero-dimensional optimization problem?");
52
53 friend class SolverBase<Real, N, 1, Optimizer<Real, N, DerivedSolver, ForceEigen>>;
54
55 static constexpr bool is_rootfinder{false};
56 static constexpr bool is_optimizer{true};
57
58 static constexpr bool requires_function{DerivedSolver::requires_function};
59 static constexpr bool requires_first_derivative{DerivedSolver::requires_first_derivative};
60 static constexpr bool requires_second_derivative{DerivedSolver::requires_second_derivative};
61
63
64 // I/O types
65 using Vector = typename SolverBase<Real, N, 1, DerivedSolver, ForceEigen>::InputType;
66
67 // Derivative types
68 using RowVector = typename SolverBase<Real, N, 1, DerivedSolver, ForceEigen>::FirstDerivativeType;
69 using Matrix = typename SolverBase<Real, N, 1, DerivedSolver, ForceEigen>::SecondDerivativeType;
70
71 // Function types
72 using FunctionWrapper = typename SolverBase<Real, N, 1, DerivedSolver, ForceEigen>::FunctionWrapper;
73 using GradientWrapper = typename SolverBase<Real, N, 1, DerivedSolver, ForceEigen>::FirstDerivativeWrapper;
74 using HessianWrapper = typename SolverBase<Real, N, 1, DerivedSolver, ForceEigen>::SecondDerivativeWrapper;
75
80
85 std::string name() const {return static_cast<const DerivedSolver *>(this)->name_impl();}
86
92
98
103 void max_gradient_evaluations(Integer t_gradient_evaluations)
104 {
105 this->max_first_derivative_evaluations(t_gradient_evaluations);
106 }
107
113
119
124 void max_hessian_evaluations(Integer t_hessian_evaluations)
125 {
126 this->max_second_derivative_evaluations(t_hessian_evaluations);
127 }
128
129 protected:
136 void evaluate_gradient(GradientWrapper gradient, const Vector & x, Matrix & out)
137 {
138 this->evaluate_first_derivative_impl(gradient, x, out);
139 }
140
147 void evaluate_hessian(HessianWrapper hessian, const Vector & x, Matrix & out)
148 {
149 this->evaluate_second_derivative_impl(hessian, x, out);
150 }
151
159 bool solve(FunctionWrapper function, Vector const & x_ini, Vector & x_sol)
160 {
161 #define CMD "Optimist::Optimizer::solve(...): "
162
163 static_assert(DerivedSolver::requires_function,
164 CMD "the solver requires the function.");
165 return static_cast<DerivedSolver *>(this)->solve_impl(function, x_ini, x_sol);
166
167 #undef CMD
168 }
169
178 bool solve(FunctionWrapper function, GradientWrapper gradient, Vector const & x_ini, Vector & x_sol)
179 {
180 #define CMD "Optimist::Optimizer::solve(...): "
181
182 static_assert(DerivedSolver::requires_function,
183 CMD "the solver requires the function.");
184 static_assert(DerivedSolver::requires_first_derivative,
185 CMD "the solver requires the first derivative.");
186 return static_cast<DerivedSolver *>(this)->solve_impl(function, gradient, x_ini, x_sol);
187
188 #undef CMD
189 }
190
201 const & x_ini, Vector & x_sol)
202 {
203 #define CMD "Optimist::Optimizer::solve(...): "
204
205 static_assert(DerivedSolver::requires_function,
206 CMD "the solver requires the function.");
207 static_assert(DerivedSolver::requires_first_derivative,
208 CMD "the solver requires the first derivative.");
209 static_assert(DerivedSolver::requires_second_derivative,
210 CMD "the solver requires the second derivative.");
211 return static_cast<DerivedSolver *>(this)->solve_impl(function, gradient, hessian, x_ini, x_sol);
212
213 #undef CMD
214 }
215
216 }; // class Optimizer
217
226 template <typename Real, typename DerivedSolver>
227 class Optimizer<Real, 1, DerivedSolver> : public SolverBase<Real, 1, 1, DerivedSolver>
228 {
229 public:
230 friend class SolverBase<Real, 1, 1, Optimizer<Real, 1, DerivedSolver>>;
231
232 static constexpr bool is_rootfinder{false};
233 static constexpr bool is_optimizer{true};
234
235 static constexpr bool requires_function{DerivedSolver::requires_function};
236 static constexpr bool requires_first_derivative{DerivedSolver::requires_first_derivative};
237 static constexpr bool requires_second_derivative{DerivedSolver::requires_second_derivative};
238
240
241 using FunctionWrapper = typename SolverBase<Real, 1, 1, DerivedSolver>::FunctionWrapper;
242 using FirstDerivativeWrapper = typename SolverBase<Real, 1, 1, DerivedSolver>::FirstDerivativeWrapper;
243 using SecondDerivativeWrapper = typename SolverBase<Real, 1, 1, DerivedSolver>::SecondDerivativeWrapper;
244
248 Optimizer<Real, 1, DerivedSolver>() {}
249
254 std::string name() const {return static_cast<const DerivedSolver *>(this)->name_impl();}
255
256 }; // class Optimizer
257
258 } // namespace Optimizer
259
260} // namespace Optimist
261
262#endif // OPTIMIST_OPTIMIZER_HH
#define OPTIMIST_BASIC_CONSTANTS(Real)
Definition Optimist.hh:70
#define CMD
static constexpr bool requires_first_derivative
Definition Optimizer.hh:236
static constexpr bool requires_function
Definition Optimizer.hh:235
std::string name() const
Definition Optimizer.hh:254
static constexpr bool requires_second_derivative
Definition Optimizer.hh:237
static constexpr bool is_rootfinder
Definition Optimizer.hh:232
typename SolverBase< Real, 1, 1, DerivedSolver >::FirstDerivativeWrapper FirstDerivativeWrapper
Definition Optimizer.hh:242
static constexpr bool is_optimizer
Definition Optimizer.hh:233
typename SolverBase< Real, 1, 1, DerivedSolver >::FunctionWrapper FunctionWrapper
Definition Optimizer.hh:241
typename SolverBase< Real, 1, 1, DerivedSolver >::SecondDerivativeWrapper SecondDerivativeWrapper
Definition Optimizer.hh:243
Class container for the multi-dimensional optimizer.
Definition Optimizer.hh:47
Optimizer()
Definition Optimizer.hh:79
typename SolverBase< Real, N, 1, DerivedSolver, ForceEigen >::InputType Vector
Definition Optimizer.hh:65
typename SolverBase< Real, N, 1, DerivedSolver, ForceEigen >::FirstDerivativeWrapper GradientWrapper
Definition Optimizer.hh:73
static constexpr bool requires_function
Definition Optimizer.hh:58
std::string name() const
Definition Optimizer.hh:85
typename SolverBase< Real, N, 1, DerivedSolver, ForceEigen >::SecondDerivativeWrapper HessianWrapper
Definition Optimizer.hh:74
static constexpr bool is_rootfinder
Definition Optimizer.hh:55
bool solve(FunctionWrapper function, GradientWrapper gradient, Vector const &x_ini, Vector &x_sol)
Definition Optimizer.hh:178
Integer max_gradient_evaluations() const
Definition Optimizer.hh:97
static constexpr bool requires_first_derivative
Definition Optimizer.hh:59
static constexpr bool is_optimizer
Definition Optimizer.hh:56
typename SolverBase< Real, N, 1, DerivedSolver, ForceEigen >::FirstDerivativeType RowVector
Definition Optimizer.hh:68
void evaluate_hessian(HessianWrapper hessian, const Vector &x, Matrix &out)
Definition Optimizer.hh:147
Integer hessian_evaluations() const
Definition Optimizer.hh:112
Integer max_hessian_evaluations() const
Definition Optimizer.hh:118
void max_gradient_evaluations(Integer t_gradient_evaluations)
Definition Optimizer.hh:103
Integer gradient_evaluations() const
Definition Optimizer.hh:91
void max_hessian_evaluations(Integer t_hessian_evaluations)
Definition Optimizer.hh:124
static constexpr bool requires_second_derivative
Definition Optimizer.hh:60
void evaluate_gradient(GradientWrapper gradient, const Vector &x, Matrix &out)
Definition Optimizer.hh:136
typename SolverBase< Real, N, 1, DerivedSolver, ForceEigen >::SecondDerivativeType Matrix
Definition Optimizer.hh:69
bool solve(FunctionWrapper function, GradientWrapper gradient, HessianWrapper hessian, Vector const &x_ini, Vector &x_sol)
Definition Optimizer.hh:200
typename SolverBase< Real, N, 1, DerivedSolver, ForceEigen >::FunctionWrapper FunctionWrapper
Definition Optimizer.hh:72
bool solve(FunctionWrapper function, Vector const &x_ini, Vector &x_sol)
Definition Optimizer.hh:159
std::conditional_t< ForceEigen||(SolInDim > 1)||(SolOutDim > 1), Eigen::Matrix< Real, SolOutDim, SolInDim >, Real > FirstDerivativeType
Definition SolverBase.hh:61
Integer max_first_derivative_evaluations() const
Definition SolverBase.hh:273
typename std::function< void(const InputType &, SecondDerivativeType &)> SecondDerivativeWrapper
Definition SolverBase.hh:69
Integer max_second_derivative_evaluations() const
Definition SolverBase.hh:297
typename std::conditional_t< ForceEigen||(SolInDim > 1), Eigen::Vector< Real, SolInDim >, Real > InputType
Definition SolverBase.hh:54
Integer second_derivative_evaluations() const
Definition SolverBase.hh:291
std::conditional_t< ForceEigen||(SolInDim > 1)||(SolOutDim > 1), std::conditional_t< SolInDim==1||SolOutDim==1, Eigen::Matrix< Real, SolInDim, SolInDim >, std::vector< Eigen::Matrix< Real, SolInDim, SolInDim > > >, Real > SecondDerivativeType
Definition SolverBase.hh:62
Integer first_derivative_evaluations() const
Definition SolverBase.hh:267
typename std::function< void(const InputType &, FirstDerivativeType &)> FirstDerivativeWrapper
Definition SolverBase.hh:68
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