Theoretica
Scientific Computing
Loading...
Searching...
No Matches
multi_extrema.h
Go to the documentation of this file.
1
5
6#ifndef THEORETICA_MULTI_EXTREMA_H
7#define THEORETICA_MULTI_EXTREMA_H
8
9#include "../core/constants.h"
10#include "../autodiff/autodiff.h"
11#include "./extrema.h"
12
13#include <functional>
14
15
16namespace theoretica {
17
18
31 template <
32 typename Vector = vec<real>,
33 typename ReturnVector = Vector,
34 typename DualObjectiveFunction,
35 autodiff::enable_scalar_field<DualObjectiveFunction> = true
36 >
43 ) {
44
46
47 if(gamma <= 0) {
48 TH_MATH_ERROR("multi_minimize_grad", gamma, MathError::InvalidArgument);
49 return algebra::vec_error(x);
50 }
51
53 unsigned int iter = 0;
54
55 do {
56
58 x -= gamma * grad;
59 iter++;
60
61 } while(grad.norm() > tolerance && iter <= max_iter);
62
63 if(iter > max_iter) {
64 TH_MATH_ERROR("multi_minimize_grad", iter, MathError::NoConvergence);
65 return algebra::vec_error(x);
66 }
67
68 return x;
69 }
70
71
84 template <
85 typename Vector = vec<real>,
86 typename ReturnVector = Vector,
87 typename DualObjectiveFunction,
88 autodiff::enable_scalar_field<DualObjectiveFunction> = true
89 >
96 ) {
97
98 using ArgType = typename _internal::func_helper<DualObjectiveFunction>::first_arg_type;
99
100 return multi_minimize_grad(
101 [f](ArgType x) { return -f(x); },
102 guess, gamma,
104 );
105 }
106
107
119 template <
120 typename Vector = vec<real>,
121 typename ReturnVector = Vector,
122 typename DualObjectiveFunction,
123 autodiff::enable_scalar_field<DualObjectiveFunction> = true
124 >
130 ) {
131
134 unsigned int iter = 0;
135
136 constexpr size_t N = ReturnVector::size_argument;
137
138 do {
139
140 grad = autodiff::gradient(f, x);
141
142 // Minimize f(x + gamma * gradient) in [-1, 0]
143 // using Golden Section extrema search
144 real gamma = minimize_golden(
145 [f, x, grad](real gamma){
146 return f(multidual<N>::make_argument(x + gamma * grad)).Re();
147 }, -1, 0);
148
149 // Fallback value
150 if(-gamma <= MACH_EPSILON)
152
153 x += gamma * grad;
154 iter++;
155
156 } while(grad.norm() > tolerance && iter <= max_iter);
157
158 if(iter > max_iter) {
159 TH_MATH_ERROR("multi_minimize_lingrad", iter, MathError::NoConvergence);
160 return algebra::vec_error(x);
161 }
162
163 return x;
164 }
165
166
178 template <
179 typename Vector = vec<real>,
180 typename ReturnVector = Vector,
181 typename DualObjectiveFunction,
182 autodiff::enable_scalar_field<DualObjectiveFunction> = true
183 >
188 unsigned int max_iter = OPTIMIZATION_MINGRAD_ITER) {
189
190 using ArgType = typename _internal::func_helper<DualObjectiveFunction>::first_arg_type;
191
193 [f](ArgType x) { return -f(x); },
195 );
196 }
197
198
208 template <
209 typename Vector = vec<real>,
210 typename ReturnVector = Vector,
211 typename DualObjectiveFunction,
212 autodiff::enable_scalar_field<DualObjectiveFunction> = true
213 >
220
221
231 template <
232 typename Vector = vec<real>,
233 typename ReturnVector = Vector,
234 typename DualObjectiveFunction,
235 autodiff::enable_scalar_field<DualObjectiveFunction> = true
236 >
243
244}
245
246
247#endif
Multidual number algebra for functions of the form .
Definition multidual.h:26
#define TH_MATH_ERROR(F_NAME, VALUE, EXCEPTION)
TH_MATH_ERROR is a macro which throws exceptions or modifies errno (depending on which compilation op...
Definition error.h:219
Extrema approximation of real functions.
Vector & vec_error(Vector &v)
Overwrite the given vector with the error vector with NaN values.
Definition algebra.h:58
auto gradient(Function f, const Vector &x)
Compute the gradient for a given of a scalar field of the form using automatic differentiation.
Definition autodiff.h:122
Main namespace of the library which contains all functions and objects.
Definition algebra.h:27
double real
A real number, defined as a floating point type.
Definition constants.h:207
Vector make_error()
Create a vector representing an error state, with all NaN values.
Definition algebra.h:103
ReturnVector multi_minimize(DualObjectiveFunction f, Vector guess, real tolerance=OPTIMIZATION_MINGRAD_TOLERANCE)
Use the best available algorithm to find a local minimum of the given multivariate function.
Definition multi_extrema.h:214
constexpr real OPTIMIZATION_MINGRAD_TOLERANCE
Default tolerance for gradient descent minimization.
Definition constants.h:333
ReturnVector multi_maximize_lingrad(DualObjectiveFunction f, Vector guess, real tolerance=OPTIMIZATION_MINGRAD_TOLERANCE, unsigned int max_iter=OPTIMIZATION_MINGRAD_ITER)
Find a local maximum of the given multivariate function using gradient descent with linear search.
Definition multi_extrema.h:184
constexpr unsigned int OPTIMIZATION_MINGRAD_ITER
Maximum number of iterations for gradient descent minimization.
Definition constants.h:336
@ InvalidArgument
Invalid argument.
@ NoConvergence
Algorithm did not converge.
constexpr real MACH_EPSILON
Machine epsilon for the real type.
Definition constants.h:216
ReturnVector multi_maximize_grad(DualObjectiveFunction f, Vector guess, real gamma=OPTIMIZATION_MINGRAD_GAMMA, real tolerance=OPTIMIZATION_MINGRAD_TOLERANCE, unsigned int max_iter=OPTIMIZATION_MINGRAD_ITER)
Find a local maximum of the given multivariate function using fixed-step gradient descent.
Definition multi_extrema.h:90
ReturnVector multi_minimize_grad(DualObjectiveFunction f, Vector guess, real gamma=OPTIMIZATION_MINGRAD_GAMMA, real tolerance=OPTIMIZATION_MINGRAD_TOLERANCE, unsigned int max_iter=OPTIMIZATION_MINGRAD_ITER)
Find a local minimum of the given multivariate function using fixed-step gradient descent.
Definition multi_extrema.h:37
ReturnVector multi_minimize_lingrad(DualObjectiveFunction f, Vector guess, real tolerance=OPTIMIZATION_MINGRAD_TOLERANCE, unsigned int max_iter=OPTIMIZATION_MINGRAD_ITER)
Find a local minimum of the given multivariate function using gradient descent with linear search.
Definition multi_extrema.h:125
iter_result< real > minimize_golden(RealFunction f, real a, real b, real tolerance=OPTIMIZATION_TOL, unsigned int max_iter=OPTIMIZATION_GOLDENSECTION_ITER)
Approximate a function minimum using the Golden Section search algorithm.
Definition extrema.h:74
constexpr real OPTIMIZATION_MINGRAD_GAMMA
Default step size for gradient descent minimization.
Definition constants.h:330
ReturnVector multi_maximize(DualObjectiveFunction f, Vector guess, real tolerance=OPTIMIZATION_MINGRAD_TOLERANCE)
Use the best available algorithm to find a local maximum of the given multivariate function.
Definition multi_extrema.h:237