# t-eoReplacement.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 //-----------------------------------------------------------------------------
    00011 // tt.cpp:
    00012 //
    00013 //-----------------------------------------------------------------------------
    00014
    00015
    00016 // general
    00017 #include <eo>
    00018 //-----------------------------------------------------------------------------
    00019
    00020 struct Dummy : public EO<double>
    00021 {
    00022     typedef double Type;
    00023   void printOn(std::ostream & _os) const
    00024   {
    00025       _os << " - ";
    00026       EO<double>::printOn(_os);
    00027   }
    00028 };
    00029
    00030
    00031 struct eoDummyPop : public eoPop<Dummy>
    00032 {
    00033 public :
    00034     eoDummyPop(int s=0) { resize(s); }
    00035 };
    00036
    00037 //-----------------------------------------------------------------------------
    00038
    00039 int the_main(int argc, char **argv)
    00040 {
    00041   eoParser parser(argc, argv);
    00042   eoValueParam<unsigned int> parentSizeParam(10, "parentSize", "Parent size",'P');
    00043   parser.processParam( parentSizeParam );
    00044     unsigned int pSize = parentSizeParam.value();
    00045
    00046   eoValueParam<unsigned int> offsrpringSizeParam(10, "offsrpringSize", "Offsrpring size",'O');
    00047   parser.processParam( offsrpringSizeParam );
    00048     unsigned int oSize = offsrpringSizeParam.value();
    00049
    00050   eoValueParam<unsigned int> tournamentSizeParam(2, "tournamentSize", "Deterministic tournament size",'T');
    00051   parser.processParam( tournamentSizeParam );
    00052     unsigned int tSize = tournamentSizeParam.value();
    00053
    00054   eoValueParam<double> tournamentRateParam(0.75, "tournamentRate", "Stochastic tournament rate",'R');
    00055   parser.processParam( tournamentRateParam );
    00056     double tRate = tournamentRateParam.value();
    00057
    00058   eoValueParam<double> sParentsElitismRateParam(0.1, "sParentsElitismRateParam", "Strong elitism rate for parents",'E');
    00059   parser.processParam( sParentsElitismRateParam );
    00060     double sParentsElitismRate = sParentsElitismRateParam.value();
    00061
    00062   eoValueParam<double> sParentsEugenismRateParam(0, "sParentsEugenismRateParam", "Strong Eugenism rate",'e');
    00063   parser.processParam( sParentsEugenismRateParam );
    00064     double sParentsEugenismRate = sParentsEugenismRateParam.value();
    00065
    00066   eoValueParam<double> sOffspringElitismRateParam(0, "sOffspringElitismRateParam", "Strong elitism rate for parents",'E');
    00067   parser.processParam( sOffspringElitismRateParam );
    00068     double sOffspringElitismRate = sOffspringElitismRateParam.value();
    00069
    00070   eoValueParam<double> sOffspringEugenismRateParam(0, "sOffspringEugenismRateParam", "Strong Eugenism rate",'e');
    00071   parser.processParam( sOffspringEugenismRateParam );
    00072     double sOffspringEugenismRate = sOffspringEugenismRateParam.value();
    00073
    00074     if (parser.userNeedsHelp())
    00075       {
    00076         parser.printHelp(std::cout);
    00077         exit(1);
    00078       }
    00079
    00080     unsigned i;
    00081
    00082     std::cout << "Testing the replacements\nParents SIze = " << pSize
    00083          << " and offspring size = " << oSize << std::endl;
    00084
    00085     rng.reseed(42);
    00086
    00087
    00088     eoDummyPop orgParents(pSize);
    00089     eoDummyPop orgOffspring(oSize);
    00090
    00091     // initialize so we can recognize them later!
    00092     for (i=0; i<pSize; i++)
    00093       orgParents[i].fitness(2*i+1);
    00094     for (i=0; i<oSize; i++)
    00095       orgOffspring[i].fitness(2*i);
    00096
    00097 std::cout << "Initial parents (odd)\n" << orgParents << "\n And initial offsprings (even)\n" << orgOffspring << std::endl;
    00098
    00099     // now the ones we're going to play with
    00100     eoDummyPop parents(0);
    00101     eoDummyPop offspring(0);
    00102
    00103 // the replacement procedures under test
    00104     eoGenerationalReplacement<Dummy> genReplace;
    00105     eoPlusReplacement<Dummy> plusReplace;
    00106     eoEPReplacement<Dummy> epReplace(tSize);
    00107     eoCommaReplacement<Dummy> commaReplace;
    00108     eoWeakElitistReplacement<Dummy> weakElitistReplace(commaReplace);
    00109     // the SSGA replacements
    00110     eoSSGAWorseReplacement<Dummy> ssgaWorseReplace;
    00111     eoSSGADetTournamentReplacement<Dummy> ssgaDTReplace(tSize);
    00112     eoSSGAStochTournamentReplacement<Dummy> ssgaDSReplace(tRate);
    00113
    00114     // here we go
    00115     // Generational
    00116     parents = orgParents;
    00117     offspring = orgOffspring;
    00118
    00119     std::cout << "eoGenerationalReplacement\n";
    00120     std::cout << "=========================\n";
    00121     genReplace(parents, offspring);
    00122 std::cout << "Parents (originally odd)\n" << parents << "\n And offsprings (orogonally even\n" << offspring << std::endl;
    00123
    00124     // Plus
    00125     parents = orgParents;
    00126     offspring = orgOffspring;
    00127
    00128     std::cout << "eoPlusReplacement\n";
    00129     std::cout << "=================\n";
    00130     plusReplace(parents, offspring);
    00131 std::cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << std::endl;
    00132
    00133     // EP (proche d'un PLUS
    00134     parents = orgParents;
    00135     offspring = orgOffspring;
    00136
    00137     std::cout << "eoEPReplacement\n";
    00138     std::cout << "===============\n";
    00139     epReplace(parents, offspring);
    00140 std::cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << std::endl;
    00141
    00142     // Comma
    00143     parents = orgParents;
    00144     offspring = orgOffspring;
    00145
    00146     if (parents.size() > offspring.size() )
    00147         std::cout << "Skipping Comma Replacement, more parents than offspring\n";
    00148     else
    00149       {
    00150         std::cout << "eoCommaReplacement\n";
    00151         std::cout << "==================\n";
    00152         commaReplace(parents, offspring);
    00153         std::cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << std::endl;
    00154
    00155         // Comma with weak elitism
    00156         parents = orgParents;
    00157         offspring = orgOffspring;
    00158
    00159         std::cout << "The same, with WEAK elitism\n";
    00160         std::cout << "===========================\n";
    00161         weakElitistReplace(parents, offspring);
    00162         std::cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << std::endl;
    00163       }
    00164
    00165         // preparing SSGA replace worse
    00166         parents = orgParents;
    00167         offspring = orgOffspring;
    00168
    00169     if (parents.size() < offspring.size() )
    00170         std::cout << "Skipping all SSGA Replacements, more offspring than parents\n";
    00171     else
    00172       {
    00173         std::cout << "SSGA replace worse\n";
    00174         std::cout << "==================\n";
    00175         ssgaWorseReplace(parents, offspring);
    00176         std::cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << std::endl;
    00177
    00178     // SSGA deterministic tournament
    00179         parents = orgParents;
    00180         offspring = orgOffspring;
    00181
    00182         std::cout << "SSGA deterministic tournament\n";
    00183         std::cout << "=============================\n";
    00184         ssgaDTReplace(parents, offspring);
    00185         std::cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << std::endl;
    00186
    00187     // SSGA stochastic tournament
    00188         parents = orgParents;
    00189         offspring = orgOffspring;
    00190
    00191         std::cout << "SSGA stochastic tournament\n";
    00192         std::cout << "==========================\n";
    00193         ssgaDTReplace(parents, offspring);
    00194         std::cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << std::endl;
    00195       }
    00196
    00197     // the general replacement
    00198     eoDeterministicSaDReplacement<Dummy> sAdReplace(sParentsElitismRate, sParentsEugenismRate, sOffspringElitismRate, sOffspringEugenismRate);// 10% parents survive
    00199
    00200     parents = orgParents;
    00201     offspring = orgOffspring;
    00202
    00203     std::cout << "General - strong elitism\n";
    00204     std::cout << "========================\n";
    00205     sAdReplace(parents, offspring);
    00206     std::cout << "Parents (originally odd)\n" << parents << "\n And offsprings (originally even)\n" << offspring << std::endl;
    00207
    00208
    00209     return 1;
    00210 }
    00211
    00212 int main(int argc, char **argv)
    00213 {
    00214     try
    00215     {
    00216         the_main(argc, argv);
    00217     }
    00218     catch(std::exception& e)
    00219     {
    00220         std::cout << "Exception: " << e.what() << std::endl;
    00221     }
    00222
    00223 }