27 #ifndef PAGMO_PROBLEM_BASE_H
28 #define PAGMO_PROBLEM_BASE_H
35 #include <boost/circular_buffer.hpp>
36 #include <boost/numeric/conversion/cast.hpp>
37 #include <boost/shared_ptr.hpp>
43 #include "../config.h"
44 #include "../exceptions.h"
45 #include "../serialization.h"
65 typedef boost::shared_ptr<base>
base_ptr;
153 typedef boost::circular_buffer<decision_vector> decision_vector_cache_type;
154 typedef boost::circular_buffer<fitness_vector> fitness_vector_cache_type;
155 typedef boost::circular_buffer<constraint_vector> constraint_vector_cache_type;
158 static const std::size_t cache_capacity = 5;
165 base(
int,
int = 0,
int = 1,
int = 0,
int = 0,
const double & = 0);
166 base(
int,
int,
int,
int,
int,
const std::vector<double> &);
167 base(
const double &,
const double &,
int,
int = 0,
int = 1,
int = 0,
int = 0,
const double & = 0);
185 template <std::
size_t N>
186 base(
const double (&v1)[N],
const double (&v2)[N],
int ni = 0,
int nf = 1,
int nc = 0,
int nic = 0,
const double &c_tol = 0):
187 m_i_dimension(
boost::numeric_cast<size_type>(ni)),m_f_dimension(
boost::numeric_cast<f_size_type>(nf)),
188 m_c_dimension(
boost::numeric_cast<c_size_type>(nc)),m_ic_dimension(
boost::numeric_cast<c_size_type>(nic)),
190 m_decision_vector_cache_f(
boost::numeric_cast<decision_vector_cache_type::size_type>(cache_capacity)),
191 m_fitness_vector_cache(
boost::numeric_cast<fitness_vector_cache_type::size_type>(cache_capacity)),
192 m_decision_vector_cache_c(
boost::numeric_cast<decision_vector_cache_type::size_type>(cache_capacity)),
193 m_constraint_vector_cache(
boost::numeric_cast<constraint_vector_cache_type::size_type>(cache_capacity))
196 pagmo_throw(value_error,
"constraints tolerance must be non-negative");
198 if (!m_f_dimension) {
199 pagmo_throw(value_error,
"fitness dimension must be strictly positive");
201 if (m_ic_dimension > m_c_dimension) {
202 pagmo_throw(value_error,
"inequality constraints dimension must not be greater than global constraints dimension");
204 construct_from_iterators(v1,v1 + N,v2,v2 + N);
205 if (m_i_dimension > m_lb.size()) {
206 pagmo_throw(value_error,
"integer dimension must not be greater than global dimension");
209 m_tmp_f1.resize(m_f_dimension);
210 m_tmp_f2.resize(m_f_dimension);
211 m_tmp_c1.resize(m_c_dimension);
212 m_tmp_c2.resize(m_c_dimension);
234 template <
class Iterator1,
class Iterator2>
235 base(Iterator1 start1, Iterator1 end1, Iterator2 start2, Iterator2 end2,
int ni = 0,
int nf = 1,
int nc = 0,
int nic = 0,
const double &c_tol = 0):
236 m_i_dimension(
boost::numeric_cast<size_type>(ni)),m_f_dimension(
boost::numeric_cast<f_size_type>(nf)),
237 m_c_dimension(
boost::numeric_cast<c_size_type>(nc)),m_ic_dimension(
boost::numeric_cast<c_size_type>(nic)),
239 m_decision_vector_cache_f(
boost::numeric_cast<decision_vector_cache_type::size_type>(cache_capacity)),
240 m_fitness_vector_cache(
boost::numeric_cast<fitness_vector_cache_type::size_type>(cache_capacity)),
241 m_decision_vector_cache_c(
boost::numeric_cast<decision_vector_cache_type::size_type>(cache_capacity)),
242 m_constraint_vector_cache(
boost::numeric_cast<constraint_vector_cache_type::size_type>(cache_capacity))
245 pagmo_throw(value_error,
"constraints tolerance must be non-negative");
247 if (!m_f_dimension) {
248 pagmo_throw(value_error,
"fitness dimension must be strictly positive");
250 if (m_ic_dimension > m_c_dimension) {
251 pagmo_throw(value_error,
"inequality constraints dimension must not be greater than global constraints dimension");
253 construct_from_iterators(start1,end1,start2,end2);
254 if (m_i_dimension > m_lb.size()) {
255 pagmo_throw(value_error,
"integer dimension must not be greater than global dimension");
258 m_tmp_f1.resize(m_f_dimension);
259 m_tmp_f2.resize(m_f_dimension);
260 m_tmp_c1.resize(m_c_dimension);
261 m_tmp_c2.resize(m_c_dimension);
283 template <
class Iterator1,
class Iterator2>
284 void set_bounds(Iterator1 start1, Iterator1 end1, Iterator2 start2, Iterator2 end2)
286 typedef typename std::iterator_traits<Iterator1>::difference_type d_type1;
287 typedef typename std::iterator_traits<Iterator2>::difference_type d_type2;
288 const d_type1 d1 = std::distance(start1,end1);
289 const d_type2 d2 = std::distance(start2,end2);
290 if (d1 != d2 || d1 != std::distance(m_lb.begin(),m_lb.end())) {
291 pagmo_throw(value_error,
"invalid or inconsistent bounds dimensions in set_bounds()");
293 verify_bounds(start1,end1,start2,end2);
294 std::copy(start1,end1,m_lb.begin());
295 std::copy(start2,end2,m_ub.begin());
307 template <std::
size_t N>
308 void set_bounds(
const double (&v1)[N],
const double (&v2)[N])
310 if (m_lb.size() != N) {
311 pagmo_throw(value_error,
"invalid bounds dimensions in set_bounds()");
313 verify_bounds(v1,v1 + N,v2,v2 + N);
314 std::copy(v1,v1 + N,m_lb.begin());
315 std::copy(v2,v2 + N,m_ub.begin());
319 void set_bounds(
const double &,
const double &);
320 void set_bounds(
int,
const double &,
const double &);
322 void set_lb(
int,
const double &);
323 void set_lb(
const double &);
332 template <
class Iterator>
333 void set_lb(Iterator start, Iterator end)
335 if (std::distance(start,end) != std::distance(m_lb.begin(),m_lb.end())) {
336 pagmo_throw(value_error,
"invalid bounds dimension in set_lb()");
338 verify_bounds(start,end,m_ub.begin(),m_ub.end());
339 std::copy(start,end,m_lb.begin());
350 template <std::
size_t N>
353 if (N != m_lb.size()) {
354 pagmo_throw(value_error,
"invalid bounds dimension in set_lb()");
356 verify_bounds(v,v + N,m_ub.begin(),m_ub.end());
357 std::copy(v,v + N,m_lb.begin());
362 void set_ub(
int,
const double &);
363 void set_ub(
const double &);
372 template <
class Iterator>
373 void set_ub(Iterator start, Iterator end)
375 if (std::distance(start,end) != std::distance(m_lb.begin(),m_lb.end())) {
376 pagmo_throw(value_error,
"invalid bounds dimension in set_ub()");
378 verify_bounds(m_lb.begin(),m_lb.end(),start,end);
379 std::copy(start,end,m_ub.begin());
390 template <std::
size_t N>
393 if (N != m_lb.size()) {
394 pagmo_throw(value_error,
"invalid bounds dimension in set_ub()");
396 verify_bounds(m_lb.begin(),m_lb.end(),v,v + N);
397 std::copy(v,v + N,m_ub.begin());
404 unsigned int get_fevals()
const;
405 unsigned int get_cevals()
const;
406 size_type get_dimension()
const;
407 size_type get_i_dimension()
const;
408 f_size_type get_f_dimension()
const;
409 c_size_type get_c_dimension()
const;
410 c_size_type get_ic_dimension()
const;
411 const std::vector<double>& get_c_tol()
const;
412 double get_diameter()
const;
413 virtual std::string get_name()
const;
431 virtual base_ptr clone()
const = 0;
432 std::string human_readable()
const;
433 virtual std::string human_readable_extra()
const;
434 bool operator==(
const base &)
const;
435 bool operator!=(
const base &)
const;
436 bool is_compatible(
const base &)
const;
440 virtual void pre_evolution(
population &)
const;
441 virtual void post_evolution(
population &)
const;
443 virtual bool equality_operator_extra(
const base &)
const;
447 void estimate_sparsity(
const decision_vector &,
int& lenG, std::vector<int>& iGfun, std::vector<int>& jGvar)
const;
448 void estimate_sparsity(
int& lenG, std::vector<int>& iGfun, std::vector<int>& jGvar)
const;
450 virtual void set_sparsity(
int& lenG, std::vector<int>& iGfun, std::vector<int>& jGvar)
const;
458 void reset_caches()
const;
460 const std::vector<constraint_vector>& get_best_c(
void)
const;
461 const std::vector<decision_vector>& get_best_x(
void)
const;
462 const std::vector<fitness_vector>& get_best_f(
void)
const;
463 void set_best_x(
const std::vector<decision_vector>&);
477 void normalise_bounds();
479 template <
class Iterator1,
class Iterator2>
480 void construct_from_iterators(Iterator1 start1, Iterator1 end1, Iterator2 start2, Iterator2 end2)
482 m_lb.insert(m_lb.end(),start1,end1);
483 m_ub.insert(m_ub.end(),start2,end2);
484 if (m_lb.size() != m_ub.size() || m_lb.size() == 0) {
485 pagmo_throw(value_error,
"null or inconsistent dimension(s) for upper/lower bounds while constructing problem");
487 verify_bounds(m_lb.begin(),m_lb.end(),m_ub.begin(),m_ub.end());
491 template <
class Iterator1,
class Iterator2>
492 static void verify_bounds(Iterator1 start1, Iterator1 end1, Iterator2 start2, Iterator2 end2)
494 for (; start1 != end1 && start2 != end2; ++start1, ++start2) {
495 if (*start1 > *start2) {
496 pagmo_throw(value_error,
"lower bound is greater than upper bound");
501 friend class boost::serialization::access;
502 template <
class Archive>
503 void serialize(Archive &ar,
const unsigned int)
505 ar &
const_cast<size_type &
>(m_i_dimension);
506 ar &
const_cast<f_size_type &
>(m_f_dimension);
507 ar &
const_cast<c_size_type &
>(m_c_dimension);
508 ar &
const_cast<c_size_type &
>(m_ic_dimension);
511 ar &
const_cast<std::vector<double> &
>(m_c_tol);
512 ar & m_decision_vector_cache_f;
513 ar & m_fitness_vector_cache;
514 ar & m_decision_vector_cache_c;
515 ar & m_constraint_vector_cache;
529 const size_type m_i_dimension;
531 const f_size_type m_f_dimension;
533 const c_size_type m_c_dimension;
535 const c_size_type m_ic_dimension;
541 const std::vector<double> m_c_tol;
543 mutable decision_vector_cache_type m_decision_vector_cache_f;
545 mutable fitness_vector_cache_type m_fitness_vector_cache;
547 mutable decision_vector_cache_type m_decision_vector_cache_c;
549 mutable constraint_vector_cache_type m_constraint_vector_cache;
558 std::vector<decision_vector> m_best_x;
559 std::vector<fitness_vector> m_best_f;
560 std::vector<constraint_vector> m_best_c;
563 mutable unsigned int m_fevals;
564 mutable unsigned int m_cevals;
567 std::ostream __PAGMO_VISIBLE_FUNC &
operator<<(std::ostream &,
const base &);
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base problem.
std::vector< double > decision_vector
Decision vector type.
void set_lb(const double(&v)[N])
Lower bounds setter from raw array.
fitness_vector::size_type f_size_type
Fitness' size type: the same as pagmo::fitness_vector's size type.
std::ostream & operator<<(std::ostream &s, const base &p)
Overload stream operator for problem::base.
void set_bounds(Iterator1 start1, Iterator1 end1, Iterator2 start2, Iterator2 end2)
Bounds setter from iterators.
std::vector< double > fitness_vector
Fitness vector type.
std::vector< double > constraint_vector
Constraint vector type.
void set_ub(Iterator start, Iterator end)
Upper bounds setter from iterators.
base(Iterator1 start1, Iterator1 end1, Iterator2 start2, Iterator2 end2, int ni=0, int nf=1, int nc=0, int nic=0, const double &c_tol=0)
Constructor from iterators, integer dimension, fitness dimension, global constraints dimension...
constraint_vector::size_type c_size_type
Constraints' size type: the same as pagmo::constraint_vector's size type.
void set_bounds(const double(&v1)[N], const double(&v2)[N])
Bounds setter from raw arrays.
void set_ub(const double(&v)[N])
Upper bounds setter from raw array.
decision_vector::size_type size_type
Problem's size type: the same as pagmo::decision_vector's size type.
base(const double(&v1)[N], const double(&v2)[N], int ni=0, int nf=1, int nc=0, int nic=0, const double &c_tol=0)
Constructor from raw arrays, integer dimension, fitness dimension, global constraints dimension...
void set_lb(Iterator start, Iterator end)
Lower bounds setter from iterators.