Added pareto based stuff

This commit is contained in:
maartenkeijzer 2001-03-12 16:03:08 +00:00
commit 3c19641c70
4 changed files with 168 additions and 27 deletions

View file

@ -10,10 +10,10 @@ DEPS = $(top_builddir)/src/utils/libeoutils.a $(top_builddir)/src/libeo.a
INCLUDES = -I$(top_builddir)/src
LDADDS = $(top_builddir)/src/utils/libeoutils.a $(top_builddir)/src/libeo.a
CXXFLAGS = -g -Wall -pg
CXXFLAGS = -g -Wall
###############################################################################
check_PROGRAMS = t-eofitness t-eoRandom t-eobin t-eoStateAndParser t-eoCheckpointing t-eoSSGA \
check_PROGRAMS = t-eoPareto t-eofitness t-eoRandom t-eobin t-eoStateAndParser t-eoCheckpointing t-eoSSGA \
t-eoExternalEO t-eoSymreg t-eo t-eoReplacement t-eoSelect t-eoGenOp t-eoGA t-eoVector
TESTS=run_tests t-eoVector t-eoRandom t-eoSSGA
# removing temporarily t-eoESFull
@ -121,3 +121,10 @@ t_eoSSGA_LDFLAGS = -lm
t_eoSSGA_LDADD = $(LDADDS)
###############################################################################
t_eoPareto_SOURCES = t-eoPareto.cpp
t_eoPareto_DEPENDENCIES = $(DEPS) $(top_builddir)/src/ga/libga.a
t_eoPareto_LDFLAGS = -lm
t_eoPareto_LDADD = $(LDADDS)
###############################################################################

View file

@ -225,7 +225,7 @@ int the_main(int argc, char **argv)
sOpQuadPlusMon.add(quad, 1);
sOpQuadPlusMon.add(mon, 1);
// this corresponds
// this corresponds
eoProportionalOp<EOT> pOpSAGLike;
pOpSAGLike.add(sOpQuadPlusMon, 0.24);
pOpSAGLike.add(quad, 0.56);
@ -234,10 +234,11 @@ int the_main(int argc, char **argv)
// init
eoPop<EOT> pop;
eoPop<EOT> offspring;
init(pop, pSize);
// sort pop so seqPopulator is identical to SelectPopulator(SequentialSelect)
pop.sort();
pop.sort();
cout << "Population initiale\n" << pop << endl;
// To simulate SGA: first a prop between quadOp and quadClone
@ -249,23 +250,25 @@ int the_main(int argc, char **argv)
virtualSGA.add(pSGAOp, 1.0);
virtualSGA.add(mon, 0.3);
eoSeqPopulator<EOT> popit(pop); // no selection, a copy of pop
eoSeqPopulator<EOT> popit(pop, offspring); // no selection, a copy of pop
// until we filled a new population
try
{
while (popit.size() < pop.size())
{
virtualSGA(popit);
}
while (offspring.size() < pop.size())
{
virtualSGA(popit);
++popit;
}
}
catch(eoPopulator<EOT>::OutOfIndividuals&)
{
cout << "Warning: not enough individuals to handle\n";
}
swap(pop, popit);
swap(pop, offspring);
offspring.clear();
// ok, now print
cout << "Apres virtualSGA \n" << pop << endl;
@ -276,14 +279,16 @@ int the_main(int argc, char **argv)
eoSequentialSelect<EOT> seqSelect;
// select.init(); should be sorted out: is it the setup method???
eoSelectivePopulator<EOT> it_step3(pop, seqSelect);
eoSelectivePopulator<EOT> it_step3(pop, offspring, seqSelect);
while (it_step3.size() < 2*pop.size())
while (offspring.size() < 2*pop.size())
{
virtualSGA(it_step3);
++it_step3;
}
swap(pop, it_step3);
swap(pop, offspring);
offspring.clear();
// ok, now print
cout << "Apres SGA-like eoSelectivePopulator\n" << pop << endl;
@ -292,14 +297,16 @@ int the_main(int argc, char **argv)
cout << "Now the pure addition !" << endl;
init(pop, pSize);
eoSelectivePopulator<EOT> it_step4(pop, seqSelect);
while (it_step4.size() < 2*pop.size())
eoSelectivePopulator<EOT> it_step4(pop, offspring, seqSelect);
while (offspring.size() < 2*pop.size())
{
sOpQuadPlusMon(it_step4);
++it_step4;
}
swap(pop, it_step4);
swap(pop, offspring);
offspring.clear();
// ok, now print
cout << "Apres Quad+Mon ds un eoSelectivePopulator\n" << pop << endl;

120
eo/test/t-eoPareto.cpp Normal file
View file

@ -0,0 +1,120 @@
#include <eo>
using namespace std;
typedef vector<double> fitness_type;
struct eoDouble : public EO<fitness_type>
{
double value;
void printOn(ostream& os) const { os << fitness()[0] << ' ' << fitness()[1] << ' ' << value; }
void readFrom(istream& is) { is >> value; }
};
class Mutate : public eoMonOp<eoDouble>
{
bool operator()(eoDouble& _eo)
{
_eo.value += rng.normal() * 0.1 * _eo.value;
return true;
}
};
class Eval : public eoEvalFunc<eoDouble>
{
void operator()(eoDouble& _eo)
{
double v = _eo.value;
fitness_type f(2);
f[0] = v * v;
f[1] = (v - 1.) * (v - 1.);
_eo.fitness(f);
}
};
class Init : public eoInit<eoDouble>
{
void operator()(eoDouble& _eo)
{
_eo.value = rng.normal() * 10.;
_eo.invalidate();
}
};
// Test pareto dominance and perf2worth
void the_main()
{
Init init;
Eval eval;
Mutate mutate;
unsigned pop_size = 50;
eoPop<eoDouble> pop(pop_size, init);
vector<bool> maximizes(2, false); // minimize both objectives
// The dominance map needs to know how to compare
eoDominanceMap<eoDouble> dominance(maximizes);
// Pareto ranking needs a dominance map
eoParetoRanking<eoDouble> perf2worth(dominance);
// Three selectors
eoDetTournamentWorthSelect<eoDouble> select1(perf2worth, 3);
eoStochTournamentWorthSelect<eoDouble> select2(perf2worth, 0.95);
eoRouletteWorthSelect<eoDouble> select3(perf2worth);
// One general operator
eoProportionalOp<eoDouble> opsel;
opsel.add(mutate, 1.0);
// Three breeders
eoGeneralBreeder<eoDouble> breeder1(select1, opsel);
eoGeneralBreeder<eoDouble> breeder2(select2, opsel);
eoGeneralBreeder<eoDouble> breeder3(select3, opsel);
// Comma replacement
eoCommaReplacement<eoDouble> replace;
unsigned long generation = 0;
eoGenContinue<eoDouble> gen(10, generation);
eoCheckPoint<eoDouble> cp(gen);
// Three algos
eoEasyEA<eoDouble> ea1(cp, eval, breeder1, replace);
eoEasyEA<eoDouble> ea2(cp, eval, breeder2, replace);
eoEasyEA<eoDouble> ea3(cp, eval, breeder3, replace);
apply<eoDouble>(eval, pop);
ea1(pop);
apply<eoDouble>(init, pop);
apply<eoDouble>(eval, pop);
generation = 0;
ea2(pop);
apply<eoDouble>(init, pop);
apply<eoDouble>(eval, pop);
generation = 0;
ea3(pop);
}
int main()
{
try
{
the_main();
}
catch (exception& e)
{
cout << "Exception thrown: " << e.what() << endl;
throw e; // make sure it does not pass the test
}
}

View file

@ -1,6 +1,8 @@
#include <eo>
// tests a Steady State GA
// Needed to define this breeder, maybe make it a breeder
template <class EOT>
class eoBreedOne : public eoBreed<EOT>
{
@ -9,9 +11,9 @@ public :
void operator()(const eoPop<EOT>& _src, eoPop<EOT>& _dest)
{
eoSelectivePopulator<EOT> pop(_src, select);
_dest.clear();
eoSelectivePopulator<EOT> pop(_src, _dest, select);
op(pop);
_dest = pop;
}
private :
@ -72,15 +74,17 @@ int main()
opsel.add(xover, 0.8);
opsel.add(mutate, 0.2);
/*
eoDetTournamentSelect<EoType> selector(3);
eoBreedOne<EoType> breed(selector, opsel);
eoSSGAWorseReplacement<EoType> replace;
*/
eoRandomSelect<EoType> selector;
eoGeneralBreeder<EoType> breed(selector, opsel);
eoPlusReplacement<EoType> replace;
// Replace a single one
eoSSGAWorseReplacement<EoType> replace;
// eoRandomSelect<EoType> selector;
// eoGeneralBreeder<EoType> breed(selector, opsel);
// eoPlusReplacement<EoType> replace;
eoMyEval<EoType> eval;
@ -92,6 +96,7 @@ int main()
eoPop<EoType> pop(pop_size, init);
// evaluate
apply<EoType>(eval, pop);
eoBestFitnessStat<EoType> best("Best_Fitness");
@ -100,11 +105,13 @@ int main()
cp.add(best);
cp.add(avg);
cp.add(mon);
// cp.add(mon);
mon.add(best);
mon.add(avg);
// and run
algo(pop);
}