Individual.h
Go to the documentation of this file.
1 /*!
2  *
3  *
4  * \brief TypedIndividual
5  *
6  * \author T.Voss, T. Glasmachers, O.Krause
7  * \date 2010-2014
8  *
9  *
10  * \par Copyright 1995-2015 Shark Development Team
11  *
12  * <BR><HR>
13  * This file is part of Shark.
14  * <http://image.diku.dk/shark/>
15  *
16  * Shark is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU Lesser General Public License as published
18  * by the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * Shark is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU Lesser General Public License for more details.
25  *
26  * You should have received a copy of the GNU Lesser General Public License
27  * along with Shark. If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30 #ifndef SHARK_ALGORITHMS_DIRECT_SEARCH_TYPED_INDIVIDUAL_H
31 #define SHARK_ALGORITHMS_DIRECT_SEARCH_TYPED_INDIVIDUAL_H
32 
33 #include <shark/LinAlg/Base.h>
34 
35 namespace shark {
36 
37 /**
38  * \brief Individual is a simple templated class modelling
39  * an individual that acts as a candidate solution in an evolutionary algorithm.
40  *
41  * The class holds the current search point as well as the penalized and unpenalized fitness,
42  * its domination rank with respect to the population, its age, a boolean variable determining
43  * whether the individual is selected for the next parent generation and some payload chromosome
44  * which is by default a RealVector.
45  *
46  * The states mean the following:
47  * - the search point is the point in search space the individual represents.
48  * - the penalized and unpenailzed fitness are related by:
49  * if the search point is in the search region of the optimization region, penalized and unpenalized
50  * fitness are the same. otherwise the unpenalized fitness is the value of the closest feasible point
51  * to the search point. The penalized fitness is the same value plus an penalty term. Usually this
52  * is ||s-closestFeasible(s)||^2, the squared distance between the search point and the closest
53  * feasible point.
54  * - the domination rank indicates in which front the individual is. a nondominated individual has rank
55  * 1, individuals that are only dominated by individuals with rank one have rank 2 and so on.
56  * In single objective optimization, the rank is simply the number of individuals with better fitness+1.
57  * - the age is the number of generations the individual has survived.
58  * - selection: survival selection schemes never delete or move points, instead they indicate which points
59  * are to be deleted.
60  */
61 template< typename PointType, class FitnessTypeT, class Chromosome = RealVector >
62 class Individual {
63 public:
64 
65  typedef FitnessTypeT FitnessType;
66 
67  typedef PointType SearchPointType;
68 
69  // Functors to use for the stl algorithms
70  ///\brief returns true if the individual is selected for the next parent set
71  static bool IsSelected(Individual const& individual){
72  return individual.selected();
73  }
74 
75  ///\brief Ordering relation by the ranks of the individuals
76  struct RankOrdering{
77  bool operator()(Individual const& individual1, Individual const& individual2){
78  return individual1.rank() < individual2.rank();
79  }
80  };
81 
82  ///\brief Ordering relation by the fitness of the individuals(only single objective)
84  bool operator()(Individual const& individual1, Individual const& individual2){
85  return individual1.unpenalizedFitness() < individual2.unpenalizedFitness() ;
86  }
87  };
88 
89  /**
90  * \brief Default constructor that initializes the individual's attributes to default values.
91  */
93  : m_age(0)
94  , m_rank(0)
95  , m_selected(false)
96  {}
97 
98  /**
99  * \brief Returns a reference to the search point that is associated with the individual.
100  */
101  SearchPointType& searchPoint() {
102  return m_searchPoint;
103  }
104 
105  /**
106  * \brief Returns a const reference to the search point that is associated with the individual.
107  */
108  const SearchPointType& searchPoint() const {
109  return m_searchPoint;
110  }
111 
112  /**
113  * \brief Returns a reference to the chromosome that is associated with the individual.
114  */
115  Chromosome& chromosome() {
116  return m_chromosome;
117  }
118 
119  /**
120  * \brief Returns a const reference to the chromosome that is associated with the individual.
121  */
122  Chromosome const& chromosome() const{
123  return m_chromosome;
124  }
125 
126  /**
127  * \brief Returns the age of the individual (in generations).
128  */
129  unsigned int age() const {
130  return m_age;
131  }
132 
133  /**
134  * \brief Returns a reference to the age of the individual (in generations).
135  */
136  unsigned int& age() {
137  return m_age;
138  }
139 
140  /**
141  * \brief Returns a reference to the unpenalized fitness of the individual.
142  */
143  FitnessType& unpenalizedFitness() {
144  return m_unpenalizedFitness;
145  }
146 
147  /**
148  * \brief Returns the unpenalized fitness of the individual.
149  */
150  FitnessType const& unpenalizedFitness() const {
151  return m_unpenalizedFitness;
152  }
153 
154  /**
155  * \brief Returns a reference to the penalized fitness of the individual.
156  */
157  FitnessType& penalizedFitness() {
158  return m_penalizedFitness;
159  }
160  /**
161  * \brief Returns the unpenalized fitness of the individual.
162  */
163  FitnessType const& penalizedFitness() const {
164  return m_penalizedFitness;
165  }
166 
167  /**
168  * \brief Returns the level of non-dominance of the individual.
169  */
170  unsigned int rank() const {
171  return m_rank;
172  }
173 
174  /**
175  * \brief Returns a reference to the level of non-dominance of the individual. Allows for lvalue()-semantic.
176  */
177  unsigned int& rank() {
178  return m_rank;
179  }
180 
181  /**
182  * \brief Returns true if the individual is selected for the next parent generation
183  */
184  bool selected() const {
185  return m_selected;
186  }
187 
188  /**
189  * \brief Returns true if the individual is selected for the next parent generation
190  */
191  bool& selected() {
192  return m_selected;
193  }
194 
195  /**
196  * \brief Stores the individual and all of its chromosomes in an archive.
197  */
198  template<typename Archive>
199  void serialize(Archive & archive, const unsigned int version) {
200  archive & BOOST_SERIALIZATION_NVP(m_searchPoint);
201  archive & BOOST_SERIALIZATION_NVP(m_chromosome);
202  archive & BOOST_SERIALIZATION_NVP(m_age);
203  archive & BOOST_SERIALIZATION_NVP(m_penalizedFitness);
204  archive & BOOST_SERIALIZATION_NVP(m_unpenalizedFitness);
205  archive & BOOST_SERIALIZATION_NVP(m_rank);
206  archive & BOOST_SERIALIZATION_NVP(m_selected);
207 
208  }
209 
210  friend void swap(Individual& i1, Individual& i2){
211  using std::swap;
214  swap(i1.m_age,i2.m_age);
215  swap(i1.m_rank,i2.m_rank);
216  swap(i1.m_selected,i2.m_selected);
219  }
220 
221 protected:
222  SearchPointType m_searchPoint; ///< The search point associated with the individual.
223  Chromosome m_chromosome; ///< The search point associated with the individual.
224 
225  unsigned int m_age; ///< The age of the individual (in generations).
226  unsigned int m_rank; ///< The level of non-dominance of the individual. The lower the better.
227  bool m_selected; ///< Is the individual selected for the next parent set?
228 
229  FitnessType m_penalizedFitness; ///< Penalized fitness of the individual.
230  FitnessType m_unpenalizedFitness; ///< Unpenalized fitness of the individual.
231 };
232 
233 }
234 #endif