PaGMO  1.1.5
spea2.h
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 #ifndef PAGMO_ALGORITHM_SPEA2_H
26 #define PAGMO_ALGORITHM_SPEA2_H
27 
28 #include "../config.h"
29 #include "../serialization.h"
30 #include "../util/neighbourhood.h"
31 #include "base.h"
32 
33 namespace pagmo { namespace algorithm {
34 
35 class distance_sorter {
36 public:
37  distance_sorter(const std::vector<std::vector<pagmo::population::size_type> > &neighbours,
38  const std::vector<fitness_vector> &fit):
39  m_neighbours(neighbours),
40  m_fit(fit) {}
41  bool operator()(unsigned int a, unsigned int b) {
42  if(a>=m_fit.size() || a>=m_neighbours.size()){
43  pagmo_throw(value_error,"SPEA2 sorting of KNN values failure");
44  }
45  if(b>=m_fit.size() || b>=m_neighbours.size()){
46  pagmo_throw(value_error,"SPEA2 sorting of KNN values failure");
47  }
48  double delta_a, delta_b;
49  unsigned int i = 0;
50  do{
51  if(m_neighbours[b][i]>=m_fit.size() || m_neighbours[a][i]>=m_fit.size()){
52  pagmo_throw(value_error,"SPEA2 sorting of KNN values failure");
53  }
54  delta_a = pagmo::util::neighbourhood::euclidian::distance(m_fit[a], m_fit[m_neighbours[a][i]]);
55  delta_b = pagmo::util::neighbourhood::euclidian::distance(m_fit[b], m_fit[m_neighbours[b][i]]);
56  i++;
57  } while (i<m_neighbours[0].size() && delta_a == delta_b);
58  return delta_a > delta_b;
59  }
60 private:
61  const std::vector<std::vector<pagmo::population::size_type> > &m_neighbours;
62  const std::vector<fitness_vector> &m_fit;
63 };
64 
66 
80 class __PAGMO_VISIBLE spea2: public base
81 {
82 public:
83  spea2(int gen=100, double cr = 0.95, double eta_c = 10, double m = 0.01, double eta_m = 50, int archive_size = 0);
84  base_ptr clone() const;
85  void evolve(population &) const;
86  std::string get_name() const;
87 
88 protected:
89  std::string human_readable_extra() const;
90 
91 private:
92  struct spea2_individual {
96  };
97  void compute_spea2_fitness(std::vector<double> &,
98  int K,
99  const std::vector<spea2_individual> &pop,
100  const pagmo::problem::base &prob) const;
101  std::vector<std::vector<population::size_type> > compute_domination_list(const pagmo::problem::base &,
102  const std::vector<fitness_vector> &,
103  const std::vector<constraint_vector> &) const;
104  std::vector<population::size_type> compute_pareto_rank(const std::vector<std::vector<population::size_type> > &) const;
106  const std::vector<population::size_type> &) const;
107  std::vector<population::size_type> compute_domination_count(const std::vector<std::vector<population::size_type> > &) const;
109  const std::vector<spea2_individual> &, const pagmo::problem::base &) const;
110  void mutate(decision_vector&, const pagmo::problem::base&) const;
111  friend class boost::serialization::access;
112  template <class Archive>
113  void serialize(Archive &ar, const unsigned int)
114  {
115  ar & boost::serialization::base_object<base>(*this);
116  ar & const_cast<int &>(m_gen);
117  ar & const_cast<double &>(m_cr);
118  ar & const_cast<double &>(m_eta_c);
119  ar & const_cast<double &>(m_m);
120  ar & const_cast<double &>(m_eta_m);
121  ar & const_cast<int &>(m_archive_size);
122  }
123  //Number of generations
124  const int m_gen;
125  //Crossover rate
126  const double m_cr;
127  // Ditribution index for crossover
128  const double m_eta_c;
129  // Mutation rate
130  const double m_m;
131  // Ditribution index for mutation
132  const double m_eta_m;
133  // Size of the archive
134  int m_archive_size;
135 };
136 
137 }} //namespaces
138 
139 BOOST_CLASS_EXPORT_KEY(pagmo::algorithm::spea2)
140 
141 #endif // PAGMO_ALGORITHM_SPEA2_H
boost::shared_ptr< base > base_ptr
Alias for shared pointer to base algorithm.
Root PaGMO namespace.
std::vector< double > decision_vector
Decision vector type.
Definition: types.h:40
"Strength Pareto Evolutionary Algorithm (SPEA2)"
Definition: spea2.h:80
Base algorithm class.
Base problem class.
Definition: problem/base.h:148
Population class.
Definition: population.h:70
static double distance(const std::vector< double > &, const std::vector< double > &)
std::vector< double > fitness_vector
Fitness vector type.
Definition: types.h:42
std::vector< double > constraint_vector
Constraint vector type.
Definition: types.h:44
container_type::size_type size_type
Population size type.
Definition: population.h:192