exercise1.3.cpp

00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 //-----------------------------------------------------------------------------
00006 // FirstBitGA.cpp
00007 //-----------------------------------------------------------------------------
00008 //*
00009 // An instance of a VERY simple Bitstring Genetic Algorithm
00010 //
00011 //-----------------------------------------------------------------------------
00012 // standard includes
00013 #include <iostream>
00014 #include <stdexcept>
00015 
00016 // the general include for eo
00017 #include <eo>
00018 
00019 //-----------------------------------------------------------------------------
00020 // Include the corresponding file
00021 #include <ga.h>          // bitstring representation & operators
00022 // define your individuals
00023 typedef eoBit<double> Indi;     // A bitstring with fitness double
00024 
00025 using namespace std;
00026 
00027 //-----------------------------------------------------------------------------
00032 double binary_value(const Indi & _indi)
00033 {
00034   double sum = 0;
00035   for (unsigned i = 0; i < _indi.size(); i++)
00036     sum += _indi[i];
00037   return sum;
00038 }
00039 
00040 //-----------------------------------------------------------------------------
00041 
00042 void main_function(int argc, char **argv)
00043 {
00044   const unsigned int SEED = 42; // seed for random number generator
00045   const unsigned int VEC_SIZE = 8; // Number of bits in genotypes
00046   const unsigned int POP_SIZE = 20; // Size of population
00047   const unsigned int MAX_GEN = 500; // Maximum number of generation before STOP
00048   const float CROSS_RATE = 0.8; // Crossover rate
00049   const double P_MUT_PER_BIT = 0.01;    // probability of bit-flip mutation
00050   const float MUT_RATE = 1.0;   // mutation rate
00051 
00053   //  Random seed
00055   //reproducible random seed: if you don't change SEED above,
00056   // you'll aways get the same result, NOT a random run
00057   rng.reseed(SEED);
00058 
00060   // Fitness function
00062   // Evaluation: from a plain C++ fn to an EvalFunc Object
00063   eoEvalFuncPtr<Indi> eval(  binary_value );
00064 
00066   // Initilisation of population
00068 
00069   // declare the population
00070   eoPop<Indi> pop;
00071   // fill it!
00072   for (unsigned int igeno=0; igeno<POP_SIZE; igeno++)
00073     {
00074       Indi v;           // void individual, to be filled
00075       for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)
00076         {
00077           bool r = rng.flip(); // new value, random in {0,1}
00078           v.push_back(r);       // append that random value to v
00079         }
00080       eval(v);                  // evaluate it
00081       pop.push_back(v);         // and put it in the population
00082     }
00083 
00084   // sort pop before printing it!
00085   pop.sort();
00086   // Print (sorted) intial population (raw printout)
00087   cout << "Initial Population" << endl;
00088   cout << pop;
00089 
00091   // selection and replacement
00093 
00094   // solution solution solution: uncomment one of the following,
00095   //                             comment out the eoDetTournament lines
00096 
00097   // The well-known roulette
00098   // eoProportionalSelect<Indi> select;
00099 
00100   // could also use stochastic binary tournament selection
00101   //
00102   //  const double RATE = 0.75;
00103   //  eoStochTournamentSelect<Indi> select(RATE);     // RATE in ]0.5,1]
00104   // The robust tournament selection
00105   const unsigned int T_SIZE = 3; // size for tournament selection
00106   eoDetTournamentSelect<Indi> select(T_SIZE);       // T_SIZE in [2,POP_SIZE]
00107 
00108   // and of course the random selection
00109   // eoRandomSelect<Indi> select;
00110 
00111   // The simple GA evolution engine uses generational replacement
00112   // so no replacement procedure is needed
00113 
00115   // termination condition
00117   // stop after MAX_GEN generations
00118   eoGenContinue<Indi> continuator(MAX_GEN);
00119 
00120 
00122   // The variation operators
00124   // standard bit-flip mutation for bitstring
00125   eoBitMutation<Indi>  mutation(P_MUT_PER_BIT);
00126   // 1-point mutation for bitstring
00127   eo1PtBitXover<Indi> xover;
00128 
00130   // the algorithm
00132   // standard Generational GA requires as parameters
00133   // selection, evaluation, crossover and mutation, stopping criterion
00134 
00135 
00136   eoSGA<Indi> gga(select, xover, CROSS_RATE, mutation, MUT_RATE,
00137                    eval, continuator);
00138 
00139   // Apply algo to pop - that's it!
00140   gga(pop);
00141 
00142   // Print (sorted) intial population
00143   pop.sort();
00144   cout << "FINAL Population\n" << pop << endl;
00145 }
00146 
00147 // A main that catches the exceptions
00148 
00149 int main(int argc, char **argv)
00150 {
00151 
00152     try
00153     {
00154         main_function(argc, argv);
00155     }
00156     catch(exception& e)
00157     {
00158         cout << "Exception: " << e.what() << '\n';
00159     }
00160 
00161     return 1;
00162 }

Generated on Thu Oct 19 05:06:39 2006 for EO by  doxygen 1.3.9.1