first example of automatic algorithm assembling

- add "Forges" tools, to wrap several operator with deferred
instanciation.
- add t-forge-algo to show how to enumerate several algorithms instances
from a common grammar and several alternative operators.
- add several missing className().
This commit is contained in:
Johann Dreo 2020-03-27 15:59:49 +01:00
commit 30c99f290f
14 changed files with 407 additions and 3 deletions

View file

@ -71,6 +71,8 @@ set (TEST_LIST
t-eoParser
t-eoPartiallyMappedXover
t-eoEvalCmd
t-operator-forge
t-forge-algo
)

110
eo/test/t-forge-algo.cpp Normal file
View file

@ -0,0 +1,110 @@
#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);
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()
<< " algorithm instances to test." << std::endl;
EOT best_sol;
std::string best_algo = "";
for(auto& forge_cont : continuators) {
auto& continuator = forge_cont->instanciate();
for(auto& forge_cross : crossovers) {
auto& crossover = forge_cross->instanciate();
for(auto& forge_mut : mutations ) {
auto& mutation = forge_mut->instanciate();
for(auto& forge_sel : selectors) {
auto& selector = forge_sel->instanciate();
for(auto& forge_rep : replacors) {
auto& replacor = forge_rep->instanciate();
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);
eoEasyEA<EOT> algo(continuator, 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;
}

View file

@ -0,0 +1,61 @@
#include <iostream>
#include <string>
#include <eo>
struct OpInterface
{
std::string _name;
OpInterface(std::string name) : _name(name) {}
virtual void operator()() = 0;
};
struct OpA : public OpInterface
{
OpA(std::string name) : OpInterface(name) {}
void operator()(){std::cout << _name << std::endl;}
};
struct OpB : public OpInterface
{
OpB(std::string name, std::string suffix) : OpInterface(name+suffix) {}
void operator()(){std::cout << _name << std::endl;}
};
int main(int /*argc*/, char** /*argv*/)
{
// Forge container using names.
eoForgeMap<OpInterface> named_factories;
// Capture constructor's parameters and defer instanciation.
named_factories.add<OpA>("OpA", "I'm A");
named_factories.setup<OpA>("OpA", "I'm actually A"); // Edit
named_factories.add<OpB>("OpB1", "I'm B", " prime");
named_factories.add<OpB>("OpB2", "I'm a B", " junior");
// Actually instanciante.
OpInterface& opa = named_factories.instanciate("OpA");
OpInterface& opb1 = named_factories.instanciate("OpB1");
// Call.
opa();
opb1();
// Instanciate and call.
named_factories.instanciate("OpB2")();
// Forge container using indices.
eoForgeVector<OpInterface> indexed_factories;
// Capture constructor's parameters and defer instanciation.
indexed_factories.add<OpA>("I'm A");
indexed_factories.setup<OpA>(0, "I'm actually A"); // Edit
indexed_factories.add<OpB>("I'm B", " prime");
indexed_factories.add<OpB>("I'm a B", " junior");
// Actually instanciante and call.
indexed_factories.instanciate(0)();
indexed_factories.instanciate(1)();
indexed_factories.instanciate(2)();
}