PaGMO  1.1.5
sga_gray.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_SGA_GRAY_H
26 #define PAGMO_ALGORITHM_SGA_GRAY_H
27 
28 #include "../config.h"
29 #include "../problem/base.h"
30 #include "../serialization.h"
31 #include "base.h"
32 
33 namespace pagmo { namespace algorithm {
34 
36 
50 class __PAGMO_VISIBLE sga_gray: public base
51 {
52 public:
54  struct selection {
56  enum type {BEST20 = 0,ROULETTE = 1};
57  };
59  struct mutation {
61  enum type {UNIFORM = 0};
62  };
63 
65  struct crossover {
67  enum type {SINGLE_POINT = 0};
68  };
69  sga_gray(int gen = 1, const double &cr = .95, const double &m = .02, int elitism = 1,
70  mutation::type mut = mutation::UNIFORM,
71  selection::type sel = selection::ROULETTE,
72  crossover::type cro = crossover::SINGLE_POINT);
73  base_ptr clone() const;
74  void evolve(population &) const;
75  std::string get_name() const;
76 protected:
77  std::string human_readable_extra() const;
78 private:
79  friend class boost::serialization::access;
80  template <class Archive>
81  void serialize(Archive &ar, const unsigned int)
82  {
83  ar & boost::serialization::base_object<base>(*this);
84  ar & const_cast<int &>(m_gen);
85  ar & const_cast<double &>(m_cr);
86  ar & const_cast<double &>(m_m);
87  ar & const_cast<int &>(m_elitism);
88  ar & const_cast<mutation::type &>(m_mut);
89  ar & const_cast<selection::type &>(m_sel);
90  ar & const_cast<crossover::type &>(m_cro);
91  ar & m_max_encoding_integer;
92  ar & m_bit_encoding;
93  }
94  //Number of generations
95  const int m_gen;
96  //Crossover rate
97  const double m_cr;
98  //Mutation rate
99  const double m_m;
100 
101  //Elitism (number of generations after which to reinsert the best)
102  const int m_elitism;
103  //Mutation
104  const mutation::type m_mut;
105  //Selection_type
106  const selection::type m_sel;
107  //Crossover_type
108  const crossover::type m_cro;
109 
110 private:
111  // genetic algoritms operators
112  std::vector<int> selection(const std::vector<fitness_vector> &, const problem::base &) const;
113  void crossover(std::vector< std::vector<int> > &pop_x) const;
114  void mutate(std::vector< std::vector<int> > &pop_x) const;
115 
116 private:
117  // binary conversion
118  std::vector<int> double_to_binary(const double &number, const double &lb, const double &ub) const;
119  double binary_to_double(const std::vector<int> &binary, const double &lb, const double &ub) const;
120  std::vector<int> gray_to_binary(const std::vector<int> &gray) const;
121  std::vector<int> binary_to_gray(const std::vector<int> &binary) const;
122 
123  // encoding/decoding
124  std::vector<int> encode(const decision_vector &x_vector, const decision_vector &lb, const decision_vector &ub) const;
125  decision_vector decode(const std::vector<int> &chrom, const decision_vector &lb, const decision_vector &ub) const;
126 
127  // encoding size
128  int m_max_encoding_integer;
129  int m_bit_encoding;
130 };
131 
132 }} //namespaces
133 
134 BOOST_CLASS_EXPORT_KEY(pagmo::algorithm::sga_gray)
135 
136 #endif // PAGMO_ALGORITHM_SGA_GRAY_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
type
Mutation type, uniform.
Definition: sga_gray.h:61
Base algorithm class.
Base problem class.
Definition: problem/base.h:148
Population class.
Definition: population.h:70
type
Selection type, best 20% or roulette.
Definition: sga_gray.h:56
Mutation operator info.
Definition: sga_gray.h:59
The Simple Genetic Algorithm with gray encoding (sga_gray)
Definition: sga_gray.h:50
type
Crossover type, single point.
Definition: sga_gray.h:67
Crossover operator info.
Definition: sga_gray.h:65