GPLIB++
UniquePop.h
Go to the documentation of this file.
1 #ifndef CUNIQUEPOP_H
2 #define CUNIQUEPOP_H
3 #include <boost/unordered_map.hpp>
4 #include <fstream>
5 #include "gentypes.h"
6 namespace gplib
7  {
8  /** \addtogroup gainv Genetic algorithm optimization */
9  /* @{ */
10  //! This class stores a single unique copy of each population member that is added to it
11  /*! In order to avoid duplicate output of inversion results at the end of the genetic algorithm
12  * run and have a compact history of all models evaluated during the GA run, we have this class
13  * that makes sure that we only have a single copy of each population member. This class also
14  * stores the associated fitness values, so we can use it to look up the fitness of a member
15  * instead of calculating it if the member has already been evaluated before.
16  */
17  class UniquePop
18  {
19  private:
20  struct memb_equal
21  {
22  bool operator()(ttranscribed const& p1, ttranscribed const& p2) const
23  {
24  return std::equal(p1.begin(), p1.end(), p2.begin());
25  }
26  };
27 
28  struct memb_hash: std::unary_function<ttranscribed, std::size_t>
29  {
30  std::size_t operator()(ttranscribed const& p) const
31  {
32  std::size_t seed = 0;
33  const size_t length = p.size();
34  for (size_t i = 0; i < length; ++i)
35  {
36  boost::hash_combine(seed, p(i));
37  }
38  return seed;
39  }
40  };
41 
42  typedef boost::unordered_map<ttranscribed, tfitvec,memb_hash,memb_equal> tmembermap;
43  tmembermap MemberHashMap;
44  public:
45  bool Find(const ttranscribed &popmember, tfitvec &fitness);
46  bool Insert(const tfitvec &fitness, const ttranscribed &popmember);
47  void PrintAll(std::ostream &output);
48  UniquePop();
49  virtual ~UniquePop();
50  };
51  /* @} */
52  }
53 #endif // CUNIQUEPOP_H
ublas::vector< double > ttranscribed
Definition: gentypes.h:21
bool Insert(const tfitvec &fitness, const ttranscribed &popmember)
Definition: UniquePop.cpp:17
virtual ~UniquePop()
Definition: UniquePop.cpp:13
bool Find(const ttranscribed &popmember, tfitvec &fitness)
Definition: UniquePop.cpp:25
void PrintAll(std::ostream &output)
Definition: UniquePop.cpp:36
This class stores a single unique copy of each population member that is added to it...
Definition: UniquePop.h:17
ublas::vector< double > tfitvec
Definition: gentypes.h:23