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
16#include "Solver.hh"
17
18namespace Optimist
19{
20
24 namespace Optimizer
25 {
26
27 /*\
28 | ___ _ _ _
29 | / _ \ _ __ | |_(_)_ __ ___ (_)_______ _ __
30 | | | | | '_ \| __| | '_ ` _ \| |_ / _ \ '__|
31 | | |_| | |_) | |_| | | | | | | |/ / __/ |
32 | \___/| .__/ \__|_|_| |_| |_|_/___\___|_|
33 | |_|
34 \*/
35
44 template <typename Real, Integer N, typename DerivedSolver>
45 class Optimizer : public Solver<Real, N, 1, DerivedSolver>
46 {
47 public:
48 // Fancy static assertions (just for fun, don't take it too seriously)
49 static_assert(N != static_cast<Integer>(0),
50 "Have you ever heard of a zero-dimensional optimization problem?");
51 static_assert(N != static_cast<Integer>(1),
52 "Good try, but you should use a scalar solver for a one-dimensional optimization problem.");
53
55
56 static constexpr bool is_rootfinder{false};
57 static constexpr bool is_optimizer{true};
58
59 static constexpr bool requires_function{DerivedSolver::requires_function};
60 static constexpr bool requires_first_derivative{DerivedSolver::requires_first_derivative};
61 static constexpr bool requires_second_derivative{DerivedSolver::requires_second_derivative};
62
64
65 // I/O types
66 using Vector = typename Solver<Real, N, 1, DerivedSolver>::InputType;
67
68 // Derivative types
69 using RowVector = typename Solver<Real, N, 1, DerivedSolver>::FirstDerivativeType;
70 using Matrix = typename Solver<Real, N, 1, DerivedSolver>::SecondDerivativeType;
71
72 // Function types
73 using FunctionWrapper = typename Solver<Real, N, 1, DerivedSolver>::FunctionWrapper;
74 using GradientWrapper = typename Solver<Real, N, 1, DerivedSolver>::FirstDerivativeWrapper;
75 using HessianWrapper = typename Solver<Real, N, 1, DerivedSolver>::SecondDerivativeWrapper;
76
81
86 std::string name() const {return static_cast<const DerivedSolver *>(this)->name_impl();}
87
93
99
104 void max_gradient_evaluations(Integer t_gradient_evaluations)
105 {
106 this->max_first_derivative_evaluations(t_gradient_evaluations);
107 }
108
114
120
125 void max_hessian_evaluations(Integer t_hessian_evaluations)
126 {
127 this->max_second_derivative_evaluations(t_hessian_evaluations);
128 }
129
130 protected:
137 void evaluate_gradient(GradientWrapper gradient, const Vector & x, Matrix & out)
138 {
139 this->evaluate_first_derivative_impl(gradient, x, out);
140 }
141
148 void evaluate_hessian(HessianWrapper hessian, const Vector & x, Matrix & out)
149 {
150 this->evaluate_second_derivative_impl(hessian, x, out);
151 }
152
160 bool solve(FunctionWrapper function, Vector const & x_ini, Vector & x_sol)
161 {
162 #define CMD "Optimist::Optimizer::solve(...): "
163
164 static_assert(DerivedSolver::requires_function,
165 CMD "the solver requires the function.");
166 return static_cast<DerivedSolver *>(this)->solve_impl(function, x_ini, x_sol);
167
168 #undef CMD
169 }
170
179 bool solve(FunctionWrapper function, GradientWrapper gradient, Vector const & x_ini, Vector & x_sol)
180 {
181 #define CMD "Optimist::Optimizer::solve(...): "
182
183 static_assert(DerivedSolver::requires_function,
184 CMD "the solver requires the function.");
185 static_assert(DerivedSolver::requires_first_derivative,
186 CMD "the solver requires the first derivative.");
187 return static_cast<DerivedSolver *>(this)->solve_impl(function, gradient, x_ini, x_sol);
188
189 #undef CMD
190 }
191
202 const & x_ini, Vector & x_sol)
203 {
204 #define CMD "Optimist::Optimizer::solve(...): "
205
206 static_assert(DerivedSolver::requires_function,
207 CMD "the solver requires the function.");
208 static_assert(DerivedSolver::requires_first_derivative,
209 CMD "the solver requires the first derivative.");
210 static_assert(DerivedSolver::requires_second_derivative,
211 CMD "the solver requires the second derivative.");
212 return static_cast<DerivedSolver *>(this)->solve_impl(function, gradient, hessian, x_ini, x_sol);
213
214 #undef CMD
215 }
216
217 }; // class Optimizer
218
219 } // namespace Optimizer
220
221} // namespace Optimist
222
223#endif // OPTIMIST_OPTIMIZER_HH
#define OPTIMIST_BASIC_CONSTANTS(Real)
Definition Optimist.hh:70
#define CMD
Class container for the multi-dimensional optimizer.
Definition Optimizer.hh:46
void max_gradient_evaluations(Integer t_gradient_evaluations)
Definition Optimizer.hh:104
void evaluate_gradient(GradientWrapper gradient, const Vector &x, Matrix &out)
Definition Optimizer.hh:137
std::string name() const
Definition Optimizer.hh:86
Integer max_gradient_evaluations() const
Definition Optimizer.hh:98
static constexpr bool requires_function
Definition Optimizer.hh:59
typename Solver< Real, N, 1, DerivedSolver >::SecondDerivativeWrapper HessianWrapper
Definition Optimizer.hh:75
bool solve(FunctionWrapper function, Vector const &x_ini, Vector &x_sol)
Definition Optimizer.hh:160
Optimizer()
Definition Optimizer.hh:80
Integer gradient_evaluations() const
Definition Optimizer.hh:92
Integer hessian_evaluations() const
Definition Optimizer.hh:113
void evaluate_hessian(HessianWrapper hessian, const Vector &x, Matrix &out)
Definition Optimizer.hh:148
static constexpr bool is_rootfinder
Definition Optimizer.hh:56
static constexpr bool requires_second_derivative
Definition Optimizer.hh:61
typename Solver< Real, N, 1, DerivedSolver >::InputType Vector
Definition Optimizer.hh:66
typename Solver< Real, N, 1, DerivedSolver >::FirstDerivativeType RowVector
Definition Optimizer.hh:69
typename Solver< Real, N, 1, DerivedSolver >::SecondDerivativeType Matrix
Definition Optimizer.hh:70
bool solve(FunctionWrapper function, GradientWrapper gradient, HessianWrapper hessian, Vector const &x_ini, Vector &x_sol)
Definition Optimizer.hh:201
typename Solver< Real, N, 1, DerivedSolver >::FirstDerivativeWrapper GradientWrapper
Definition Optimizer.hh:74
void max_hessian_evaluations(Integer t_hessian_evaluations)
Definition Optimizer.hh:125
typename Solver< Real, N, 1, DerivedSolver >::FunctionWrapper FunctionWrapper
Definition Optimizer.hh:73
static constexpr bool requires_first_derivative
Definition Optimizer.hh:60
bool solve(FunctionWrapper function, GradientWrapper gradient, Vector const &x_ini, Vector &x_sol)
Definition Optimizer.hh:179
static constexpr bool is_optimizer
Definition Optimizer.hh:57
Integer max_hessian_evaluations() const
Definition Optimizer.hh:119
Integer first_derivative_evaluations() const
Definition Solver.hh:266
typename std::conditional_t< SolInDim==1, Real, Eigen::Vector< Real, SolInDim > > InputType
Definition Solver.hh:53
typename std::function< void(const InputType &, FirstDerivativeType &)> FirstDerivativeWrapper
Definition Solver.hh:67
std::conditional_t< SolInDim==1 &&SolOutDim==1, Real, std::conditional_t< SolInDim==1||SolOutDim==1, Eigen::Matrix< Real, SolInDim, SolInDim >, std::vector< Eigen::Matrix< Real, SolInDim, SolInDim > > > > SecondDerivativeType
Definition Solver.hh:61
Integer max_second_derivative_evaluations() const
Definition Solver.hh:296
Integer max_first_derivative_evaluations() const
Definition Solver.hh:272
typename std::function< void(const InputType &, SecondDerivativeType &)> SecondDerivativeWrapper
Definition Solver.hh:68
Integer second_derivative_evaluations() const
Definition Solver.hh:290
std::conditional_t< SolInDim==1 &&SolOutDim==1, Real, Eigen::Matrix< Real, SolOutDim, SolInDim > > FirstDerivativeType
Definition Solver.hh:60
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