25 #include <boost/random/uniform_int.hpp>
26 #include <boost/random/uniform_real.hpp>
27 #include <boost/random/variate_generator.hpp>
28 #include <boost/random/normal_distribution.hpp>
32 #include "../exceptions.h"
33 #include "../population.h"
34 #include "../problem/base.h"
35 #include "../problem/cstrs_co_evolution.h"
38 #include "cstrs_co_evolution.h"
40 namespace pagmo {
namespace algorithm {
65 const base &original_algo_penalties,
int pop_penalties_size,
67 double pen_upper_bound,
68 double ftol,
double xtol):
69 base(),m_original_algo(original_algo.clone()), m_original_algo_penalties(original_algo_penalties.clone()),
70 m_gen(gen),m_pop_penalties_size(pop_penalties_size),m_method(method),
71 m_pen_lower_bound(pen_lower_bound),m_pen_upper_bound(pen_upper_bound),m_ftol(ftol),m_xtol(xtol)
74 pagmo_throw(value_error,
"number of generations must be nonnegative");
76 if(pop_penalties_size <= 0) {
77 pagmo_throw(value_error,
"the population size of the penalty weights must be greater than 0");
79 if(pen_lower_bound>=pen_upper_bound){
80 pagmo_throw(value_error,
"Lower Bound of penalty coefficients must be smaller than Upper Bound");
86 base(algo),m_original_algo(algo.m_original_algo->clone()),
87 m_original_algo_penalties(algo.m_original_algo_penalties->clone()),m_gen(algo.m_gen),
88 m_pop_penalties_size(algo.m_pop_penalties_size),m_method(algo.m_method),
89 m_pen_lower_bound(algo.m_pen_lower_bound),m_pen_upper_bound(algo.m_pen_upper_bound),
90 m_ftol(algo.m_ftol),m_xtol(algo.m_xtol)
116 if(prob_c_dimension < 1) {
117 pagmo_throw(value_error,
"The problem is not constrained and co-evolution is not suitable to solve it");
120 pagmo_throw(value_error,
"The problem is multiobjective and co-evolution is not suitable to solve it");
129 unsigned int pop_2_dim = 0;
132 case algorithm::cstrs_co_evolution::SIMPLE:
137 case algorithm::cstrs_co_evolution::SPLIT_NEQ_EQ:
142 case algorithm::cstrs_co_evolution::SPLIT_CONSTRAINTS:
146 pagmo_throw(value_error,
"The constraints co-evolutionary method is not valid.");
159 problem::cstrs_co_evolution_penalty prob_2(prob,pop_2_dim,pop_2_size);
160 prob_2.set_bounds(m_pen_lower_bound,m_pen_upper_bound);
163 std::vector<decision_vector> pop_2_x(pop_2_size);
164 std::vector<fitness_vector> pop_2_f(pop_2_size);
170 pop_2_x[j][i] = boost::uniform_real<double>(m_pen_lower_bound,m_pen_upper_bound)(
m_drng);
175 std::vector<population> pop_1_vector;
181 for(
int k=0; k<m_gen; k++) {
186 problem::cstrs_co_evolution prob_1(prob, pop_1_vector.at(j), m_method);
190 prob_1.set_penalty_coeff(pop_2_x.at(j));
201 pop_1.push_back(pop_1_vector.at(j).get_individual(i).cur_x);
205 m_original_algo->evolve(pop_1);
208 pop_1_vector.at(j).clear();
215 prob_2.update_penalty_coeff(j,pop_2_x.at(j),pop_1_vector.at(j));
224 pop_2.push_back(pop_2_x[i]);
227 m_original_algo_penalties->evolve(pop_2);
240 if(pop_2_f[j][0] < pop_2_f[best_idx][0]) {
245 const population ¤t_population = pop_1_vector.at(best_idx);
250 for(decision_vector::size_type i=0; i<prob_dimension; i++) {
251 tmp[i] = current_population.
get_individual(current_population.get_worst_idx()).best_x[i] -
252 current_population.
get_individual(current_population.get_best_idx()).best_x[i];
253 dx += std::fabs(tmp[i]);
258 std::cout <<
"Exit condition -- xtol < " << m_xtol << std::endl;
263 double mah = std::fabs(current_population.
get_individual(current_population.get_worst_idx()).best_f[0] -
264 current_population.
get_individual(current_population.get_best_idx()).best_f[0]);
268 std::cout <<
"Exit condition -- ftol < " << m_ftol << std::endl;
275 std::cout <<
"Generation " << k <<
" ***" << std::endl;
276 std::cout <<
" Best global fitness: " << current_population.champion().
f << std::endl;
277 std::cout <<
" xtol: " << dx <<
", ftol: " << mah << std::endl;
278 std::cout <<
" xtol: " << dx <<
", ftol: " << mah << std::endl;
286 if(pop_2_f[j][0] < pop_2_f[best_idx][0]) {
297 pop.push_back(pop_1_vector.at(best_idx).get_individual(i).cur_x);
304 return m_original_algo->get_name() +
"[Co-Evolution]";
313 return m_original_algo->clone();
324 m_original_algo = algo.
clone();
333 std::ostringstream s;
334 s <<
"algorithms: " << m_original_algo->get_name() <<
" - " << m_original_algo_penalties->get_name() <<
" ";
335 s <<
"\n\tConstraints handled with co-evolution algorithm";
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base algorithm.
std::vector< double > decision_vector
Decision vector type.
fitness_vector cur_f
Current fitness vector.
method_type
Type of co-evolution.
const individual_type & get_individual(const size_type &) const
Get constant reference to individual at position n.
base_ptr get_algorithm() const
Get a copy of the internal local algorithm.
size_type get_dimension() const
Return global dimension.
void set_algorithm(const base &)
Set algorithm.
virtual base_ptr clone() const =0
Clone method.
fitness_vector f
Fitness vector.
Co-Evolution constraints handling meta-algorithm.
bool m_screen_output
Indicates to the derived class whether to print stuff on screen.
c_size_type get_c_dimension() const
Return global constraints dimension.
void evolve(population &) const
Evolve implementation.
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.
rng_double m_drng
Random number generator for double-precision floating point values.
std::string human_readable_extra() const
Extra human readable algorithm info.
base_ptr clone() const
Clone method.
decision_vector::size_type size_type
Problem's size type: the same as pagmo::decision_vector's size type.
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.