nonDerivativeMinimizer.hpp
Go to the documentation of this file.
1 #ifndef JULIAN_NONDERIVATIVEMINIMIZER_HPP
2 #define JULIAN_NONDERIVATIVEMINIMIZER_HPP
3 
4 #include <gsl/gsl_multimin.h>
6 
7 namespace julian {
8 
35  };
36 
49  template<typename F>
50  std::vector<double> nonDerivativeMinimizer(F f,std::vector<double> x_init, NonDerivativeMinimizer t, double abs = 1e-3, int number_of_iterations = 100) {
51  int n = x_init.size();
52 
54 
55  gsl_multimin_function *gsl_f = static_cast<gsl_multimin_function*>(&Func);
56 
57  const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex2;
58 
59  switch(t) {
61  T = gsl_multimin_fminimizer_nmsimplex2;
62  break;
64  T = gsl_multimin_fminimizer_nmsimplex2rand;
65  break;
66  }
67 
68  gsl_multimin_fminimizer* s = gsl_multimin_fminimizer_alloc (T, n);
69 
70  gsl_vector *ss, *x;
71  x = gsl_vector_alloc (n);
72  ss = gsl_vector_alloc (n);
73  gsl_vector_set_all (ss, 1.0);
74 
75  for (int i = 0; i < n; i++) {
76  gsl_vector_set (x, i, x_init.at(i));
77  }
78 
79  gsl_multimin_fminimizer_set (s, gsl_f, x, ss);
80  int status = 0;
81  int iter = 0;
82  do {
83  iter++;
84  status = gsl_multimin_fminimizer_iterate(s);
85 
86  if (status)
87  break;
88 
89  double size = gsl_multimin_fminimizer_size (s);
90  status = gsl_multimin_test_size (size, abs);
91 
92  } while (status == GSL_CONTINUE && iter < number_of_iterations);
93 
94  std::vector<double> ret(n);
95 
96  for( int i = 0; i < n; i++) {
97  ret.at(i) = gsl_vector_get(s -> x, i);
98  }
99 
100  return ret;
101  }
102 } // namespace julian
103 #endif
Definition: cadHoliday.cpp:3
Class implements adapter for gsl_multimin_function.
Definition: GslMultiminFunctionAdapter.hpp:23
std::vector< double > nonDerivativeMinimizer(F f, std::vector< double > x_init, NonDerivativeMinimizer t, double abs=1e-3, int number_of_iterations=100)
Multi-dimension minimizer that does not require derivative of function.
Definition: nonDerivativeMinimizer.hpp:50
File contains adapter of GSL Function.
NonDerivativeMinimizer
Types of multi-dimension minimizer that does not require derivative of function.
Definition: nonDerivativeMinimizer.hpp:33