From a27aa7112a316ef187e2249e9302534da6031a67 Mon Sep 17 00:00:00 2001 From: maartenkeijzer Date: Fri, 9 Mar 2001 14:14:53 +0000 Subject: [PATCH] Test for a ssga added. --- eo/test/Makefile.am | 15 ++++-- eo/test/t-eoGenOp.cpp | 4 +- eo/test/t-eoSSGA.cpp | 110 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 eo/test/t-eoSSGA.cpp diff --git a/eo/test/Makefile.am b/eo/test/Makefile.am index 5c1c29f11..62c5fe62e 100644 --- a/eo/test/Makefile.am +++ b/eo/test/Makefile.am @@ -9,13 +9,13 @@ 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 +LDADDS = $(top_builddir)/src/utils/libeoutils.a $(top_builddir)/src/libeo.a +CXXFLAGS = -g -Wall -pg ############################################################################### -check_PROGRAMS = t-eofitness t-eoRandom t-eobin t-eoStateAndParser t-eoCheckpointing \ +check_PROGRAMS = 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 +TESTS=run_tests t-eoVector t-eoRandom t-eoSSGA # removing temporarily t-eoESFull #noinst_PROGRAMS = t-eofitness t-eobin t-eoStateAndParser t-eoCheckpointing t-eoExternalEO t-eoESFull t-eoSymreg t-eo t-eoReplacement t-eoSelect t-eoGenOp t-eoGA @@ -114,3 +114,10 @@ t_eoGA_LDFLAGS = -lm t_eoGA_LDADD = $(top_builddir)/src/ga/libga.a $(LDADDS) ############################################################################### + +t_eoSSGA_SOURCES = t-eoSSGA.cpp binary_value.h +t_eoSSGA_DEPENDENCIES = $(DEPS) $(top_builddir)/src/ga/libga.a +t_eoSSGA_LDFLAGS = -lm +t_eoSSGA_LDADD = $(LDADDS) + +############################################################################### diff --git a/eo/test/t-eoGenOp.cpp b/eo/test/t-eoGenOp.cpp index 40c79d19e..6fe36c112 100644 --- a/eo/test/t-eoGenOp.cpp +++ b/eo/test/t-eoGenOp.cpp @@ -132,10 +132,8 @@ class two2oneOp : public eoGenOp // :-) void apply(eoPopulator& _plop) { EOT& eo = *_plop; // select the guy - ++_plop; // advance - EOT& eo2 = *_plop; + const EOT& eo2 = _plop.select(); eo.s = "221(" + eo.s + ", " + eo2.s + ")"; - _plop.erase(); // oh right, and invalidate fitnesses } virtual string className() {return "two2oneOp";} diff --git a/eo/test/t-eoSSGA.cpp b/eo/test/t-eoSSGA.cpp new file mode 100644 index 000000000..b42db6ef5 --- /dev/null +++ b/eo/test/t-eoSSGA.cpp @@ -0,0 +1,110 @@ +#include + + +template +class eoBreedOne : public eoBreed +{ +public : + eoBreedOne(eoSelectOne& _select, eoGenOp& _op) : select(_select), op(_op) {} + + void operator()(const eoPop& _src, eoPop& _dest) + { + eoSelectivePopulator pop(_src, select); + op(pop); + _dest = pop; + } + +private : + eoSelectOne& select; + eoGenOp& op; +}; + +typedef eoMinimizingFitness FitnessType; +typedef eoVector EoType; + +template +class eoMyEval : public eoEvalFunc +{ + public : + + void operator()(EOT& _eo) + { + _eo.fitness(*max_element(_eo.begin(), _eo.end())); + } +}; + +template +class Xover : public eoBinOp +{ + bool operator()(EOT& _eo, const EOT& _eo2) + { + unsigned point = rng.random(_eo.size()); + copy(_eo2.begin() + point, _eo2.end(), _eo.begin() + point); + return true; + } +}; + +template +class Mutate : public eoMonOp +{ + bool operator()(EOT& _eo) + { + unsigned point = rng.random(_eo.size()); + _eo[point] = rng.random(1024); + return true; + } +}; + + +int main() +{ + int pop_size = 10; + + eoGenContinue cnt(10); + eoCheckPoint cp(cnt); + + + Xover xover; + Mutate mutate; + + eoProportionalOp opsel; + + opsel.add(xover, 0.8); + opsel.add(mutate, 0.2); + +/* + eoDetTournamentSelect selector(3); + eoBreedOne breed(selector, opsel); + eoSSGAWorseReplacement replace; +*/ + + eoRandomSelect selector; + eoGeneralBreeder breed(selector, opsel); + eoPlusReplacement replace; + + + eoMyEval eval; + + eoEasyEA algo(cp, eval, breed, replace); + + eoUniformGenerator unif(0,1024); + eoInitFixedLength init(20, unif); + + eoPop pop(pop_size, init); + + apply(eval, pop); + + eoBestFitnessStat best("Best_Fitness"); + eoAverageStat avg("Avg_Fitness"); + eoStdoutMonitor mon; + + cp.add(best); + cp.add(avg); + cp.add(mon); + + mon.add(best); + mon.add(avg); + + algo(pop); + +} \ No newline at end of file