Added tests for roulette wheel selection

This commit is contained in:
maartenkeijzer 2003-06-04 09:34:26 +00:00
commit b6104c6f32
2 changed files with 76 additions and 1 deletions

View file

@ -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)
###############################################################################

68
eo/test/t-eoRoulette.cpp Normal file
View file

@ -0,0 +1,68 @@
#include <eoPop.h>
#include <EO.h>
#include <eoProportionalSelect.h>
#include <eoStochasticUniversalSelect.h>
class TestEO : public EO<double> { public: unsigned index; };
using namespace std;
template <class Select>
int test_select()
{
vector<double> probs(4);
probs[0] = 0.1;
probs[1] = 0.4;
probs[2] = 0.2;
probs[3] = 0.3;
vector<double> counts(4,0.0);
// setup population
eoPop<TestEO> 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<eoProportionalSelect<TestEO> >()) return 1;
return test_select<eoStochasticUniversalSelect<TestEO> >();
}