1 #ifndef JULIAN_DERIVATIVEMINIMIZER_HPP 2 #define JULIAN_DERIVATIVEMINIMIZER_HPP 4 #include <gsl/gsl_multimin.h> 71 template<
typename F,
typename dF,
typename FdF>
75 std::vector<double> x_init,
77 double step_size = 0.1,
80 unsigned int max_iter = 200) {
82 int n = x_init.size();
88 const gsl_multimin_fdfminimizer_type *T;
92 T = gsl_multimin_fdfminimizer_conjugate_fr;
95 T = gsl_multimin_fdfminimizer_conjugate_pr;
98 T = gsl_multimin_fdfminimizer_vector_bfgs2;
101 T = gsl_multimin_fdfminimizer_steepest_descent;
105 gsl_multimin_fdfminimizer *s;
107 x = gsl_vector_alloc (n);
108 for (
int i = 0; i < n; ++i) {
109 gsl_vector_set(x, i, x_init.at(i));
111 s = gsl_multimin_fdfminimizer_alloc (T, n);
112 gsl_multimin_fdfminimizer_set (s, &Func, x, step_size, tol);
116 status = gsl_multimin_fdfminimizer_iterate (s);
119 status = gsl_multimin_test_gradient (s->gradient, abs);
120 }
while (status == GSL_CONTINUE && iter < max_iter);
122 std::vector<double> ret;
123 for (
unsigned int i = 0; i < n; ++i) {
124 ret.push_back(gsl_vector_get(s -> x, i));
127 gsl_multimin_fdfminimizer_free (s);
152 std::vector<double> x_init,
154 double step_size = 0.1,
156 double increment = 1e-5,
158 unsigned int max_iter = 200) {
159 int n = x_init.size();
161 auto inverse_increment = 1.0 / increment;
162 auto df = [f,n,increment,inverse_increment](std::vector<double> x)->std::vector<double> {
163 std::vector<double> ret;
165 for (
int i = 0; i < n; ++i) {
167 ret.push_back( (f(x) -val) * inverse_increment);
173 auto fdf = [f,n,increment,inverse_increment](std::vector<double> x)-> std::pair<
double, std::vector<double> > {
174 std::vector<double> ret;
176 auto inverse_increment = 1.0 / increment;
177 for (
int i = 0; i < n; ++i) {
179 ret.push_back( (f(x) -val) * inverse_increment);
182 return std::make_pair(val,ret);
190 const gsl_multimin_fdfminimizer_type *T;
194 T = gsl_multimin_fdfminimizer_conjugate_fr;
197 T = gsl_multimin_fdfminimizer_conjugate_pr;
200 T = gsl_multimin_fdfminimizer_vector_bfgs2;
203 T = gsl_multimin_fdfminimizer_steepest_descent;
207 gsl_multimin_fdfminimizer *s;
210 x = gsl_vector_alloc (n);
212 for (
int i = 0; i < n; ++i) {
213 gsl_vector_set(x, i, x_init.at(i));
216 s = gsl_multimin_fdfminimizer_alloc (T, n);
217 gsl_multimin_fdfminimizer_set (s, &Func, x, step_size, tol);
221 status = gsl_multimin_fdfminimizer_iterate (s);
225 status = gsl_multimin_test_gradient (s->gradient, abs);
226 }
while (status == GSL_CONTINUE && iter < max_iter);
228 std::vector<double> ret;
229 for (
int i = 0; i < n; ++i) {
230 ret.push_back(gsl_vector_get(s -> x, i));
233 gsl_multimin_fdfminimizer_free (s);
Class implements adapter for gsl_multimin_function.
Definition: GslMultiminFunctionFdfAdapter.hpp:27
Definition: cadHoliday.cpp:3
File contains adapter of GSL Function.
DerivativeMinimizer
Types of multi-dimension minimizer that requires gradient of function.
Definition: derivativeMinimizer.hpp:49
Broyden-Fletcher-Goldfarb-Shanno.
Fletcher-Reeves conjugate gradient.
std::vector< double > derivativeMinimizer(F f, std::vector< double > x_init, DerivativeMinimizer type, double step_size=0.1, double tol=1e-4, double increment=1e-5, double abs=1e-6, unsigned int max_iter=200)
Multidimension minimizer that requires derivative of function.
Definition: derivativeMinimizer.hpp:151
Polak-Ribiere conjugate gradient.