diff --git a/eo/test/t-eoSharing.cpp b/eo/test/t-eoSharing.cpp new file mode 100644 index 00000000..2cd436da --- /dev/null +++ b/eo/test/t-eoSharing.cpp @@ -0,0 +1,236 @@ +//----------------------------------------------------------------------------- + +// to avoid long name warnings +#ifdef _MSC_VER +#pragma warning(disable:4786) +#endif + +#include +#include +#include // runtime_error + +// general +#include +#include +//----------------------------------------------------------------------------- + +struct Dummy : public EO +{ + typedef double Type; + void printOn(std::ostream & _os) const + { + EO::printOn(_os); + cout << " " << xdist ; + } + double xdist; +}; + +class +eoDummyDistance : public eoDistance +{ + double operator()(const Dummy & _v1, const Dummy & _v2) + { + double r= _v1.xdist - _v2.xdist; + return sqrt(r*r); + } +}; + + +bool operator==(const Dummy & _d1, const Dummy & _d2) +{ + return _d1.fitness() == _d2.fitness(); +} + +struct eoDummyPop : public eoPop +{ +public : + eoDummyPop(int s=0) { resize(s); } +}; + +// helper - DOES NOT WORK if different individuals have same fitness!!! +template +unsigned isInPop(EOT & _indi, eoPop & _pop) +{ + for (unsigned i=0; i<_pop.size(); i++) + if (_pop[i] == _indi) + return i; + return _pop.size(); +} + +unsigned int pSize; // global variable, bouh! +std::string fitnessType; // yes, a global variable :-) +eoDummyPop parentsOrg; + +template +void testSelectMany(eoSelect & _select, std::string _name) +{ + unsigned i; + std::cout << "\n\n" << fitnessType + _name << std::endl; + std::cout << "===============\n"; + + eoDummyPop parents(parentsOrg); + eoDummyPop offspring(0); + + // do the selection + _select(parents, offspring); + + // cout << "Pop offspring \n" << offspring << endl; + + // compute stats + std::vector nb(parents.size(), 0); + for (i=0; i(offspring[i], parents); + if (trouve == parents.size()) // pas trouve + throw std::runtime_error("Pas trouve ds parents"); + nb[trouve]++; + } + // dump to file so you can plot using gnuplot - dir name is hardcoded! + std::string fName = "ResSelect/" + fitnessType + _name + ".select"; + std::ofstream os(fName.c_str()); + for (i=0; i " << ( (double)nb[i])/offspring.size() << std::endl; + os << i << " " << ( (double)nb[i])/offspring.size() << std::endl; + } + +} + +template +void testSelectOne(eoSelectOne & _select, eoHowMany & _offspringRate, + eoHowMany & _fertileRate, std::string _name) +{ + eoTruncatedSelectOne truncSelect(_select, _fertileRate); + eoSelectMany percSelect(truncSelect, _offspringRate); + testSelectMany(percSelect, _name); +} + + +//----------------------------------------------------------------------------- + +int the_main(int argc, char **argv) +{ + eoParser parser(argc, argv); + + // random seed + eoValueParam& seedParam = parser.createParam(uint32(0), "seed", "Random number seed", 'S'); + if (seedParam.value() == 0) + seedParam.value() = time(0); + rng.reseed(seedParam.value()); + + + // pSize global variable ! + eoValueParam pSizeParam = parser.createParam(unsigned(10), "parentSize", "Parent size",'P'); + pSize = pSizeParam.value(); + + eoHowMany oRate = parser.createParam(eoHowMany(1.0), "offsrpringRate", "Offsrpring rate (% or absolute)",'O').value(); + + eoHowMany fRate = parser.createParam(eoHowMany(1.0), "fertileRate", "Fertility rate (% or absolute)",'F').value(); + + + double nicheSize = parser.createParam(0.1, "nicheSize", "Paramter Sigma for Sharing",'\0').value(); + + eoParamParamType & peakParam = parser.createParam(eoParamParamType("2(1,2)"), "peaks", "Description of the peaks: N(nb1,nb2,...,nbN)", 'p').value(); + + // the number of peaks: first item of the paramparam + unsigned peakNumber = atoi(peakParam.first.c_str()); + if (peakNumber < 2) + { + std::cerr << "WARNING, nb of peaks must be larger than 2, using 2" << std::endl; + peakNumber = 2; + } + + vector nbIndiPerPeak(peakNumber); + unsigned i, sum=0; + + // the second item is a vector containing all values + if (!peakParam.second.size()) // no other parameter : equal peaks + { + std::cerr << "WARNING, no nb of indis per peaks, using equal nbs" << std::endl; + for (i=0; i detSelect(oRate); + // testSelectMany(detSelect, "detSelect"); + + // Sharing using the perf2Worth construct + // need a distance for that + eoDummyDistance dist; + eoSharingSelect newSharingSelect(nicheSize, dist); + sprintf(fileName,"Niche_%g",nicheSize); + testSelectOne(newSharingSelect, oRate, fRate, fileName); + + return 1; +} + +int main(int argc, char **argv) +{ + try + { + the_main(argc, argv); + } + catch(std::exception& e) + { + std::cout << "Exception: " << e.what() << std::endl; + return 1; + } +}