26 #include <boost/numeric/conversion/cast.hpp>
33 #include "../exceptions.h"
34 #include "../population.h"
35 #include "../problem/base.h"
38 #include "base_nlopt.h"
40 namespace pagmo {
namespace algorithm {
55 base_nlopt::base_nlopt(nlopt::algorithm algo,
bool constrained,
bool only_ineq,
int max_iter,
const double &ftol,
const double &xtol):
base(),
56 m_algo(algo),m_constrained(constrained),m_only_ineq(only_ineq),m_max_iter(
boost::numeric_cast<
std::size_t>(max_iter)),m_ftol(ftol),m_xtol(xtol)
58 if ( (ftol <= 0) || (xtol <= 0) ) {
59 pagmo_throw(value_error,
"tolerances must be positive");
62 nlopt::opt opt(algo,1);
68 std::ostringstream oss;
70 oss <<
"ftol: " <<
m_ftol <<
" ";
76 double base_nlopt::objfun_wrapper(
const std::vector<double> &x, std::vector<double> &grad,
void* data)
78 nlopt_wrapper_data *d = (nlopt_wrapper_data *)data;
79 pagmo_assert(d->f.size() == 1);
86 std::copy(x.begin(),x.end(),d->dx.begin());
91 for (
size_t i =0; i < d->dx.size(); ++i)
93 h = h0 * std::max(1.,fabs(d->dx[i]));
96 d->prob->objfun(d->f,d->dx);
97 central_diff = d->f[0];
99 d->prob->objfun(d->f,d->dx);
100 central_diff = (central_diff-d->f[0]) / 2 / h;
101 grad[i] = central_diff;
107 d->prob->objfun(d->f,x);
114 double base_nlopt::constraints_wrapper(
const std::vector<double> &x, std::vector<double> &grad,
void* data)
116 nlopt_wrapper_data *d = (nlopt_wrapper_data *)data;
117 pagmo_assert(d->c.size() == d->prob->get_c_dimension());
124 std::copy(x.begin(),x.end(),d->dx.begin());
126 const double h0=1e-8;
129 for (
size_t i =0; i < d->dx.size(); ++i)
131 h = h0 * std::max(1.,fabs(d->dx[i]));
134 d->prob->compute_constraints(d->c,d->dx);
135 central_diff = d->c[d->c_comp];
137 d->prob->compute_constraints(d->c,d->dx);
138 central_diff = (central_diff-d->c[d->c_comp]) / 2 / h;
139 grad[i] = central_diff;
145 d->prob->compute_constraints(d->c,x);
147 return (d->c)[d->c_comp];
156 pagmo_throw(value_error,
"this algorithm does not support multi-objective optimisation");
160 if (c_size && !m_constrained) {
161 pagmo_throw(value_error,
"this algorithm does not support constraints");
163 if (ec_size && m_only_ineq) {
164 pagmo_throw(value_error,
"this algorithm does not support equality constraints");
168 pagmo_throw(value_error,
"the problem has no continuous part");
180 nlopt_wrapper_data data_objfun;
182 data_objfun.prob = &problem;
185 data_objfun.f.resize(1);
188 std::vector<nlopt_wrapper_data> data_constrfun(boost::numeric_cast<std::vector<nlopt_wrapper_data>::size_type>(c_size));
190 data_constrfun[i].prob = &problem;
194 data_constrfun[i].c_comp = i;
202 m_opt.set_lower_bounds(problem.
get_lb());
203 m_opt.set_upper_bounds(problem.
get_ub());
204 m_opt.set_min_objective(objfun_wrapper, &data_objfun);
206 m_opt.add_equality_constraint(constraints_wrapper, &data_constrfun[i], problem.
get_c_tol().at(i));
209 m_opt.add_inequality_constraint(constraints_wrapper, &data_constrfun[i], problem.
get_c_tol().at(i));
212 m_opt.set_ftol_abs(
m_ftol);
213 m_opt.set_xtol_abs(
m_xtol);
219 m_opt.optimize(x0, dummy);
220 pop.set_x(best_ind_idx,x0);
223 void base_nlopt::set_local(
size_t d)
const
std::vector< double > decision_vector
Decision vector type.
nlopt::opt m_opt
NLOPT optimization method.
const double m_xtol
Tolerance on the decision_vector variation function (stopping criteria)
const individual_type & get_individual(const size_type &) const
Get constant reference to individual at position n.
c_size_type get_ic_dimension() const
Return inequality constraints dimension.
Individuals stored in the population.
const std::size_t m_max_iter
Maximum number of iterations.
size_type get_dimension() const
Return global dimension.
void evolve(population &) const
Evolve method.
std::string human_readable_extra() const
Extra information in human readable format.
size_type get_i_dimension() const
Return integer dimension.
const double m_ftol
Tolerance on the fitness function variation (stopping criteria)
const std::vector< double > & get_c_tol() const
Return constraints tolerance.
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.
decision_vector cur_x
Current decision vector.
f_size_type get_f_dimension() const
Return fitness dimension.
constraint_vector::size_type c_size_type
Constraints' size type: the same as pagmo::constraint_vector's size type.
const decision_vector & get_lb() const
Lower bounds getter.
base_nlopt(nlopt::algorithm, bool, bool, int, const double &, const double &)
Constructor.
decision_vector::size_type size_type
Problem's size type: the same as pagmo::decision_vector's size type.