28 #include "../exceptions.h"
30 #include "../population.h"
31 #include "cstrs_co_evolution.h"
36 namespace pagmo {
namespace problem {
60 problem.get_dimension(),
61 problem.get_i_dimension(),
62 problem.get_f_dimension(),
65 std::vector<double>()),
70 m_decision_vector_hash()
72 if(m_original_problem->get_c_dimension() <= 0){
73 pagmo_throw(value_error,
"The original problem has no constraints.");
77 if (m_original_problem->get_f_dimension() != 1) {
78 pagmo_throw(value_error,
"The original fitness dimension of the problem must be one, multi objective problems can't be handled with co-evolution meta problem.");
81 std::fill(m_penalty_coeff.begin(),m_penalty_coeff.end(),0.);
87 problem.get_dimension(),
88 problem.get_i_dimension(),
89 problem.get_f_dimension(),
92 std::vector<double>()),
97 m_decision_vector_hash()
99 if(m_original_problem->get_c_dimension() <= 0){
100 pagmo_throw(value_error,
"The original problem has no constraints.");
104 if (m_original_problem->get_f_dimension() != 1) {
105 pagmo_throw(value_error,
"The original fitness dimension of the problem must be one, multi objective problems can't be handled with co-evolution meta problem.");
107 if(problem != pop.problem()) {
108 pagmo_throw(value_error,
"The problem linked to the population is not the same as the problem given in argument.");
111 std::fill(m_penalty_coeff.begin(),m_penalty_coeff.end(),0.);
113 m_map_fitness.clear();
114 m_map_constraint.clear();
117 const population::individual_type ¤t_individual = pop.get_individual(i);
119 m_map_fitness[m_decision_vector_hash(current_individual.cur_x)]=current_individual.cur_f;
120 m_map_constraint[m_decision_vector_hash(current_individual.cur_x)]=current_individual.cur_c;
139 std::map<std::size_t, fitness_vector>::const_iterator it_f;
141 it_f = m_map_fitness.find(m_decision_vector_hash(x));
142 if(it_f != m_map_fitness.end()) {
145 m_original_problem->objfun(f, x);
148 std::vector<double> sum_viol;
149 std::vector<int> num_viol;
151 compute_penalty(sum_viol,num_viol,x);
156 case algorithm::cstrs_co_evolution::SIMPLE:
158 f[0] += sum_viol.at(0) * m_penalty_coeff.at(0) + double(num_viol.at(0)) * m_penalty_coeff.at(1);
161 case algorithm::cstrs_co_evolution::SPLIT_NEQ_EQ:
163 f[0] += sum_viol.at(0) * m_penalty_coeff.at(0) + double(num_viol.at(0)) * m_penalty_coeff.at(1);
164 f[0] += sum_viol.at(1) * m_penalty_coeff.at(2) + double(num_viol.at(1)) * m_penalty_coeff.at(3);
167 case algorithm::cstrs_co_evolution::SPLIT_CONSTRAINTS:
169 int c_dimension = m_original_problem->get_c_dimension();
170 for(
int i=0; i<c_dimension; i++) {
171 f[0] += sum_viol.at(i) * m_penalty_coeff.at(i*2) + double(num_viol.at(i)) * m_penalty_coeff.at(1+i*2);
184 std::ostringstream oss;
185 oss << m_original_problem->human_readable_extra() << std::endl;
186 oss <<
"\n\tConstraints handled with co-evolution method ";
193 return m_original_problem->get_name() +
" [cstrs_co_evolution]";
201 void cstrs_co_evolution::set_penalty_coeff(
const std::vector<double> &penalty_coeff)
205 case algorithm::cstrs_co_evolution::SIMPLE:
207 if(penalty_coeff.size() != 2) {
208 pagmo_throw(value_error,
"The size of the penalty coefficient vector is not 2.");
212 case algorithm::cstrs_co_evolution::SPLIT_NEQ_EQ:
214 if(penalty_coeff.size() != 4) {
215 pagmo_throw(value_error,
"The size of the penalty coefficient vector is not 4.");
219 case algorithm::cstrs_co_evolution::SPLIT_CONSTRAINTS:
220 if(penalty_coeff.size() != 2*m_original_problem->get_c_dimension()) {
221 pagmo_throw(value_error,
"The size of the penalty coefficient vector is not 2*number constraints");
225 pagmo_throw(value_error,
"The constraints co-evolutionary method is not valid.");
228 m_penalty_coeff = penalty_coeff;
233 int cstrs_co_evolution::get_penalty_coeff_size() {
236 case algorithm::cstrs_co_evolution::SIMPLE:
241 case algorithm::cstrs_co_evolution::SPLIT_NEQ_EQ:
246 case algorithm::cstrs_co_evolution::SPLIT_CONSTRAINTS:
247 return 2*m_original_problem->get_c_dimension();
250 pagmo_throw(value_error,
"The constraints co-evolutionary method is not valid.");
262 void cstrs_co_evolution::compute_penalty(std::vector<double> &sum_viol, std::vector<int> &num_viol,
const decision_vector &x)
const
270 const std::vector<double> &c_tol = m_original_problem->get_c_tol();
273 std::map<std::size_t, constraint_vector>::const_iterator it_c;
275 it_c = m_map_constraint.find(m_decision_vector_hash(x));
276 if(it_c != m_map_constraint.end()) {
279 m_original_problem->compute_constraints(c,x);
285 if(j<number_of_eq_constraints){
286 c_vio[j] = std::abs(c.at(j)) - c_tol.at(j);
289 c_vio[j] = c.at(j) - c_tol.at(j);
293 c_vio[j] = std::max(0., c_vio.at(j));
299 case algorithm::cstrs_co_evolution::SIMPLE:
303 std::fill(sum_viol.begin(),sum_viol.end(),0.);
304 std::fill(num_viol.begin(),num_viol.end(),0.);
308 sum_viol[0] += c_vio.at(j);
312 if(!m_original_problem->test_constraint(c, j)) {
318 case algorithm::cstrs_co_evolution::SPLIT_NEQ_EQ:
322 std::fill(sum_viol.begin(),sum_viol.end(),0.);
323 std::fill(num_viol.begin(),num_viol.end(),0.);
327 sum_viol[0] += c_vio.at(j);
330 sum_viol[1] += c_vio.at(j);
333 if(!m_original_problem->test_constraint(c, j)) {
338 if(!m_original_problem->test_constraint(c, j)) {
344 case algorithm::cstrs_co_evolution::SPLIT_CONSTRAINTS:
346 sum_viol.resize(prob_c_dimension);
347 num_viol.resize(prob_c_dimension);
349 std::fill(sum_viol.begin(),sum_viol.end(),0.);
350 std::fill(num_viol.begin(),num_viol.end(),0.);
354 sum_viol[j] += c_vio.at(j);
357 if(!m_original_problem->test_constraint(c, j)) {
376 cstrs_co_evolution_penalty::cstrs_co_evolution_penalty(
const base &problem,
int dimension,
int size):
378 problem.get_i_dimension(),
383 m_original_problem(problem.clone()),
385 m_feasible_count_vector(size,0),
386 m_feasible_fitness_sum_vector(size,0.0),
387 m_max_feasible_fitness(0.),
388 m_total_sum_viol(size,0.0),
389 m_total_num_viol(size,0)
391 if(m_original_problem->get_c_dimension() <= 0){
392 pagmo_throw(value_error,
"The original problem has no constraints.");
396 if (m_original_problem->get_f_dimension() != 1) {
397 pagmo_throw(value_error,
"The original fitness dimension of the problem must be one, multi objective problems can't be handled with co evolution meta problem.");
400 set_bounds(0.,10000.);
404 cstrs_co_evolution_penalty::cstrs_co_evolution_penalty(
const cstrs_co_evolution_penalty &prob):
406 m_original_problem(prob.m_original_problem->clone()),
407 m_pop_2_x_vector(prob.m_pop_2_x_vector),
408 m_feasible_count_vector(prob.m_feasible_count_vector),
409 m_feasible_fitness_sum_vector(prob.m_feasible_fitness_sum_vector),
410 m_max_feasible_fitness(prob.m_max_feasible_fitness),
411 m_total_sum_viol(prob.m_total_sum_viol),
412 m_total_num_viol(prob.m_total_num_viol)
414 set_bounds(prob.get_lb(),prob.get_ub());
418 base_ptr cstrs_co_evolution_penalty::clone()
const
420 return base_ptr(
new cstrs_co_evolution_penalty(*
this));
432 for(std::vector<decision_vector>::size_type i=0; i<m_pop_2_x_vector.size(); i++) {
433 if(m_pop_2_x_vector.at(i) == x) {
442 if(m_feasible_count_vector.at(position) > 0) {
443 f[0] = m_feasible_fitness_sum_vector[position] / double(m_feasible_count_vector[position]) - double(m_feasible_count_vector[position]);
445 f[0] = m_max_feasible_fitness +
446 m_total_sum_viol.at(position)/double(m_total_num_viol.at(position)) -
447 double(m_total_num_viol.at(position));
452 f[0] = m_max_feasible_fitness;
463 return m_original_problem->compare_fitness(v_f1,v_f2);
470 std::string cstrs_co_evolution_penalty::human_readable_extra()
const
472 std::ostringstream oss;
473 oss << m_original_problem->human_readable_extra() << std::endl;
474 oss <<
"\n\tConstraints handled with co-evolution method ";
479 std::string cstrs_co_evolution_penalty::get_name()
const
481 return m_original_problem->get_name() +
" [cstrs_co_evolution_2]";
491 m_pop_2_x_vector.at(index) = pop_2_x;
495 m_max_feasible_fitness = 0.;
498 m_total_sum_viol[index] = 0.;
499 m_total_num_viol[index] = 0;
502 int feasible_count = 0;
503 double feasible_fitness_sum = 0.;
505 double sum_viol_temp = 0.;
506 int num_viol_temp = 0;
511 if(m_original_problem->feasibility_c(current_c)) {
513 feasible_fitness_sum += current_f[0];
517 m_max_feasible_fitness = current_f[0];
519 if(m_max_feasible_fitness < current_f[0]) {
520 m_max_feasible_fitness = current_f[0];
525 compute_penalty(sum_viol_temp,num_viol_temp,current_c);
526 m_total_sum_viol[index] += sum_viol_temp;
527 m_total_num_viol[index] += num_viol_temp;
529 m_feasible_count_vector[index] = feasible_count;
530 m_feasible_fitness_sum_vector[index] = feasible_fitness_sum;
540 void cstrs_co_evolution_penalty::compute_penalty(
double &sum_viol,
int &num_viol,
const constraint_vector &c)
const
546 const std::vector<double> &c_tol = m_original_problem->get_c_tol();
551 sum_viol += std::max(0.,std::abs(c.at(j)) - c_tol.at(j));
555 sum_viol += std::max(0.,c.at(j));
560 if(!m_original_problem->test_constraint(c, j)) {
569 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::problem::cstrs_co_evolution)
570 BOOST_CLASS_EXPORT_IMPLEMENT(
pagmo::problem::cstrs_co_evolution_penalty)
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base algorithm.
std::vector< double > decision_vector
Decision vector type.
method_type
Type of co-evolution.
std::vector< double > fitness_vector
Fitness vector type.
std::vector< double > constraint_vector
Constraint vector type.
container_type::size_type size_type
Population size type.
constraint_vector::size_type c_size_type
Constraints' size type: the same as pagmo::constraint_vector's size type.
std::string human_readable_extra() const
Extra human readable algorithm info.
base_ptr clone() const
Clone method.
std::string get_name() const
Algorithm name.
cstrs_co_evolution(const base &=jde(), const base &=sga(1), int pop_penalties_size=30, int gen=1, method_type method=SIMPLE, double pen_lower_bound=0., double pen_upper_bound=100000., double=1e-15, double=1e-15)
Constructor.