26 #include <boost/numeric/conversion/cast.hpp>
29 #include <gsl/gsl_multimin.h>
30 #include <gsl/gsl_vector.h>
36 #include "../exceptions.h"
37 #include "../population.h"
38 #include "../problem/base.h"
41 #include "gsl_derivative_free.h"
43 namespace pagmo {
namespace algorithm {
55 base_gsl(),m_max_iter(
boost::numeric_cast<
std::size_t>(max_iter)),m_tol(tol),m_step_size(step_size)
58 pagmo_throw(value_error,
"step size must be positive");
61 pagmo_throw(value_error,
"tolerance must be positive");
71 std::ostringstream oss;
72 oss <<
"max_iter:" << m_max_iter <<
' ';
73 oss <<
"tol:" << m_tol <<
' ';
74 oss <<
"step_size:" << m_step_size <<
' ';
97 pagmo_throw(value_error,
"this algorithm does not support multi-objective optimisation");
100 pagmo_throw(value_error,
"this algorithm does not support constrained optimisation");
104 pagmo_throw(value_error,
"the problem has no continuous part");
115 std::copy(best_ind.
cur_x.begin() + cont_size, best_ind.
cur_x.end(), params.
x.begin() + cont_size);
118 gsl_multimin_function gsl_func;
120 gsl_func.n = boost::numeric_cast<std::size_t>(cont_size);
122 gsl_func.params = (
void *)¶ms;
124 gsl_multimin_fminimizer *s = 0;
126 gsl_vector *x = 0, *ss = 0;
129 const std::size_t s_cont_size = boost::numeric_cast<std::size_t>(cont_size);
131 x = gsl_vector_alloc(s_cont_size);
132 ss = gsl_vector_alloc(s_cont_size);
134 pagmo_assert(minimiser);
135 s = gsl_multimin_fminimizer_alloc(minimiser,s_cont_size);
137 check_allocs(x,ss,s);
139 for (std::size_t i = 0; i < s_cont_size; ++i) {
140 gsl_vector_set(x,i,best_ind.
cur_x[i]);
143 gsl_vector_set_all(ss,m_step_size);
145 gsl_multimin_fminimizer_set(s,&gsl_func,x,ss);
147 std::size_t iter = 0;
153 status = gsl_multimin_fminimizer_iterate(s);
158 size = gsl_multimin_fminimizer_size(s);
159 status = gsl_multimin_test_size(size, m_tol);
161 if (!((iter-1)%20)) {
162 std::cout << std::endl << std::left << std::setw(20) <<
163 "Iter." << std::setw(20) <<
164 "Best " << std::setw(20) <<
167 std::cout << std::left << std::setprecision(14) << std::setw(20) <<
168 iter << std::setw(20) <<
169 gsl_multimin_fminimizer_minimum(s) << std::setw(20) <<
172 }
while (status == GSL_CONTINUE && iter < m_max_iter);
173 }
catch (
const std::exception &e) {
180 pagmo_throw(std::runtime_error,
"unknown exception caught in gsl_derivative_free::evolve");
186 if (params.
x[i] < problem.
get_lb()[i]) {
187 params.
x[i] = problem.
get_lb()[i];
189 if (params.
x[i] > problem.
get_ub()[i]) {
190 params.
x[i] = problem.
get_ub()[i];
194 pop.set_x(best_ind_idx,params.
x);
198 void gsl_derivative_free::check_allocs(gsl_vector *x, gsl_vector *ss, gsl_multimin_fminimizer *s)
201 if (!x || !ss || !s) {
203 throw std::bad_alloc();
208 void gsl_derivative_free::cleanup(gsl_vector *x, gsl_vector *ss, gsl_multimin_fminimizer *s)
217 gsl_multimin_fminimizer_free(s);
const individual_type & get_individual(const size_type &) const
Get constant reference to individual at position n.
Individuals stored in the population.
size_type get_dimension() const
Return global dimension.
Base class for GSL algorithms.
std::string human_readable_extra() const
Extra information in human-readable format.
virtual const gsl_multimin_fminimizer_type * get_gsl_minimiser_ptr() const =0
Selected minimiser.
fitness_vector f
Fitness vector.
bool m_screen_output
Indicates to the derived class whether to print stuff on screen.
void evolve(population &) const
Evolve method.
size_type get_i_dimension() const
Return integer dimension.
c_size_type get_c_dimension() const
Return global constraints dimension.
const decision_vector & get_ub() const
Upper bounds getter.
container_type::size_type size_type
Population size type.
static double objfun_wrapper(const gsl_vector *, void *)
Objective function wrapper.
decision_vector cur_x
Current decision vector.
Structure to feed parameters to the wrappers for the objective function and its derivative.
f_size_type get_f_dimension() const
Return fitness dimension.
gsl_derivative_free(int, const double &, const double &)
Constructor.
decision_vector x
Decision vector.
problem::base const * p
Pointer to the problem.
const decision_vector & get_lb() const
Lower bounds getter.
decision_vector::size_type size_type
Problem's size type: the same as pagmo::decision_vector's size type.