Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages | Search for

# t-eoSelect.cpp

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