PaGMO  1.1.5
sea.cpp
1 /*****************************************************************************
2  * Copyright (C) 2004-2015 The PaGMO development team, *
3  * Advanced Concepts Team (ACT), European Space Agency (ESA) *
4  * *
5  * https://github.com/esa/pagmo *
6  * *
7  * act@esa.int *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the *
21  * Free Software Foundation, Inc., *
22  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23  *****************************************************************************/
24 
25 #include <boost/random/uniform_int.hpp>
26 #include <string>
27 #include <vector>
28 #include <algorithm>
29 
30 #include "../exceptions.h"
31 #include "../population.h"
32 #include "../types.h"
33 #include "base.h"
34 #include "sea.h"
35 
36 namespace pagmo { namespace algorithm {
37 
39 
46 sea::sea(int gen)
47  :base(),m_gen(gen)
48 {
49  if (gen < 0) {
50  pagmo_throw(value_error,"number of generations must be nonnegative");
51  }
52 }
53 
56 {
57  return base_ptr(new sea(*this));
58 }
59 
61 
67 void sea::evolve(population &pop) const
68 {
69  // Let's store some useful variables.
70  const problem::base &prob = pop.problem();
71  const problem::base::size_type Di = prob.get_i_dimension();
72  const decision_vector &lb = prob.get_lb(), &ub = prob.get_ub();
73 
74  //We perform some checks to determine wether the problem/population are suitable for SGA
75  if ( prob.get_c_dimension() != 0 ) {
76  pagmo_throw(value_error,"The problem is not box constrained and EA is not suitable to solve it");
77  }
78 
79  if ( prob.get_dimension() - prob.get_i_dimension() != 0 ) {
80  pagmo_throw(value_error,"The problem has a continuous dimension and this (N+1)-EA Simple Evolutionary Algorithm is not suitable to solve it");
81  }
82 
83  // Get out if there is nothing to do.
84  if (m_gen == 0) {
85  return;
86  }
87 
88  int new_gene;
89  // Main loop
90  for (int j = 0; j<m_gen; j++) {
91 
92  // Offspring is generated from the best individual
93  decision_vector offspring = pop.get_individual(pop.get_best_idx()).cur_x;
94 
95  // Mutation of the best individual. Each gene is flipped with probability 1/Di to a different value
96  for (pagmo::problem::base::size_type j = 0; j < Di;++j) {//for each integer variable
97  if (m_drng() < 1.0/Di) {
98  do {
99  new_gene = boost::uniform_int<int>(lb[j],ub[j])(m_urng);
100  } while(new_gene == offspring[j]);
101  offspring[j] = new_gene;
102  }
103  }
104 
105  // We add the mutated individual to the population (this will also evaluate its fitness)
106  pop.push_back(offspring);
107  // We get rid of the worst individual (in multi-objective this is computed using
108  // the crowding distance operator)
109  pop.erase(pop.get_worst_idx());
110 
111  } // end of main loop
112 }
113 
115 std::string sea::get_name() const
116 {
117  return "(N+1)-EA Simple Evolutionary Algorithm";
118 }
119 
121 
124 std::string sea::human_readable_extra() const
125 {
126  std::ostringstream s;
127  s << "gen:" << m_gen << ' ';
128 
129  return s.str();
130 }
131 
132 }} //namespaces
133 
134 BOOST_CLASS_EXPORT_IMPLEMENT(pagmo::algorithm::sea)
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base algorithm.
Root PaGMO namespace.
base_ptr clone() const
Clone method.
Definition: sea.cpp:55
std::vector< double > decision_vector
Decision vector type.
Definition: types.h:40
const individual_type & get_individual(const size_type &) const
Get constant reference to individual at position n.
Definition: population.cpp:277
(N+1)-EA Simple Evolutionary Algorithm
Definition: sea.h:53
Base algorithm class.
Base problem class.
Definition: problem/base.h:148
sea(int gen=1)
Constructor.
Definition: sea.cpp:46
Population class.
Definition: population.h:70
size_type get_dimension() const
Return global dimension.
std::string get_name() const
Algorithm name.
Definition: sea.cpp:115
void evolve(population &) const
Evolve implementation.
Definition: sea.cpp:67
size_type get_i_dimension() const
Return integer dimension.
c_size_type get_c_dimension() const
Return global constraints dimension.
const decision_vector & get_ub() const
Upper bounds getter.
rng_uint32 m_urng
Random number generator for unsigned integer values.
const decision_vector & get_lb() const
Lower bounds getter.
rng_double m_drng
Random number generator for double-precision floating point values.
decision_vector::size_type size_type
Problem's size type: the same as pagmo::decision_vector's size type.
Definition: problem/base.h:160
std::string human_readable_extra() const
Extra human readable algorithm info.
Definition: sea.cpp:124