00001
00002 #ifdef _MSC_VER
00003 #pragma warning(disable:4786)
00004 #endif
00005
00006 #include <cstring>
00007 #include <stdexcept>
00008
00009 #include <eo>
00010
00011
00012
00013 struct Dummy : public EO<double>
00014 {
00015 typedef double Type;
00016 void printOn(std::ostream & _os) const
00017 {
00018 _os << " - ";
00019 EO<double>::printOn(_os);
00020 }
00021 };
00022
00023 bool operator==(const Dummy & _d1, const Dummy & _d2)
00024 {
00025 return _d1.fitness() == _d2.fitness();
00026 }
00027
00028 struct eoDummyPop : public eoPop<Dummy>
00029 {
00030 public :
00031 eoDummyPop(int s=0) { resize(s); }
00032 };
00033
00034
00035 template <class EOT>
00036 unsigned isInPop(EOT & _indi, eoPop<EOT> & _pop)
00037 {
00038 for (unsigned i=0; i<_pop.size(); i++)
00039 if (_pop[i] == _indi)
00040 return i;
00041 return _pop.size();
00042 }
00043
00044 unsigned int pSize;
00045 std::string fitnessType;
00046 eoDummyPop parentsOrg;
00047
00048 template <class EOT>
00049 void testSelectMany(eoSelect<EOT> & _select, std::string _name)
00050 {
00051 unsigned i;
00052 std::cout << "\n\n" << fitnessType + _name << std::endl;
00053 std::cout << "===============\n";
00054
00055 eoDummyPop parents(parentsOrg);
00056 eoDummyPop offspring(0);
00057
00058
00059 _select(parents, offspring);
00060
00061
00062 std::vector<unsigned> nb(parents.size(), 0);
00063 for (i=0; i<offspring.size(); i++)
00064 {
00065 unsigned trouve = isInPop<Dummy>(offspring[i], parents);
00066 if (trouve == parents.size())
00067 throw std::runtime_error("Pas trouve ds parents");
00068 nb[trouve]++;
00069 }
00070
00071 std::string fName = "ResSelect/" + fitnessType + _name + ".select";
00072 std::ofstream os(fName.c_str());
00073 for (i=0; i<parents.size(); i++)
00074 {
00075 std::cout << i << " -> " << ( (double)nb[i])/offspring.size() << std::endl;
00076 os << i << " " << ( (double)nb[i])/offspring.size() << std::endl;
00077 }
00078
00079 }
00080
00081 template <class EOT>
00082 void testSelectOne(eoSelectOne<EOT> & _select, eoHowMany & _offspringRate,
00083 eoHowMany & _fertileRate, std::string _name)
00084 {
00085 eoTruncatedSelectOne<EOT> truncSelect(_select, _fertileRate);
00086 eoSelectMany<EOT> percSelect(truncSelect, _offspringRate);
00087 testSelectMany<EOT>(percSelect, _name);
00088 }
00089
00090
00091
00092
00093 int the_main(int argc, char **argv)
00094 {
00095 eoParser parser(argc, argv);
00096 eoValueParam<unsigned> parentSizeParam = parser.createParam(unsigned(10), "parentSize", "Parent size",'P');
00097 pSize = parentSizeParam.value();
00098
00099
00100
00101 eoValueParam<eoHowMany> offsrpringRateParam = parser.createParam(eoHowMany(1.0), "offsrpringRate", "Offsrpring rate (% or absolute)",'O');
00102 eoHowMany oRate = offsrpringRateParam.value();
00103
00104 eoValueParam<eoHowMany> fertileRateParam = parser.createParam(eoHowMany(1.0), "fertileRate", "Fertility rate (% or absolute)",'F');
00105 eoHowMany fRate = fertileRateParam.value();
00106
00107 eoValueParam<unsigned> tournamentSizeParam = parser.createParam(unsigned(2), "tournamentSize", "Deterministic tournament size",'T');
00108 unsigned int tSize = tournamentSizeParam.value();
00109
00110 eoValueParam<double> tournamentRateParam = parser.createParam(1.0, "tournamentRate", "Stochastic tournament rate",'t');
00111 double tRate = tournamentRateParam.value();
00112
00113 eoValueParam<double> rankingPressureParam = parser.createParam(2.0, "rankingPressure", "Selective pressure for the ranking selection",'p');
00114 double rankingPressure = rankingPressureParam.value();
00115
00116 eoValueParam<double> rankingExponentParam = parser.createParam(1.0, "rankingExponent", "Exponent for the ranking selection",'e');
00117 double rankingExponent = rankingExponentParam.value();
00118
00119 eoValueParam<std::string> fitTypeParam = parser.createParam(std::string("linear"), "fitType", "Type of fitness (linear, exp, log, super",'f');
00120 fitnessType = fitTypeParam.value();
00121
00122 if (parser.userNeedsHelp())
00123 {
00124 parser.printHelp(std::cout);
00125 exit(0);
00126 }
00127
00128
00129 system("mkdir ResSelect");
00130 std::cout << "Testing the Selections\nParents size = " << pSize
00131 << ", offspring rate = " << oRate;
00132 std::cout << " and putting rsulting files in dir ResSelect" << std::endl;
00133
00134
00135 parentsOrg.resize(pSize);
00136 if (fitnessType == std::string("linear"))
00137 for (unsigned i=0; i<pSize; i++)
00138 parentsOrg[i].fitness(i);
00139 else if (fitnessType == std::string("exp"))
00140 for (unsigned i=0; i<pSize; i++)
00141 parentsOrg[i].fitness(exp((double)i));
00142 else if (fitnessType == std::string("log"))
00143 for (unsigned i=0; i<pSize; i++)
00144 parentsOrg[i].fitness(log(i+1.));
00145 else if (fitnessType == std::string("super"))
00146 {
00147 for (unsigned i=0; i<pSize-1; i++)
00148 parentsOrg[i].fitness(i);
00149 parentsOrg[pSize-1].fitness(10*pSize);
00150 }
00151 else
00152 throw std::runtime_error("Invalid fitness Type"+fitnessType);
00153
00154 std::cout << "Initial parents (odd)\n" << parentsOrg << std::endl;
00155
00156
00157 eoValueParam<uint32_t>& seedParam = parser.createParam(uint32_t(0), "seed",
00158 "Random number seed", 'S');
00159 if (seedParam.value() == 0)
00160 seedParam.value() = time(0);
00161 rng.reseed(seedParam.value());
00162
00163 char fileName[1024];
00164
00165
00166
00167
00168
00169
00170 eoProportionalSelect<Dummy> propSelect;
00171 testSelectOne<Dummy>(propSelect, oRate, fRate, "PropSelect");
00172
00173
00174 eoRankingSelect<Dummy> newRankingSelect(rankingPressure);
00175 sprintf(fileName,"LinRank_%g",rankingPressure);
00176 testSelectOne<Dummy>(newRankingSelect, oRate, fRate, fileName);
00177
00178
00179 std::cout << "rankingExponent " << rankingExponent << std::endl;
00180 eoRankingSelect<Dummy> expRankingSelect(rankingPressure,rankingExponent);
00181 sprintf(fileName,"ExpRank_%g_%g",rankingPressure, rankingExponent);
00182 testSelectOne<Dummy>(expRankingSelect, oRate, fRate, fileName);
00183
00184
00185 eoDetTournamentSelect<Dummy> detTourSelect(tSize);
00186 sprintf(fileName,"DetTour_%d",tSize);
00187 testSelectOne<Dummy>(detTourSelect, oRate, fRate, fileName);
00188
00189
00190 eoStochTournamentSelect<Dummy> stochTourSelect(tRate);
00191 sprintf(fileName,"StochTour_%g",tRate);
00192 testSelectOne<Dummy>(stochTourSelect, oRate, fRate, fileName);
00193
00194
00195 eoFitnessScalingSelect<Dummy> newFitScaleSelect(rankingPressure);
00196 sprintf(fileName,"LinFitScale_%g",rankingPressure);
00197 testSelectOne<Dummy>(newFitScaleSelect, oRate, fRate, fileName);
00198
00199
00200 eoSequentialSelect<Dummy> seqSel(false);
00201 strcpy(fileName,"Sequential");
00202 testSelectOne<Dummy>(seqSel, oRate, fRate, fileName);
00203
00204 eoEliteSequentialSelect<Dummy> eliteSeqSel;
00205 strcpy(fileName,"EliteSequential");
00206 testSelectOne<Dummy>(eliteSeqSel, oRate, fRate, fileName);
00207
00208 return 1;
00209 }
00210
00211 int main(int argc, char **argv)
00212 {
00213 try
00214 {
00215 the_main(argc, argv);
00216 }
00217 catch(std::exception& e)
00218 {
00219 std::cout << "Exception: " << e.what() << std::endl;
00220 return 1;
00221 }
00222 }