From b6104c6f324bbfa84e53746e298533dfcdcc5b6b Mon Sep 17 00:00:00 2001 From: maartenkeijzer Date: Wed, 4 Jun 2003 09:34:26 +0000 Subject: [PATCH] Added tests for roulette wheel selection --- eo/test/Makefile.am | 9 +++++- eo/test/t-eoRoulette.cpp | 68 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 eo/test/t-eoRoulette.cpp diff --git a/eo/test/Makefile.am b/eo/test/Makefile.am index e1238d633..5d30ccd00 100644 --- a/eo/test/Makefile.am +++ b/eo/test/Makefile.am @@ -17,7 +17,7 @@ CXXFLAGS = -g -Wall # PLEASE don't break the line (see create_batch.sh) -check_PROGRAMS = t-eoParetoFitness t-eoPareto t-eofitness t-eoRandom t-eobin t-eoVirus t-MGE t-MGE1bit t-MGE-control t-eoStateAndParser t-eoCheckpointing t-eoSSGA t-eoExternalEO t-eoSymreg t-eo t-eoReplacement t-eoSelect t-eoGenOp t-eoGA t-eoReal t-eoVector t-eoESAll t-eoPBIL t-eoFitnessAssembled t-eoFitnessAssembledEA +check_PROGRAMS = t-eoParetoFitness t-eoPareto t-eofitness t-eoRandom t-eobin t-eoVirus t-MGE t-MGE1bit t-MGE-control t-eoStateAndParser t-eoCheckpointing t-eoSSGA t-eoExternalEO t-eoSymreg t-eo t-eoReplacement t-eoSelect t-eoGenOp t-eoGA t-eoReal t-eoVector t-eoESAll t-eoPBIL t-eoFitnessAssembled t-eoFitnessAssembledEA t-eoRoulette #The run_tests script can be used to check various arguments TESTS=$(check_PROGRAMS) run_tests @@ -191,3 +191,10 @@ t_eoFitnessAssembledEA_LDFLAGS = -lm t_eoFitnessAssembledEA_LDADD = $(top_builddir)/src/es/libes.a $(LDADDS) ############################################################################### + +t_eoRoulette_SOURCES = t-eoRoulette.cpp +t_eoRoulette_DEPENDENCIES = $(DEPS) +t_eoRoulette_LDFLAGS = -lm +t_eoRoulette_LDADD = $(LDADDS) + +############################################################################### diff --git a/eo/test/t-eoRoulette.cpp b/eo/test/t-eoRoulette.cpp new file mode 100644 index 000000000..5c350122c --- /dev/null +++ b/eo/test/t-eoRoulette.cpp @@ -0,0 +1,68 @@ + +#include +#include +#include +#include + +class TestEO : public EO { public: unsigned index; }; + +using namespace std; + +template +int test_select() +{ + vector probs(4); + probs[0] = 0.1; + probs[1] = 0.4; + probs[2] = 0.2; + probs[3] = 0.3; + + vector counts(4,0.0); + + // setup population + eoPop pop; + for (unsigned i = 0; i < probs.size(); ++i) + { + pop.push_back( TestEO()); + pop.back().fitness( probs[i] * 2.1232 ); // some number to check scaling + pop.back().index = i; + } + + Select select; + + unsigned ndraws = 10000; + + for (unsigned i = 0; i < ndraws; ++i) + { + const TestEO& eo = select(pop); + + counts[eo.index]++; + } + + cout << "Threshold = " << 1./sqrt(double(ndraws)) << endl; + + for (unsigned i = 0; i < 4; ++i) + { + cout << counts[i]/ndraws << ' '; + + double c = counts[i]/ndraws; + + if (fabs(c - probs[i]) > 1./sqrt((double)ndraws)) { + cout << "ERROR" << endl; + return 1; + } + } + + cout << endl; + return 0; +} + +int main() +{ + rng.reseed(44); + + if (test_select >()) return 1; + + return test_select >(); +} +