GPLIB++
BinaryTournamentSelect.cpp
Go to the documentation of this file.
2 #include "NumUtil.h"
3 #include <iostream>
4 #include <algorithm> // for swap
5 #include "FatalException.h"
6 
7 using namespace std;
8 namespace gplib
9  {
10  BinaryTournamentSelect::BinaryTournamentSelect(GeneralRNG &LocalRandom,
12  distancecount(0), Random(LocalRandom), ProbabilityFunction(myPF),
13  DistanceFunction(myDF), ReturnIndex(0)
14  {
15 
16  }
17 
19  {
20  cout << "Distance Count: " << distancecount << endl;
21  }
22 
23  void BinaryTournamentSelect::DoInit()
24  {
25  if (ProbabilityFunction().size() != DistanceFunction().size())
26  throw FatalException(
27  "Probabilities and Crowding distances do not have the same size !");
28  //Get Probabiliteis and crowding distances
29  localprobabilities = ProbabilityFunction();
30  localdistances = DistanceFunction();
31  const size_t popsize = localdistances.size();
32  PopulationIndex.assign(popsize * 2, 0); // we need every member index twice
33  for (size_t i = 0; i < popsize; ++i)
34  {
35  PopulationIndex.at(i) = i;
36  PopulationIndex.at(i + popsize) = i;
37  }
38  for (size_t i = 0; i < popsize; ++i)//shuffle the indices
39  {
40  int CurrentIndex = Random.GetNumber(popsize - i); //select an index for the first half
41  swap(PopulationIndex.at(popsize - i - 1), PopulationIndex.at(
42  CurrentIndex)); //swap indices
43  CurrentIndex = Random.GetNumber(popsize - i); //generate a new index
44  swap(PopulationIndex.at(2 * popsize - i - 1), PopulationIndex.at(
45  popsize + CurrentIndex)); //swap indices in the secondhalf
46  }
47  ReturnIndex = 0;
48  }
49 
50  size_t BinaryTournamentSelect::DoGetOne()
51  {
52  const double tolerance = 1e-5;
53  //get two indices from the randomized PopulationIndex
54  const int fatherindex = PopulationIndex.at(ReturnIndex);
55  const int motherindex = PopulationIndex.at(ReturnIndex + 1);
56  //increase index for next call
57  ReturnIndex += 2;
58  //compare within numerical precision
59  const int comparison = fcmp(localprobabilities(fatherindex),
60  localprobabilities(motherindex), tolerance);
61  switch (comparison)
62  {
63  case 1:
64  return fatherindex; //father has higher fitness
65  case -1:
66  return motherindex; //mother has higher fitness
67  case 0:
68  distancecount++; //have same fitness, decision based on crowding distance
69  if (localdistances(fatherindex) >= localdistances(motherindex))
70  return fatherindex;
71  else
72  return motherindex;
73  default:
74  throw FatalException(
75  "Something went very wrong in CBinaryTournamentSelect::GetOne");
76  }
77  }
78  }
boost::function< const tprobabilityv &()> tProbabilityFunction
Definition: gentypes.h:29
virtual float GetNumber()=0
boost::function< const tcrowddistv &()> tDistanceFunction
Definition: gentypes.h:30
The base class for all random number generators, defines the basic interface.
Definition: GeneralRNG.h:9
const int size
Definition: perftest.cpp:14
The basic exception class for all errors that arise in gplib.