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

# t-eoSSGA.cpp

    00001 #include <eo>
    00002
    00003 // tests a Steady State GA
    00004
    00005 // Needed to define this breeder, maybe make it a breeder
    00006 template <class EOT>
    00007 class eoBreedOne : public eoBreed<EOT>
    00008 {
    00009 public :
    00010   eoBreedOne(eoSelectOne<EOT>& _select, eoGenOp<EOT>& _op) : select(_select), op(_op) {}
    00011
    00012   void operator()(const eoPop<EOT>& _src, eoPop<EOT>& _dest)
    00013   {
    00014     _dest.clear();
    00015     eoSelectivePopulator<EOT> pop(_src, _dest, select);
    00016     op(pop);
    00017   }
    00018
    00019 private :
    00020   eoSelectOne<EOT>& select;
    00021   eoGenOp<EOT>& op;
    00022 };
    00023
    00024 typedef eoMinimizingFitness FitnessType;
    00025 typedef eoVector<FitnessType, unsigned> EoType;
    00026
    00027 template <class EOT>
    00028 class eoMyEval : public eoEvalFunc<EOT>
    00029 {
    00030   public :
    00031
    00032   void operator()(EOT& _eo)
    00033   {
    00034     _eo.fitness(*std::max_element(_eo.begin(), _eo.end()));
    00035   }
    00036 };
    00037
    00038 template <class EOT>
    00039 class Xover : public eoBinOp<EOT>
    00040 {
    00041   bool operator()(EOT& _eo, const EOT& _eo2)
    00042   {
    00043     unsigned point = rng.random(_eo.size());
    00044     std::copy(_eo2.begin() + point, _eo2.end(), _eo.begin() + point);
    00045     return true;
    00046   }
    00047 };
    00048
    00049 template <class EOT>
    00050 class Mutate : public eoMonOp<EOT>
    00051 {
    00052   bool operator()(EOT& _eo)
    00053   {
    00054     unsigned point = rng.random(_eo.size());
    00055     _eo[point] = rng.random(1024);
    00056     return true;
    00057   }
    00058 };
    00059
    00060
    00061 int main()
    00062 {
    00063   int pop_size = 10;
    00064
    00065   eoGenContinue<EoType> cnt(10);
    00066   eoCheckPoint<EoType> cp(cnt);
    00067
    00068
    00069   Xover<EoType> xover;
    00070   Mutate<EoType> mutate;
    00071
    00072   eoProportionalOp<EoType> opsel;
    00073
    00074   opsel.add(xover, 0.8);
    00075   opsel.add(mutate, 0.2);
    00076
    00077
    00078   eoDetTournamentSelect<EoType> selector(3);
    00079   eoBreedOne<EoType> breed(selector, opsel);
    00080
    00081   // Replace a single one
    00082   eoSSGAWorseReplacement<EoType> replace;
    00083
    00084
    00085 //  eoRandomSelect<EoType> selector;
    00086 //  eoGeneralBreeder<EoType> breed(selector, opsel);
    00087 //  eoPlusReplacement<EoType> replace;
    00088
    00089
    00090   eoMyEval<EoType> eval;
    00091
    00092   eoEasyEA<EoType> algo(cp, eval, breed, replace);
    00093
    00094   eoUniformGenerator<unsigned> unif(0,1024);
    00095   eoInitFixedLength<EoType> init(20, unif);
    00096
    00097   eoPop<EoType> pop(pop_size, init);
    00098
    00099   // evaluate
    00100   apply<EoType>(eval, pop);
    00101
    00102   eoBestFitnessStat<EoType>  best("Best_Fitness");
    00103         eoAverageStat<EoType> avg("Avg_Fitness");
    00104   eoStdoutMonitor mon;
    00105
    00106   cp.add(best);
    00107   cp.add(avg);
    00108
    00109 //  cp.add(mon);
    00110
    00111   mon.add(best);
    00112   mon.add(avg);
    00113
    00114   // and run
    00115   algo(pop);
    00116
    00117 }