Back to Lesson 1 - Tutorial main page - Algorithm-Based - Component-Based page - Programming hints - EO documentation

# FirstBitGA and FirstRealGA: differences

Below is a comparison of the codes for both algorithms (comments have been removed).
Warning: the pink background here denotes the differences, not the section of the algorithm, which is only recalled by the color of the text!
These differences are limited to

- the declaration of the type of the genotypes,
- the fitness function (what did you expect :-),
- the initialization (and if you look carefully, you'll find out that only a small part of the initialization is different, as both genotypes are eoFixedLength objects) and of course
- the choice of variation operators (including the parameter for the mutation).

#include <stdexcept > #include <iostream> #include <strstream> #include <eo> | #include <stdexcept > #include <iostream> #include <strstream> #include <eo>
#include <es.h> typedef eoReal<double> Indi; | #include <ga.h> typedef eoBit<double> Indi;
double real_value(const Indi & _indi) { double sum = 0; for (unsigned i = 0; i < _indi.size(); i++) sum += _indi[i]*_indi[i]; return (-sum); // maximizing } | double binary_value(const Indi & _indi) { double sum = 0; for (unsigned i = 0; i < _indi.size(); i++) sum += _indi[i]; return (sum); }
void main_function(int argc, char **argv) { const unsigned int SEED = 42; const unsigned int VEC_SIZE = 8; const unsigned int POP_SIZE = 20; const unsigned int T_SIZE = 3; const unsigned int MAX_GEN = 500; const float CROSS_RATE = 0.8; const float MUT_RATE = 0.5; | void main_function(int argc, char **argv) { const unsigned int SEED = 42; const unsigned int VEC_SIZE = 8; const unsigned int POP_SIZE = 20; const unsigned int T_SIZE = 3; const unsigned int MAX_GEN = 500; const float CROSS_RATE = 0.8; const float MUT_RATE = 0.5;
const double EPSILON = 0.01; | const double P_MUT_PER_BIT = 0.01;
eoEvalFuncPtr<Indi> eval(real_value); | eoEvalFuncPtr<Indi> eval(binary_value);
eoPop<Indi> pop; for (unsigned int igeno=0; igeno<POP_SIZE; igeno++) { Indi v; for (unsigned ivar=0; ivar<VEC_SIZE; ivar++) { | eoPop<Indi> pop; for (unsigned int igeno=0; igeno<POP_SIZE; igeno++) { Indi v; for (unsigned ivar=0; ivar<VEC_SIZE; ivar++) {
double r = 2*rng.uniform() - 1; | bool r = rng.flip();
v.push_back(r); // } eval(v); pop.push_back(v); } pop.sort(); cout << "Initial Population" << endl; cout << pop; eoDetTournament<Indi> select(T_SIZE); eoGenContinue<Indi> continuator(MAX_GEN); | v.push_back(r); // } eval(v); pop.push_back(v); } pop.sort(); cout << "Initial Population" << endl; cout << pop; eoDetTournament<Indi> select(T_SIZE); eoGenContinue<Indi> continuator(MAX_GEN);
eoUniformMutation<Indi> mutation(EPSILON); eoSegmentCrossover<Indi> xover; | eoBitMutation<Indi> mutation(P_MUT_PER_BIT); eo1PtBitXover<Indi> xover;
eoSGA<Indi> gga(select, xover, CROSS_RATE, mutation, MUT_RATE, eval, continuator); gga(pop); pop.sort(); cout << "FINAL Population\n" << pop << endl; } int main(int argc, char **argv) { ... [technical code removed] } | eoSGA<Indi> gga(select, xover, CROSS_RATE, mutation, MUT_RATE, eval, continuator); gga(pop); pop.sort(); cout << "FINAL Population\n" << pop << endl; } int main(int argc, char **argv) { [... technical code removed ] }

Back to Lesson 1 - Tutorial main page - Algorithm-Based - Component-Based page - Programming hints - EO documentation

Last modified: Tue Nov 7 07:49:47 CET 2000