StandardPropagation.cpp

Go to the documentation of this file.
00001 #include "StandardPropagation.h"
00002 #include <algorithm>
00003 #include <boost/numeric/ublas/matrix_proxy.hpp>
00004 #include <boost/numeric/ublas/io.hpp>
00005 #include <iostream>
00006 
00007 using namespace std;
00008 namespace ublas = boost::numeric::ublas;
00009 namespace gplib
00010   {
00011     StandardPropagation::StandardPropagation(GeneralSelect* const LocalSelect,
00012         GeneralPopulation* const LocalPopulation, GeneralRNG* const LocalRandom) :
00013       GeneralPropagation(LocalSelect, LocalPopulation, LocalRandom)
00014       {
00015       }
00016 
00017     StandardPropagation::~StandardPropagation()
00018       {
00019       }
00020 
00021     void StandardPropagation::NextGeneration()
00022       {
00023         const int popsize = Population->GetPopulation().size1();
00024         const int genesize = Population->GetPopulation().size2();
00025         tpopulation NewPopulation(popsize, genesize);
00026 
00027         //first we initialize the selection object so that
00028         //it has the right probabilities etc.
00029         Select->Init();
00030         //we go through the whole population
00031         //because we go in pairs we have to treat the last elements specially
00032         for (int i = 0; i < popsize - 1; i += 2)
00033           {
00034             //select a "father" and a "mother" that will produce twins
00035             const int fatherindex = Select->GetOne();
00036             const int motherindex = Select->GetOne();
00037             //first take a local copy to work on
00038             tpopmember Son(ublas::row(Population->GetPopulation(), fatherindex));
00039             tpopmember Daughter(ublas::row(Population->GetPopulation(), motherindex));
00040             //then we do mutation and crossover
00041             Crossover(Son, Daughter);
00042             Mutation(Son);
00043             Mutation(Daughter);
00044             //then we assign to the new population
00045             ublas::row(NewPopulation, i) = Son;
00046             ublas::row(NewPopulation, i + 1) = Daughter;
00047           }
00048         //if we have an odd population sixze and therefore an extra member
00049         if ((popsize % 2) != 0)
00050           {
00051             //we just have a father that replicates and maybe mutates
00052             const int fatherindex = Select->GetOne();
00053             tpopmember Son(ublas::row(Population->GetPopulation(), fatherindex));
00054             Mutation(Son);
00055             ublas::row(NewPopulation, popsize - 1) = Son;
00056           }
00057         //The new population becomes the "main" population
00058         Population->SetPopulation(NewPopulation);
00059       }
00060   }

Generated on Tue May 4 16:52:15 2010 for GPLIB++ by  doxygen 1.5.8