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/antibodies_problem.h"
36 #include "../problem/con2uncon.h"
39 #include "cstrs_immune_system.h"
41 namespace pagmo {
namespace algorithm {
69 double ftol,
double xtol):
70 base(),m_gen(gen),m_select_method(select_method),m_inject_method(inject_method),m_distance_method(distance_method),
71 m_phi(phi),m_gamma(gamma),m_sigma(sigma),m_ftol(ftol),m_xtol(xtol)
73 m_original_algo = original_algo.
clone();
74 m_original_algo_immune = original_algo_immune.
clone();
77 pagmo_throw(value_error,
"number of generations must be nonnegative");
83 base(algo),m_original_algo(algo.m_original_algo->clone()),
84 m_original_algo_immune(algo.m_original_algo_immune->clone()),m_gen(algo.m_gen),
85 m_select_method(algo.m_select_method),m_inject_method(algo.m_inject_method),m_distance_method(algo.m_distance_method),
86 m_phi(algo.m_phi),m_gamma(algo.m_gamma),m_sigma(algo.m_sigma),m_ftol(algo.m_ftol), m_xtol(algo.m_xtol)
112 if(prob_c_dimension < 1) {
113 pagmo_throw(value_error,
"The problem is not constrained and co-evolution is not suitable to solve it");
116 pagmo_throw(value_error,
"The problem is multiobjective and co-evolution is not suitable to solve it");
129 std::vector<decision_vector> pop_mixed_c(pop_size);
134 std::vector<decision_vector> pop_antigens;
137 std::vector<population::size_type> pop_antigens_pool;
138 std::vector<population::size_type> pop_antibodies_pool;
147 for(
int k=0; k<m_gen; k++) {
149 pop_antigens.clear();
152 pop_antigens_pool.clear();
153 pop_antibodies_pool.clear();
162 bool has_feasible =
false;
179 pop_antigens_pool.push_back(i);
181 pop_antibodies_pool.push_back(i);
197 std::swap(pop_antigens_pool[i],pop_antigens_pool[j]);
206 double mean_fitness = 0.;
211 mean_fitness /= pop_antigens_subset_size;
220 int mean_position_idx=0;
224 mean_position_idx = i;
230 int pop_antigens_size = std::max((
int)(m_gamma * pop_antigens_pool_size), 1);
236 if(mean_position_idx - pop_antigens_size/2 < 0) {
237 begin_antigen_idx = 0;
238 end_antigen_idx = std::min((
int)(end_antigen_idx + (pop_antigens_size/2 - mean_position_idx)),(
int)pop_antigens_pool_size);
240 if(mean_position_idx + pop_antigens_size/2 >= pop_antigens_size) {
241 begin_antigen_idx = std::max((
int)(begin_antigen_idx - (mean_position_idx + pop_antigens_size/2 - pop_antigens_size)),0);
242 end_antigen_idx = pop_antigens_size;
251 if(pop_antigens.size() == 0) {
252 pop_antigens.push_back(pop_mixed.
get_individual(pop_antigens_pool.at(mean_position_idx)).cur_x);
259 switch(m_select_method) {
260 case(BEST_ANTIBODY): {
269 pop_antigens_pool.push_back(best_idx);
275 pop_antibodies_pool.push_back(i);
280 case(INFEASIBILITY): {
284 pop_antigens_pool.push_back(i);
297 std::swap(pop_antigens_pool[i],pop_antigens_pool[j]);
313 if(std::find(pop_antigens_pool.begin(), pop_antigens_pool.begin()+pop_antigens_size, i) == pop_antigens_pool.begin()+pop_antigens_size) {
314 pop_antibodies_pool.push_back(i);
320 pagmo_throw(value_error,
"The antibody selection method must be either BEST_ANTIBODY or INFEASIBILITY.");
328 if(initial_pop_antibodies_pool_size != 0) {
333 if(initial_pop_antibodies_pool_size > 1) {
336 int j = boost::uniform_int<int>(0, initial_pop_antibodies_pool_size - 1)(
m_urng);
337 std::swap(pop_antibodies_pool[i], pop_antibodies_pool[j]);
348 population::size_type pop_antibodies_size = std::max( (
int)(m_sigma * pop_antigens_size), (
int)min_individual_for_algo);
349 pop_antibodies_size = std::min(pop_antibodies_size, initial_pop_antibodies_pool_size);
360 pop_antibodies.clear();
363 pop_antibodies.push_back(pop_mixed.
get_individual(pop_antibodies_pool.at(i)).cur_x);
367 if(min_individual_for_algo>pop_antibodies_size){
372 if(initial_pop_antibodies_pool_size > 1) {
373 int j = boost::uniform_int<int>(0, initial_pop_antibodies_pool_size - 1)(
m_urng);
374 pop_antibodies.push_back(pop_mixed.
get_individual(pop_antibodies_pool.at(j)).cur_x);
376 pop_antibodies.push_back(pop_mixed.
get_individual(pop_antibodies_pool.at(0)).cur_x);
381 pop_antigens_size = pop_antigens.size();
382 pop_antibodies_size = pop_antibodies.size();
385 m_original_algo_immune->evolve(pop_antibodies);
389 switch(m_inject_method) {
394 pop_mixed.set_x(current_idx, pop_antibodies.champion().
x);
399 std::vector<population::size_type> pop_best_25(pop_antibodies_size);
413 std::swap(pop_best_25[i],pop_best_25[j]);
418 int best25_size = pop_antibodies_size/4;
423 pop_mixed.set_x(current_idx, pop_antibodies.
get_individual(pop_best_25.at(i % best25_size)).cur_x);
428 pagmo_throw(value_error,
"The antibody injection method must be either CHAMPION or BEST25.");
437 m_original_algo->evolve(pop_mixed);
444 for(decision_vector::size_type i=0; i<prob_dimension; i++) {
446 dx += std::fabs(tmp[i]);
451 std::cout <<
"Exit condition -- xtol < " << m_xtol << std::endl;
456 double mah = std::fabs(pop_mixed.
get_individual(pop_mixed.get_worst_idx()).best_f[0] - pop_mixed.
get_individual(pop_mixed.get_best_idx()).best_f[0]);
460 std::cout <<
"Exit condition -- ftol < " << m_ftol << std::endl;
467 std::cout <<
"Generation " << k <<
" ***" << std::endl;
468 std::cout <<
" Best global fitness: " << pop.champion().
f << std::endl;
469 std::cout <<
" xtol: " << dx <<
", ftol: " << mah << std::endl;
470 std::cout <<
" xtol: " << dx <<
", ftol: " << mah << std::endl;
485 return m_original_algo->get_name() +
"[Immune]";
494 return m_original_algo->clone();
506 m_original_algo_immune = algo.
clone();
515 return m_original_algo_immune->clone();
526 m_original_algo = algo.
clone();
535 std::ostringstream s;
536 s <<
"algorithms: " << m_original_algo->get_name() <<
" - " << m_original_algo_immune->get_name() <<
" ";
537 s <<
"\n\tConstraints handled with immune system algorithm";
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base algorithm.
std::vector< double > decision_vector
Decision vector type.
void set_algorithm_immune(const base &)
Set algorithm.
Immune system constraints handling meta-algorithm.
fitness_vector cur_f
Current fitness vector.
const individual_type & get_individual(const size_type &) const
Get constant reference to individual at position n.
bool feasibility_c(const constraint_vector &) const
Test feasibility of constraint vector.
Individuals stored in the population.
bool compare_constraints(const constraint_vector &, const constraint_vector &) const
Compare constraint vectors.
std::string get_name() const
Algorithm name.
size_type get_dimension() const
Return global dimension.
Constrained to unconstrained meta-problem.
decision_vector x
Decision vector.
distance_method_type
Type of antibodies problem distance method.
virtual base_ptr clone() const =0
Clone method.
select_method_type
Type of immune system antibody selection method.
bool compare_fitness(const fitness_vector &, const fitness_vector &) const
Compare fitness vectors.
fitness_vector f
Fitness vector.
base_ptr get_algorithm_immune() const
Get a copy of the internal local algorithm.
std::string human_readable_extra() const
Extra human readable algorithm info.
bool m_screen_output
Indicates to the derived class whether to print stuff on screen.
void set_antigens(const std::vector< decision_vector > &)
Updates the antigens population used to compute the fitness.
base_ptr clone() const
Clone method.
constraint_vector compute_constraints(const decision_vector &) const
Compute constraints and return constraint vector.
base_ptr get_algorithm() const
Get a copy of the internal local algorithm.
c_size_type get_c_dimension() const
Return global constraints dimension.
container_type::size_type size_type
Population size type.
inject_method_type
Type of immune system antibody injection method.
decision_vector cur_x
Current decision vector.
rng_uint32 m_urng
Random number generator for unsigned integer values.
void evolve(population &) const
Evolve implementation.
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.
decision_vector::size_type size_type
Problem's size type: the same as pagmo::decision_vector's size type.
void set_algorithm(const base &)
Set algorithm.
cstrs_immune_system(const base &=jde(1), const base &=sga(), int gen=1, select_method_type=BEST_ANTIBODY, inject_method_type=CHAMPION, distance_method_type=EUCLIDEAN, double=0.5, double=0.5, double=1./3., double=1e-15, double=1e-15)
Constructor.