add two FastGA tests

This commit is contained in:
Amine Aziz-Alaoui 2020-09-21 18:05:40 +02:00 committed by nojhan
commit 5d3253ef7a
3 changed files with 178 additions and 0 deletions

View file

@ -76,6 +76,7 @@ set (TEST_LIST
t-algo-forged
t-algo-forged-search
t-FastGA
t-forge-FastGA
t-eoFoundryFastGA
)

62
eo/test/t-FastGA.cpp Normal file
View file

@ -0,0 +1,62 @@
#include <iostream>
#include <string>
#include <eo>
#include <ga.h>
#include "../../problems/eval/oneMaxEval.h"
using EOT = eoBit<double>;
int main(int /*argc*/, char** /*argv*/)
{
size_t dim = 5;
size_t pop_size = 3;
oneMaxEval<EOT> evalfunc;
eoPopLoopEval<EOT> eval(evalfunc);
eoBooleanGenerator gen(0.5);
eoInitFixedLength<EOT> init(dim, gen);
double cross_rate = 0.5;
eoProportionalOp<EOT> cross;
// Cross-over that produce only one offspring,
// made by wrapping the quad op (which produce 2 offsprings)
// in a bin op (which ignore the second offspring).
eo1PtBitXover<EOT> crossover;
eoQuad2BinOp<EOT> mono_cross(crossover);
cross.add(mono_cross, cross_rate);
eoBinCloneOp<EOT> bin_clone;
cross.add(bin_clone, 1 - cross_rate); // Clone
double mut_rate = 0.5;
eoProportionalOp<EOT> mut;
eoShiftedBitMutation<EOT> mutation(0.5);
mut.add(mutation, mut_rate);
eoMonCloneOp<EOT> mon_clone;
mut.add(mon_clone, 1 - mut_rate); // FIXME TBC
eoSequentialOp<EOT> variator;
variator.add(cross,1.0);
variator.add(mut,1.0);
double lambda = 1.0; // i.e. 100%
eoStochTournamentSelect<EOT> selector(0.5);
eoGeneralBreeder<EOT> breeder(selector, variator, lambda);
eoGenContinue<EOT> common_cont(3);
eoCombinedContinue<EOT> gen_cont(common_cont);
//gen_cont.add(continuator);
eoPlusReplacement<EOT> replacement;
eoEasyEA<EOT> algo = eoEasyEA<EOT>(gen_cont, eval, breeder, replacement);
eoPop<EOT> pop;
pop.append(pop_size, init);
eval(pop,pop);
algo(pop);
std::cout << pop.best_element() << std::endl;
}

115
eo/test/t-forge-FastGA.cpp Normal file
View file

@ -0,0 +1,115 @@
#include <iostream>
#include <string>
#include <eo>
#include <ga.h>
#include "../../problems/eval/oneMaxEval.h"
int main(int /*argc*/, char** /*argv*/)
{
size_t dim = 500;
size_t pop_size = 10;
eo::log << eo::setlevel(eo::warnings);
using EOT = eoBit<double>;
oneMaxEval<EOT> eval;
eoBooleanGenerator gen(0.5);
eoInitFixedLength<EOT> init(dim, gen);
eoGenContinue<EOT> common_cont(100);
eoForgeVector< eoContinue<EOT> > continuators;
continuators.add< eoSteadyFitContinue<EOT> >(10,10);
continuators.add< eoGenContinue<EOT> >(100);
eoForgeVector< eoQuadOp<EOT> > crossovers;
crossovers.add< eo1PtBitXover<EOT> >();
crossovers.add< eoUBitXover<EOT> >(0.5); // preference over 1
crossovers.add< eoNPtsBitXover<EOT> >(2); // nb of points
eoForgeVector< eoMonOp<EOT> > mutations;
mutations.add< eoBitMutation<EOT> >(0.01); // proba of flipping one bit
mutations.add< eoDetBitFlip<EOT> >(1); // mutate k bits
eoForgeVector< eoSelectOne<EOT> > selectors;
selectors.add< eoDetTournamentSelect<EOT> >(pop_size/2);
selectors.add< eoStochTournamentSelect<EOT> >(0.5);
selectors.add< eoSequentialSelect<EOT> >();
selectors.add< eoProportionalSelect<EOT> >();
eoForgeVector< eoReplacement<EOT> > replacors;
replacors.add< eoCommaReplacement<EOT> >();
replacors.add< eoPlusReplacement<EOT> >();
replacors.add< eoSSGAWorseReplacement<EOT> >();
replacors.add< eoSSGADetTournamentReplacement<EOT> >(pop_size/2);
replacors.add< eoSSGAStochTournamentReplacement<EOT> >(0.51);
std::clog << continuators.size() * crossovers.size() * mutations.size() * selectors.size() * replacors.size()
<< " possible algorithms instances." << std::endl;
EOT best_sol;
std::string best_algo = "";
for(auto& forge_cont : continuators) {
auto& continuator = forge_cont->instantiate();
for(auto& forge_cross : crossovers) {
auto& crossover = forge_cross->instantiate();
for(auto& forge_mut : mutations ) {
auto& mutation = forge_mut->instantiate();
for(auto& forge_sel : selectors) {
auto& selector = forge_sel->instantiate();
for(auto& forge_rep : replacors) {
auto& replacor = forge_rep->instantiate();
std::ostringstream algo_name;
algo_name << continuator.className() << " + "
<< crossover.className() << " + "
<< mutation.className() << " + "
<< selector.className() << " + "
<< replacor.className();
std::clog << "ALGO: " << algo_name.str();
std::clog.flush();
eoSequentialOp<EOT> variator;
variator.add(crossover, 1.0);
variator.add(mutation, 1.0);
eoGeneralBreeder<EOT> breeder(selector, variator, 1.0);
eoCombinedContinue<EOT> gen_cont(common_cont);
gen_cont.add(continuator);
eoEasyEA<EOT> algo(gen_cont, eval, breeder, replacor);
eoPop<EOT> pop;
pop.append(pop_size, init);
::apply(eval,pop);
algo(pop);
std::clog << " = " << pop.best_element().fitness() << std::endl;
if(best_sol.invalid()) {
best_sol = pop.best_element();
best_algo = algo_name.str();
} else if(pop.best_element().fitness() > best_sol.fitness()) {
best_sol = pop.best_element();
best_algo = algo_name.str();
}
}
}
}
}
}
std::cout << "Best algo: " << best_algo << ", with " << best_sol << std::endl;
}