00001
00002 #include <eoPop.h>
00003 #include <EO.h>
00004 #include <eoProportionalSelect.h>
00005 #include <eoStochasticUniversalSelect.h>
00006
00007 class TestEO : public EO<double> { public: unsigned index; };
00008
00009 using namespace std;
00010
00011 template <class Select>
00012 int test_select()
00013 {
00014 vector<double> probs(4);
00015 probs[0] = 0.1;
00016 probs[1] = 0.4;
00017 probs[2] = 0.2;
00018 probs[3] = 0.3;
00019
00020 vector<double> counts(4,0.0);
00021
00022
00023 eoPop<TestEO> pop;
00024 for (unsigned i = 0; i < probs.size(); ++i)
00025 {
00026 pop.push_back( TestEO());
00027 pop.back().fitness( probs[i] * 2.1232 );
00028 pop.back().index = i;
00029 }
00030
00031 Select select;
00032
00033 unsigned ndraws = 10000;
00034
00035 for (unsigned i = 0; i < ndraws; ++i)
00036 {
00037 const TestEO& eo = select(pop);
00038
00039 counts[eo.index]++;
00040 }
00041
00042 cout << "Threshold = " << 1./sqrt(double(ndraws)) << endl;
00043
00044 for (unsigned i = 0; i < 4; ++i)
00045 {
00046 cout << counts[i]/ndraws << ' ';
00047
00048 double c = counts[i]/ndraws;
00049
00050 if (fabs(c - probs[i]) > 1./sqrt((double)ndraws)) {
00051 cout << "ERROR" << endl;
00052 return 1;
00053 }
00054 }
00055
00056 cout << endl;
00057 return 0;
00058 }
00059
00060 int main()
00061 {
00062 rng.reseed(44);
00063
00064 if (test_select<eoProportionalSelect<TestEO> >()) return 1;
00065
00066 return test_select<eoStochasticUniversalSelect<TestEO> >();
00067 }
00068