26 #ifdef MANGO_GSL_AVAILABLE
27 #include <gsl/gsl_vector.h>
28 #include <gsl/gsl_matrix.h>
29 #include <gsl/gsl_multimin.h>
33 #ifdef MANGO_GSL_AVAILABLE
34 if (solver->
verbose>0) std::cout <<
"Hello from optimize_gsl" << std::endl;
37 gsl_vector *gsl_state_vector = gsl_vector_alloc(solver->
N_parameters);
47 const gsl_multimin_fdfminimizer_type *Tfdf;
50 Tfdf = gsl_multimin_fdfminimizer_conjugate_fr;
53 Tfdf = gsl_multimin_fdfminimizer_conjugate_pr;
57 Tfdf = gsl_multimin_fdfminimizer_vector_bfgs2;
60 throw std::runtime_error(
"Error in optimize_gsl.cpp switch 1! Should not get here!");
63 gsl_multimin_fdfminimizer* fdfminimizer = gsl_multimin_fdfminimizer_alloc(Tfdf, solver->
N_parameters);
64 gsl_multimin_function_fdf fdf_parameters;
65 fdf_parameters.f = &mango::Package_gsl::gsl_objective_function;
66 fdf_parameters.df = &mango::Package_gsl::gsl_gradient;
67 fdf_parameters.fdf = &mango::Package_gsl::gsl_objective_function_and_gradient;
69 fdf_parameters.params = (
void*)solver;
71 double step_size = 0.01;
72 double line_search_tolerance = 0.1;
73 gsl_multimin_fdfminimizer_set(fdfminimizer, &fdf_parameters, gsl_state_vector, step_size, line_search_tolerance);
78 status = gsl_multimin_fdfminimizer_iterate(fdfminimizer);
80 status = gsl_multimin_test_gradient (fdfminimizer->gradient, 1e-5);
83 gsl_multimin_fdfminimizer_free(fdfminimizer);
90 const gsl_multimin_fminimizer_type *Tf;
93 Tf = gsl_multimin_fminimizer_nmsimplex2;
96 throw std::runtime_error(
"Error in optimize_gsl.cpp switch 2! Should not get here!");
98 gsl_multimin_fminimizer* fminimizer = gsl_multimin_fminimizer_alloc(Tf, solver->
N_parameters);
99 gsl_multimin_function f_parameters;
100 f_parameters.f = &gsl_objective_function;
102 f_parameters.params = (
void*)solver;
105 gsl_vector* step_sizes;
107 gsl_vector_set_all(step_sizes, 0.1);
109 gsl_multimin_fminimizer_set(fminimizer, &f_parameters, gsl_state_vector, step_sizes);
115 status = gsl_multimin_fminimizer_iterate(fminimizer);
117 size = gsl_multimin_fminimizer_size (fminimizer);
118 status = gsl_multimin_test_size (size, 1e-6);
121 gsl_vector_free(step_sizes);
122 gsl_multimin_fminimizer_free(fminimizer);
128 gsl_vector_free(gsl_state_vector);
129 if (solver->
verbose>0) std::cout <<
"Goodbye from optimize_gsl" << std::endl;
131 throw std::runtime_error(
"Error! A GSL algorithm was requested, but Mango was compiled without GSL support.");
137 #ifdef MANGO_GSL_AVAILABLE
139 double mango::Package_gsl::gsl_objective_function(
const gsl_vector * x,
void *params) {
142 if (solver->
verbose > 0) std::cout <<
"Hello from gsl_objective_function." << std::endl << std::flush;
147 assert(x->stride == 1);
153 if (solver->
verbose > 0) std::cout <<
"Goodbye from gsl_objective_function" << std::endl << std::flush;
164 void mango::Package_gsl::gsl_objective_function_and_gradient(
const gsl_vector * x,
void *params,
double* f, gsl_vector* gradient) {
166 if (solver->
verbose > 0) std::cout <<
"Hello from gsl_objective_function_and_gradient" << std::endl << std::flush;
171 assert(x->stride == 1);
172 assert(gradient->stride == 1);
176 if (solver->
verbose > 0) std::cout <<
"Goodbye from gsl_objective_function_and_gradient" << std::endl << std::flush;
182 void mango::Package_gsl::gsl_gradient(
const gsl_vector * x,
void *params, gsl_vector* gradient) {
184 if (solver->
verbose > 0) std::cout <<
"Hello from gsl_gradient" << std::endl << std::flush;
187 mango::Package_gsl::gsl_objective_function_and_gradient(x, params, &f, gradient);