GPLIB++
StandardPropagation.cpp
Go to the documentation of this file.
1 #include "StandardPropagation.h"
2 #include <algorithm>
3 #include <boost/numeric/ublas/matrix_proxy.hpp>
4 #include <boost/numeric/ublas/io.hpp>
5 #include <iostream>
6 
7 using namespace std;
8 namespace ublas = boost::numeric::ublas;
9 namespace gplib
10  {
11  StandardPropagation::StandardPropagation(GeneralSelect* const LocalSelect,
12  GeneralPopulation* const LocalPopulation, GeneralRNG* const LocalRandom) :
13  GeneralPropagation(LocalSelect, LocalPopulation, LocalRandom)
14  {
15  }
16 
18  {
19  }
20 
22  {
23  const int popsize = Population->GetPopulation().size1();
24  const int genesize = Population->GetPopulation().size2();
25  tpopulation NewPopulation(popsize, genesize);
26 
27  //first we initialize the selection object so that
28  //it has the right probabilities etc.
29  Select->Init();
30  //we go through the whole population
31  //because we go in pairs we have to treat the last elements specially
32  for (int i = 0; i < popsize - 1; i += 2)
33  {
34  //select a "father" and a "mother" that will produce twins
35  const int fatherindex = Select->GetOne();
36  const int motherindex = Select->GetOne();
37  //first take a local copy to work on
38  tpopmember Son(ublas::row(Population->GetPopulation(), fatherindex));
39  tpopmember Daughter(ublas::row(Population->GetPopulation(), motherindex));
40  //then we do mutation and crossover
41  Crossover(Son, Daughter);
42  Mutation(Son);
43  Mutation(Daughter);
44  //then we assign to the new population
45  ublas::row(NewPopulation, i) = Son;
46  ublas::row(NewPopulation, i + 1) = Daughter;
47  }
48  //if we have an odd population sixze and therefore an extra member
49  if ((popsize % 2) != 0)
50  {
51  //we just have a father that replicates and maybe mutates
52  const int fatherindex = Select->GetOne();
53  tpopmember Son(ublas::row(Population->GetPopulation(), fatherindex));
54  Mutation(Son);
55  ublas::row(NewPopulation, popsize - 1) = Son;
56  }
57  //The new population becomes the "main" population
58  Population->SetPopulation(NewPopulation);
59  }
60  }
The base class for the population of a genetic algorithm, implements storage and access functions...
GeneralSelect is the abstract base class for any selection mechanism in genetic algorithms.
Definition: GeneralSelect.h:16
The base class for genetic algorithm propagation methods.
virtual void Mutation(tpopmember &child)
ublas::vector< bool > tpopmember
Definition: gentypes.h:22
GeneralPopulation *const Population
virtual void Crossover(tpopmember &father, tpopmember &mother)
GeneralSelect *const Select
void SetPopulation(const tpopulation &LocalPop)
The base class for all random number generators, defines the basic interface.
Definition: GeneralRNG.h:9
gplib::rmat tpopulation
Definition: gentypes.h:25
const tpopulation & GetPopulation() const