faif
VectorIndividual.hpp
1 #ifndef FAIF_SEARCH_VECTOR_INDIVIDUAL_H
2 #define FAIF_SEARCH_VECTOR_INDIVIDUAL_H
3 
4 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
5 //msvc9.0 generuje smieci dla boost/random
6 #pragma warning(disable:4127)
7 #endif
8 
9 #include <vector>
10 #include <algorithm>
11 #include <boost/concept_check.hpp>
12 #include <boost/bind.hpp>
13 
14 #include "../utils/Random.hpp"
15 
16 namespace faif {
17  namespace search {
18 
19  /** \brief the concept for evolutionary algorithm gene
20  the type gives the generateRandom method and the mutate method
21  */
22  template<typename T>
24  typedef typename T::value_type value_type;
25  BOOST_CONCEPT_USAGE(EvolutionaryAlgorithmGeneConcept)
26  {
27  //the method to generate random gene is required
28  T::generateRandom();
29  //the metod to mutate gene is requred
30  T::mutate(0.1, t );
31  }
32  value_type t;
33  };
34 
35  /** gene as boolean variable */
36  struct BooleanGene {
37  typedef bool value_type;
38  static value_type generateRandom() {
39  RandomDouble random;
40  return random() < 0.5;
41  }
42  static value_type mutate(double prob_mutation, const value_type& val) {
43  RandomDouble random;
44  if( random() < prob_mutation ) {
45  return !val;
46  }
47  else {
48  return val;
49  }
50  }
51  };
52 
53  /** \brief Template to generate individual which is the vector of Genes */
54  template<typename Gene> class VectorIndividual {
55  public:
56  //the methods for gene are required
57  BOOST_CONCEPT_ASSERT((EvolutionaryAlgorithmGeneConcept<Gene>));
58 
59  typedef typename Gene::value_type value_type;
60  typedef std::vector<value_type> Container;
61  typedef typename Container::iterator iterator;
62  typedef typename Container::const_iterator const_iterator;
63 
64  /** create the random initial individual. The vector size is parameter */
65  explicit VectorIndividual(int size) {
66  std::generate_n( std::back_inserter( chromosome_ ), size, &Gene::generateRandom );
67  }
68 
69  /** init individual with given data */
70  explicit VectorIndividual(Container value)
71  : chromosome_(value) {}
72 
73  VectorIndividual(const VectorIndividual& i) : chromosome_(i.chromosome_) {}
74  ~VectorIndividual() {}
75  VectorIndividual& operator=(const VectorIndividual& i) {
76  chromosome_ = i.chromosome_;
77  return *this;
78  }
79 
80  /** \brief change the object at random positions */
81  void mutate(double prob_mutation) {
82  std::transform( chromosome_.begin(), chromosome_.end(), chromosome_.begin(),
83  boost::bind( &Gene::mutate, prob_mutation, _1) );
84  }
85 
86  bool operator==(const VectorIndividual& i) const {
87  return chromosome_ == i.chromosome_;
88  }
89  bool operator!=(const VectorIndividual& i) const {
90  return chromosome_ != i.chromosome_;
91  }
92  /** accessor */
93  const Container& getChromosome() const { return chromosome_; }
94  private:
95  Container chromosome_;
96  };
97 
98  } //namespace search
99 } //namespace faif
100 
101 #endif //FAIF_SEARCH_VECTOR_INDIVIDUAL_H
Definition: Chain.h:17
Template to generate individual which is the vector of Genes.
Definition: VectorIndividual.hpp:54
void mutate(double prob_mutation)
change the object at random positions
Definition: VectorIndividual.hpp:81
the uniform distribution for double, in given range, e.g. <0,1), uses RandomSingleton ...
Definition: Random.hpp:74
VectorIndividual(int size)
Definition: VectorIndividual.hpp:65
the concept for evolutionary algorithm gene the type gives the generateRandom method and the mutate m...
Definition: VectorIndividual.hpp:23
Definition: VectorIndividual.hpp:36
VectorIndividual(Container value)
Definition: VectorIndividual.hpp:70
const Container & getChromosome() const
Definition: VectorIndividual.hpp:93