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
00028
00029 Select->Init();
00030
00031
00032 for (int i = 0; i < popsize - 1; i += 2)
00033 {
00034
00035 const int fatherindex = Select->GetOne();
00036 const int motherindex = Select->GetOne();
00037
00038 tpopmember Son(ublas::row(Population->GetPopulation(), fatherindex));
00039 tpopmember Daughter(ublas::row(Population->GetPopulation(), motherindex));
00040
00041 Crossover(Son, Daughter);
00042 Mutation(Son);
00043 Mutation(Daughter);
00044
00045 ublas::row(NewPopulation, i) = Son;
00046 ublas::row(NewPopulation, i + 1) = Daughter;
00047 }
00048
00049 if ((popsize % 2) != 0)
00050 {
00051
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
00058 Population->SetPopulation(NewPopulation);
00059 }
00060 }