diff --git a/eo/tutorial/Lesson1/FirstBitGA.cpp b/eo/tutorial/Lesson1/FirstBitGA.cpp new file mode 100644 index 00000000..db7dc22c --- /dev/null +++ b/eo/tutorial/Lesson1/FirstBitGA.cpp @@ -0,0 +1,164 @@ +//----------------------------------------------------------------------------- +// FirstBitGA.cpp +//----------------------------------------------------------------------------- +//* +// An instance of a VERY simple Bitstring Genetic Algorithm +// +//----------------------------------------------------------------------------- +// standard includes +#include // runtime_error +#include // cout +#include // ostrstream, istrstream + +// the general include for eo + +#include + +// REPRESENTATION +//----------------------------------------------------------------------------- +// define your individuals +typedef eoBin Indi; // A bitstring with fitness double + +// EVAL +//----------------------------------------------------------------------------- +// a simple fitness function that computes the number of ones of a bitstring +// @param _indi A biststring individual + +double binary_value(const Indi & _indi) +{ + double sum = 0; + for (unsigned i = 0; i < _indi.size(); i++) + sum += _indi[i]; + return sum; +} +// GENERAL +//----------------------------------------------------------------------------- +void main_function(int argc, char **argv) +{ +// PARAMETRES + // all parameters are hard-coded! + const unsigned int SEED = 42; // seed for random number generator + const unsigned int T_SIZE = 3; // size for tournament selection + const unsigned int VEC_SIZE = 8; // Number of bits in genotypes + const unsigned int POP_SIZE = 20; // Size of population + const unsigned int MAX_GEN = 100; // Maximum number of generation before STOP + const float CROSS_RATE = 0.8; // Crossover rate + const double P_MUT_PER_BIT = 0.01; // probability of bit-flip mutation + const float MUT_RATE = 1.0; // mutation rate + +// GENERAL + ////////////////////////// + // Random seed + ////////////////////////// + //reproducible random seed: if you don't change SEED above, + // you'll aways get the same result, NOT a random run + rng.reseed(SEED); + +// EVAL + ///////////////////////////// + // Fitness function + //////////////////////////// + // Evaluation: from a plain C++ fn to an EvalFunc Object + eoEvalFuncPtr eval( binary_value ); + +// INIT + //////////////////////////////// + // Initilisation of population + //////////////////////////////// + + // declare the population + eoPop pop; + // fill it! + for (unsigned int igeno=0; igeno select(T_SIZE); // T_SIZE in [2,POP_SIZE] + +// REPLACE + // The simple GA evolution engine uses generational replacement + // so no replacement procedure is needed + +// OPERATORS + ////////////////////////////////////// + // The variation operators + ////////////////////////////////////// +// CROSSOVER + // 1-point mutation for bitstring + eoBinCrossover xover; +// MUTATION + // standard bit-flip mutation for bitstring + eoBinMutation mutation(P_MUT_PER_BIT); + +// STOP +// CHECKPOINT + ////////////////////////////////////// + // termination condition + ///////////////////////////////////// + // stop after MAX_GEN generations + eoGenContinue continuator(MAX_GEN); + +// GENERATION + ///////////////////////////////////////// + // the algorithm + //////////////////////////////////////// + // standard Generational GA requires as parameters + // selection, evaluation, crossover and mutation, stopping criterion + + + eoSGA gga(select, xover, CROSS_RATE, mutation, MUT_RATE, + eval, continuator); + + // Apply algo to pop - that's it! + gga(pop); + +// OUTPUT + // Print (sorted) intial population + pop.sort(); + cout << "FINAL Population\n" << pop << endl; +// GENERAL +} + // A main that catches the exceptions + +int main(int argc, char **argv) +{ +#ifdef _MSC_VER + // rng.reseed(42); + int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF); + flag |= _CRTDBG_LEAK_CHECK_DF; + _CrtSetDbgFlag(flag); +// _CrtSetBreakAlloc(100); +#endif + + try + { + main_function(argc, argv); + } + catch(exception& e) + { + cout << "Exception: " << e.what() << '\n'; + } + + return 1; +} diff --git a/eo/tutorial/Lesson1/FirstRealGA.cpp b/eo/tutorial/Lesson1/FirstRealGA.cpp new file mode 100644 index 00000000..ff1cadd3 --- /dev/null +++ b/eo/tutorial/Lesson1/FirstRealGA.cpp @@ -0,0 +1,165 @@ +//----------------------------------------------------------------------------- +// FirstRealGA.cpp +//----------------------------------------------------------------------------- +//* +// An instance of a VERY simple Real-coded Genetic Algorithm +// +//----------------------------------------------------------------------------- +// standard includes +#include // runtime_error +#include // cout +#include // ostrstream, istrstream + +// the general include for eo + +#include + +// REPRESENTATION +//----------------------------------------------------------------------------- +// define your individuals + typedef eoReal Indi; + +// EVAL +//----------------------------------------------------------------------------- +// a simple fitness function that computes the euclidian norm of a real vector +// @param _indi A real-valued individual + +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 only +} +// GENERAL +//----------------------------------------------------------------------------- +void main_function(int argc, char **argv) +{ +// PARAMETRES + // all parameters are hard-coded! + const unsigned int SEED = 42; // seed for random number generator + const unsigned int VEC_SIZE = 8; // Number of object variables in genotypes + const unsigned int POP_SIZE = 20; // Size of population + const unsigned int T_SIZE = 3; // size for tournament selection + const unsigned int MAX_GEN = 500; // Maximum number of generation before STOP + const float CROSS_RATE = 0.8; // Crossover rate + const double EPSILON = 0.01; // range for real uniform mutation + const float MUT_RATE = 0.5; // mutation rate + +// GENERAL + ////////////////////////// + // Random seed + ////////////////////////// + //reproducible random seed: if you don't change SEED above, + // you'll aways get the same result, NOT a random run + rng.reseed(SEED); + +// EVAL + ///////////////////////////// + // Fitness function + //////////////////////////// + // Evaluation: from a plain C++ fn to an EvalFunc Object + eoEvalFuncPtr eval( real_value ); + +// INIT + //////////////////////////////// + // Initilisation of population + //////////////////////////////// + + // declare the population + eoPop pop; + // fill it! + for (unsigned int igeno=0; igeno select(T_SIZE); // T_SIZE in [2,POP_SIZE] + +// REPLACE + // eoSGA uses generational replacement by default + // so no replacement procedure has to be given + +// OPERATORS + ////////////////////////////////////// + // The variation operators + ////////////////////////////////////// +// CROSSOVER + // offspring(i) is a linear combination of parent(i) + eoArithmeticCrossover xover; +// MUTATION + // offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon] + eoUniformMutation mutation(EPSILON); + +// STOP +// CHECKPOINT + ////////////////////////////////////// + // termination condition + ///////////////////////////////////// + // stop after MAX_GEN generations + eoGenContinue continuator(MAX_GEN); + +// GENERATION + ///////////////////////////////////////// + // the algorithm + //////////////////////////////////////// + // standard Generational GA requires + // selection, evaluation, crossover and mutation, stopping criterion + + + eoSGA gga(select, xover, CROSS_RATE, mutation, MUT_RATE, + eval, continuator); + + // Apply algo to pop - that's it! + gga(pop); + +// OUTPUT + // Print (sorted) intial population + pop.sort(); + cout << "FINAL Population\n" << pop << endl; +// GENERAL +} + +// A main that catches the exceptions + +int main(int argc, char **argv) +{ +#ifdef _MSC_VER + // rng.reseed(42); + int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF); + flag |= _CRTDBG_LEAK_CHECK_DF; + _CrtSetDbgFlag(flag); +// _CrtSetBreakAlloc(100); +#endif + + try + { + main_function(argc, argv); + } + catch(exception& e) + { + cout << "Exception: " << e.what() << '\n'; + } + + return 1; +} diff --git a/eo/tutorial/Lesson1/Makefile b/eo/tutorial/Lesson1/Makefile new file mode 100644 index 00000000..396873de --- /dev/null +++ b/eo/tutorial/Lesson1/Makefile @@ -0,0 +1,12 @@ +.cpp: ; g++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\" -I. -I../../src -Wall -g -o $@ $*.cpp ../../src/libeo.a ../../src/utils/libeoutils.a + +.cpp.o: ; g++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\" -I. -I../../src -Wall -g -c $*.cpp + +firstGA = FirstRealGA FirstBitGA + +ALL = $(firstGA) exercise3 + +lesson1 : $(firstGA) + +clean : + @/bin/rm $(ALL) *.o *~ diff --git a/eo/tutorial/Lesson1/exercise3.cpp b/eo/tutorial/Lesson1/exercise3.cpp new file mode 100644 index 00000000..64ca5df0 --- /dev/null +++ b/eo/tutorial/Lesson1/exercise3.cpp @@ -0,0 +1,165 @@ +#include // runtime_error + +//----------------------------------------------------------------------------- +// FirstBitGA.cpp +//----------------------------------------------------------------------------- +//* +// An instance of a VERY simple Bitstring Genetic Algorithm +// +//----------------------------------------------------------------------------- +// standard includes + +#include // cout +#include // ostrstream, istrstream + +// the general include for eo + +#include + +//----------------------------------------------------------------------------- +// define your individuals +typedef eoBin Indi; // A bitstring with fitness double + +//----------------------------------------------------------------------------- +/** a simple fitness function that computes the number of ones of a bitstring + @param _indi A biststring individual +*/ + +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; // seed for random number generator + const unsigned int VEC_SIZE = 8; // Number of bits in genotypes + const unsigned int POP_SIZE = 20; // Size of population + const unsigned int MAX_GEN = 500; // Maximum number of generation before STOP + const float CROSS_RATE = 0.8; // Crossover rate + const double P_MUT_PER_BIT = 0.01; // probability of bit-flip mutation + const float MUT_RATE = 1.0; // mutation rate + + ////////////////////////// + // Random seed + ////////////////////////// + //reproducible random seed: if you don't change SEED above, + // you'll aways get the same result, NOT a random run + rng.reseed(SEED); + + ///////////////////////////// + // Fitness function + //////////////////////////// + // Evaluation: from a plain C++ fn to an EvalFunc Object + eoEvalFuncPtr eval( binary_value ); + + //////////////////////////////// + // Initilisation of population + //////////////////////////////// + + // declare the population + eoPop pop; + // fill it! + for (unsigned int igeno=0; igeno select; + + // could also use stochastic binary tournament selection + // + // const double RATE = 0.75; + // eoStochTournament select(RATE); // RATE in ]0.5,1] + // The robust tournament selection + const unsigned int T_SIZE = 3; // size for tournament selection + eoDetTournament select(T_SIZE); // T_SIZE in [2,POP_SIZE] + + // and of course the random selection + // eoSelectRandom select; + + // The simple GA evolution engine uses generational replacement + // so no replacement procedure is needed + + ////////////////////////////////////// + // termination condition + ///////////////////////////////////// + // stop after MAX_GEN generations + eoGenContinue continuator(MAX_GEN); + + + ////////////////////////////////////// + // The variation operators + ////////////////////////////////////// + // standard bit-flip mutation for bitstring + eoBinMutation mutation(P_MUT_PER_BIT); + // 1-point mutation for bitstring + eoBinCrossover xover; + + ///////////////////////////////////////// + // the algorithm + //////////////////////////////////////// + // standard Generational GA requires as parameters + // selection, evaluation, crossover and mutation, stopping criterion + + + eoSGA gga(select, xover, CROSS_RATE, mutation, MUT_RATE, + eval, continuator); + + // Apply algo to pop - that's it! + gga(pop); + + // Print (sorted) intial population + pop.sort(); + cout << "FINAL Population\n" << pop << endl; +} + +// A main that catches the exceptions + +int main(int argc, char **argv) +{ +#ifdef _MSC_VER + // rng.reseed(42); + int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF); + flag |= _CRTDBG_LEAK_CHECK_DF; + _CrtSetDbgFlag(flag); +// _CrtSetBreakAlloc(100); +#endif + + try + { + main_function(argc, argv); + } + catch(exception& e) + { + cout << "Exception: " << e.what() << '\n'; + } + + return 1; +} diff --git a/eo/tutorial/Lesson2/FirstBitEA.cpp b/eo/tutorial/Lesson2/FirstBitEA.cpp new file mode 100644 index 00000000..0d82a78f --- /dev/null +++ b/eo/tutorial/Lesson2/FirstBitEA.cpp @@ -0,0 +1,193 @@ +//----------------------------------------------------------------------------- +// FirstBitEA.cpp +//----------------------------------------------------------------------------- +//* +// Still an instance of a VERY simple Bitstring Genetic Algorithm +// (see FirstBitGA.cpp) but now with Breeder - and Combined Ops +// +//----------------------------------------------------------------------------- +// standard includes +#include // runtime_error +#include // cout +#include // ostrstream, istrstream + +// the general include for eo +#include + +// REPRESENTATION +//----------------------------------------------------------------------------- +// define your individuals +typedef eoBin Indi; // A bitstring with fitness double + +// EVALFUNC +//----------------------------------------------------------------------------- +// a simple fitness function that computes the number of ones of a bitstring +// Now in a separate file, and declared as binary_value(const vector &) + +#include "binary_value.h" + +// GENERAL +//----------------------------------------------------------------------------- + +void main_function(int argc, char **argv) +{ +// PARAMETRES + const unsigned int SEED = 42; // seed for random number generator + const unsigned int T_SIZE = 3; // size for tournament selection + const unsigned int VEC_SIZE = 8; // Number of bits in genotypes + const unsigned int POP_SIZE = 20; // Size of population + + const unsigned int MAX_GEN = 500; // Maximum number of generation before STOP + const unsigned int MIN_GEN = 10; // Minimum number of generation before ... + const unsigned int STEADY_GEN = 50; // stop after STEADY_GEN gen. without improvelent + + const double P_CROSS = 0.8; // Crossover probability + const double P_MUT = 1.0; // mutation probability + + const double P_MUT_PER_BIT = 0.01; // internal probability for bit-flip mutation + // some parameters for chosing among different operators + const double onePointRate = 0.5; // rate for 1-pt Xover + const double twoPointsRate = 0.5; // rate for 2-pt Xover + const double URate = 0.5; // rate for Uniform Xover + const double bitFlipRate = 0.5; // rate for bit-flip mutation + const double oneBitRate = 0.5; // rate for one-bit mutation + +// GENERAL + ////////////////////////// + // Random seed + ////////////////////////// + //reproducible random seed: if you don't change SEED above, + // you'll aways get the same result, NOT a random run + rng.reseed(SEED); + +// EVAL + ///////////////////////////// + // Fitness function + //////////////////////////// + // Evaluation: from a plain C++ fn to an EvalFunc Object + // you need to give the full description of the function + eoEvalFuncPtr& > eval( binary_value ); + +// INIT + //////////////////////////////// + // Initilisation of population + //////////////////////////////// + + // based on boolean_generator class (see utils/rnd_generator.h) + eoInitFixedLength + random(VEC_SIZE, boolean_generator()); + // Initialization of the population + eoPop pop(POP_SIZE, random); + + // and evaluate it in one loop + apply(eval, pop); // STL syntax + +// OUTPUT + // sort pop before printing it! + pop.sort(); + // Print (sorted) intial population (raw printout) + cout << "Initial Population" << endl; + cout << pop; + +// ENGINE + ///////////////////////////////////// + // selection and replacement + //////////////////////////////////// +// SELECT + // The robust tournament selection + eoDetTournament selectOne(T_SIZE); // T_SIZE in [2,POP_SIZE] + // is now encapsulated in a eoSelectPerc (entage) + eoSelectPerc select(selectOne);// by default rate==1 + +// REPLACE + // And we now have the full slection/replacement - though with + // no replacement (== generational replacement) at the moment :-) + eoNoReplacement replace; + +// OPERATORS + ////////////////////////////////////// + // The variation operators + ////////////////////////////////////// +// CROSSOVER + // 1-point crossover for bitstring + eoBinCrossover xover1; + // uniform crossover for bitstring + eoBinUxOver xoverU; + // 2-pots xover + eoBinNxOver xover2(2); + // Combine them with relative rates + eoPropCombinedQuadOp xover(xover1, onePointRate); + xover.add(xoverU, URate); + xover.add(xover2, twoPointsRate, true); + +// MUTATION + // standard bit-flip mutation for bitstring + eoBinMutation mutationBitFlip(P_MUT_PER_BIT); + // mutate exactly 1 bit per individual + eoDetBitFlip mutationOneBit; + // Combine them with relative rates + eoPropCombinedMonOp mutation(mutationBitFlip, bitFlipRate); + mutation.add(mutationOneBit, oneBitRate, true); + + // The operators are encapsulated into an eoTRansform object + eoSGATransform transform(xover, P_CROSS, mutation, P_MUT); + +// STOP +// CHECKPOINT + ////////////////////////////////////// + // termination conditions: use more than one + ///////////////////////////////////// + // stop after MAX_GEN generations + eoGenContinue genCont(MAX_GEN); + // do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement + eoSteadyFitContinue steadyCont(MIN_GEN, STEADY_GEN); + // stop when fitness reaches a target (here VEC_SIZE) + eoFitContinue fitCont(VEC_SIZE); + // do stop when one of the above says so + eoCombinedContinue continuator(genCont); + continuator.add(steadyCont); + continuator.add(fitCont); + +// GENERATION + ///////////////////////////////////////// + // the algorithm + //////////////////////////////////////// + + // Easy EA requires + // selection, transformation, eval, replacement, and stopping criterion + eoEasyEA gga(continuator, eval, select, transform, replace); + + // Apply algo to pop - that's it! + cout << "\n Here we go\n\n"; + gga(pop); + +// OUTPUT + // Print (sorted) intial population + pop.sort(); + cout << "FINAL Population\n" << pop << endl; +// GENERAL +} + +// A main that catches the exceptions + +int main(int argc, char **argv) +{ +#ifdef _MSC_VER + // rng.reseed(42); + int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF); + flag |= _CRTDBG_LEAK_CHECK_DF; + _CrtSetDbgFlag(flag); +// _CrtSetBreakAlloc(100); +#endif + + try + { + main_function(argc, argv); + } + catch(exception& e) + { + cout << "Exception: " << e.what() << '\n'; + } + + return 1; +} diff --git a/eo/tutorial/Lesson2/FirstRealEA.cpp b/eo/tutorial/Lesson2/FirstRealEA.cpp new file mode 100644 index 00000000..1d697977 --- /dev/null +++ b/eo/tutorial/Lesson2/FirstRealEA.cpp @@ -0,0 +1,188 @@ +//----------------------------------------------------------------------------- +// FirstRealEA.cpp +//----------------------------------------------------------------------------- +//* +// Still an instance of a VERY simple Real-coded Genetic Algorithm +// (see FirstBitGA.cpp) but now with Breeder - and Combined Ops +// +//----------------------------------------------------------------------------- +// standard includes +#include // runtime_error +#include // cout +#include // ostrstream, istrstream + +// the general include for eo +#include + +// REPRESENTATION +//----------------------------------------------------------------------------- +// define your individuals +typedef eoReal Indi; + +// EVALFUNC +//----------------------------------------------------------------------------- +// a simple fitness function that computes the euclidian norm of a real vector +// Now in a separate file, and declared as binary_value(const vector &) + +#include "real_value.h" + +// GENERAL +//----------------------------------------------------------------------------- + +void main_function(int argc, char **argv) +{ +// PARAMETRES + const unsigned int SEED = 42; // seed for random number generator + const unsigned int T_SIZE = 3; // size for tournament selection + const unsigned int VEC_SIZE = 8; // Number of object variables in genotypes + const unsigned int POP_SIZE = 20; // Size of population + + const unsigned int MAX_GEN = 500; // Maximum number of generation before STOP + const unsigned int MIN_GEN = 10; // Minimum number of generation before ... + const unsigned int STEADY_GEN = 50; // stop after STEADY_GEN gen. without improvelent + + const float P_CROSS = 0.8; // Crossover probability + const float P_MUT = 0.5; // mutation probability + + const double EPSILON = 0.01; // range for real uniform mutation + // some parameters for chosing among different operators + const double segmentRate = 0.5; // rate for 1-pt Xover + const double arithmeticRate = 0.5; // rate for 2-pt Xover + const double uniformMutRate = 0.5; // rate for bit-flip mutation + const double detMutRate = 0.5; // rate for one-bit mutation + +// GENERAL + ////////////////////////// + // Random seed + ////////////////////////// + //reproducible random seed: if you don't change SEED above, + // you'll aways get the same result, NOT a random run + rng.reseed(SEED); + +// EVAL + ///////////////////////////// + // Fitness function + //////////////////////////// + // Evaluation: from a plain C++ fn to an EvalFunc Object + // you need to give the full description of the function + eoEvalFuncPtr& > eval( real_value ); + +// INIT + //////////////////////////////// + // Initilisation of population + //////////////////////////////// + // based on a uniform generator + eoInitFixedLength > + random(VEC_SIZE, uniform_generator(-1.0, 1.0)); + // Initialization of the population + eoPop pop(POP_SIZE, random); + + // and evaluate it in one loop + apply(eval, pop); // STL syntax + +// OUTPUT + // sort pop before printing it! + pop.sort(); + // Print (sorted) intial population (raw printout) + cout << "Initial Population" << endl; + cout << pop; + +// ENGINE + ///////////////////////////////////// + // selection and replacement + //////////////////////////////////// +// SELECT + // The robust tournament selection + eoDetTournament selectOne(T_SIZE); + // is now encapsulated in a eoSelectPerc (entage) + eoSelectPerc select(selectOne);// by default rate==1 + +// REPLACE + // And we now have the full slection/replacement - though with + // no replacement (== generational replacement) at the moment :-) + eoNoReplacement replace; + +// OPERATORS + ////////////////////////////////////// + // The variation operators + ////////////////////////////////////// +// CROSSOVER + // uniform chooce on segment made by the parents + eoSegmentCrossover xoverS; + // uniform choice in hypercube built by the parents + eoArithmeticCrossover xoverA; + // Combine them with relative rates + eoPropCombinedQuadOp xover(xoverS, segmentRate); + xover.add(xoverA, arithmeticRate, true); + +// MUTATION + // offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon] + eoUniformMutation mutationU(EPSILON); + // k (=1) coordinates of parents are uniformly modified + eoDetUniformMutation mutationD(EPSILON); + // Combine them with relative rates + eoPropCombinedMonOp mutation(mutationU, uniformMutRate); + mutation.add(mutationD, detMutRate, true); + +// STOP +// CHECKPOINT + ////////////////////////////////////// + // termination conditions: use more than one + ///////////////////////////////////// + // stop after MAX_GEN generations + eoGenContinue genCont(MAX_GEN); + // do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement + eoSteadyFitContinue steadyCont(MIN_GEN, STEADY_GEN); + // stop when fitness reaches a target (here VEC_SIZE) + eoFitContinue fitCont(0); + // do stop when one of the above says so + eoCombinedContinue continuator(genCont); + continuator.add(steadyCont); + continuator.add(fitCont); + + // The operators are encapsulated into an eoTRansform object + eoSGATransform transform(xover, P_CROSS, mutation, P_MUT); + +// GENERATION + ///////////////////////////////////////// + // the algorithm + //////////////////////////////////////// + + // Easy EA requires + // selection, transformation, eval, replacement, and stopping criterion + eoEasyEA gga(continuator, eval, select, transform, replace); + + // Apply algo to pop - that's it! + cout << "\n Here we go\n\n"; + gga(pop); + +// OUTPUT + // Print (sorted) intial population + pop.sort(); + cout << "FINAL Population\n" << pop << endl; +// GENERAL +} + +// A main that catches the exceptions + +int main(int argc, char **argv) +{ +#ifdef _MSC_VER + // rng.reseed(42); + int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF); + flag |= _CRTDBG_LEAK_CHECK_DF; + _CrtSetDbgFlag(flag); +// _CrtSetBreakAlloc(100); +#endif + + try + { + main_function(argc, argv); + } + catch(exception& e) + { + cout << "Exception: " << e.what() << '\n'; + } + + return 1; +} diff --git a/eo/tutorial/Lesson2/Makefile b/eo/tutorial/Lesson2/Makefile new file mode 100644 index 00000000..75b3a2ec --- /dev/null +++ b/eo/tutorial/Lesson2/Makefile @@ -0,0 +1,12 @@ +.cpp: ; g++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\" -I. -I../../src -Wall -g -o $@ $*.cpp ../../src/libeo.a ../../src/utils/libeoutils.a + +.cpp.o: ; g++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\" -I. -I../../src -Wall -g -c $*.cpp + +firstEA = FirstRealEA FirstBitEA + +ALL = $(firstEA) exercise1 exercise2 exercise3 + +lesson2 : $(firstEA) + +clean : + @/bin/rm $(ALL) *.o *~ diff --git a/eo/tutorial/Lesson2/binary_value.h b/eo/tutorial/Lesson2/binary_value.h new file mode 100644 index 00000000..a31c0864 --- /dev/null +++ b/eo/tutorial/Lesson2/binary_value.h @@ -0,0 +1,17 @@ +#include + +//----------------------------------------------------------------------------- + +/** Just a simple function that takes binary value of a chromosome and sets + the fitnes. + @param _chrom A binary chromosome +*/ +// INIT +double binary_value(const vector& _chrom) +{ + double sum = 0; + for (unsigned i = 0; i < _chrom.size(); i++) + sum += _chrom[i]; + return sum; +} + diff --git a/eo/tutorial/Lesson2/exercise3.cpp b/eo/tutorial/Lesson2/exercise3.cpp new file mode 100644 index 00000000..c8e7ab6c --- /dev/null +++ b/eo/tutorial/Lesson2/exercise3.cpp @@ -0,0 +1,195 @@ +//----------------------------------------------------------------------------- +// FirstBitEA.cpp +//----------------------------------------------------------------------------- +//* +// Still an instance of a VERY simple Bitstring Genetic Algorithm +// (see FirstBitGA.cpp) but now with Breeder - and Combined Ops +// +//----------------------------------------------------------------------------- +// standard includes +#include // runtime_error +#include // cout +#include // ostrstream, istrstream + +// the general include for eo +#include + +// REPRESENTATION +//----------------------------------------------------------------------------- +// define your individuals +typedef eoBin Indi; // A bitstring with fitness double + +// EVAL +//----------------------------------------------------------------------------- +// a simple fitness function that computes the number of ones of a bitstring +// Now in a separate file, and declared as binary_value(const vector &) + +#include "binary_value.h" + +// GENERAL +//----------------------------------------------------------------------------- + +void main_function(int argc, char **argv) +{ +// PARAMETRES + const unsigned int SEED = 42; // seed for random number generator + const unsigned int T_SIZE = 3; // size for tournament selection + const unsigned int VEC_SIZE = 8; // Number of bits in genotypes + const unsigned int POP_SIZE = 20; // Size of population + + const unsigned int MAX_GEN = 500; // Maximum number of generation before STOP + const unsigned int MIN_GEN = 10; // Minimum number of generation before ... + const unsigned int STEADY_GEN = 50; // stop after STEADY_GEN gen. without improvelent + + const double P_CROSS = 0.8; // Crossover probability + const double P_MUT = 1.0; // mutation probability + + const double P_MUT_PER_BIT = 0.01; // internal probability for bit-flip mutation + // some parameters for chosing among different operators + const double onePointRate = 0.5; // rate for 1-pt Xover + const double twoPointsRate = 0.5; // rate for 2-pt Xover + const double URate = 0.5; // rate for Uniform Xover + const double bitFlipRate = 0.5; // rate for bit-flip mutation + const double oneBitRate = 0.5; // rate for one-bit mutation + +// GENERAL + ////////////////////////// + // Random seed + ////////////////////////// + //reproducible random seed: if you don't change SEED above, + // you'll aways get the same result, NOT a random run + rng.reseed(SEED); + +// EVAL + ///////////////////////////// + // Fitness function + //////////////////////////// + // Evaluation: from a plain C++ fn to an EvalFunc Object + // you need to give the full description of the function + eoEvalFuncPtr& > eval( binary_value ); + +// INIT + //////////////////////////////// + // Initilisation of population + //////////////////////////////// + + // based on boolean_generator class (see utils/rnd_generator.h) + eoInitFixedLength + random(VEC_SIZE, boolean_generator()); + // Initialization of the population + eoPop pop(POP_SIZE, random); + + // and evaluate it in one loop + apply(eval, pop); // STL syntax + +// OUTPUT + // sort pop before printing it! + pop.sort(); + // Print (sorted) intial population (raw printout) + cout << "Initial Population" << endl; + cout << pop; + +// ENGINE + ///////////////////////////////////// + // selection and replacement + //////////////////////////////////// +// SELECT + // The robust tournament selection + eoDetTournament selectOne(T_SIZE); // T_SIZE in [2,POP_SIZE] + // solution solution solution solution solution solution solution + // modify the rate in the constructor + eoSelectPerc select(selectOne,2.0);// rate is second arg. + +// REPLACE + // solution solution solution solution solution solution solution + // eoCommaReplacement keeps the best among offspring + // eoPlusReplacement keeps the best among parents + offspring + // eoCommaReplacement replace; + eoPlusReplacement replace; + +// OPERATORS + ////////////////////////////////////// + // The variation operators + ////////////////////////////////////// +// CROSSOVER + // 1-point crossover for bitstring + eoBinCrossover xover1; + // uniform crossover for bitstring + eoBinUxOver xoverU; + // 2-pots xover + eoBinNxOver xover2(2); + // Combine them with relative rates + eoPropCombinedQuadOp xover(xover1, onePointRate); + xover.add(xoverU, URate); + xover.add(xover2, twoPointsRate, true); + +// MUTATION + // standard bit-flip mutation for bitstring + eoBinMutation mutationBitFlip(P_MUT_PER_BIT); + // mutate exactly 1 bit per individual + eoDetBitFlip mutationOneBit; + // Combine them with relative rates + eoPropCombinedMonOp mutation(mutationBitFlip, bitFlipRate); + mutation.add(mutationOneBit, oneBitRate, true); + + // The operators are encapsulated into an eoTRansform object + eoSGATransform transform(xover, P_CROSS, mutation, P_MUT); + +// STOP +// CHECKPOINT + ////////////////////////////////////// + // termination conditions: use more than one + ///////////////////////////////////// + // stop after MAX_GEN generations + eoGenContinue genCont(MAX_GEN); + // do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement + eoSteadyFitContinue steadyCont(MIN_GEN, STEADY_GEN); + // stop when fitness reaches a target (here VEC_SIZE) + eoFitContinue fitCont(VEC_SIZE); + // do stop when one of the above says so + eoCombinedContinue continuator(genCont); + continuator.add(steadyCont); + continuator.add(fitCont); + +// GENERATION + ///////////////////////////////////////// + // the algorithm + //////////////////////////////////////// + + // Easy EA requires + // selection, transformation, eval, replacement, and stopping criterion + eoEasyEA gga(continuator, eval, select, transform, replace); + + // Apply algo to pop - that's it! + gga(pop); + +// OUTPUT + // Print (sorted) intial population + pop.sort(); + cout << "FINAL Population\n" << pop << endl; +// GENERAL +} + +// A main that catches the exceptions + +int main(int argc, char **argv) +{ +#ifdef _MSC_VER + // rng.reseed(42); + int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF); + flag |= _CRTDBG_LEAK_CHECK_DF; + _CrtSetDbgFlag(flag); +// _CrtSetBreakAlloc(100); +#endif + + try + { + main_function(argc, argv); + } + catch(exception& e) + { + cout << "Exception: " << e.what() << '\n'; + } + + return 1; +} diff --git a/eo/tutorial/Lesson2/real_value.h b/eo/tutorial/Lesson2/real_value.h new file mode 100644 index 00000000..17d96ede --- /dev/null +++ b/eo/tutorial/Lesson2/real_value.h @@ -0,0 +1,18 @@ +#include +//----------------------------------------------------------------------------- +/** Just a simple function that takes an vector and sets the fitnes + to the sphere function. Please use doubles not float!!! + @param _ind A floatingpoint vector +*/ + +// INIT +double real_value(const std::vector& _ind) +{ + double sum = 0; + for (unsigned i = 0; i < _ind.size(); i++) + sum += _ind[i] * _ind[i]; + return -sum; +} + + + diff --git a/eo/tutorial/Lesson3/Makefile b/eo/tutorial/Lesson3/Makefile new file mode 100644 index 00000000..c4d3ec8e --- /dev/null +++ b/eo/tutorial/Lesson3/Makefile @@ -0,0 +1,12 @@ +.cpp: ; g++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\" -I. -I../../src -Wall -g -o $@ $*.cpp ../../src/libeo.a ../../src/utils/libeoutils.a + +.cpp.o: ; g++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\" -I. -I../../src -Wall -g -c $*.cpp + +secondEA = SecondBitEA + +ALL = $(secondEA) exercise1 exercise2 exercise3 + +lesson3 : $(secondEA) + +clean : + @/bin/rm $(ALL) *.o *.sav *.xg *.status *~ diff --git a/eo/tutorial/Lesson3/SecondBitEA.cpp b/eo/tutorial/Lesson3/SecondBitEA.cpp new file mode 100644 index 00000000..457346a2 --- /dev/null +++ b/eo/tutorial/Lesson3/SecondBitEA.cpp @@ -0,0 +1,375 @@ +//----------------------------------------------------------------------------- +// SecondGA.cpp +//----------------------------------------------------------------------------- +//* +// Same code than FirstBitEA as far as Evolutionary Computation is concerned +// but now you learn to enter the parameters in a more flexible way +// and to twidle the output to your preferences! +//----------------------------------------------------------------------------- +// standard includes +#include // runtime_error +#include // cout +#include // ostrstream, istrstream +#include + +// the general include for eo +#include + +// EVAL +#include "binary_value.h" + +// REPRESENTATION +//----------------------------------------------------------------------------- +// define your genotype and fitness types +typedef eoBin Indi; + +// PARAMETRES +//----------------------------------------------------------------------------- +// instead of having all values of useful parameters as constants, read them: +// either on the command line (--option=value or -o=value) +// or in a parameter file (same syntax, order independent, +// # = usual comment character +// or in the environment (TODO) + +// note that the parameters are passed by reference so they can be updated +void read_param(int argc, char *argv[], + uint32 & _seed, + unsigned int & _vecSize, + unsigned int & _popSize, + unsigned int & _tSize, + double & _pCross, + double & _pMut, + string & _load_name, + unsigned int & _maxGen, + unsigned int & _minGen, + unsigned int & _steadyGen, + double & _onePointRate, + double & _twoPointsRate, + double & _uRate, + double & _pMutPerBit, + double & _bitFlipRate, + double & _oneBitRate + ) +{ + // define a parser from the command-line arguments + eoParser parser(argc, argv); + + // For each parameter, define Parameters directly in the parser, + // and assign the value to the variable + eoValueParam& seedParam = parser.createParam(time(0), "seed", "Random number seed", 'S'); + _seed = seedParam.value(); + + eoValueParam& vecSizeParam = parser.createParam(8, "vecSize", "Genotype size",'V', "Representation"); + _vecSize = vecSizeParam.value(); + + eoValueParam& popSizeParam = parser.createParam(10, "popSize", "Population size",'P', "Evolution"); + _popSize = popSizeParam.value(); + + eoValueParam& tSizeParam = parser.createParam(10, "tSize", "Tournament size",'T', "Evolution"); + _tSize = tSizeParam.value(); + + eoValueParam& load_nameParam = parser.createParam("", "Load","A save file to restart from",'L', "Persistence"); + _load_name = load_nameParam.value(); + + eoValueParam& maxGenParam = parser.createParam(100, "maxGen", "Maximum number of generations",'G', "Stopping criterion"); + _maxGen = maxGenParam.value(); + + eoValueParam& minGenParam = parser.createParam(100, "minGen", "Minimum number of generations",'g', "Stopping criterion"); + _minGen = minGenParam.value(); + + eoValueParam& steadyGenParam = parser.createParam(100, "steadyGen", "Number of generations with no improvement",'s', "Stopping criterion"); + _steadyGen = steadyGenParam.value(); + + eoValueParam& pCrossParam = parser.createParam(0.6, "pCross", "Probability of Crossover", 'C', "Genetic Operators"); + _pCross = pCrossParam.value(); + + eoValueParam& pMutParam = parser.createParam(0.1, "pMut", "Probability of Mutation", 'M', "Genetic Operators"); + _pMut = pMutParam.value(); + + eoValueParam& onePointRateParam = parser.createParam(1, "onePointRate", "Relative rate for one point crossover", '1', "Genetic Operators"); + _onePointRate = onePointRateParam.value(); + + eoValueParam& twoPointsRateParam = parser.createParam(1, "twoPointRate", "Relative rate for two point crossover", '2', "Genetic Operators"); + _twoPointsRate = twoPointsRateParam.value(); + + eoValueParam& uRateParam = parser.createParam(2, "uRate", "Relative rate for uniform crossover", 'U', "Genetic Operators"); + _uRate = uRateParam.value(); + + eoValueParam& pMutPerBitParam = parser.createParam(0.01, "pMutPerBit", "Probability of flipping 1 bit in bit-flip mutation", 'b', "Genetic Operators"); + _pMutPerBit = pMutPerBitParam.value(); + + eoValueParam& bitFlipRateParam = parser.createParam(0.01, "bitFlipRate", "Relative rate for bit-flip mutation", 'B', "Genetic Operators"); + _bitFlipRate = bitFlipRateParam.value(); + + eoValueParam& oneBitRateParam = parser.createParam(0.01, "oneBitRate", "Relative rate for deterministic bit-flip mutation", 'D', "Genetic Operators"); + _oneBitRate = oneBitRateParam.value(); + + // the name of the "status" file where all actual parameter values will be saved + string str_status = parser.ProgramName() + ".status"; + eoValueParam& status_nameParam = parser.createParam(str_status.c_str(), "status","Status file",'S', "Persistence"); + + // do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED + // i.e. in case you need parameters somewhere else, postpone these + if (parser.userNeedsHelp()) + { + parser.printHelp(cout); + exit(1); + } + if (status_nameParam.value() != "") + { + ofstream os(status_nameParam.value().c_str()); + os << parser; // and you can use that file as parameter file + } +} + +// GENERAL +// now the main_function: nothing changed, except input/output +void main_function(int argc, char **argv) +{ +// PARAMETRES + uint32 seed; + // decription of genotype + unsigned int vecSize; + // parameters for evolution engine + unsigned int popSize; + unsigned int tSize; + // operators probabilities at the algorithm level + double pCross; + double pMut; + // init and stop + string load_name; + unsigned int maxGen; + unsigned int minGen; + unsigned int steadyGen; + // rates for crossovers + double onePointRate; + double twoPointsRate; + double URate; + // rates and private parameters for mutations; + double pMutPerBit; + double bitFlipRate; + double oneBitRate; + + // Now read the parameters of the program + read_param(argc, argv, seed, vecSize, popSize, tSize, + pCross, pMut, load_name, maxGen, minGen, steadyGen, + onePointRate, twoPointsRate, URate, + pMutPerBit, bitFlipRate, oneBitRate ); + +// EVAL + ///////////////////////////// + // Fitness function + //////////////////////////// + // Evaluation: from a plain C++ fn to an EvalFunc Object ... + eoEvalFuncPtr& > plainEval( binary_value ); + // ... to an object that counts the nb of actual evaluations + eoEvalFuncCounter eval(plainEval); + +// INIT + //////////////////////////////// + // Initilisation of population + //////////////////////////////// + // Either load or initialize + // create an empty pop + eoPop pop; + // create a state for reading + eoState inState; // a state for loading - WITHOUT the parser + // register the rng and the pop in the state, so they can be loaded, + // and the present run will be the exact conitnuation of the saved run + // eventually with different parameters + inState.registerObject(rng); + inState.registerObject(pop); + + if (load_name != "") + { + inState.load(load_name); // load the pop and the rng + // the fitness is read in the file: + // do only evaluate the pop if the fitness has changed + } + else + { + rng.reseed(seed); + // a Indi random initializer + // based on boolean_generator class (see utils/rnd_generator.h) + eoInitFixedLength + random(vecSize, boolean_generator()); + + // Init pop from the randomizer: need to use the append function + pop.append(popSize, random); + // and evaluate pop (STL syntax) + apply(eval, pop); + } // end of initializatio of the population + +// OUTPUT + // sort pop for pretty printout + pop.sort(); + // Print (sorted) intial population (raw printout) + cout << "Initial Population" << endl << pop << endl; + +// ENGINE + ///////////////////////////////////// + // selection and replacement + //////////////////////////////////// +// SELECT + // The robust tournament selection + eoDetTournament selectOne(tSize); // tSize in [2,POPSIZE] + // is now encapsulated in a eoSelectPerc (entage) + eoSelectPerc select(selectOne);// by default rate==1 + +// REPLACE + // And we now have the full slection/replacement - though with + // no replacement (== generational replacement) at the moment :-) + eoNoReplacement replace; + +// OPERATORS + ////////////////////////////////////// + // The variation operators + ////////////////////////////////////// +// CROSSOVER + // 1-point crossover for bitstring + eoBinCrossover xover1; + // uniform crossover for bitstring + eoBinUxOver xoverU; + // 2-pots xover + eoBinNxOver xover2(2); + // Combine them with relative rates + eoPropCombinedQuadOp xover(xover1, onePointRate); + xover.add(xoverU, URate); + xover.add(xover2, twoPointsRate, true); + +// MUTATION + // standard bit-flip mutation for bitstring + eoBinMutation mutationBitFlip(pMutPerBit); + // mutate exactly 1 bit per individual + eoDetBitFlip mutationOneBit; + // Combine them with relative rates + eoPropCombinedMonOp mutation(mutationBitFlip, bitFlipRate); + mutation.add(mutationOneBit, oneBitRate, true); + + // The operators are encapsulated into an eoTRansform object + eoSGATransform transform(xover, pCross, mutation, pMut); + +// STOP + ////////////////////////////////////// + // termination condition see FirstBitEA.cpp + ///////////////////////////////////// + eoGenContinue genCont(maxGen); + eoSteadyFitContinue steadyCont(minGen, steadyGen); + eoFitContinue fitCont(vecSize); + eoCombinedContinue continuator(genCont); + continuator.add(steadyCont); + continuator.add(fitCont); + + +// CHECKPOINT + // but now you want to make many different things every generation + // (e.g. statistics, plots, ...). + // the class eoCheckPoint is dedicated to just that: + + // Declare a checkpoint (from a continuator: an eoCheckPoint + // IS AN eoContinue and will be called in the loop of all algorithms) + eoCheckPoint checkpoint(continuator); + + // Create a counter parameter + eoValueParam generationCounter(0, "Gen."); + + // Create an incrementor (sub-class of eoUpdater). Note that the + // parameter's value is passed by reference, + // so every time the incrementer is updated (every generation), + // the data in generationCounter will change. + eoIncrementor increment(generationCounter.value()); + + // Add it to the checkpoint, + // so the counter is updated (here, incremented) every generation + checkpoint.add(increment); + + // now some statistics on the population: + // Best fitness in population + eoBestFitnessStat bestStat; + // Second moment stats: average and stdev + eoSecondMomentStats SecondStat; + + // Add them to the checkpoint to get them called at the appropriate time + checkpoint.add(bestStat); + checkpoint.add(SecondStat); + + // The Stdout monitor will print parameters to the screen ... + eoStdoutMonitor monitor(false); + + // when called by the checkpoint (i.e. at every generation) + checkpoint.add(monitor); + + // the monitor will output a series of parameters: add them + monitor.add(generationCounter); + monitor.add(eval); // because now eval is an eoEvalFuncCounter! + monitor.add(bestStat); + monitor.add(SecondStat); + + // A file monitor: will print parameters to ... a File, yes, you got it! + eoFileMonitor fileMonitor("stats.xg", " "); + + // the checkpoint mechanism can handle multiple monitors + checkpoint.add(fileMonitor); + + // the fileMonitor can monitor parameters, too, but you must tell it! + fileMonitor.add(generationCounter); + fileMonitor.add(bestStat); + fileMonitor.add(SecondStat); + + // Last type of item the eoCheckpoint can handle: state savers: + eoState outState; + // Register the algorithm into the state (so it has something to save!!) + outState.registerObject(rng); + outState.registerObject(pop); + + // and feed the state to state savers + // save state every 100th generation + eoCountedStateSaver stateSaver1(100, outState, "generation"); + // save state every 1 seconds + eoTimedStateSaver stateSaver2(1, outState, "time"); + + // Don't forget to add the two savers to the checkpoint + checkpoint.add(stateSaver1); + checkpoint.add(stateSaver2); + // and that's it for the (control and) output + +// GENERATION + ///////////////////////////////////////// + // the algorithm + //////////////////////////////////////// + + // Easy EA requires + // selection, transformation, eval, replacement, and stopping criterion + eoEasyEA gga(checkpoint, eval, select, transform, replace); + + // Apply algo to pop - that's it! + gga(pop); + +// OUTPUT + // Print (sorted) intial population + pop.sort(); + cout << "FINAL Population\n" << pop << endl; +// GENERAL +} + +// A main that catches the exceptions +int main(int argc, char **argv) +{ +#ifdef _MSC_VER + int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF); + flag |= _CRTDBG_LEAK_CHECK_DF; + _CrtSetDbgFlag(flag); +// _CrtSetBreakAlloc(100); +#endif + + try + { + main_function(argc, argv); + } + catch(exception& e) + { + cout << "Exception: " << e.what() << '\n'; + } + + return 1; +} diff --git a/eo/tutorial/Lesson3/binary_value.h b/eo/tutorial/Lesson3/binary_value.h new file mode 100644 index 00000000..a31c0864 --- /dev/null +++ b/eo/tutorial/Lesson3/binary_value.h @@ -0,0 +1,17 @@ +#include + +//----------------------------------------------------------------------------- + +/** Just a simple function that takes binary value of a chromosome and sets + the fitnes. + @param _chrom A binary chromosome +*/ +// INIT +double binary_value(const vector& _chrom) +{ + double sum = 0; + for (unsigned i = 0; i < _chrom.size(); i++) + sum += _chrom[i]; + return sum; +} + diff --git a/eo/tutorial/Lesson3/exercise1.cpp b/eo/tutorial/Lesson3/exercise1.cpp new file mode 100644 index 00000000..98439530 --- /dev/null +++ b/eo/tutorial/Lesson3/exercise1.cpp @@ -0,0 +1,384 @@ +//----------------------------------------------------------------------------- +// SecondBitGA.cpp +//----------------------------------------------------------------------------- +//* +// Same code than FirstBitEA as far as Evolutionary Computation is concerned +// but now you learn to enter the parameters in a more flexible way +// and to twidle the output to your preferences! +//----------------------------------------------------------------------------- +// standard includes +#include // runtime_error +#include // cout +#include // ostrstream, istrstream +#include + +// the general include for eo +#include +#include + +// EVAL +#include "binary_value.h" + +// REPRESENTATION +//----------------------------------------------------------------------------- +// define your genotype and fitness types +typedef eoBin Indi; + +// PARAMETRES +//----------------------------------------------------------------------------- +// instead of having all values of useful parameters as constants, read them: +// either on the command line (--option=value or -o=value) +// or in a parameter file (same syntax, order independent, +// # = usual comment character +// or in the environment (TODO) + +// note that the parameters are passed by reference so they can be updated +void read_param(int argc, char *argv[], + uint32 & _seed, + unsigned int & _vecSize, + unsigned int & _popSize, + unsigned int & _tSize, + double & _pCross, + double & _pMut, + string & _load_name, + unsigned int & _maxGen, + unsigned int & _minGen, + unsigned int & _steadyGen, + double & _onePointRate, + double & _twoPointsRate, + double & _uRate, + double & _pMutPerBit, + double & _bitFlipRate, + double & _oneBitRate + ) +{ + // define a parser from the command-line arguments + eoParser parser(argc, argv); + + // For each parameter, define Parameters directly in the parser, + // and assign the value to the variable + eoValueParam& seedParam = parser.createParam(time(0), "seed", "Random number seed", 'S'); + _seed = seedParam.value(); + + eoValueParam& vecSizeParam = parser.createParam(8, "vecSize", "Genotype size",'V', "Representation"); + _vecSize = vecSizeParam.value(); + + eoValueParam& popSizeParam = parser.createParam(10, "popSize", "Population size",'P', "Evolution"); + _popSize = popSizeParam.value(); + + eoValueParam& tSizeParam = parser.createParam(10, "tSize", "Tournament size",'T', "Evolution"); + _tSize = tSizeParam.value(); + + eoValueParam& load_nameParam = parser.createParam("", "Load","A save file to restart from",'L', "Persistence"); + _load_name = load_nameParam.value(); + + eoValueParam& maxGenParam = parser.createParam(100, "maxGen", "Maximum number of generations",'G', "Stopping criterion"); + _maxGen = maxGenParam.value(); + + eoValueParam& minGenParam = parser.createParam(100, "minGen", "Minimum number of generations",'g', "Stopping criterion"); + _minGen = minGenParam.value(); + + eoValueParam& steadyGenParam = parser.createParam(100, "steadyGen", "Number of generations with no improvement",'s', "Stopping criterion"); + _steadyGen = steadyGenParam.value(); + + eoValueParam& pCrossParam = parser.createParam(0.6, "pCross", "Probability of Crossover", 'C', "Genetic Operators"); + _pCross = pCrossParam.value(); + + eoValueParam& pMutParam = parser.createParam(0.1, "pMut", "Probability of Mutation", 'M', "Genetic Operators"); + _pMut = pMutParam.value(); + + eoValueParam& onePointRateParam = parser.createParam(1, "onePointRate", "Relative rate for one point crossover", '1', "Genetic Operators"); + _onePointRate = onePointRateParam.value(); + + eoValueParam& twoPointsRateParam = parser.createParam(1, "twoPointRate", "Relative rate for two point crossover", '2', "Genetic Operators"); + _twoPointsRate = twoPointsRateParam.value(); + + eoValueParam& uRateParam = parser.createParam(2, "uRate", "Relative rate for uniform crossover", 'U', "Genetic Operators"); + _uRate = uRateParam.value(); + + eoValueParam& pMutPerBitParam = parser.createParam(0.01, "pMutPerBit", "Probability of flipping 1 bit in bit-flip mutation", 'b', "Genetic Operators"); + _pMutPerBit = pMutPerBitParam.value(); + + eoValueParam& bitFlipRateParam = parser.createParam(0.01, "bitFlipRate", "Relative rate for bit-flip mutation", 'B', "Genetic Operators"); + _bitFlipRate = bitFlipRateParam.value(); + + eoValueParam& oneBitRateParam = parser.createParam(0.01, "oneBitRate", "Relative rate for deterministic bit-flip mutation", 'D', "Genetic Operators"); + _oneBitRate = oneBitRateParam.value(); + + // the name of the "status" file where all actual parameter values will be saved + string str_status = parser.ProgramName() + ".status"; + eoValueParam& status_nameParam = parser.createParam(str_status.c_str(), "status","Status file",'S', "Persistence"); + + // do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED + // i.e. in case you need parameters somewhere else, postpone these + if (parser.userNeedsHelp()) + { + parser.printHelp(cout); + exit(1); + } + if (status_nameParam.value() != "") + { + ofstream os(status_nameParam.value().c_str()); + os << parser; // and you can use that file as parameter file + } +} + +// GENERAL +// now the main_function: nothing changed, except input/output +void main_function(int argc, char **argv) +{ +// PARAMETRES + uint32 seed; + // decription of genotype + unsigned int vecSize; + // parameters for evolution engine + unsigned int popSize; + unsigned int tSize; + // operators probabilities at the algorithm level + double pCross; + double pMut; + // init and stop + string load_name; + unsigned int maxGen; + unsigned int minGen; + unsigned int steadyGen; + // rates for crossovers + double onePointRate; + double twoPointsRate; + double URate; + // rates and private parameters for mutations; + double pMutPerBit; + double bitFlipRate; + double oneBitRate; + + // Now read the parameters of the program + read_param(argc, argv, seed, vecSize, popSize, tSize, + pCross, pMut, load_name, maxGen, minGen, steadyGen, + onePointRate, twoPointsRate, URate, + pMutPerBit, bitFlipRate, oneBitRate ); + +// EVAL + ///////////////////////////// + // Fitness function + //////////////////////////// + // Evaluation: from a plain C++ fn to an EvalFunc Object ... + eoEvalFuncPtr& > plainEval( binary_value ); + // ... to an object that counts the nb of actual evaluations + eoEvalFuncCounter eval(plainEval); + +// INIT + //////////////////////////////// + // Initilisation of population + //////////////////////////////// + // Either load or initialize + // create an empty pop + eoPop pop; + // create a state for reading + eoState inState; // a state for loading - WITHOUT the parser + // register the rng and the pop in the state, so they can be loaded, + // and the present run will be the exact conitnuation of the saved run + // eventually with different parameters + inState.registerObject(rng); + inState.registerObject(pop); + + if (load_name != "") + { + inState.load(load_name); // load the pop and the rng + // the fitness is read in the file: + // do only evaluate the pop if the fitness has changed + } + else + { + rng.reseed(seed); + // a Indi random initializer + // based on boolean_generator class (see utils/rnd_generator.h) + eoInitFixedLength + random(vecSize, boolean_generator()); + + // Init pop from the randomizer: need to use the append function + pop.append(popSize, random); + // and evaluate pop (STL syntax) + apply(eval, pop); + } // end of initializatio of the population + +// OUTPUT + // sort pop for pretty printout + pop.sort(); + // Print (sorted) intial population (raw printout) + cout << "Initial Population" << endl << pop << endl; + +// ENGINE + ///////////////////////////////////// + // selection and replacement + //////////////////////////////////// +// SELECT + // The robust tournament selection + eoDetTournament selectOne(tSize); // tSize in [2,POPSIZE] + // is now encapsulated in a eoSelectPerc (entage) + eoSelectPerc select(selectOne);// by default rate==1 + +// REPLACE + // And we now have the full slection/replacement - though with + // no replacement (== generational replacement) at the moment :-) + eoNoReplacement replace; + +// OPERATORS + ////////////////////////////////////// + // The variation operators + ////////////////////////////////////// +// CROSSOVER + // 1-point crossover for bitstring + eoBinCrossover xover1; + // uniform crossover for bitstring + eoBinUxOver xoverU; + // 2-pots xover + eoBinNxOver xover2(2); + // Combine them with relative rates + eoPropCombinedQuadOp xover(xover1, onePointRate); + xover.add(xoverU, URate); + xover.add(xover2, twoPointsRate, true); + +// MUTATION + // standard bit-flip mutation for bitstring + eoBinMutation mutationBitFlip(pMutPerBit); + // mutate exactly 1 bit per individual + eoDetBitFlip mutationOneBit; + // Combine them with relative rates + eoPropCombinedMonOp mutation(mutationBitFlip, bitFlipRate); + mutation.add(mutationOneBit, oneBitRate, true); + + // The operators are encapsulated into an eoTRansform object + eoSGATransform transform(xover, pCross, mutation, pMut); + +// STOP + ////////////////////////////////////// + // termination condition see FirstBitEA.cpp + ///////////////////////////////////// + eoGenContinue genCont(maxGen); + eoSteadyFitContinue steadyCont(minGen, steadyGen); + eoFitContinue fitCont(vecSize); + eoCombinedContinue continuator(genCont); + continuator.add(steadyCont); + continuator.add(fitCont); + + +// CHECKPOINT + // but now you want to make many different things every generation + // (e.g. statistics, plots, ...). + // the class eoCheckPoint is dedicated to just that: + + // Declare a checkpoint (from a continuator: an eoCheckPoint + // IS AN eoContinue and will be called in the loop of all algorithms) + eoCheckPoint checkpoint(continuator); + + // Create a counter parameter + eoValueParam generationCounter(0, "Gen."); + + // Create an incrementor (sub-class of eoUpdater). Note that the + // parameter's value is passed by reference, + // so every time the incrementer is updated (every generation), + // the data in generationCounter will change. + eoIncrementor increment(generationCounter.value()); + + // Add it to the checkpoint, + // so the counter is updated (here, incremented) every generation + checkpoint.add(increment); + + // now some statistics on the population: + // Best fitness in population + eoBestFitnessStat bestStat; + eoAverageStat averageStat; + // Second moment stats: average and stdev + eoSecondMomentStats SecondStat; + + // Add them to the checkpoint to get them called at the appropriate time + checkpoint.add(bestStat); + checkpoint.add(averageStat); + checkpoint.add(SecondStat); + + // The Stdout monitor will print parameters to the screen ... + eoStdoutMonitor monitor(false); + + // when called by the checkpoint (i.e. at every generation) + checkpoint.add(monitor); + + // the monitor will output a series of parameters: add them + monitor.add(generationCounter); + monitor.add(eval); // because now eval is an eoEvalFuncCounter! + monitor.add(bestStat); + monitor.add(SecondStat); + + // A file monitor: will print parameters to ... a File, yes, you got it! + eoFileMonitor fileMonitor("stats.xg", " "); + eoGnuplot1DMonitor gnuMonitor("best_average.xg"); + + // the checkpoint mechanism can handle multiple monitors + checkpoint.add(fileMonitor); + checkpoint.add(gnuMonitor); + + // the fileMonitor can monitor parameters, too, but you must tell it! + fileMonitor.add(generationCounter); + fileMonitor.add(bestStat); + fileMonitor.add(SecondStat); + // the fileMonitor can monitor parameters, too, but you must tell it! + gnuMonitor.add(eval); + gnuMonitor.add(bestStat); + gnuMonitor.add(averageStat); + + // Last type of item the eoCheckpoint can handle: state savers: + eoState outState; + // Register the algorithm into the state (so it has something to save!!) + outState.registerObject(rng); + outState.registerObject(pop); + + // and feed the state to state savers + // save state every 100th generation + eoCountedStateSaver stateSaver1(100, outState, "generation"); + // save state every 1 seconds + eoTimedStateSaver stateSaver2(1, outState, "time"); + + // Don't forget to add the two savers to the checkpoint + checkpoint.add(stateSaver1); + checkpoint.add(stateSaver2); + // and that's it for the (control and) output + +// GENERATION + ///////////////////////////////////////// + // the algorithm + //////////////////////////////////////// + + // Easy EA requires + // selection, transformation, eval, replacement, and stopping criterion + eoEasyEA gga(checkpoint, eval, select, transform, replace); + + // Apply algo to pop - that's it! + gga(pop); + +// OUTPUT + // Print (sorted) intial population + pop.sort(); + cout << "FINAL Population\n" << pop << endl; +// GENERAL +} + +// A main that catches the exceptions +int main(int argc, char **argv) +{ +#ifdef _MSC_VER + int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF); + flag |= _CRTDBG_LEAK_CHECK_DF; + _CrtSetDbgFlag(flag); +// _CrtSetBreakAlloc(100); +#endif + + try + { + main_function(argc, argv); + } + catch(exception& e) + { + cout << "Exception: " << e.what() << '\n'; + } + + return 1; +} diff --git a/eo/tutorial/html/EA_tutorial.jpg b/eo/tutorial/html/EA_tutorial.jpg new file mode 100644 index 00000000..4a63e1ec Binary files /dev/null and b/eo/tutorial/html/EA_tutorial.jpg differ diff --git a/eo/tutorial/html/FirstBitEA.html b/eo/tutorial/html/FirstBitEA.html new file mode 100644 index 00000000..b659883d --- /dev/null +++ b/eo/tutorial/html/FirstBitEA.html @@ -0,0 +1,368 @@ + + + + + + FirstBitEA.cpp + + + +Back to Lesson 2 - Tutorial +main page - Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+
+

+FirstBitEA.cpp

+Click on the figure to see the corresponding code.
+In the code, the colors are meaningfull
+The actual code is in boldface and the comment in normal face. +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
  + + + + + +
+//-----------------------------------------------------------------------------
+// FirstBitEA.cpp
+//-----------------------------------------------------------------------------
+//*
+// Still an instance of a VERY simple Bitstring Genetic Algorithm
+// (see FirstBitGA.cpp) but now with  Breeder - and Combined Ops
+//
+//-----------------------------------------------------------------------------
+// standard includes
+#include <stdexcept>  // runtime_error
+#include <iostream>    // cout
+#include <strstream>  // ostrstream, istrstream
+// the general include for eo
+#include <eo>
+
+
+ + + + +
+ +//-----------------------------------------------------------------------------
+// define your individuals
+typedef eoBin<double> Indi; // A bitstring with fitness double
+
+
+ + + + +
+ +//-----------------------------------------------------------------------------
+// a simple fitness function that computes the number of ones of a bitstring
+// Now in a separate file, and declared as binary_value(const vector<bool> &)
+#include "binary_value.h"
+
+
+ + + + +
+ +//-----------------------------------------------------------------------------
+void main_function(int argc, char **argv)
+{
+
+
+ + + + +
+ +  const unsigned int SEED = 42; // seed for random number generator
+  const unsigned int T_SIZE = 3; // size for tournament selection
+  const unsigned int VEC_SIZE = 8; // Number of bits in genotypes
+  const unsigned int POP_SIZE = 20; // Size of population
+  const unsigned int MAX_GEN = 500; // Maximum number of generation before STOP
+  const float CROSS_RATE = 0.8; // Crossover rate
+  const double P_MUT_PER_BIT = 0.01; // probability of bit-flip mutation
+  const float MUT_RATE = 1.0; // mutation rate
+  // some parameters for chosing among different operators
+  const double onePointRate = 0.5;        // rate for 1-pt Xover
+  const double twoPointsRate = 0.5;        // rate for 2-pt Xover
+  const double URate = 0.5;                      // rate for Uniform Xover
+  const double bitFlipRate = 0.5;          // rate for bit-flip mutation
+  const double oneBitRate = 0.5;            // rate for one-bit mutation
+
+
+ + + + +
+ +  //////////////////////////
+  //  Random seed
+  //////////////////////////
+  //reproducible random seed: if you don't change SEED above,
+  // you'll aways get the same result, NOT a random run
+  rng.reseed(SEED);
+
+
+ + + + +
+ +  /////////////////////////////
+  // Fitness function
+  ////////////////////////////
+  // Evaluation: from a plain C++ fn to an EvalFunc Object
+  // you need to give the full description of the function
+  eoEvalFuncPtr<Indi, double, const vector<bool>& > eval(  binary_value );
+
+
+ + + + +
+ +  ////////////////////////////////
+  // Initilisation of population
+  ////////////////////////////////
+  // based on boolean_generator class (see utils/rnd_generator.h)
+  eoInitFixedLength<Indi, boolean_generator>
+      random(VEC_SIZE, boolean_generator());
+  // Initialization of the population
+  eoPop<Indi> pop(POP_SIZE, random);
+  // and evaluate it in one loop
+  apply<Indi>(eval, pop); // STL syntax
+
+
+ + + + +
+ +  // sort pop before printing it!
+  pop.sort();
+  // Print (sorted) intial population (raw printout)
+  cout << "Initial Population" << endl;
+  cout << pop;
+
+
+ + + + +
+ +  /////////////////////////////////////
+  // selection and replacement
+  ////////////////////////////////////
+
+
+ + + + +
+ +  // The robust tournament selection
+  eoDetTournament<Indi> selectOne(T_SIZE);            // T_SIZE in [2,POP_SIZE]
+ +  // is now encapsulated in a eoSelectPerc (entage)
+  eoSelectPerc<Indi> select(selectOne);// by default rate==1
+
+
+ + + + +
+ +  // And we now have the full slection/replacement - though with
+  // no replacement (== generational replacement) at the moment :-)
+  eoNoReplacement<Indi> replace;
+
+
+ + + + +
+ +  //////////////////////////////////////
+  // The variation operators
+  //////////////////////////////////////
+
+
+ + + + +
+ +  // 1-point crossover for bitstring
+  eoBinCrossover<Indi> xover1;
+  // uniform crossover for bitstring
+  eoBinUxOver<Indi> xoverU;
+  // 2-pots xover
+  eoBinNxOver<Indi> xover2(2);
+  // Combine them with relative rates
+  eoPropCombinedQuadOp<Indi> xover(xover1, onePointRate);
+  xover.add(xoverU, URate);
+  xover.add(xover2, twoPointsRate, true);
+
+
+ + + + +
+ +  
+  // standard bit-flip mutation for bitstring
+  eoBinMutation<Indi>  mutationBitFlip(P_MUT_PER_BIT);
+  // mutate exactly 1 bit per individual
+  eoDetBitFlip<Indi> mutationOneBit;
+  // Combine them with relative rates
+  eoPropCombinedMonOp<Indi> mutation(mutationBitFlip, bitFlipRate);
+  mutation.add(mutationOneBit, oneBitRate, true);
+  
+ +  // The operators are  encapsulated into an eoTRansform object
+  eoSGATransform<Indi> transform(xover, CROSS_RATE, mutation, MUT_RATE);
+
+
+ + + + +
+ + +
+ + + + +
+ +  //////////////////////////////////////
+  // termination conditions: use more than one
+  /////////////////////////////////////
+  // stop after MAX_GEN generations
+  eoGenContinue<Indi> genCont(MAX_GEN);
+  // do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement
+  eoSteadyFitContinue<Indi> steadyCont(MIN_GEN, STEADY_GEN);
+  // stop when fitness reaches a target (here VEC_SIZE)
+  eoFitContinue<Indi> fitCont(0);
+  // do stop when one of the above says so
+  eoCombinedContinue<Indi> continuator(genCont);
+  continuator.add(steadyCont);
+  continuator.add(fitCont);
+
+
+ + + + +
+ +  /////////////////////////////////////////
+  // the algorithm
+  ////////////////////////////////////////
+  // Easy EA requires
+  // selection, transformation, eval, replacement, and stopping criterion
+  eoEasyEA<Indi> gga(continuator, eval, select, transform, replace);
+  // Apply algo to pop - that's it!
+  gga(pop);
+  
+
+
+ + + + +
+ +  // Print (sorted) intial population
+  pop.sort();
+  cout << "FINAL Population\n" << pop << endl;
+
+
+ + + + +
+ +}
+// A main that catches the exceptions
+int main(int argc, char **argv)
+{
+#ifdef _MSC_VER
+  //  rng.reseed(42);
+      int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
+        flag |= _CRTDBG_LEAK_CHECK_DF;
+      _CrtSetDbgFlag(flag);
+//    _CrtSetBreakAlloc(100);
+#endif
+      try
+      {
+              main_function(argc, argv);
+      }
+      catch(exception& e)
+      {
+              cout << "Exception: " << e.what() << '\n';
+      }
+      return 1;
+}
+
+
Back to Lesson 2 - Tutorial +main page - Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+Marc Schoenauer
+
Last +modified: Sun Nov 19 22:26:27 2000 + + + diff --git a/eo/tutorial/html/FirstBitGA.html b/eo/tutorial/html/FirstBitGA.html new file mode 100644 index 00000000..05e859e9 --- /dev/null +++ b/eo/tutorial/html/FirstBitGA.html @@ -0,0 +1,350 @@ + + + + + + FirstBitGA.html + + + +Back to Lesson 1 - Tutorial +main page - Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+
+

+FirstBitGA.html

+Click on the figure to see the corresponding code.
+In the code, the colors are meaningfull
+The actual code is in boldface and the comment in normal face. +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
  + + + + + +
+//-----------------------------------------------------------------------------
+// FirstBitGA.cpp
+//-----------------------------------------------------------------------------
+//*
+// An instance of a VERY simple Bitstring Genetic Algorithm
+//
+//-----------------------------------------------------------------------------
+// standard includes
+#include <stdexcept>  // runtime_error
+#include <iostream>    // cout
+#include <strstream>  // ostrstream, istrstream
+// the general include for eo
+#include <eo>
+
+
+ + + + +
+ +//-----------------------------------------------------------------------------
+// define your individuals
+typedef eoBin<double> Indi;        // A bitstring with fitness double
+
+
+ + + + +
+ +//-----------------------------------------------------------------------------
+// a simple fitness function that computes the number of ones of a bitstring
+//  @param _indi A biststring individual
+ +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)
+{
+
+
+ + + + +
+ +  // all parameters are hard-coded!
+  const unsigned int SEED = 42;          // seed for random number generator
+  const unsigned int T_SIZE = 3;        // size for tournament selection
+  const unsigned int VEC_SIZE = 8;    // Number of bits in genotypes
+  const unsigned int POP_SIZE = 20;  // Size of population
+  const unsigned int MAX_GEN = 100;  // Maximum number of generation before STOP
+  const float CROSS_RATE = 0.8;          // Crossover rate
+  const double P_MUT_PER_BIT = 0.01; // probability of bit-flip mutation
+  const float MUT_RATE = 1.0;              // mutation rate
+
+
+ + + + +
+ + +  //////////////////////////
+  //  Random seed
+  //////////////////////////
+  //reproducible random seed: if you don't change SEED above,
+  // you'll aways get the same result, NOT a random run
+  rng.reseed(SEED);
+
+
+ + + + +
+ +  /////////////////////////////
+  // Fitness function
+  ////////////////////////////
+  // Evaluation: from a plain C++ fn to an EvalFunc Object
+  eoEvalFuncPtr<Indi> eval(  binary_value );
+
+
+ + + + +
+ +  ////////////////////////////////
+  // Initilisation of population
+  ////////////////////////////////
+  // declare the population
+  eoPop<Indi> pop;
+  // fill it!
+  for (unsigned int igeno=0; igeno<POP_SIZE; igeno++)
+      {
+          Indi v;                    // void individual, to be filled
+          for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)
+              {
+                  bool r = rng.flip(); // new value, random in {0,1}
+                  v.push_back(r);          // append that random value to v
+              }
+          eval(v);                                // evaluate it
+          pop.push_back(v);              // and put it in the population
+      }
+
+
+ + + + +
+ +  // sort pop before printing it!
+  pop.sort();
+  // Print (sorted) intial population (raw printout)
+  cout << "Initial Population" << endl;
+  cout << pop;
+
+
+ + + + +
+ +  /////////////////////////////////////
+  // selection and replacement
+  ////////////////////////////////////
+
+
+ + + + +
+ +  // The robust tournament selection
+  eoDetTournament<Indi> select(T_SIZE);  // T_SIZE in [2,POP_SIZE]
+
+
+ + + + +
+ +  // The simple GA evolution engine uses generational replacement
+  // so no replacement procedure is needed
+
+
+ + + + +
+ + +
+ + + + +
+ +  //////////////////////////////////////
+  // The variation operators
+  //////////////////////////////////////
+
+
+ + + + +
+ +  // 1-point mutation for bitstring
+  eoBinCrossover<Indi> xover;
+
+
+ + + + +
+ +  
+  // standard bit-flip mutation for bitstring
+  eoBinMutation<Indi>  mutation(P_MUT_PER_BIT);
+
+
+ + + + +
+ +  //////////////////////////////////////
+  // termination condition
+  /////////////////////////////////////
+  // stop after MAX_GEN generations
+  eoGenContinue<Indi> continuator(MAX_GEN);
+  
+
+
+ + + + +
+ +  /////////////////////////////////////////
+  // the algorithm
+  ////////////////////////////////////////
+  // standard Generational GA requires as parameters
+  // selection, evaluation, crossover and mutation, stopping criterion
+
+  eoSGA<Indi> gga(select, xover, CROSS_RATE, mutation, MUT_RATE,
+                                  eval, continuator);
+  // Apply algo to pop - that's it!
+  gga(pop);
+  
+
+
+ + + + +
+ +  // Print (sorted) intial population
+  pop.sort();
+  cout << "FINAL Population\n" << pop << endl;
+
+
+ + + + +
+ +}
+ // A main that catches the exceptions
+int main(int argc, char **argv)
+{
+#ifdef _MSC_VER
+  //  rng.reseed(42);
+      int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
+        flag |= _CRTDBG_LEAK_CHECK_DF;
+      _CrtSetDbgFlag(flag);
+//    _CrtSetBreakAlloc(100);
+#endif
+      try
+      {
+              main_function(argc, argv);
+      }
+      catch(exception& e)
+      {
+              cout << "Exception: " << e.what() << '\n';
+      }
+      return 1;
+}
+
+
Back to Lesson 1 - Tutorial +main page - Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+Marc Schoenauer
+
Last +modified: Sun Nov 19 08:31:26 2000 + + + diff --git a/eo/tutorial/html/FirstRealEA.html b/eo/tutorial/html/FirstRealEA.html new file mode 100644 index 00000000..9b0372fd --- /dev/null +++ b/eo/tutorial/html/FirstRealEA.html @@ -0,0 +1,366 @@ + + + + + + FirstRealEA.cpp + + +Back to Lesson 2 - Tutorial +main page - Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+
+

+FirstRealEA.cpp

+Click on the figure to see the corresponding code.
+In the code, the colors are meaningfull
+The actual code is in boldface and the comment in normal face. +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
  + + + + + +
+//-----------------------------------------------------------------------------
+// FirstRealEA.cpp
+//-----------------------------------------------------------------------------
+//*
+// Still an instance of a VERY simple Real-coded  Genetic Algorithm
+// (see FirstBitGA.cpp) but now with  Breeder - and Combined Ops
+//
+//-----------------------------------------------------------------------------
+// standard includes
+#include <stdexcept>  // runtime_error
+#include <iostream>    // cout
+#include <strstream>  // ostrstream, istrstream
+// the general include for eo
+#include <eo>
+
+
+ + + + +
+ +//-----------------------------------------------------------------------------
+// define your individuals
+typedef eoReal<double> Indi;
+
+
+ + + + +
+ +//-----------------------------------------------------------------------------
+// a simple fitness function that computes the euclidian norm of a real vector
+// Now in a separate file, and declared as binary_value(const vector<bool> &)
+#include "real_value.h"
+
+
+ + + + +
+ +//-----------------------------------------------------------------------------
+void main_function(int argc, char **argv)
+{
+
+
+ + + + +
+ +  const unsigned int SEED = 42; // seed for random number generator
+  const unsigned int T_SIZE = 3; // size for tournament selection
+  const unsigned int VEC_SIZE = 8; // Number of object variables in genotypes
+  const unsigned int POP_SIZE = 20; // Size of population
+  const unsigned int MAX_GEN = 500; // Maximum number of generation before STOP
+  const unsigned int MIN_GEN = 10;  // Minimum number of generation before ...
+  const unsigned int STEADY_GEN = 50; // stop after STEADY_GEN gen. without improvelent
+  const float P_CROSS = 0.8; // Crossover probability
+  const float P_MUT = 0.5; // mutation probability
+  const double EPSILON = 0.01; // range for real uniform mutation
+  // some parameters for chosing among different operators
+  const double segmentRate = 0.5;        // rate for 1-pt Xover
+  const double arithmeticRate = 0.5;        // rate for 2-pt Xover
+  const double uniformMutRate = 0.5;          // rate for bit-flip mutation
+  const double detMutRate = 0.5;            // rate for one-bit mutation
+
+
+ + + + +
+ +  //////////////////////////
+  //  Random seed
+  //////////////////////////
+  //reproducible random seed: if you don't change SEED above,
+  // you'll aways get the same result, NOT a random run
+  rng.reseed(SEED);
+
+
+ + + + +
+ +  /////////////////////////////
+  // Fitness function
+  ////////////////////////////
+  // Evaluation: from a plain C++ fn to an EvalFunc Object
+  // you need to give the full description of the function
+  eoEvalFuncPtr<Indi, double, const vector<double>& > eval(  real_value );
+
+
+ + + + +
+ +  ////////////////////////////////
+  // Initilisation of population
+  ////////////////////////////////
+  // based on a uniform generator
+  eoInitFixedLength<Indi, uniform_generator<double> >
+          random(VEC_SIZE, uniform_generator<double>(-1.0, 1.0));
+    // Initialization of the population
+  eoPop<Indi> pop(POP_SIZE, random);
+
+  // and evaluate it in one loop
+  apply<Indi>(eval, pop); // STL syntax
+
+
+ + + + +
+ +  // sort pop before printing it!
+  pop.sort();
+  // Print (sorted) intial population (raw printout)
+  cout << "Initial Population" << endl;
+  cout << pop;
+
+
+ + + + +
+ +  /////////////////////////////////////
+  // selection and replacement
+  ////////////////////////////////////
+
+
+ + + + +
+ +  // The robust tournament selection
+  eoDetTournament<Indi> selectOne(T_SIZE);
+ +  // is now encapsulated in a eoSelectPerc (entage)
+  eoSelectPerc<Indi> select(selectOne);// by default rate==1
+
+
+ + + + +
+ +  // And we now have the full slection/replacement - though with
+  // no replacement (== generational replacement) at the moment :-)
+  eoNoReplacement<Indi> replace;
+
+
+ + + + +
+ +  //////////////////////////////////////
+  // The variation operators
+  //////////////////////////////////////
+
+
+ + + + +
+ +  // uniform chooce on segment made by the parents
+  eoSegmentCrossover<Indi> xoverS;
+  // uniform choice in hypercube built by the parents
+  eoArithmeticCrossover<Indi> xoverA;
+  // Combine them with relative rates
+  eoPropCombinedQuadOp<Indi> xover(xoverS, segmentRate);
+  xover.add(xoverA, arithmeticRate, true);
+
+
+ + + + +
+ +  
+  // offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]
+  eoUniformMutation<Indi>  mutationU(EPSILON);
+  // k (=1) coordinates of parents are uniformly modified
+  eoDetUniformMutation<Indi>  mutationD(EPSILON);
+  // Combine them with relative rates
+  eoPropCombinedMonOp<Indi> mutation(mutationU, uniformMutRate);
+  mutation.add(mutationD, detMutRate, true);
+  
+  // The operators are  encapsulated into an eoTRansform object
+  eoSGATransform<Indi> transform(xover, P_CROSS, mutation, P_MUT);
+
+
+ + + + +
+ + +
+ + + + +
+ +  //////////////////////////////////////
+  // termination conditions: use more than one
+  /////////////////////////////////////
+  // stop after MAX_GEN generations
+  eoGenContinue<Indi> genCont(MAX_GEN);
+  // do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement
+  eoSteadyFitContinue<Indi> steadyCont(MIN_GEN, STEADY_GEN);
+  // stop when fitness reaches a target (here VEC_SIZE)
+  eoFitContinue<Indi> fitCont(0);
+  // do stop when one of the above says so
+  eoCombinedContinue<Indi> continuator(genCont);
+  continuator.add(steadyCont);
+  continuator.add(fitCont);
+
+
+ + + + +
+ +  /////////////////////////////////////////
+  // the algorithm
+  ////////////////////////////////////////
+  // Easy EA requires
+  // selection, transformation, eval, replacement, and stopping criterion
+  eoEasyEA<Indi> gga(continuator, eval, select, transform, replace);
+  // Apply algo to pop - that's it!
+  cout << "\n              Here we go\n\n";
+  gga(pop);
+  
+
+
+ + + + +
+ +  // Print (sorted) intial population
+  pop.sort();
+  cout << "FINAL Population\n" << pop << endl;
+
+
+ + + + +
+ +}
+// A main that catches the exceptions
+int main(int argc, char **argv)
+{
+#ifdef _MSC_VER
+  //  rng.reseed(42);
+      int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
+        flag |= _CRTDBG_LEAK_CHECK_DF;
+      _CrtSetDbgFlag(flag);
+//    _CrtSetBreakAlloc(100);
+#endif
+      try
+      {
+              main_function(argc, argv);
+      }
+      catch(exception& e)
+      {
+              cout << "Exception: " << e.what() << '\n';
+      }
+      return 1;
+}
+
+
Back to Lesson 2 - Tutorial +main page - Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+Marc Schoenauer
+
Last +modified: Wed Nov 29 07:38:36 2000 + + + diff --git a/eo/tutorial/html/FirstRealGA.html b/eo/tutorial/html/FirstRealGA.html new file mode 100644 index 00000000..eaeecc28 --- /dev/null +++ b/eo/tutorial/html/FirstRealGA.html @@ -0,0 +1,349 @@ + + + + + + ../FirstRealGA.html + + + +Back to Lesson 1 - Tutorial +main page - Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+
+

+../FirstRealGA.html

+Click on the figure to see the corresponding code.
+In the code, the colors are meaningfull
+The actual code is in boldface and the comment in normal face. +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
  + + + + + +
+//-----------------------------------------------------------------------------
+// FirstRealGA.cpp
+//-----------------------------------------------------------------------------
+//*
+// An instance of a VERY simple Real-coded Genetic Algorithm
+//
+//-----------------------------------------------------------------------------
+// standard includes
+#include <stdexcept>  // runtime_error
+#include <iostream>    // cout
+#include <strstream>  // ostrstream, istrstream
+// the general include for eo
+#include <eo>
+
+
+ + + + +
+ +//-----------------------------------------------------------------------------
+// define your individuals
+ typedef eoReal<double> Indi;
+
+
+ + + + +
+ +//-----------------------------------------------------------------------------
+// a simple fitness function that computes the euclidian norm of a real vector
+//      @param _indi A real-valued individual
+ +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 only
+}
+
+
+ + + + +
+ +//-----------------------------------------------------------------------------
+void main_function(int argc, char **argv)
+{
+
+
+ + + + +
+ +  // all parameters are hard-coded!
+  const unsigned int SEED = 42; // seed for random number generator
+  const unsigned int VEC_SIZE = 8; // Number of object variables in genotypes
+  const unsigned int POP_SIZE = 20; // Size of population
+  const unsigned int T_SIZE = 3; // size for tournament selection
+  const unsigned int MAX_GEN = 500; // Maximum number of generation before STOP
+  const float CROSS_RATE = 0.8; // Crossover rate
+  const double EPSILON = 0.01;  // range for real uniform mutation
+  const float MUT_RATE = 0.5;    // mutation rate
+
+
+ + + + +
+ + +  //////////////////////////
+  //  Random seed
+  //////////////////////////
+  //reproducible random seed: if you don't change SEED above,
+  // you'll aways get the same result, NOT a random run
+  rng.reseed(SEED);
+
+
+ + + + +
+ +  /////////////////////////////
+  // Fitness function
+  ////////////////////////////
+  // Evaluation: from a plain C++ fn to an EvalFunc Object
+  eoEvalFuncPtr<Indi> eval(  real_value );
+
+
+ + + + +
+ +  ////////////////////////////////
+  // Initilisation of population
+  ////////////////////////////////
+  // declare the population
+  eoPop<Indi> pop;
+  // fill it!
+  for (unsigned int igeno=0; igeno<POP_SIZE; igeno++)
+      {
+          Indi v;                  // void individual, to be filled
+          for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)
+              {
+                  double r = 2*rng.uniform() - 1; // new value, random in [-1,1)
+                  v.push_back(r);            // append that random value to v
+              }
+          eval(v);                                  // evaluate it
+          pop.push_back(v);                // and put it in the population
+      }
+
+
+ + + + +
+ +  // sort pop before printing it!
+  pop.sort();
+  // Print (sorted) intial population (raw printout)
+  cout << "Initial Population" << endl;
+  cout << pop;
+
+
+ + + + +
+ +  /////////////////////////////////////
+  // selection and replacement
+  ////////////////////////////////////
+
+
+ + + + +
+ +  // The robust tournament selection
+  eoDetTournament<Indi> select(T_SIZE);            // T_SIZE in [2,POP_SIZE]
+
+
+ + + + +
+ +  // eoSGA uses generational replacement by default
+  // so no replacement procedure has to be given
+
+
+ + + + +
+ + +
+ + + + +
+ +  //////////////////////////////////////
+  // termination condition
+  /////////////////////////////////////
+  // stop after MAX_GEN generations
+  eoGenContinue<Indi> continuator(MAX_GEN);
+  
+
+
+ + + + +
+ +  //////////////////////////////////////
+  // The variation operators
+  //////////////////////////////////////
+
+
+ + + + +
+ +  // offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]
+  eoUniformMutation<Indi>  mutation(EPSILON);
+
+
+ + + + +
+ +  // offspring(i) is a linear combination of parent(i)
+  eoArithmeticCrossover<Indi> xover;
+
+
+ + + + +
+ +  /////////////////////////////////////////
+  // the algorithm
+  ////////////////////////////////////////
+  // standard Generational GA requires
+  // selection, evaluation, crossover and mutation, stopping criterion
+
+  eoSGA<Indi> gga(select, xover, CROSS_RATE, mutation, MUT_RATE,
+                                    eval, continuator);
+  // Apply algo to pop - that's it!
+  gga(pop);
+  
+
+
+ + + + +
+ +  // Print (sorted) intial population
+  pop.sort();
+  cout << "FINAL Population\n" << pop << endl;
+
+
+ + + + +
+ +}
+// A main that catches the exceptions
+int main(int argc, char **argv)
+{
+#ifdef _MSC_VER
+  //  rng.reseed(42);
+      int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
+        flag |= _CRTDBG_LEAK_CHECK_DF;
+      _CrtSetDbgFlag(flag);
+//    _CrtSetBreakAlloc(100);
+#endif
+      try
+      {
+              main_function(argc, argv);
+      }
+      catch(exception& e)
+      {
+              cout << "Exception: " << e.what() << '\n';
+      }
+      return 1;
+}
+
+
Back to Lesson 1 - Tutorial +main page - Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+Marc Schoenauer
+
Last +modified: Sun Nov 19 08:31:29 2000 + + + diff --git a/eo/tutorial/html/FirstRealGA_old.html b/eo/tutorial/html/FirstRealGA_old.html new file mode 100644 index 00000000..0609d8c6 --- /dev/null +++ b/eo/tutorial/html/FirstRealGA_old.html @@ -0,0 +1,278 @@ + + + + + + First Real GA + + +Back to Lesson 1 - Tutorial +main page - Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+
+

+A First Real GA

+Click on the figure to see the corresponding code. Tutorial comments are +in variable length fonts, after the code. +
+

+ +

//----------------------------------------------------------------------------- +

#include +<stdexcept > // runtime_error +

//----------------------------------------------------------------------------- +
// FirstRealGA.cpp +
//----------------------------------------------------------------------------- +
//* +
// An instance of a +VERY simple Real-coded Genetic Algorithm +
// +
//----------------------------------------------------------------------------- +
// standard includes +

#include +<iostream>// cout +
#include +<strstream>// ostrstream, istrstream +

// the general include +for eo +

#include +<eo> +

// specific incluse, +as Real is not (yet) in the standard eo source dir +
#include +"eoeal.h" +
#include +"eoRealOp.h" +

//----------------------------------------------------------------------------- +
// define your individual: +

typedef +eoReal<double> Indi; +

You say here that you will be handling arrays +of doubles, whose fitness is a double +
Note that this makes Indi derive from STL +class vector<double> +
//----------------------------------------------------------------------------- +
/** a simple fitness +function that computes the euclidian norm of a real vector +
    @param +_indi A real-valued individual +
*/ +

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 +
} +

This simple function computes the sum of the squares of the variables +(remember Indi is here a vector of doubles) +
//----------------------------------------------------------------------------- +

void +main_function(int argc, char **argv) +
{ +
const +unsigned int SEED = 42;// seed for random number generator +
const +unsigned int VEC_SIZE = 8; // Number of object variables in genotypes +
const +unsigned int POP_SIZE = 20; // Size of population +
const +unsigned int T_SIZE = 3; // size for tournament selection +
const +unsigned int MAX_GEN = 500; // Maximum number of generation before STOP +
const +float CROSS_RATE = 0.8; // Crossover rate +
const +double EPSILON = 0.01; // range for real uniform mutation +
const +float MUT_RATE = 0.5; // mutation rate +
  +

////////////////////////// +
// Random seed +
////////////////////////// +
//reproducible random +seed: if you don't change SEED above, +
// you'll aways get +the same result, NOT a random run +
rng.reseed(SEED); +

///////////////////////////// +
// Fitness function +
//////////////////////////// +
// Evaluation: from +a plain C++ fn to an EvalFunc Object +
eoEvalFuncPtr<Indi> +eval( real_value ); +

This encapsulate the C++ function real_value into a functor +object +

//////////////////////////////// +
// Initilisation of +population +
//////////////////////////////// +

// declare the population +
eoPop<Indi> +pop; +

Declares a population +object, which is basically an STL +vector<Indi> +

// fill it! +
for +(unsigned int igeno=0; igeno<POP_SIZE; igeno++) +
  +{ +
    +Indi v; // generate a random individual +
    +for (unsigned ivar=0; ivar<VEC_SIZE; ivar++) +
      +{ +
        +double r = 2*rng.uniform() - 1; // new value, random in [-1,1) +
        +v.push_back(r); // +
      +} +
    +eval(v); // evaluate it +
    +pop.push_back(v); // and put it in the population +
  +} +

This initialization of the population is straightforward +
rng.uniform() generates a double uniformly +in [0,1), +
v.push_back simply appends its argument +at the end of STL vector v +
eval(v)evaluates individal v +the functor way, calling eval.operator()(v) +

// sort pop before printing +it! +
pop.sort(); +

// Print (sorted) intial +population (raw printout) +
cout +<< "Initial Population" << endl; +
cout +<< pop; +

If you looked at eoPop +inheritance diagram, you noticed that it derives from eoPrintable: +hence you can stream them using the << operator. Of course, Indis +are EO objects +, which also are eoPrintable, and the << operator for eoPop +uses the << operator for Indi. +

///////////////////////////////////// +
// +selection and replacement +
//////////////////////////////////// +
// The well-known roulette +selection +
// +The robust tournament selection +
eoDetTournament<Indi> +select(T_SIZE); // SIZE in [2,POP_SIZE] +

// +eoSGA uses generational replacement by default +
// so no replacement +procedure has to be given +

////////////////////////////////////// +
// termination condition +
///////////////////////////////////// +
// stop after MAX_GEN +generations +
eoGenContinue<Indi> +continuator(MAX_GEN); +

This class is called eoGenContinue +because the main loop of all eoAlgo +says ... while continuator(pop) +

////////////////////////////////////// +
// +The variation operators +
////////////////////////////////////// +

// +offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon] +
eoUniformMutation<Indi>  +mutation(EPSILON); +
// +offspring(i) is a linear combination of parent(i) +
eoArithmeticCrossover<Indi> +xover; +

The variation operators are respectively +an eoMonOp +(unary operator) and an eoQuadOp +(binary operator that modifies both its arguments). +

///////////////////////////////////////// +
// the algorithm +
//////////////////////////////////////// +
// standard Generational +GA requires +
// selection, evaluation, +crossover and mutation, stopping criterion +
  +

eoSGA<Indi> +gga(select, xover, CROSS_RATE, mutation, MUT_RATE, +
eval, +continuator); +

// Apply algo to pop +- that's it! +
gga(pop); +

This simple algorithm of class eoSGA is a functor +object, and running the algorithm amounts to call the operator() +on the initial population! +

// Print (sorted) intial +population (raw printout) +
pop.sort(); +
cout +<< "FINAL Population\n" << pop << endl; +

that's it - just print the final population and you're done!!! +

} +

// +A main that catches the exceptions - do not modify! +
int +main(int argc, char **argv) +
{ +
#ifdef +_MSC_VER +
// +rng.reseed(42); +
int +flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF); +
flag +|= _CRTDBG_LEAK_CHECK_DF; +
_CrtSetDbgFlag(flag); +
// +_CrtSetBreakAlloc(100); +
#endif +

try +
{ +
main_function(argc, +argv); +
} +
catch(exception& +e) +
{ +
cout +<< "Exception: " << e.what() << '\n'; +
} +

return +1; +
} +

+


Back to Lesson 1 - Tutorial +main page - Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+Marc Schoenauer
+ +
Last +modified: Wed Nov 6 17:22:43 CET 2000 + + diff --git a/eo/tutorial/html/Firstmerge.html b/eo/tutorial/html/Firstmerge.html new file mode 100644 index 00000000..1aca9194 --- /dev/null +++ b/eo/tutorial/html/Firstmerge.html @@ -0,0 +1,287 @@ + + + + + + Differences + + +Back to Lesson 1 - +Tutorial +main page - +Top-Down page - Bottom-up +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>
typedef +eoReal<double> Indi;typedef +eoBin<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); +
eoArithmeticCrossover<Indi> +xover;
eoBinMutation<Indi>  +mutation(P_MUT_PER_BIT); +
eoBinCrossover<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 - +Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+
+Marc Schoenauer
+ +
Last +modified: Tue Nov 7 07:49:47 CET 2000 + + diff --git a/eo/tutorial/html/NoWay.html b/eo/tutorial/html/NoWay.html new file mode 100644 index 00000000..60726953 --- /dev/null +++ b/eo/tutorial/html/NoWay.html @@ -0,0 +1,74 @@ + + + + + + Tutorial: Solutions + + +Tutorial main page - +Top-Down +page - Bottom-up page - Programming +hints -EO +documentation +
+
+
+

+Tutorial:

+ +
Solutions of exercises
+ +

No, we won't +provide +any +hypertext link directly to the correct code, so you get a chance +to find out the solution by yourself without immediately going there  +:-))) +

What you should do: +

    +
  • +copy the code of an example onto another name (e.g. mytest.cpp)
  • + +
  • +edit and modify mytest.cpp +according to the corresponding instructions of the exercise
  • + +
  • +compile mytest.cpp by typing +make mytest at system prompt, +and you will hopefully get an executable file named mytest +(for Unix systems: if eventually someone tells me how to do that in Windows, +apart that you'll probably end up with an executable file named mytest.exe...).
  • +
+ +
+
What you may do later: +

All solutions to exercises are in the same +sub-dir of the Tutorial directory than the example files (i.e. +Lesson1 for Lesson1, yes!). Hence you may browse through the code, eventually +modifying it, and then simply type, in the corresponding directory, +
... % make exerciseN +
which will compile file exerciseN.cpp +into executable exerciseN +(for Unix systems: if eventually someone tells me how to do that in Windows, +you'll probably end up with an executable file named exerciseN.exe). +

+


+
What you may not do: +
Complain that it does not work under Windows :-)
+ +
Tutorial main page - +Top-Down +page - Bottom-up page - Programming +hints - EO +documentation +
+
+
+Marc Schoenauer
+ +
Last +modified: Fri Nov 3 18:49:12 CET 2000 + + diff --git a/eo/tutorial/html/SecondBitEA.html b/eo/tutorial/html/SecondBitEA.html new file mode 100644 index 00000000..e5fa3868 --- /dev/null +++ b/eo/tutorial/html/SecondBitEA.html @@ -0,0 +1,612 @@ + + + + + + SecondBitEA.cpp + + + +Back to Lesson 3 - Tutorial +main page - Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+
+

+SecondBitEA.cpp

+Click on the figure to see the corresponding code. +
In the code, the colors are meaningfull +
The actual code is in boldface and the comment in normal face. +

For this particular program, as all new lines are concerned with program +parameter input and information output, the font +color for program-parameter-related sections will refer +to the section where the parameters are used whereas the background +color will remain blue. +

+
+ + + + +
//----------------------------------------------------------------------------- +
// SecondGA.cpp +
//----------------------------------------------------------------------------- +
//* +
// Same code than FirstBitEA as far as Evolutionary +Computation is concerned +
// but now you learn to enter the parameters +in a more flexible way +
// and to twidle the output to your preferences! +
//----------------------------------------------------------------------------- +
// standard includes +
#include <stdexcept>  // runtime_error  +
#include <iostream>   // +cout +
#include <strstream>  // ostrstream, +istrstream +
#include <fstream> +
// the general include for eo +
#include <eo> +
// specific includes +
#include "eoSGATransform.h" +
#include "eoPropCombinedOp.h"
+ + + + + +
#include "binary_value.h"
+ + + + + +
//----------------------------------------------------------------------------- +
// define your genotype and fitness types +
typedef eoBin<double> Indi;
+ + + + + +
//----------------------------------------------------------------------------- +
// instead of having all values of useful +parameters as constants, read them: +
// either on the command line (--option=value +or -o=value) +
//        +or in a parameter file (same syntax, order independent,  +
//                                                        +# = usual comment character  +
//        +or in the environment (TODO) +
// note that the parameters are passed by +reference so they can be updated +
void read_param(int argc, char *argv[],  +
uint32 & +_seed, +
unsigned int & _vecSize, +
unsigned int & _popSize, +
unsigned int & _tSize, +
double & _pCross, +
double & _pMut, +
string & _load_name, +
unsigned int & _maxGen, +
unsigned int & _minGen, +
unsigned int & _steadyGen, +
double & _onePointRate,  +
double & _twoPointsRate,  +
double & _uRate,  +
double & _pMutPerBit,  +
double & _bitFlipRate,  +
double & _oneBitRate +
) +
+
// define a +parser from the command-line arguments +
     eoParser parser(argc, +argv); +
     // For each +parameter, define Parameters directly in the parser,  +
     // and assign +the value to the variable +
    +eoValueParam<uint32>& seedParam = parser.createParam<uint32>(time(0), +"seed", "Random number seed", 'S'); +
     +_seed = seedParam.value(); +

     eoValueParam<unsigned +int>& vecSizeParam = parser.createParam<unsigned int>(8, "vecSize", +"Genotype size",'V', "Representation"); +
     _vecSize = vecSizeParam.value(); +

     eoValueParam<unsigned +int>& popSizeParam = parser.createParam<unsigned int>(10, "popSize", +"Population size",'P', "Evolution"); +
     _popSize = popSizeParam.value(); +
     eoValueParam<unsigned +int>& tSizeParam = parser.createParam<unsigned int>(10, "tSize", +"Tournament size",'T', "Evolution"); +
     _tSize = tSizeParam.value(); +
+
    eoValueParam<string>& +load_nameParam = parser.createParam<string>("", "Load","A save file +to restart from",'L', "Persistence"); +
     _load_name = +load_nameParam.value(); +

     eoValueParam<unsigned +int>& maxGenParam = parser.createParam<unsigned int>(100, "maxGen", +"Maximum number of generations",'G', "Stopping criterion"); +
     _maxGen = maxGenParam.value(); +
     eoValueParam<unsigned +int>& minGenParam = parser.createParam<unsigned int>(100, "minGen", +"Minimum number of generations",'g', "Stopping criterion"); +
     _minGen = minGenParam.value(); +
     eoValueParam<unsigned +int>& steadyGenParam = parser.createParam<unsigned int>(100, "steadyGen", +"Number of generations with no improvement",'s', "Stopping criterion"); +
     _steadyGen = +steadyGenParam.value(); +

     eoValueParam<double>& +pCrossParam = parser.createParam<double>(0.6, "pCross", "Probability +of Crossover", 'C', "Genetic Operators");  +
     _pCross = pCrossParam.value(); +
     eoValueParam<double>& +pMutParam = parser.createParam<double>(0.1, "pMut", "Probability of +Mutation", 'M', "Genetic Operators"); +
     _pMut = pMutParam.value(); +
     eoValueParam<double>& +onePointRateParam = parser.createParam<double>(1, "onePointRate", "Relative +rate for one point crossover", '1', "Genetic Operators"); +
     _onePointRate += onePointRateParam.value(); +
     eoValueParam<double>& +twoPointsRateParam = parser.createParam<double>(1, "twoPointRate", "Relative +rate for two point crossover", '2', "Genetic Operators"); +
     _twoPointsRate += twoPointsRateParam.value(); +
     eoValueParam<double>& +uRateParam = parser.createParam<double>(2, "uRate", "Relative rate for +uniform crossover", 'U', "Genetic Operators"); +
     _uRate =  +uRateParam.value(); +
     eoValueParam<double>& +pMutPerBitParam = parser.createParam<double>(0.01, "pMutPerBit", "Probability +of flipping 1 bit in bit-flip mutation", 'b', "Genetic Operators"); +
     _pMutPerBit = +pMutPerBitParam.value(); +
     eoValueParam<double>& +bitFlipRateParam = parser.createParam<double>(0.01, "bitFlipRate", "Relative +rate for bit-flip mutation", 'B', "Genetic Operators"); +
     _bitFlipRate +=  bitFlipRateParam.value(); +
     eoValueParam<double>& +oneBitRateParam = parser.createParam<double>(0.01, "oneBitRate", "Relative +rate for deterministic bit-flip mutation", 'D', "Genetic Operators"); +
         +_oneBitRate = oneBitRateParam.value(); +

     // the name +of the "status" file where all actual parameter values will be saved +
     string str_status += parser.ProgramName() + ".status"; +
     eoValueParam<string>& +status_nameParam = parser.createParam<string>(str_status.c_str(), "status","Status +file",'S', "Persistence"); +
   // do the following AFTER +ALL PARAMETERS HAVE BEEN PROCESSED +
   // i.e. in case you need +parameters somewhere else, postpone these +
     if (parser.userNeedsHelp()) +
         +{ +
             +parser.printHelp(cout); +
             +exit(1); +
         +} +
     if (status_nameParam.value() +!= "") +
         +{ +
ofstream os(status_nameParam.value().c_str()); +
os << parser; // and you can +use that file as parameter file +
         +} +
}

+ + + + + +
// now the main_function: nothing changed, +except input/output +
void main_function(int argc, char **argv) +
{
+ + + + + +
   +uint32 seed; +
   // decription of genotype +
   unsigned int vecSize; +
   // parameters for evolution +engine +
   unsigned int popSize; +
   unsigned int tSize; +
   // operators probabilities +at the algorithm level +
   double pCross; +
   double pMut; +
   // init and stop +
   string load_name; +
   unsigned int maxGen; +
   unsigned int minGen; +
   unsigned int steadyGen; +
   // rates for crossovers +
   double onePointRate; +
   double twoPointsRate; +
   double URate; +
   // rates and private +parameters for mutations; +
   double pMutPerBit; +
   double bitFlipRate; +
   double oneBitRate; +
   // Now read the parameters +of the program +
     read_param(argc, +argv, seed, vecSize, popSize, tSize, +
           +pCross, pMut, load_name, maxGen, minGen, steadyGen, +
           +onePointRate, twoPointsRate, URate,  +
           +pMutPerBit, bitFlipRate, oneBitRate );
+ + + + + +
 ///////////////////////////// +
 // Fitness function +
 //////////////////////////// +
 // Evaluation: from a plain +C++ fn to an EvalFunc Object ... +
 eoEvalFuncPtr<Indi, double, const +vector<bool>& > plainEval( binary_value ); +
// ... to an object +that counts the nb of actual evaluations +
 eoEvalFuncCounter<Indi> eval(plainEval);
+ + + + + +
 //////////////////////////////// +
 // Initilisation of population +
 //////////////////////////////// +
 // Either load or initialize +
 // create an empty pop +
 eoPop<Indi> pop; +
 // create a state for reading +
 eoState inState; // a state +for loading - WITHOUT the parser +
// register the rng +and the pop in the state, so they can be loaded, +
 // and the present run will +be the exact conitnuation of the saved run +
 // eventually with different +parameters +
 inState.registerObject(rng); +
 inState.registerObject(pop); +

if (load_name +!= "") +
     { +
         +inState.load(load_name); //  load the pop and the rng +
        +// the fitness is read in the file:  +
        +// do only evaluate the pop if the fitness has changed +
     } +
 else +
     { +
         +rng.reseed(seed); +
        +// a Indi random initializer +
        +// based on boolean_generator class (see utils/rnd_generator.h) +
         +eoInitFixedLength<Indi, boolean_generator>  +
random(vecSize, boolean_generator()); +
        +// Init pop from the randomizer: need to use the append function +
         +pop.append(popSize, random);  +
        +// and evaluate pop (STL syntax)  +
         +apply<Indi>(eval, pop); +
     } // end +of initializatio of the population

+ + + + + +
 // sort pop for pretty printout +
 pop.sort(); +
 // Print (sorted) intial population +(raw printout) +
 cout << "Initial Population" +<< endl << pop << endl;
+ + + + + +
 ///////////////////////////////////// +
 // selection and replacement +
 ////////////////////////////////////
+ + + + + +
 // The robust tournament selection +
 eoDetTournament<Indi> selectOne(tSize);           +// tSize in [2,POPSIZE] +
 // is now encapsulated in a +eoSelectPerc (entage) +
 eoSelectPerc<Indi> select(selectOne);// +by default rate==1
+ + + + + +
 // And we now have the full +slection/replacement - though with  +
 // no replacement (== generational +replacement) at the moment :-) +
 eoNoReplacement<Indi> replace; 
+ + + + + +
 ////////////////////////////////////// +
 // The variation operators +
 //////////////////////////////////////
+ + + + + +
 // 1-point crossover for bitstring +
 eoBinCrossover<Indi> xover1; +
 // uniform crossover for bitstring +
 eoBinUxOver<Indi> xoverU; +
 // 2-pots xover +
 eoBinNxOver<Indi> xover2(2); +
 // Combine them with relative +rates +
 eoPropCombinedQuadOp<Indi> xover(xover1, +onePointRate); +
 xover.add(xoverU, URate); +
 xover.add(xover2, twoPointsRate, +true);
+ + + + + +
 // standard bit-flip mutation +for bitstring +
 eoBinMutation<Indi>  mutationBitFlip(pMutPerBit); +
 // mutate exactly 1 bit per +individual +
 eoDetBitFlip<Indi> mutationOneBit;  +
 // Combine them with relative +rates +
 eoPropCombinedMonOp<Indi> mutation(mutationBitFlip, +bitFlipRate); +
 mutation.add(mutationOneBit, oneBitRate, +true); +
 // The operators are  encapsulated +into an eoTRansform object +
 eoSGATransform<Indi> transform(xover, +pCross, mutation, pMut);
+ + + + + +
 ////////////////////////////////////// +
 // termination condition see +FirstBitEA.cpp +
 ///////////////////////////////////// +
 eoGenContinue<Indi> genCont(maxGen); +
 eoSteadyFitContinue<Indi> steadyCont(minGen, +steadyGen); +
 eoFitContinue<Indi> fitCont(vecSize); +
 eoCombinedContinue<Indi> continuator(genCont); +
 continuator.add(steadyCont); +
 continuator.add(fitCont);
+ + + + + +
 // but now you want to make +many different things every generation  +
 // (e.g. statistics, plots, +...). +
 // the class eoCheckPoint is +dedicated to just that: +
 // Declare a checkpoint (from +a continuator: an eoCheckPoint  +
 // IS AN eoContinue and will +be called in the loop of all algorithms) +
 eoCheckPoint<Indi> checkpoint(continuator); +

    +// Create a counter parameter +
     eoValueParam<unsigned> +generationCounter(0, "Gen."); +

    +// Create an incrementor (sub-class of eoUpdater). Note that the  +
     // parameter's +value is passed by reference,  +
     // so every +time the incrementer is updated (every generation), +
     // the data +in generationCounter will change. +
     eoIncrementor<unsigned> +increment(generationCounter.value()); +
    +// Add it to the checkpoint,  +
     // so the +counter is updated (here, incremented) every generation +
     checkpoint.add(increment); +
    +// now some statistics on the population: +
     // Best fitness +in population +
     eoBestFitnessStat<Indi> +bestStat; +
     // Second +moment stats: average and stdev +
     eoSecondMomentStats<Indi> +SecondStat; +
    +// Add them to the checkpoint to get them called at the appropriate +time +
     checkpoint.add(bestStat); +
     checkpoint.add(SecondStat); +
     // The Stdout +monitor will print parameters to the screen ... +
     +eoStdoutMonitor monitor(false); +

     // when called +by the checkpoint (i.e. at every generation) +
     +checkpoint.add(monitor); +
     // the monitor +will output a series of parameters: add them  +
     +monitor.add(generationCounter); + +
     monitor.add(eval); +// +because now eval is an eoEvalFuncCounter! +
     monitor.add(bestStat); +
     monitor.add(SecondStat); +
     // A file +monitor: will print parameters to ... a File, yes, you got it! +
     eoFileMonitor +fileMonitor("stats.xg", " "); +

     // the checkpoint +mechanism can handle multiple monitors +
     checkpoint.add(fileMonitor); +
     // the fileMonitor +can monitor parameters, too, but you must tell it! +
     fileMonitor.add(generationCounter); +
     fileMonitor.add(bestStat); +
     fileMonitor.add(SecondStat); +
     // Last type +of item the eoCheckpoint can handle: state savers: +
     +eoState outState; +
     // Register +the algorithm into the state (so it has something to save!!) +
     +outState.registerObject(rng); +
     outState.registerObject(pop); +
     // and feed +the state to state savers +
    +// save state every 100th  generation +
     eoCountedStateSaver +stateSaver1(100, outState, "generation");  +
     // save state +every 1 seconds  +
     eoTimedStateSaver    +stateSaver2(1, outState, "time");  +
    +// Don't forget to add the two savers to the checkpoint +
     checkpoint.add(stateSaver1); +
     checkpoint.add(stateSaver2); +
     // and that's +it for the (control and) output

+ + + + + +
 ///////////////////////////////////////// +
 // the algorithm +
 //////////////////////////////////////// +
 // Easy EA requires  +
 // selection, transformation, +eval, replacement, and stopping criterion +
 eoEasyEA<Indi> gga(checkpoint, +eval, select, transform, replace); +
 // Apply algo to pop - that's +it! +
 gga(pop);
+ + + + + +
 // Print (sorted) final population +
 pop.sort(); +
 cout << "FINAL Population\n" +<< pop << endl;
+ + + + + +
} +
// A main that catches the exceptions +
int main(int argc, char **argv) +
{ +
#ifdef _MSC_VER +
     int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF); +
       flag +|= _CRTDBG_LEAK_CHECK_DF; +
     _CrtSetDbgFlag(flag); +
//    _CrtSetBreakAlloc(100); +
#endif +
     try +
     { +
             +main_function(argc, argv); +
     } +
     catch(exception& +e) +
     { +
             +cout << "Exception: " << e.what() << '\n'; +
     } +
     return 1; +
}
+ +


Back to Lesson 3 - Tutorial +main page - Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+Marc Schoenauer
+ +
Last modified: Sun Nov +26 09:31:04 2000 + + diff --git a/eo/tutorial/html/beige009.jpg b/eo/tutorial/html/beige009.jpg new file mode 100644 index 00000000..594c01d8 Binary files /dev/null and b/eo/tutorial/html/beige009.jpg differ diff --git a/eo/tutorial/html/binary_value.html b/eo/tutorial/html/binary_value.html new file mode 100644 index 00000000..5fd4134c --- /dev/null +++ b/eo/tutorial/html/binary_value.html @@ -0,0 +1,57 @@ + + + + + + binary_value.h + + +Back to Lesson 2 - Tutorial +main page - Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+
+

+binary_value.h

+ + + + + +
#include <eo> +
//----------------------------------------------------------------------------- +
/** Just a simple function that takes binary +value of a chromosome and sets +
     the fitnes. +
     @param _chrom A +binary chromosome  +
*/
+ + + + + +
double binary_value(const vector<bool>& +_chrom) +
{ +
 double sum = 0; +
 for (unsigned i = 0; i < _chrom.size(); +i++) +
     sum += _chrom[i]; +
 return sum; +
}
+ +
Back to Lesson 2 - Tutorial +main page - Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+Marc Schoenauer
+ +
Last modified: Wed Nov +29 09:03:09 2000 + + diff --git a/eo/tutorial/html/costume.jpg b/eo/tutorial/html/costume.jpg new file mode 100644 index 00000000..b29768e4 Binary files /dev/null and b/eo/tutorial/html/costume.jpg differ diff --git a/eo/tutorial/html/debut.html b/eo/tutorial/html/debut.html new file mode 100644 index 00000000..e580012b --- /dev/null +++ b/eo/tutorial/html/debut.html @@ -0,0 +1,21 @@ + + + + TITRE + + + +Tutorial main page - +Top-Down +page - Bottom-up page - Programming +hints - EO documentation +
+
+ + +
+

TITRE

+
+ + +

diff --git a/eo/tutorial/html/eoBottomUp.html b/eo/tutorial/html/eoBottomUp.html new file mode 100644 index 00000000..aa150ab5 --- /dev/null +++ b/eo/tutorial/html/eoBottomUp.html @@ -0,0 +1,74 @@ + + + + + + EO - The Bottom-Up approach + + +Tutorial main page - +Top-Down +page - Bottom-up page - Programming +hints - EO documentation +
+


+
+

+EO - Bottom-Up approach

+ +


Congratualtions - You have chosen the bottom-up approach!  +From here you will be allowed to browse into the different components of +an Evolutionary Algorithm, and to see how to program your favorite using +the EO library. + +

+Table of Content

+ +
+

+ +

Yes, this is the table of content of this part +of the EO tutorial. If you don't already know what this symbolic + representation of an EA means, you should try here. Otherwise, click on the figure +to go directly to the corresponding section of the tutorial. +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
+


+
Tutorial main page - Top-Down +page - Bottom-up page - Programming +hints - EO documentation +
+
+
+Marc Schoenauer
+ +
Last +modified: Mon Oct 30 07:28:36 CET 2000 + + diff --git a/eo/tutorial/html/eoEngine.html b/eo/tutorial/html/eoEngine.html new file mode 100644 index 00000000..d7de5460 --- /dev/null +++ b/eo/tutorial/html/eoEngine.html @@ -0,0 +1,59 @@ + + + + + + Genetic Engine + + + + +
+

+Evolution Engine

+ +


Contents +

    +
  • +Introduction
  • + +
  • +Selection
  • + +
  • +Replacement
  • + +
  • +Popular evolution engines
  • +
+ +


The term evolution engine denotes the different parts that simulate +the Darwinism in Evolutionary Algorithms. +

+

The fittest individuals are more likely to +reproduce and survive.

+ +

Darwinism takes place in two different phases of an EA, though in many +popular variants, only one phase is activated. +

Selection is the Darwinistic choice of parents +that will be allowed to reproduce. +
Replacement takes place after reproduction, +and is the Darwinistic choice of those individuals that will survive, +i.e. become the parents of the next generation. +

Selection +
  +

Replacement +
  +

Popular +evolution engines +
  +
  +

+


+
+Marc Schoenauer
+ +
Last +modified: Mon Oct 30 18:15:17 CET 2000 + + diff --git a/eo/tutorial/html/eoEval.html b/eo/tutorial/html/eoEval.html new file mode 100644 index 00000000..7d61f7e7 --- /dev/null +++ b/eo/tutorial/html/eoEval.html @@ -0,0 +1,34 @@ + + + + Evaluation + + + +Tutorial main page - +Top-Down +page - Bottom-up page - Programming +hints - EO documentation +
+
+ +
+

Evaluation

+
+ + + +
+
+Tutorial main page - +Top-Down +page - Bottom-up page - Programming +hints - EO documentation +
+
Marc Schoenauer
+ + +Last modified: Mon Nov 6 10:54:33 CET 2000 + + + diff --git a/eo/tutorial/html/eoGeneration.html b/eo/tutorial/html/eoGeneration.html new file mode 100644 index 00000000..1c59d372 --- /dev/null +++ b/eo/tutorial/html/eoGeneration.html @@ -0,0 +1,19 @@ + + + + Generation + + + +

Generation

+ + + +
+
Marc Schoenauer
+ + +Last modified: Mon Oct 30 19:29:29 CET 2000 + + + diff --git a/eo/tutorial/html/eoInit.html b/eo/tutorial/html/eoInit.html new file mode 100644 index 00000000..10fa0f44 --- /dev/null +++ b/eo/tutorial/html/eoInit.html @@ -0,0 +1,36 @@ + + + + Initialization + + +Tutorial main page - +Top-Down +page - Bottom-up page - Programming +hints - EO documentation +
+
+ + +
+

+Initialization

+ + + + + +
+
+Tutorial main page - Top-Down +page - Bottom-up page - Programming +hints - EO documentation +
+
+Marc Schoenauer
+ + +Last modified: Mon Nov 6 11:25:18 CET 2000 + + + diff --git a/eo/tutorial/html/eoIo.html b/eo/tutorial/html/eoIo.html new file mode 100644 index 00000000..4e37700f --- /dev/null +++ b/eo/tutorial/html/eoIo.html @@ -0,0 +1,25 @@ + + + + Input / Output + + + +

Input / Output

+ +

+ +

Stopping criteria

+ +

+ +

Displaying statistics

+ +
+
Marc Schoenauer
+ + +Last modified: Tue Oct 31 18:32:22 CET 2000 + + + diff --git a/eo/tutorial/html/eoLesson1.html b/eo/tutorial/html/eoLesson1.html new file mode 100644 index 00000000..e7bf1589 --- /dev/null +++ b/eo/tutorial/html/eoLesson1.html @@ -0,0 +1,413 @@ + + + + + + Tutorial: Lesson 1 + + +Lesson 2 - +Tutorial +main page - +Top-Down page - Bottom-up +page - Programming hints -EO +documentation +
+
+
+

+Tutorial: Lesson 1

+This lesson will let you +
    +
  • +run your first Evolutionary Algorithm written within +EO Library, choosing between evolving bitstrings, +or evolving vectors of real numbers,
  • + +
  • +browse through the code of these algorithms, or
  • + +
  • +follow the guided tour.
  • +
+Later you will be asked to +
    +
  • +write your own fitness function,
  • + +
  • +use different kinds of selection procedures +in the framework of a generational GA evolution engine,
  • + +
  • +check that EO let you separate the representation +from the evolution engine.
  • +
+ +

+I want to run an Evolutionary Algorithm +now

+You can choose to run a standard bitstring Genetic +Algorithm (as defined in Goldberg's book) or a standard real-valued +genetic algorithm, as proposed in Micahlewicz's book. +

If you have not already done what was recommended in the Tutorial +main page , do it NOW. Then go +to the Lesson1 sub-dir of the tutorial dir, and simply type at the system +prompt +

(myname@myhost) EOdir/Tutorial/Lesson1 % FirstRealGA +
or +
(myname@myhost) EOdir/Tutorial/Lesson1 % FirstBitGA +

and something should happen. +

+What is happening?

+At the moment, the FirstBitGA maximizes the +number of ones in the bitstring (also calls the OneMaxfunction, +whose solution is, as you can guess, +11111111), +and the FirstRealGA is maximizing (the default +action for EO is to maximize) the inverse of the sum (i.e. minimizing the +sum) of the square of its variables (also called the sphere +function, whose solution is ... all zeroes). +

And what you see on the screen when running one of these two programs +is, in each case, the initial and final population of an Evolutionary run, +one individual per line, its fitness first, then the number of items (bits +or real numbers) of the genotype, and the genotype itself. The final population +hopefully contains the solution in the discrete case, and is close to it +in the continuous case. +

You need now to take a look at either programs +by browsing alone through the  sources for FirstBitGA +and FirstRealGA, or follow the guided tour +below, or go directly to the exercises. +

+Browsing the code:

+ +
    +
  • +General includes:Like +all C-like code, the file starts with include directives (Bit +- Real). Apart from standard includes, +the spedific file to include is eo: +this is a file that contains the list of the most important EO files.
  • + +
      +
  • +Representation: +you then have to delclare the type of individuals you will be handling. +All evolution-related objects you will need are templatized w.r.t. the +type of individuals.
  • + + + +
  • +Fitness function: +the code for the fitness function is included in the file. It must take +as argument a reference to an individual (at the moment).
  • + +
      +
    • +Bit This function simply computes +the number of ones of the bitstring (it's called the OneMax function). +The optimum is of course the all-ones bitstring.
    • + +
    • +Real This function simply computes +the inverse of the sum of the squares of all variables (also called the +sphere function). The optimum is of course the all-zeroes vector.
    • + +
       
    + +
  • +Parameters: +all parameters of the algorithm are declared here (Bit +- Real), and their values and +assigned. Of course, this means that you will need to recompile to change +these values - see Lesson 3 to get rid of that heavy requirement.
  • + +
      +
  • +Random seeding: +Random numbers play an important role in Evolutionary Algorithms. See in +EO +programming hints more details about how this is simulated in EO - +but as far as you are concerned now, remember that the global +Random Number Generator is called rng +and should be used everywhere you need a realization of a random variable +of known law. Moreover, this RNG requires a seed, +which is set here (Bit - Real): +everytime you run the algorithm with the same +seed, you will get the same +result. Hence, to test the robustness of your +algorithm, you should run it with different seeds. This is rather time +consuming in the present programs, so we suggest that you wait until Lesson +3 to do so.
  • + +
      +
  • +Fitness function encapsulation: EO +is based on the notion of functors +- hence you now need to encapsulate your fitness function into a functor +object. This is what is done here (Bit +- Real).
  • + +
      +
  • +Initialization: +to initialize the population, first declare an empty object of class eoPop<Indi>, +which is basically an STL vector<Indi>, +then fill it with Indi's. And remember that +v.push_back +simply appends its argument at the end of STL +vector v.
  • + + + +
  • +Output: take +a snapshot at the initial population (Bit +- Real). Sort it first, so the best +individuals are first, and display it. Note that an eoPop has a << +method, which means that a simple os +<< pop streams the pop +onto the ostream os. +This is true for all objects of of class eoPrintable +(most EO objects) through the method printOn +(which is then called by the << +operator).
  • + +
      +
  • +Evolution engine: +The selection/replacement mechanism (Bit +- Real) is a simple generational +GA here: a simple selector, and a generational replacement. The eoDetTournament +has been chosen as a robust selection, and the generational replacement +(all parents are replaced by the offspring) is hard-coded in the eoSGA +algorithm.
  • + +
      +
  • +Variation operators: +in the simple algorithm considered here, individuals undergo crossover +and mutation. +In EO, these operators are (functor) +objects of class eoQuadOp +(binary operator that modifies both its arguments) and eoMonOp +(unary operator).  These operators are applied in turn to all selected +parents, according to user-defined probabilities.  These probabilities +are defined with all other parameters, and will +be passed to the eoSGAalgorithm.
  • + +
      +
    • +Bit The crossover eoBinCrossover +is the standard 1-point crossover, and eoBinMutation +is the standard bit-flip mutation that randomly +flips all bits with a given probability P_MUT_PER_BIT.
    • + +
      Warning: the P_MUT_PER_BIT +probability is an internal parameter of the +eoBinMutation, +it is NOT the probability of mutation +at the individual level. EO corrects what can be viewed as an inconsistency +in Holland's original work, further used in Goldberg's book by separating +the probability of mutation for each individual (independent of the type +of mutation that will be applied) from the probability of flipping each +bit, which is specific of the bit-flip mutation.  Hence, to run the +same algorithm as Goldberg's SGA, the mutation probability (at individual +level) is 1, and the probability of flipping each bit is P_MUT_PER_BIT. +
    • +Real The crossover eoArithmeticCrossover +is the standard arithmetic crossover for real-valued +vectors, that chooses a point randomly on the segment between both parents +(also termed BLX-0). eoUniformMutation +is the uniform mutation for real-valued vectors +that choses a new value for each variable uniformly on an interval centered +on the parent value. The width of the interval is an internal +parameter of the object, here called EPSILON.
    • + +
       
    + +
  • +Stopping criterion: +Specify a maximum number of generations +to run (Bit - Real): +the simplest of all stopping criteria at the moment, using an object of +a sub-class of class eoContinue.
  • + +
      +
  • +The algorithm: the +simple algorithm that is used here, called  eoSGA +requires +as parameters a selector, +a crossover and +the associated crossover rate, +a mutation and +the associated mutation rate, +and a stopping criterion. +Take a look at the corresponding +constructor +of the class eoSGA: +it only initializes its private data +with the parameters. Now look at the operator() +method - the one that is called in the code for FirstBitGA +or FirstRealGA - and you'll find +out that is is as simple as it sounds.
  • + +
      +
  • +Output: After +running the algorithm, output the sorted final population (Bit +- Real) - and look at the best +individual: this is the result of the algorithm.
  • + +
      +
  • +Main body: for +technical reasons (intercepting the exceptions), we need a main like this +one (Bit - Real)., +and you should not touch it unless you know what you are doing. Simply +note that this main calls the function main_function, which we have been +discussing up to now!
  • +
+ +

+Exercise 1: maximize your +own function

+This is very easy - if your search space is that of bitstring or of unbounded +real numbers. +
    +
  • +Go to the tutorial directory, and copy the +program you want to modify onto mytest.cpp.
  • + +
  • +Edit mytest.cpp +with any text editor:
  • + +
  • +Modify the fitness function itself (binary_value +in FirstBitGA,real_value +in  FirstRealGA)
  • + +
  • +Compile the program by typing make +mytest at system prompt
  • + +
  • +Run the new program by entering the command +mytest +at system prompt.
  • +
+ +

+Exercise 2: check the differences between both programs

+Go and take a look at the code for these programs (Bit +- Real). Use the symbolic representation +of an Evolutionary Algorithm (you should understand that figure now, otherwise +go there and come back) to understand how each +part of the EA is coded. Try to spot the differences between both codes: +there are not so many! +
After you've tried that alone, take a look at the solution +:-) +
  +

+Exercise 3: change the selection procedure

+This is rather straightforward ... if you know what other types of selection +are available! +
At the moment, let's only consider only the following simple ones: +
    +
  • +You already know the tournament selection
  • + +
    Syntax:  eoDetTournament<Indi> +select(T_SIZE);   // T_SIZE in [2,POP_SIZE) +
  • +Try the well-known roulette wheel
  • + +
     Syntax:    eoProportional<Indi> +select; +
  • +Or the stochastic binary tournament
  • + +
    Syntax:  eoStochTournament<Indi> +select(RATE);     // RATE in ]0.5,1] +
  • +and of course the random selection should +give bad results!
  • + +
    Syntax:  eoSelectRandom<Indi> +select;
+Note that all these classes of eoObjects are derived from the abstract +class +eoSelectOne. +

Lessons: +

    +
  • +in EO, all actions are performed by functor +objects (this section is the last time in this tutorial that there +is a direct link to the EO Programming hints +page - though the link at top and bottom of all pages will remain there).
  • + +
  • +in EO, all object you will usually need to manipulate are templatized +w.r.t. the type of the individual you are handling.
  • + +
  • +The type of the individual is itself templatized +w.r.t. the type of fitness (double by default).
  • + +
  • +In EO (actually, in EC!) initialization and variation +operators are representation-dependent, while +the evolution engine is representation-independent +(well, like any rule, this one does have some exceptions).
  • + +
  • +Changing the fitness function, or the selection +procedure inside the generational GA evolution engine is straightforward.
  • + +
  • +remember, all solutions to exercises are in +the same sub-dir of dir Tutorial than the lesson itself (see here).
  • +
+ +
Lesson 2 - +Tutorial +main page - +Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+
+Marc Schoenauer
+ +
Last +modified: Fri Nov 3 18:49:12 CET 2000 + + diff --git a/eo/tutorial/html/eoLesson2.html b/eo/tutorial/html/eoLesson2.html new file mode 100644 index 00000000..4bf3e37e --- /dev/null +++ b/eo/tutorial/html/eoLesson2.html @@ -0,0 +1,301 @@ + + + + + + Tutorial: Lesson 2 + + + +Lesson 1 - +Lesson +3 - +Main page - +Top-Down +- Bottom-up - Hints +- EO +documentation +
+
+
+

+Tutorial Lesson 2: more encapsulations

+In this lesson, the same Evolutionary Algorithm will be rewritten in a +much more general context. +
First, look at the changes that have been done +to the algorithms. Then benefit from the new features by +
    +
  • +minimizing (and not only maximize) the fitness
  • + +
  • +combining several +operators of the same type
  • + +
  • +combining several +stopping criteria
  • + +
  • +use alternate selection/replacement +engines, deviating from the pure generational GA
  • +
+ +


Again, two basic algorithms are provided, namely FirstBitEA +and FirstRealEA. +
To compile and run them, go to the Lesson2 +sub-directory of the tutorial dir and simply type make. +Both examples should get compiled, and you can then run them by calling +their name from the system prompt. +

Note the slim difference in names, from GA +to EA: the behavior of these  EAs is +almost identical to that of their GA counterpart, at least with the default +settings that are provided. But their potentialities for easy modifications +are much larger, both in terms of variation operators +and of evolution engine (i.e. selection/replacement +mechanism).  +


Changes +

Browse through the code, and discover them one after the other: +

    +
  • +The fitness function +now +lies in a separate file +(Bit +- Real). But, more important, its +argument is a vector<bool> or a vector<double>, +and not an unknown type. This will allow to use the same file for any EO +object that is a sub-class of the corresponding STL vector class.
  • + +

    Note: Also, +a non-templatized fitness can be compiled +separately (not done here) into an object +file once and forall (remember +that templates forbid that). +
      +

  • +The encapsulation +of +the fitness (Bit +- Real) looks more complicated: you +have to declare 3 template arguments: the type of EO object it will be +applied to, the return type and the type of argument the function actually +requires.
  • + +

    Note: In the +previous files (Bit - Real) +, the last 2 types were deduced from the first (2nd argument = fitness +type of EO object, third = first). +
      +

  • +Both the above modifications makes it very easy to +minimize +rather than maximize a fitness function (see Exercise +1).
  • + +
      +
  • +The initialization +of the population is now encapsulatedinto +a separate initializer (based +on a boolean generator or a double-number +generator -see random_generators.h) +that is then used in the constructor of the population to build the individuals. +You can also use different initializers and call them in turn through the +call to pop.append() function +(see Exercise 2).
  • + +

    Note: Don't +forget to evaluate the population: +the eoPop has no idea of the eval function, so it has to be done from outside!!! +
      +

  • +You can now use +different +crossover +and mutation +operatorsin the same algorithm +(Bit - Real), +choosing among them according to +relative +rates. The class eoPropCombinedxxxOp, +where +xxx is either Mon (for mutation, of class eoMonOp) +or Quad (for crossovers, of class eoQuadOp), +is derived from the corresponding eoxxxOp class. When applying the eoPropCombinedxxxOp, +one of the eoxxxOp it contains is chosen by a roulette +wheel, according to their respective rates, and is applied to the arguments.
  • + +

    Note: A third optional argument +in method add is a boolean +(defaulted to false). When true, the actual rates for all operators are +displayed on the screen as percentages: you don't have to input rates that +sum up to 1, all rates are scaled anyway. +

    Note: The operators have to be encapsulated +into an eoTransform object +(Bit - Real) +to be passed to the eoEasyEA algorihtm. +The eoSGATransform is a simple +eoTransform +that does exactly the same thing than eoSGA: +each pair from the selected parents undergoes the crossover +operator with given probablity, and all individuals (after crossover +eventually) undergo mutation with given probability. +The arguments to the eoSGATransform +are an eoQuadOp with its probability +and an eoMonOp with the associated +probability. +
      +

  • +You can use combinations +of +several stopping criteria by using an object of the class eoCombinedContinue +(Bit +- Real). Initialize it with an object +of class eoContinue, and +addas +many of other such objects as you wish. And as an eoCombinedContinue +is +aneoContinue, +simply pass it to the algorithm (Bit +- Real).
  • + +
      +
  • +The +full selection/replacement mechanism is +now in place through the eoEasyEA +algorithm.
  • + +
    This means that you can use different selectors. +which was already true in Lesson 1, but also different replacement +strategies (see Exercise 3) whereas generational +replacement was hard-coded in the algorithm eoSGA +used in Lesson1. +

    Beware that we have to encapsulate  (Bit +- Real) the eoDetTournament, +which is of class eoSelectOne (i.e. allows +to select one individual from a population, its operator() +returning a single individual) into an object of the eoSelectPerc +(perc stands for percentage) which allows to select a ... percentage of +a population (his operator()  +returns a population). This was done internally in the  constructor +of eoSGA  - see lesson1.

+ +
Exercice +1: minimizing +
Modify the algorithm so that it minimizes the +fitness. +
    +
  • +For the bitstring case, you only have to modify the +declaration +of the representation, using eoMinimizingFitness +instead of double. +But is that really all? Give it a try, look at the output, and do it right +the second time!!!
  • + +
  • +For the real-valued problem, you also need to modify +the file real_value.h so +that it returns the sum of squares instead of its inverse. And again there +is something else to modify...
  • +
+Exercice +2: initialization +
Use different initializers: for instance, on +the real-valued sphere function minimization, try to initialize half of +the population in [-2,-1] and the other half in [1,2], with and without +the segment and arithmetic crossovers (and for large values of VEC_SIZE, +the size of the vectors). Amazing, isn't it! Explain that result. +

Exercice +3:  replacement +
You can now twidle the number of offspring that +will be generated from the parents. But of course you need to adjust the +replacement to keep a constant population size. +

    +
  • +To modify the number +of offspring, use the second argument of the +encapsulator +(Bit - Real) +of the selector +of class eoSelectOne +into an eoSelectPerc object. For instance, try
  • + +
                    +eoSelectPerc<Indi> select(selectOne,2.0) +
    to generate twice as many offspring as there +are parents. +
  • +To keep a constant population +size, you can use either the eoCommaReplacement +class, or the eoPlusReplacement. +The former selects the best offspring to replace the parents, the latter +selects the best among parents+offspring. Of course you cannot use eoCommaReplacement +if you have less offspring than parents!
  • + +
    Now if you use eoSelectRandom +as selector with a rate of +lambda, you end up with exactly the (mu+lambda) +or +(mu,lambda) strategies from Evolution +Strategies. +
  • +Question: what do you +get if you have use a rate of 1/POP_SIZE for the selection, and an eoPlusReplacement +strategy? Yes, you get almost the replace_worst Steady-State GA, though +rather inefficient, as you sort the population at every generation, which +could be avoided - and will be in a later lesson).
  • + +
  • +Homework: Write the +eoCommaPlusReplacement +that would start by taking the best of the offspring, and if some are still +missing to keep the population size constant, take the best of the parents. +Write the eoConservativeReplacement +that starts by taking a percentage of the parents (the best ones) and then +adds the best from the offspring. In both cases, send +use the code as we haven't done that yet (and +hence there is no solution available at the moment - Nov. 29 :-)
  • +
+Remember: all solutions +are in the same sub-directory of the Tutorial dir than the examples (i.e. +here Lesson2), and are described here. +

+


+
Lessons learned: +
    +
  • +How to write a fitness function that only +needs a genotype, not a full individual. Moreover you can compile it separately.
  • + +
    How to initialize the population using +random generators +
  • +How to use other evolution engine than the +simple generational GA.
  • + +
  • +How to combine different objects of the same kind into a single object +that you can use like a simple basic object (operators +and stopping criteria here).
  • +
+ +
Lesson 1 - +Lesson +3 - +Main page - +Top-Down +- Bottom-up - Hints +- EO +documentation +
+
+
+Marc Schoenauer
+ +
Last +modified: Fri Nov 3 18:49:12 CET 2000 + + diff --git a/eo/tutorial/html/eoLesson3.html b/eo/tutorial/html/eoLesson3.html new file mode 100644 index 00000000..a7b08a2b --- /dev/null +++ b/eo/tutorial/html/eoLesson3.html @@ -0,0 +1,531 @@ + + + + + + Tutorial: Lesson 3 + + +Lesson 2 - +Lesson +4 - +Main page - +Top-Down +- Bottom-up - Hints +- EO documentation +
+
+
+

+Tutorial Lesson 3: input/output

+In this lesson, you will still use the same Evolutionary Algorithm, BUT +in a much more user-friendly way. You +will discover how to +
    +
  • +input parameters on the command-line or from a text +file
  • + +
  • +save the population to disk, together with every part +of the algrithm you could think of - so you can decide to reload +everything later to continue the same run, eventually with different parameters.
  • + +
  • +generate statistics on the populations, and output +them to the screen, text or graphic, or to a file (or to any other +device you might want to use).
  • +
+First, but you should now have done it without being told, go into the +Lesson3 +sub-dir of the tutorial dir and type +make. +This will compile the SecondBitEA +program (and, some day, SecondRealEA). +

You can then either +

    +
  • +browse the corresponding code (only SecondBitEA +available right now, but you can figure out how SecondRealEA will look +like),
  • + +
  • +look at the summary of changes,
  • + +
  • +or find out directly explanations about the new features: the eoParser, +eoState +and eoCheckpoint classes.
  • +
+ +


+


+
Changes +
As already said, the behavior of the algorithm +will be exactly the same as the previous one as far as optimization is +concerned. Only the input (of algorithm parameters) and output (of program +results) will be very different. +
Hence, the sections corresponding to the fitness +function, the initialization, the +variation +operators, the evolution engine +and the algorithm itself are +almost identical (apart from variable name changes). +
    +
  • +Fitness function: +there is an additional line +after the encapsulation of our binary_function +into an eoEvalFunc +object, which again encapsulate the eoEvalFunc +into an eoEvalFuncCounter. +As its name says, thisobject will, in addition to computing the fitness, +count the actual +number of evaluations: the fitness of non-modified individuals is of course +not recomputed - and this is taken care of by this object. Moreover, it +can be later used for displays +in eoMonitor objects, as done in the checkpoint +section.
  • + +
  • +The initialization +section has been extended to account for the possibility to re-load +a previously saved population. This is achieved +through an eoState object, if the corresponding program +parameter is set.
  • + +
  • +The +variation +operators and the evolution engine  +sections are similar to the ones in Lesson2
  • + +
  • +The parameter section  +is completely different from the previous one. All variables corresponding +to program parameters +are now declared +in the main_function +(as before), but their values are set in a new +function, called read_param. +See the eoParser description for more details.
  • + +
  • +The stopping criterion +section, has in fact now become the checkpoint section, as it involves +much more than just stopping criteria. See all details in the eoCheckpoint +paragraph below.
  • +
+ +
eoParser: +parameter input +
The first two examples of Lessons 1 and 2 had +a very crude way to set parameter values: they were hard-coded, and you +had to recompile the whole program to change a single value. We shall now +see now to set parameter values in a flexible way (though we're still looking +for volunteers to create a Graphical User Interface :-) +
Two base classes are used for that purpose: +
    +
  • +The eoValueParam +class, templatized by the type of the variable +you want to handle (i.e. integer, double, +yourPrivateClass, ...). In this lesson, +we will not go into details: e.g. we will not tell you that the +eoValueParam +is actually a templatized sub-class of abstract class eoParam (oups, I +said it!), nor will we deal with parameters outside their use from an eoParser. +See the parameter section of the Bottom-up tutorial, or wait until lesson +4).
  • + +
  • +The eoParser +class, whose only purpose is the input of parameters.
  • +
+Modifying parameter values at run-time: +
Using an eoParser object, the parameter values +are read, by order of priority +
    +
  1. +from the command-line
  2. + +
  3. +from a text file
  4. + +
  5. +from the environement
  6. + +
  7. +from default values
  8. +
+The syntax of parameter reading is a keyword-based +syntax, now traditional in the Unix world: +
    +
  • +in EO, each parameter is designated by a (long) keyword, +and optionally by a short (1 character) keyword.
  • + +
      +
  • +the general syntax to modify parameter value at run-time is (either from +the command-line or in a text file)
  • + +
                        +--longKeyword=value     +or     -c=value    +if 'c' is the short keyword +
      +
  • +so, after compiling the executable for Lesson 3 (make +lesson3 at system prompt in Unix), you can try to type +in
  • + +
                         +SecondBitEA +
    and see the algorithm run as before (OneMax optimized on 8-bits bitstrings). +But you can now type in +
                         +SecondBitEA --vecSize=100 +
    and see the output of the optimization of OneMax on 100-bit bitstings. +
      +
  • +Take a look at all available parameters by typing in
  • + +
                         +SecondBitEA --help +
    or by going into the code: all parameter inputs have been grouped in +the +read_param function. +
      +
  • +After running the algorithm, a new file has been created, named SecondBitEA.status: +it contains the list of all actual parameters used, and can directly be +used as parameter input file: change the file name (e.g. to SecondBitEA.param), +edit it, change whichever parameter you want, and type in
  • + +
                          + +SecondBitEA @SecondBitEA.param +
    and you will see all values that you defined into the file taken into +account. +
      +
  • +The priority remains to the command-line, +so you can still override the values in the parameter file by giving a +new value directly on the command-line.
  • +
+Programming parameter input: +
the code of SeconBitEA provides examples of parameters reading. Lets +take the example of the random number generator seed. +
    +
  • +You first need to declare it +in the main_function. As parameter +reading will be done in the read_param +function, you need to pass it the variable seed. Note that read_param receives +it by reference, as it is going +to modify its value!
  • + +
  • +In read_param, you need first to declare +an eoParser object (it needs the standard argc and argv in its constructor).
  • + +
    Then, declare a parameter +of type uint32 (32-bits integer), +and read it  directly from the parser, using method create_param. +The arguments are obvious: default value, long keyword, comment (that will +appear in the help message and in the output "status" file if any). +
  • +Finally, you need to assign the +value to the variable _seed (hence modifying the original seed variable).
  • +
+ +
+
eoState: +saving and loading +
You might have noticed in the  read_param +described above a new parameter +named load_name. +Now if you go to the init section of +the code, you will see an alternative way of initializing +the population: if load_name is an empty string, +then we do as in the preceding example and use an eoInitFixedLength object. +However, if a load_name name was entered, the population is read through +the inState.load(load_name) +instruction. Moreover, the comment says "Loading pop and +rng". +

This is made possible using the eoState +class. eoState +objects maintain references to eoObjects +that have both an input method (readFrom) +and an output method (printOn), +i.e. that derive from the base class eoPersistent. +You must first register +object into a state, and can then save them to a (text) file, and later +read them from that file using the load +method, as done here. +
Of course, you can call the save +method for an eoState +object anywhere in the code. But the checkpointing +mechanism offers you better ways to do that - and it's so easy .... +

+


eoCheckpoint: +every generation I'd like to ... +
The checkpointing mechanism is a very powerfull +construct to perform some systematic actions +every generation - like saving things +(using eoState objects described above), computing statistics +on the population, updating +dynamical parameters or displaying +information. +

eoCheckpoint +objects are eoContinue +objects that contain pointers to different +types of objects. When their operator() +method is called (i.e. every generation in the examples up to now), they +first call the operator() +methods of all object they contain, and then return their result as an +eoContinue +object (i.e. should we continue or stop). +
Programming: To +do something every generation, you simply need to add +an object whose operator() +does what you want to the eoState that you will use as continuator in the +algorithm. +
+


eoCheckpoint: +Stopping +
The eoContinue +part of an eoCheckpoint +is a single object, passed to the +constructor. If you want more that one stopping criterion, use an eoCombinedContinue +object as described in Lesson2. +
+
+
eoCheckpoint: Computing +statistics +
Statistics are computed using eoStat +objects, i.e. functor objects whose operator() +receives as argument a reference to a population as argument, and can hence +compute whatever is needed over that population. eoStat +objects are templatized +over the type of what they compute (e.g. double, +or pair<double>, +or ...). But looking at the inheritance +diagram of the eoStat +class, you find that eoStat +objects are also eoValueParam +objects. And this allows eoStat +to be used within eoMonitor +object, and hence displayed +to the user! +

Available statistics: Some widely +used statistics are already available (and of course you can build you +own!). +

    +
  • +eoBestFitnessStat returns +the fitness value of the best individual in the population (of type FitnessType, +whatever this is).
  • + +
  • +eoAverageStat and eoSecondMomentStat +respectiveley return the average (type double, assumes that FitnessType +is castable to a double) and a pair made of the average and the standard +deviation (type pair<double>) +of the fitnesses in the populations.
  • + +
  • +eoDiversityStat returns the +diversity in the population: asssuming that there is a distance function +defined among individuals, it returns the average inter-individuals distance. +See also Exercise 2.
  • +
+Programming: To compute some statistics +within your algorithm, simply declare +the corresponding eoStat objects, and add +them to the eoCheckpoint you +use in the algorithm. +

Note: actually, there are 2 disctinct +classes that compute and gove access to statistics: eoStat +and eoSortedStat. As its name +indicate, the latter is used whenever computing the statistics require +a sorted population: not only this avoids to sort the population many times, +but also it avoids changing the order of the population at all as eoSortedStat +work on a temporary vector of fitnesses . But as +far as their usage is concerned, its makes no difference. +
+


+
eoCheckpoint: Displaying +eoParameters +
The eoMonitor +objects are used to display a set of eoValueParam +objects. +

Available monitors: A few monitors +are available in th eEO distribution: +

    +
  • +eoStdoutMonitor displays its +parameters in text format on the screen. The +(optional) boolean value in the constructor modifies the output: when true +(the default), vebose output is used, with one line per parameter. When +false, parcimonious output displays one line for all parameters.
  • + +
  • +eoStdoutMonitor writes its +parameters in text format in a file. A file +name is required in the constructor, and an optional separator character +can be added (default is ','). Note that the file is overwritten by next +call to the same program.
  • + +
  • +eoGnuplot1DMonitor displays +its parameters in graphical format on the screen +by calling the gnuplot program, +and as of today, only works in the Unix version of EO (as always, volunteers +are welcome to port that to MS Windows). It takes an optional filename +as input, as communication of data with gnuplot +is done through a file. If no filename is provided, the file will be erased +at the end of the run, while it is otherwise kept (though it will be overwritten +by next call to the same program).
  • +
+ +


Programming: To display something +while the algorithm is running, you need to declare +an eoMonitor object, add +some objects (that must be eoValueParam +objects) to that monitor, and of course add +the monitor to the eoCheckpoint +you use in the algorithm. +
+


+
eoCheckpoint: Updating +things +
The last type of objects that  eoCheckpoint +can handle are eoUpdater +objects. You should simply encapsulate in an eoUpdater +anything you wish to do which does not fit into one of the above category. +Note that their operator() method +does not receive any argument. +

Available monitors: A few updaters +are available in the EO distribution: +

    +
  • +eoIncrementor A simple updater +which maintains a counter (an eoValueParam +that needs to be created beforehand, and passed in the constructor). It +is incremented everytime the operator() +method is called (every generation at the moment). You can of course also +give an increment in the constructor (1 by default).
  • + +
  • +eoCountedStateSaver +and eoTimedStateSaver can +be used to save some existing eoState +(see above) to a file regularly, either based on the +generation count (e.g. every 4 generations) or based on the clock (e.g. +every 5 seconds).
  • +
+Programming: +
A very simple example of using an eoUpdater +is given in the code for SecondBitEA: First declare +an eoValueParam object, then +use +it to construct an eoIncrementor +that you must add to the eoCheckpoint +in order to activate its update. You can then use the parameter for your +purpose, for instance as a first coordinate for a monitor. +
Note also how to use the statesavers: first declare +a state, then register +whatever you think necessary to that state, then pass +the state to some state-saver - and don't forget to add +the statesavers to the current eoCheckpoint. +
+
+
Exercice 1: +
    +
  • +The code of SecondBitEA +display things in the current window in text format. Replace the eoFileMonitor +by an eoGnuplot1DMonitor +and watch the graphical output (Unix +systems with gnuplot +installed only, sorry).
  • + +
  • +Note that you must also replace the eoSecondMomentStat +by an eoAverageStat, +otherwise the standard deviations won't make any sense here.
  • + +
  • +Please try to understand why the average is always +0 before taking alook at the solution (file exercise1.cpp).
  • + +
  • +Then run
  • + +
              +exercise1 --vecSize=1000 --maxGen=1000 +
    to get a chance to see something hapenning before +the program ends!
+ +
Exercice 2: +
Write the eoDiversityStat +stat computation and test it. Thanks to send us the code! +
+
+
Exercice 3: +
Write the code for an eoGnuplot1DwithErrorbarsMonitor +that would take into account the standard deviations and display them as +errorbars. +
Again, send us the code afterwards, thanks :-) +
+
+
Lessons learned: +
    +
  • +Value of program parameters can be set at run-time +using the eoParser +class.
  • + +
  • +Snapshots of the algorithms can easily +be saved (and restored) +thanks to the eoState +class.
  • + +
  • +The eoCheckpoint +mechanism let you do things every generation +without modifying existing algorithms, by simply writing the necessary +code and encapsulating it into an object that eoCheckpoint +is aware of, that are at the moment the following:
  • + +
  • +computing statistics, displaying +parameters +(e.g. statistics),  saving the +(eo)State +of the program.
  • +
+In next lesson you will find out that many adaptive +techniques (the state-of-the-art in Evolutionary Computation) can easily +be programmed through the eoUpdater +construct. +
+
Lesson 2 - +Lesson +4 - +Main page - +Top-Down +- Bottom-up - Hints +- EO documentation +
+
+
+Marc Schoenauer
+ +
Last +modified: Mon Nov 27 8:49:12 CET 2000 + + diff --git a/eo/tutorial/html/eoOperators.html b/eo/tutorial/html/eoOperators.html new file mode 100644 index 00000000..5803ae13 --- /dev/null +++ b/eo/tutorial/html/eoOperators.html @@ -0,0 +1,56 @@ + + + + + + Variation Operators + + + +

+Variation Operators

+Variation operators modify individuals, or, equivalently, move them in +the search space. They are almost always stochastic, +i.e. they generate random variations. Variation operators are classified +depending on the number of arguments they use and/or modify. +

Variation operators involving two individuals are called +crossover operators. +They can either modify one of the parents according +to the material of the other parent, or modify both parents. In EO, the +former are called Binary operators and the latter Quadratic operators. +
  +

Variation operators involving one single individual are called mutation +operators. +

Note that in EO you can define and use variatio operators that generate +any number of offspring fromany number of parents. These are called general +operators, and require advanced knowledge of EO. +

Crossover +
Crossover operators involve two parents, and can modify one of them, +or both of them. +

Using crossover operators +

Mutation +
Mutation operators modify one single individual. The corresponding +EO class is called eoMonOp. +

+Using mutation operators

+The standard EO genotypes (bistrings and real vectors) have pre-defined +mutation operators. +
  +

+Writing a mutation operator

+  +

  +

General +Operators +
  +
  +
  +

+


+
+Marc Schoenauer
+ +
Last +modified: Mon Oct 30 18:24:39 CET 2000 + + diff --git a/eo/tutorial/html/eoOutput.html b/eo/tutorial/html/eoOutput.html new file mode 100644 index 00000000..2a9bc810 --- /dev/null +++ b/eo/tutorial/html/eoOutput.html @@ -0,0 +1,19 @@ + + + + Output + + + +

Output

+ + + +
+
Marc Schoenauer
+ + +Last modified: Mon Oct 30 19:29:19 CET 2000 + + + diff --git a/eo/tutorial/html/eoProgramming.html b/eo/tutorial/html/eoProgramming.html new file mode 100644 index 00000000..7bae8b38 --- /dev/null +++ b/eo/tutorial/html/eoProgramming.html @@ -0,0 +1,256 @@ + + + + + + EO Programming guide + + +Tutorial main page - +Top-Down +page - Bottom-up page - Programming +hints - EO +documentation +
+
+
+

+EO Programming guide

+Templates, +Functors, +STL +Library, random numbers, EO programming style! +
+
+
Templates +
Most EO code si written using templates. This allows to write generic +code, i.e. involving a class which doesn't have to be known when writing +the code -- but only when compiling it. In some sense this is similar to +naming variables in algebra: you can write a lot of equations involving +some variable $x$ without knowing even it if will be an integer or a float +(or a matrix or ...). The main basic type that is templatized in EO is +the fitness: an EO object is some object which has a fitness of some type +F that can be anything. The definition for that is (seeEO.h) +

template<F> class EO +

The idea is that, later in your code, you can declare for instance as +in FirstBitGA.cpp +

typedef eoBin<double> +Genotype; +

meaning that in that file, you will manipulate as Genotype +objects that are EO objects whosefitness +is a double. +

Whereas the advantages are obvious (writing generic reusable code instead +of having to rewrite the same pieces of code for different types), there +are some drawbacks: namely, it makes some of the compiler error messages +hard to understand; and it forbids the compilation of most parts of EO +into an object library file, as the actual types are not known in advance. +

+


+
Functors +

Though EO is a library, it contains almost no functions! +
EO only contains functors, that are objects which have a method called +operator(). +Such objects are used  as if they were a function, but the big differences +are that +

    +
  • +functors are functions with private data
  • + +
  • +you can have different funtors objects of the same class, i.e. you can +use the same functionality with different parameters
  • + +
  • +you can heave a hierarchy of functors objects, which means that you have +a hierarchy of functions with defaults behaviors and specialized sub-functions
  • + +
  • +...
  • +
+Functors are so intimately linked to EO that a base class (eoFunctorBase) +has been designed to hold all functors. This base class is itself divided +into three derived class. These classes tell you immediately what kind +of arguments the operator() method requires +and what kind of result it produces. See EO conventions, +and +the inheritance diagram of class +eoFunctorBase. +

Example +

A good example is given by the eoEvalFuncPtr +class +

class MyClass +
{ ... +
    void operator()(ArgType +arg) +
       { +
            +// do what you have to do +
       } +
}; // end of class declaration +

is used  later in the code in something  like +
  +

ArgType myArgument; +
MyClass myObject;    // myObject +is an object of class MyClass ... +
myObject(myArgument);  // calls method +operator() of object myObject with argument myArgument ... +
  +
  +
  +

+Why not plain C functions?

+Consider for instance variation operators. Of course, we could declare +them as function acting on some objects. +

Blabla +

+


+
A very brief +introduction to STL +

All EO heavily relies on STL, the Standard +Template Library. +
But you don't have to know more than a few words +of STL to use EO (like with "hello", "please" and "goodbye" you +can survive in a foreign country :-) and even to contribute to new EO features. +

You will only find here the basics of STL that you will need to understand +most of EO code - and to guess what the parts you don't understand are +actually doing. Don't worry, I don't +understand everything :-) +

STL provides the user with container +and algorithms. And you can apply (almost) +all algorithms on (almost) all containers. +

Containers +
Containers are high level data types used to hold simpler data - the +most widely used example of a container is the vector +construct. +
The use of STL containers relieve the user from memory management. +

    +
  • +vector The +most widely used container is a one-dimensional array of items.
  • + +
    Data manipulation: suppose v +is an STL vector<AtomType>. +Then +
    v[i] +is the ith element of v, as in standard C arrays +
    v.size() +is the number of elements of v +
    v.push_back(atom) +appends the atom +at end of v, +provided of course that atom +is of type AtomType +
    blabla +
  • +pair
  • + +
    This simple container allows you to hold two +data types together. It is very handy for temporary data handling. Assuming +p is a pair<AtomType1, +AtomType2>, +
    p.first() +and p.second()  +refer to the encapsulated data, of respective types AtomType1and +AtomType2. +
  • +Blabla
  • +
+There are many other types of containers that are not used in EO and that +we will not present here. +

STL Algorithms +
Algorithms are functions acting on containers - the most widely used +example of a STL algorithm is the sort +function. +
Blabla +

Drawbacks +
The main drawback I see in using STL is that it makes it almost +impossible +to use a debugger normally: whereas acess to data is made simple +to the progammer, data structures are actually so complex, and debuggers +so willing to display everything that you get lines of template instanciation +when asking your debugger what is inside some container! For instance I +could never visualize some +v[i] +with gbd, v +being an STL vector! +
But there nonehteless are so many advantages !!! +

+


+
Random +numbers +
Evolutionary Algorithms make intensive use of random numbers. Random +numbers are simulated in computers by using pseudo-random +number generators (RNGs for short), i.e. functions that return series of +numbers who look random (w.r.t. some statistical criteria). +

To make sure the random number generator is as good as possible, and +to ensure reproducibility of the results across diffrerent platforms, EO +has its own RNG, the ``Mersenne Twister'' +random number generator MT19937 (thanks to Takuji +Nishimura, see eoRNG.h +comments). +

Though you can define and use as many RNGs as you wish in EO, the library +also provides you with a global RNG termed rng: using only that single +RNG in all calls to random numbers allows one to be able to reproduce +a given run: +

    +
  • +as strange it seems for a random algorithm, it is mandatory for debugging +purposes
  • + +
  • +random numbers are computed starting from a seed - starting from the same +seed will lead to the same series of pseudo-random numbers, and hence to +the same results of the algorithms. All examples in this tutorial will +use the RNG seeding procedure, see e.g. in Lesson1.
  • + +
  • +to simulate "true" random runs, you can just seed the RNG with a machine-clock +realted number, e.g. calling time(0), as done for isntance in Lesson3 +(and after).
  • +
+As RNGs only produce (by definition) numbers that are uniformly distributed +integers between 0 and some maximal number, EO provides you with random +numbers follwing different probability distribution +(e.g. floating point following normal +distribution). +

EO also provides random_generators +that can be used in STL call to generate series of random numbers, as in +eoPop +initializers. +

+


+
EO conventions +and naming style +
A few naming conventions should help you to navigate more easily through +EO: +
    +
  • +The name of local varoiables shoudl start with a lower case letter
  • + +
  • +The name of the parameters to a function shoudl start with an underscore +(_)
  • + +
    The initialization parameters of constructors, for instance,  +shoudl be named from the names of the variables they are used to initialize. +
  • +The names of classes should start with eo + an Uppercase letter
  • + +
  • +Blabla
  • +
+ +
+
Tutorial main page - Top-Down +page - Bottom-up page - Programming +hints - EO +documentation +
+
+
+Marc Schoenauer
+ +
Last +modified: Mon Nov 6 07:01:57 CET 2000 + + diff --git a/eo/tutorial/html/eoRepresentation.html b/eo/tutorial/html/eoRepresentation.html new file mode 100644 index 00000000..f572f1b4 --- /dev/null +++ b/eo/tutorial/html/eoRepresentation.html @@ -0,0 +1,19 @@ + + + + Representation + + + +

Representation

+ + + +
+
Marc Schoenauer
+ + +Last modified: Mon Oct 30 19:28:01 CET 2000 + + + diff --git a/eo/tutorial/html/eoSGA.html b/eo/tutorial/html/eoSGA.html new file mode 100644 index 00000000..2d22f794 --- /dev/null +++ b/eo/tutorial/html/eoSGA.html @@ -0,0 +1,157 @@ + + + + + + eoSGA.h + + +Back to Lesson 1 - Tutorial +main page - Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+
+

+eoSGA.h

+ + + + + +
//----------------------------------------------------------------------------- +
// eoSGA.h +
//----------------------------------------------------------------------------- +
#ifndef _eoSGA_h +
#define _eoSGA_h +
#include <eoOp.h> +
#include <eoContinue.h> +
#include <eoPop.h> +
#include <eoSelectOne.h> +
#include <eoSelectPerc.h> +
#include <eoEvalFunc.h> +
#include <eoAlgo.h> +
#include <apply.h> +
/** The Simple Genetic Algorithm, following +Holland and Goldberg  +
*  Needs a selector (class eoSelectOne) +a crossover (eoQuadratic,  +
*      i.e. a 2->2 +operator) and a mutation with their respective rates,  +
*      of course +an evaluation function (eoEvalFunc) and a continuator  +
*      (eoContinue) +which gives the stopping criterion. Performs full +
*      generational +replacement. +
*/  +
template <class EOT> +
class eoSGA : public eoAlgo<EOT> +
{ +
public : +
 // added this second ctor as +I didn't like the ordering of the parameters +
 // in the one above. Any objection +:-) MS +
eoSGA( +
       eoSelectOne<EOT>& +_select, +
       eoQuadraticOp<EOT>& +_cross, float _crate, +
       eoMonOp<EOT>& +_mutate, float _mrate, +
       eoEvalFunc<EOT>& +_eval, +
       eoContinue<EOT>& +_cont) +
     : cont(_cont),  +
       mutate(_mutate),  +
       mutationRate(_mrate), +
       cross(_cross), +
       crossoverRate(_crate), +
       select(_select), +
       eval(_eval) +{}
+ + + + + +
 void operator()(eoPop<EOT>& +_pop) +
 { +
    eoPop<EOT> offspring; +
    do { +
         +select(_pop, offspring); +
         +unsigned i; +
         +for (i=0; i<_pop.size()/2; i++)  +
             +{   // generates 2 offspring from two parents +
                 +if ( rng.flip(crossoverRate) )  +
                   +{  +
                       +cross(offspring[2*i], offspring[2*i+1]); +
                   +} +
             +} +
         +for (i=0; i < _pop.size(); i++)  +
             +{ +
                 +if (rng.flip(mutationRate) )  +
                     +{ +
                         +mutate(offspring[i]); +
                     +} +
             +} +
         +_pop.swap(offspring); +
         +apply<EOT>(eval, _pop); +
     } while (cont(_pop)); +
 } +
 
+ + + + + +
private : +
 eoContinue<EOT>& cont; +
 eoMonOp<EOT>& mutate; +
 float mutationRate; +
 eoQuadraticOp<EOT>& cross; +
 float crossoverRate; +
 eoSelectPerc<EOT> select; +
 eoEvalFunc<EOT>& eval;
+ + + + + +
}; +
#endif
+ +
Back to Lesson 1 - Tutorial +main page - Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+Marc Schoenauer
+ +
Last modified: Sun Nov +19 19:36:21 2000 + + diff --git a/eo/tutorial/html/eoSelect.html b/eo/tutorial/html/eoSelect.html new file mode 100644 index 00000000..a415b460 --- /dev/null +++ b/eo/tutorial/html/eoSelect.html @@ -0,0 +1,19 @@ + + + + Selection + + + +

Selection

+ + + +
+
Marc Schoenauer
+ + +Last modified: Mon Oct 30 17:51:55 CET 2000 + + + diff --git a/eo/tutorial/html/eoStop.html b/eo/tutorial/html/eoStop.html new file mode 100644 index 00000000..4e37700f --- /dev/null +++ b/eo/tutorial/html/eoStop.html @@ -0,0 +1,25 @@ + + + + Input / Output + + + +

Input / Output

+ +

+ +

Stopping criteria

+ +

+ +

Displaying statistics

+ +
+
Marc Schoenauer
+ + +Last modified: Tue Oct 31 18:32:22 CET 2000 + + + diff --git a/eo/tutorial/html/eoTopDown.html b/eo/tutorial/html/eoTopDown.html new file mode 100644 index 00000000..1e8470f3 --- /dev/null +++ b/eo/tutorial/html/eoTopDown.html @@ -0,0 +1,88 @@ + + + + + + EO - The Top-Down approach + + +Tutorial main page +- +Top-Down +page - Bottom-up page - Programming +hints - EO +documentation +
+
+
+

+EO - The Top-Down approach

+ +


Congratulations - You have chosen the top-down approach!  This +means that you want to start from something that already works, and gradually +learn about the more complex constructs. We have prepared a series of "lessons" +for you. +

    +
  • +Lesson 1 - a gentle introduction to the EO +way: your first steps into EO representations +using a simple generational GA. Please, spend +the necessary time on that one, since all basic constructs presented +there are used throughout EO.
  • + +
  • +Lesson 2 - encapsulate, +encapsulate, and try more sophisticated selection/replacement +mechanisms, as well as multiple  operators
  • + +
  • +Lesson 3 - The same algorithms, but with improved +input/outputs: user-friendly input (i.e. without +the need to recompile!) of algorithm parameters, +and checkpointing (display +of on-line statistics, save +and +restore +populations, +restart stopped runs, +...).
  • + +


    Current version (Nov. 29, 2000) stops here, but here are the plans +(sujected to many changes, of course!) +
      +

  • +Lesson 4 - More about checkpointing: write your first adaptive +mechanism, and find out how easy it is to update +and monitor dynamic +parameters
  • + +
  • +Lesson 5 - more general operators: e.g. binary, n-ary, or even specific +mate selection (your brain and my beauty)!
  • + +
  • +Lesson 6 - why not go parallel? From the simple asynchronous SSGA to the +more sophisticated island model (no totally distributed population yet).
  • + +
  • +Lesson 7 - ...
  • +
+Of course, in each lesson, you have links to the Bottom-Up page of the +corresponding component of an EA you are modifying. +
( ... Well, to tell you the truth, as of today, november 28, this is +not true :-) +
+
+
Tutorial main page - Top-Down +page - Bottom-up page - Programming +hints - EO +documentation +
+
+
+Marc Schoenauer
+ +
Last modified: Fri Nov 28 +2000  + + diff --git a/eo/tutorial/html/eoTutorial.html b/eo/tutorial/html/eoTutorial.html new file mode 100644 index 00000000..7f9d2094 --- /dev/null +++ b/eo/tutorial/html/eoTutorial.html @@ -0,0 +1,178 @@ + + + + + + Tutorial EO + + +Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+

+EO Tutorial

+Welcome to EO - the Evolving Objects library. What is this tutorial good +for? +
Well,  the short term idea here is to help you build +your own Evolutionary Algorithms using EO - while the long term +idea is that you will be able to contribute to EO, and ultimately write +our +EAs :-) +

+About this tutorial

+This tutorial can be used in 2 different ways: top-down and bottom-up. +
    +
  • +Top-down means you start from a very +simple, ready-to-run algorithm, and gradually modify it, making +it both more powerful and more complex.
  • + +
  • +Bottom-up means you start by examining the +components +of an EA one by one, down to the level of complexity you feel comfortable +with, and then build the whole algorithm using those components you need +(or the one you are mastering). Such approach might be viewed as going +through a simplified user guide, too.
  • +
+However, it is strongly recommended +that you take some time on the first lesson of the Top-down approach to +get familiar with the basic concepts that are used throughout EO. Anyway, +as of today, November 29, the Bottom-up page is not written yet :-) +

Related documents +

    +
  • +There are of course a few (very few) programming +hints that you should know.
  • + +
  • +THe EO documentation - automatically +generated from the comments in the code - is very helpful, to get an idea +of the inheritance diagrams of EO classes, and to quickly reach some specific +part of the code.
  • + +
    The top page of each class documentation is for instance the inheritance +diagram of the class, and you'll learn a lot by simply looking at it. +
  • +And, last but not least, we assume you know approximately that an Evolutionary +Algorithm looks like this, but otherwise you can try this very +brief introduction).
  • +
+ +


Colors and navigation: +

You will see this diagram in quite many places, as for instance at the +top of all examples - usually it will be clicable and will help you navigate +among the different parts of an EO program. See the brief +introduction to Evolutionary Computation for a detailed explanation. +

+

+ +

But in the text itself, colors are important, +as they will be used throughout this tutorial to clearly mark which part +of the algorithm we are discussing. So please keep in mind that, whereas +orange +is for emphasis, +

    +
  • +Yellowish is for representation, +i.e. the choice of the genotype
  • + +
  • +Magenta is for the stochastic +operators that are representation-dependent, +i.e. initialisation and variation operators +(crossover, mutation +and the like).
  • + +
  • +Green is for the implementation of Darwinism, +i.e. the way the individuals are selected +for reproduction and survive.
  • + +
  • +Red is for evaluation, i.e. the computation +of the fitness of all individuals
  • + +
  • +Blue is for interactions of the user and the +program, as for instance choice of stopping criterion, +on-line display of nice statistics or initial +choice +of all program parameters.
  • + +
  • +Brown is for everything that is NOT part of +any of the above, i.e. random number generator, or basic C++/STL syntax +.
  • + +
  • +Note that pink will be used to desctibe the +syntax of compile orders (i.e. at the oepratoring system level, see e.g. +below).
  • + +
  • +Last, but not least, all links into EO documentation +will use the Helvetica typeface, like this line you are now reading.
  • +
+ +
    +
  • +an interface that would allow you to build your Evolutionary Programs by +a few clics; such a thing does exist, is called EASEA, +and is complementary to this tutorial as it helps the user to build some +simple EO programs from simple description. But there are things that EASEA +cannot do, and you will have to do it yourself and will need to imcrease +your knowledge about EO for that.
  • +
+ +

+Before you start

+You should of course have downloaded and installed the whole EO +library (how did you get this file if not???). +
So we'll assume that you are now in the Tutorial directory, and that +your prompt looks something like +

(myname@myhost) EOdir/Tutorial % +

so you should now type in +

make lesson1 +

and see something like +

(myname@myhost) +EOdir/Tutorial % make lesson1 +
c++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\" +-I. -I../../src -Wall -g -c FirstBitGA.cpp +
c++ -Wall -g -o FirstBitGA FirstBitGA.o +../../src/libeo.a ../../src/utils/libeoutils.a +
c++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\" +-I. -I../../src -Wall -g -c FirstRealGA.cpp +
c++ -Wall -g -o FirstRealGA FirstRealGA.o +../../src/libeo.a ../../src/utils/libeoutils.a +

and two now executable files should have appeared in the subdirectory +Lesson1, namely FirstBitGA +and FirstRealGA (see First +lesson to know more about these two ready-to-run programs). If this +doesn't work, please go back to the main EO directory and run the installation +program. +

You should also test that you can access the EO documentation in the +menu line below: you might not need to go there immediately, but just in +case you make rapid progress ... This menu bar should be on all pages of +this tutorial, allowing you to navigate easily. +

Last, but not least: EO is improving only  from the good will of +contributors. This is also true for this tutorial: If you find anything +that you think could be improved, you are welcome to e-mail +me. +

+

Enjoy! +


Top-Down page +- Bottom-up page - Programming +hints - EO +documentation
+ +
+
+Marc Schoenauer
+ +
Last +modified: Fri Nov 28 2000  + + diff --git a/eo/tutorial/html/index.html b/eo/tutorial/html/index.html new file mode 100644 index 00000000..093aba7d --- /dev/null +++ b/eo/tutorial/html/index.html @@ -0,0 +1,178 @@ + + + + + + Tutorial EO + + +Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+

+EO Tutorial

+Welcome to EO - the Evolving Objects library. What is this tutorial good +for? +
Well,  the short term idea here is to help you build +your own Evolutionary Algorithms using EO - while the long term +idea is that you will be able to contribute to EO, and ultimately write +our +EAs :-) +

+About this tutorial

+This tutorial can be used in 2 different ways: top-down and bottom-up. +
    +
  • +Top-down means you start from a very +simple, ready-to-run algorithm, and gradually modify it, making +it both more powerful and more complex.
  • + +
  • +Bottom-up means you start by examining the +components +of an EA one by one, down to the level of complexity you feel comfortable +with, and then build the whole algorithm using those components you need +(or the one you are mastering). Such approach might be viewed as going +through a simplified user guide, too.
  • +
+However, it is strongly recommended +that you take some time on the first lesson of the Top-down approach to +get familiar with the basic concepts that are used throughout EO. Anyway, +as of today, November 29, the Bottom-up page is not written yet :-) +

Related documents +

    +
  • +There are of course a few (very few) programming +hints that you should know.
  • + +
  • +THe EO documentation - automatically +generated from the comments in the code - is very helpful, to get an idea +of the inheritance diagrams of EO classes, and to quickly reach some specific +part of the code.
  • + +
    The top page of each class documentation is for instance the inheritance +diagram of the class, and you'll learn a lot by simply looking at it. +
  • +And, last but not least, we assume you know approximately that an Evolutionary +Algorithm looks like this, but otherwise you can try this very +brief introduction).
  • +
+ +


Colors and navigation: +

You will see this diagram in quite many places, as for instance at the +top of all examples - usually it will be clicable and will help you navigate +among the different parts of an EO program. See the brief +introduction to Evolutionary Computation for a detailed explanation. +

+

+ +

But in the text itself, colors are important, +as they will be used throughout this tutorial to clearly mark which part +of the algorithm we are discussing. So please keep in mind that, whereas +orange +is for emphasis, +

    +
  • +Yellowish is for representation, +i.e. the choice of the genotype
  • + +
  • +Magenta is for the stochastic +operators that are representation-dependent, +i.e. initialisation and variation operators +(crossover, mutation +and the like).
  • + +
  • +Green is for the implementation of Darwinism, +i.e. the way the individuals are selected +for reproduction and survive.
  • + +
  • +Red is for evaluation, i.e. the computation +of the fitness of all individuals
  • + +
  • +Blue is for interactions of the user and the +program, as for instance choice of stopping criterion, +on-line display of nice statistics or initial +choice +of all program parameters.
  • + +
  • +Brown is for everything that is NOT part of +any of the above, i.e. random number generator, or basic C++/STL syntax +.
  • + +
  • +Note that pink will be used to desctibe the +syntax of compile orders (i.e. at the oepratoring system level, see e.g. +below).
  • + +
  • +Last, but not least, all links into EO documentation +will use the Helvetica typeface, like this line you are now reading.
  • +
+ +
    +
  • +an interface that would allow you to build your Evolutionary Programs by +a few clics; such a thing does exist, is called EASEA, +and is complementary to this tutorial as it helps the user to build some +simple EO programs from simple description. But there are things that EASEA +cannot do, and you will have to do it yourself and will need to imcrease +your knowledge about EO for that.
  • +
+ +

+Before you start

+You should of course have downloaded and installed the whole EO +library (how did you get this file if not???). +
So we'll assume that you are now in the Tutorial directory, and that +your prompt looks something like +

(myname@myhost) EOdir/Tutorial % +

so you should now type in +

make lesson1 +

and see something like +

(myname@myhost) +EOdir/Tutorial % make lesson1 +
c++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\" +-I. -I../../src -Wall -g -c FirstBitGA.cpp +
c++ -Wall -g -o FirstBitGA FirstBitGA.o +../../src/libeo.a ../../src/utils/libeoutils.a +
c++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\" +-I. -I../../src -Wall -g -c FirstRealGA.cpp +
c++ -Wall -g -o FirstRealGA FirstRealGA.o +../../src/libeo.a ../../src/utils/libeoutils.a +

and two now executable files should have appeared in the subdirectory +Lesson1, namely FirstBitGA +and FirstRealGA (see First +lesson to know more about these two ready-to-run programs). If this +doesn't work, please go back to the main EO directory and run the installation +program. +

You should also test that you can access the EO documentation in the +menu line below: you might not need to go there immediately, but just in +case you make rapid progress ... This menu bar should be on all pages of +this tutorial, allowing you to navigate easily. +

Last, but not least: EO is improving only  from the good will of +contributors. This is also true for this tutorial: If you find anything +that you think could be improved, you are welcome to e-mail +me. +

+

Enjoy! +


Top-Down page +- Bottom-up page - Programming +hints - EO +documentation
+ +
+
+Marc Schoenauer
+ +
Last +modified: Fri Nov 28 2000  + + diff --git a/eo/tutorial/html/lesson1.ps b/eo/tutorial/html/lesson1.ps new file mode 100644 index 00000000..d71bcc53 --- /dev/null +++ b/eo/tutorial/html/lesson1.ps @@ -0,0 +1,1952 @@ +%!PS-Adobe-3.0 +%%BoundingBox: 54 72 535 761 +%%Creator: Mozilla (NetScape) HTML->PS +%%DocumentData: Clean7Bit +%%Orientation: Portrait +%%Pages: 5 +%%PageOrder: Ascend +%%Title: Tutorial: Lesson 1 +%%EndComments +%%BeginProlog +[ /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef + /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef + /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef + /.notdef /.notdef /space /exclam /quotedbl /numbersign /dollar /percent /ampersand /quoteright + /parenleft /parenright /asterisk /plus /comma /hyphen /period /slash /zero /one + /two /three /four /five /six /seven /eight /nine /colon /semicolon + /less /equal /greater /question /at /A /B /C /D /E + /F /G /H /I /J /K /L /M /N /O + /P /Q /R /S /T /U /V /W /X /Y + /Z /bracketleft /backslash /bracketright /asciicircum /underscore /quoteleft /a /b /c + /d /e /f /g /h /i /j /k /l /m + /n /o /p /q /r /s /t /u /v /w + /x /y /z /braceleft /bar /braceright /asciitilde /.notdef /.notdef /.notdef + /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef + /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef + /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef /.notdef + /space /exclamdown /cent /sterling /currency /yen /brokenbar /section /dieresis /copyright + /ordfeminine /guillemotleft /logicalnot /hyphen /registered /macron /degree /plusminus /twosuperior /threesuperior + /acute /mu /paragraph /periodcentered /cedilla /onesuperior /ordmasculine /guillemotright /onequarter /onehalf + /threequarters /questiondown /Agrave /Aacute /Acircumflex /Atilde /Adieresis /Aring /AE /Ccedilla + /Egrave /Eacute /Ecircumflex /Edieresis /Igrave /Iacute /Icircumflex /Idieresis /Eth /Ntilde + /Ograve /Oacute /Ocircumflex /Otilde /Odieresis /multiply /Oslash /Ugrave /Uacute /Ucircumflex + /Udieresis /Yacute /Thorn /germandbls /agrave /aacute /acircumflex /atilde /adieresis /aring + /ae /ccedilla /egrave /eacute /ecircumflex /edieresis /igrave /iacute /icircumflex /idieresis + /eth /ntilde /ograve /oacute /ocircumflex /otilde /odieresis /divide /oslash /ugrave + /uacute /ucircumflex /udieresis /yacute /thorn /ydieresis] /isolatin1encoding exch def +/c { matrix currentmatrix currentpoint translate + 3 1 roll scale newpath 0 0 1 0 360 arc setmatrix } bind def +/F0 + /Times-Roman findfont + dup length dict begin + {1 index /FID ne {def} {pop pop} ifelse} forall + /Encoding isolatin1encoding def + currentdict end +definefont pop +/f0 { /F0 findfont exch scalefont setfont } bind def +/F1 + /Times-Bold findfont + dup length dict begin + {1 index /FID ne {def} {pop pop} ifelse} forall + /Encoding isolatin1encoding def + currentdict end +definefont pop +/f1 { /F1 findfont exch scalefont setfont } bind def +/F2 + /Times-Italic findfont + dup length dict begin + {1 index /FID ne {def} {pop pop} ifelse} forall + /Encoding isolatin1encoding def + currentdict end +definefont pop +/f2 { /F2 findfont exch scalefont setfont } bind def +/F3 + /Times-BoldItalic findfont + dup length dict begin + {1 index /FID ne {def} {pop pop} ifelse} forall + /Encoding isolatin1encoding def + currentdict end +definefont pop +/f3 { /F3 findfont exch scalefont setfont } bind def +/F4 + /Courier findfont + dup length dict begin + {1 index /FID ne {def} {pop pop} ifelse} forall + /Encoding isolatin1encoding def + currentdict end +definefont pop +/f4 { /F4 findfont exch scalefont setfont } bind def +/F5 + /Courier-Bold findfont + dup length dict begin + {1 index /FID ne {def} {pop pop} ifelse} forall + /Encoding isolatin1encoding def + currentdict end +definefont pop +/f5 { /F5 findfont exch scalefont setfont } bind def +/F6 + /Courier-Oblique findfont + dup length dict begin + {1 index /FID ne {def} {pop pop} ifelse} forall + /Encoding isolatin1encoding def + currentdict end +definefont pop +/f6 { /F6 findfont exch scalefont setfont } bind def +/F7 + /Courier-BoldOblique findfont + dup length dict begin + {1 index /FID ne {def} {pop pop} ifelse} forall + /Encoding isolatin1encoding def + currentdict end +definefont pop +/f7 { /F7 findfont exch scalefont setfont } bind def +/rhc { + { + currentfile read { + dup 97 ge + { 87 sub true exit } + { dup 48 ge { 48 sub true exit } { pop } ifelse } + ifelse + } { + false + exit + } ifelse + } loop +} bind def + +/cvgray { % xtra_char npix cvgray - (string npix long) + dup string + 0 + { + rhc { cvr 4.784 mul } { exit } ifelse + rhc { cvr 9.392 mul } { exit } ifelse + rhc { cvr 1.824 mul } { exit } ifelse + add add cvi 3 copy put pop + 1 add + dup 3 index ge { exit } if + } loop + pop + 3 -1 roll 0 ne { rhc { pop } if } if + exch pop +} bind def + +/smartimage12rgb { % w h b [matrix] smartimage12rgb - + /colorimage where { + pop + { currentfile rowdata readhexstring pop } + false 3 + colorimage + } { + exch pop 8 exch + 3 index 12 mul 8 mod 0 ne { 1 } { 0 } ifelse + 4 index + 6 2 roll + { 2 copy cvgray } + image + pop pop + } ifelse +} def +/cshow { dup stringwidth pop 2 div neg 0 rmoveto show } bind def +/rshow { dup stringwidth pop neg 0 rmoveto show } bind def +/BeginEPSF { + /b4_Inc_state save def + /dict_count countdictstack def + /op_count count 1 sub def + userdict begin + /showpage {} def + 0 setgray 0 setlinecap 1 setlinewidth 0 setlinejoin + 10 setmiterlimit [] 0 setdash newpath + /languagelevel where + { pop languagelevel 1 ne + { false setstrokeadjust false setoverprint } if + } if +} bind def +/EndEPSF { + count op_count sub {pop} repeat + countdictstack dict_count sub {end} repeat + b4_Inc_state restore +} bind def +%%EndProlog +%%Page: 1 1 +%%BeginPageSetup +/pagelevel save def +54 0 translate +%%EndPageSetup +newpath 0 72 moveto 481 0 rlineto 0 689 rlineto -481 0 rlineto closepath clip newpath +0 748.4 moveto +12 f0 +(Lesson 2) show +42.9 748.4 moveto +12 f0 +( - ) show +52.8 748.4 moveto +12 f0 +(Tutorial main page) show +144.1 748.4 moveto +12 f0 +( - ) show +154 748.4 moveto +12 f0 +(Top-Down page) show +232.3 748.4 moveto +12 f0 +( - ) show +242.2 748.4 moveto +12 f0 +(Bottom-up page) show +319.8 748.4 moveto +12 f0 +( - ) show +329.7 748.4 moveto +12 f0 +(Programming hints) show +422 748.4 moveto +12 f0 +( -) show +428.9 748.4 moveto +14 f0 +(EO) show +0 732.9 moveto +14 f0 +(documentation) show +83.2 732.9 moveto +12 f0 +( ) show +0 723.4 moveto +481 0 rlineto 0 -1.4 rlineto -481 0 rlineto closepath fill +146.5 680.9 moveto +24 f1 +(Tutorial: Lesson 1) show +0 651.7 moveto +12 f0 +(This lesson will let you ) show +18.1 629.2 moveto +3.3 3.3 c fill +28 625.1 moveto +12 f0 +(run) show +43.9 625.1 moveto +12 f0 +( your first Evolutionary Algorithm written within EO Library, choosing between evolving) show +28 611.8 moveto +12 f0 +(bitstrings) show +72.6 611.8 moveto +12 f0 +(, or evolving ) show +136.5 611.8 moveto +12 f0 +(vectors of real numbers) show +249.4 611.8 moveto +12 f0 +(,) show +252.3 611.8 moveto +12 f0 +( ) show +18.1 602.6 moveto +3.3 3.3 c fill +28 598.5 moveto +12 f0 +(browse) show +62.6 598.5 moveto +12 f0 +( through the code of these algorithms, or) show +257.2 598.5 moveto +12 f0 +( ) show +18.1 589.3 moveto +3.3 3.3 c fill +28 585.2 moveto +12 f0 +(follow the ) show +79.9 585.2 moveto +12 f0 +(guided tour) show +134.9 585.2 moveto +12 f0 +(.) show +137.8 585.2 moveto +12 f0 +( ) show +0 558.6 moveto +12 f0 +(Later you will be asked to ) show +18.1 536.1 moveto +3.3 3.3 c fill +28 532 moveto +12 f0 +(write your own ) show +104.3 532 moveto +12 f0 +(fitness function) show +178.6 532 moveto +12 f0 +(,) show +181.5 532 moveto +12 f0 +( ) show +18.1 522.8 moveto +3.3 3.3 c fill +28 518.7 moveto +12 f0 +(use different kinds of ) show +133.3 518.7 moveto +12 f0 +(selection procedures) show +231.6 518.7 moveto +12 f0 +( in the framework of a generational GA ) show +424.2 518.7 moveto +12 f0 +(evolution) show +28 505.4 moveto +12 f0 +(engine) show +59.9 505.4 moveto +12 f0 +(,) show +62.8 505.4 moveto +12 f0 +( ) show +18.1 496.2 moveto +3.3 3.3 c fill +28 492.1 moveto +12 f0 +(check that EO let you separate the ) show +194.9 492.1 moveto +12 f0 +(representation) show +262.8 492.1 moveto +12 f0 +( from the ) show +309.7 492.1 moveto +12 f0 +(evolution engine) show +390 492.1 moveto +12 f0 +(.) show +392.9 492.1 moveto +12 f0 +( ) show +0 463.2 moveto +14 f1 +(I want to run an Evolutionary Algorithm ) show +248.5 463.2 moveto +14 f1 +(now) show +0 436.2 moveto +12 f0 +(You can choose to run a standard ) show +162.9 436.2 moveto +12 f0 +(bitstring Genetic Algorithm) show +296.2 436.2 moveto +12 f0 +( \(as defined in Goldberg's book\) or a) show +0 422.9 moveto +12 f0 +(standard ) show +43.6 422.9 moveto +12 f0 +(real-valued genetic algorithm) show +184.8 422.9 moveto +12 f0 +(, as proposed in Micahlewicz's book. ) show +0 395.8 moveto +12 f0 +(If you have not already done what was recommended in the ) show +289.5 395.8 moveto +12 f0 +(Tutorial main page) show +380.8 395.8 moveto +12 f0 +( , do it ) show +414.4 395.8 moveto +12 f1 +(NOW) show +444.3 395.8 moveto +12 f0 +(. Then) show +0 382.5 moveto +12 f0 +(go to the Lesson1 sub-dir of the tutorial dir, and simply type at the system prompt ) show +0 355.9 moveto +10 f4 +(\(myname@myhost\) EOdir/Tutorial/Lesson1 % ) show +246 355.9 moveto +10 f4 +(FirstRealGA) show +312 355.9 moveto +12 f0 +( ) show +0 342.6 moveto +12 f0 +(or ) show +0 329.3 moveto +10 f4 +(\(myname@myhost\) EOdir/Tutorial/Lesson1 % ) show +246 329.3 moveto +10 f4 +(FirstBitGA) show +306 329.3 moveto +12 f0 +( ) show +0 302.7 moveto +12 f0 +(and something should happen. ) show +0 273.8 moveto +14 f1 +(What is happening?) show +0 246.8 moveto +12 f0 +(At the moment, the ) show +95.6 246.8 moveto +12 f0 +(FirstBitGA) show +149.6 246.8 moveto +12 f0 +( maximizes the number of ones in the bitstring \(also calls the) show +0 233.5 moveto +12 f0 +(OneMaxfunction) show +81.9 233.5 moveto +12 f0 +(, whose solution is, as you can guess, ) show +263.5 233.5 moveto +12 f0 +(11111111) show +311.4 233.5 moveto +12 f0 +(\), and the ) show +359.3 233.5 moveto +12 f0 +(FirstRealGA) show +420.6 233.5 moveto +12 f0 +( is) show +0 220.2 moveto +12 f0 +(maximizing \(the default action for EO is to maximize\) the inverse of the sum \(i.e. minimizing the) show +0 206.9 moveto +12 f0 +(sum\) of the square of its variables \(also called the ) show +241.2 206.9 moveto +12 f0 +(sphere function) show +315.5 206.9 moveto +12 f0 +(, whose solution is ... ) show +419.8 206.9 moveto +12 f0 +(all zeroes) show +465.4 206.9 moveto +12 f0 +(\). ) show +0 180.3 moveto +12 f0 +(And what you see on the screen when running one of these two programs is, in each case, the initial) show +0 167 moveto +12 f0 +(and final population of an Evolutionary run, one individual per line, its fitness first, then the number) show +0 153.7 moveto +12 f0 +(of items \(bits or real numbers\) of the genotype, and the genotype itself. The final population) show +0 140.4 moveto +12 f0 +(hopefully contains the solution in the discrete case, and is close to it in the continuous case. ) show +0 113.8 moveto +12 f0 +(You need now to take a look at either programs by browsing alone through the sources for) show +0 100.5 moveto +12 f0 +(FirstBitGA) show +54 100.5 moveto +12 f0 +( and ) show +77.3 100.5 moveto +12 f0 +(FirstRealGA) show +138.6 100.5 moveto +12 f0 +(, or follow the guided tour below, or go directly to the ) show +400.5 100.5 moveto +12 f0 +(exercises) show +444.4 100.5 moveto +12 f0 +(. ) show +pagelevel restore +showpage +%%Page: 2 2 +%%BeginPageSetup +/pagelevel save def +54 0 translate +%%EndPageSetup +newpath 0 72 moveto 481 0 rlineto 0 689 rlineto -481 0 rlineto closepath clip newpath +0 747.9 moveto +14 f1 +(Browsing the code:) show +18.1 724.5 moveto +3.3 3.3 c fill +28 720.4 moveto +12 f1 +(General includes:) show +118.3 720.4 moveto +12 f0 +(Like all C-like code, the file starts with include directives \() show +401.2 720.4 moveto +12 f0 +(Bit) show +415.8 720.4 moveto +12 f0 +( - ) show +425.7 720.4 moveto +12 f0 +(Real) show +447.6 720.4 moveto +12 f0 +(\).) show +28 706.6 moveto +12 f0 +(Apart from standard includes, the spedific file to include is ) show +313.6 706.6 moveto +12 f1 +(eo) show +324.9 706.6 moveto +12 f0 +(: this is a file that contains the) show +28 693.3 moveto +12 f0 +(list of the most important EO files.) show +195 693.3 moveto +12 f0 +( ) show +28 680 moveto +12 f0 +( ) show +18.1 670.3 moveto +3.3 3.3 c fill +28 666.2 moveto +12 f1 +(Representation) show +105.9 666.2 moveto +12 f0 +(: you then have to delclare the type of individuals you will be handling. All) show +28 652.9 moveto +12 f0 +(evolution-related objects you will need are templatized w.r.t. the type of individuals.) show +433.6 652.9 moveto +12 f0 +( ) show +46.1 643.2 moveto +3.3 3.3 c stroke +56 639.1 moveto +12 f0 +(Bit) show +70.6 639.1 moveto +12 f0 +( You say here that you will be handling ) show +262.9 639.1 moveto +12 f1 +(Bitstring genotypes) show +361.8 639.1 moveto +12 f0 +(, whose fitness is a) show +56 625.8 moveto +12 f0 +(double. This makes Indi derive from the ) show +251.9 625.8 moveto +12 f0 +(STL class) show +299.5 625.8 moveto +12 f0 +( ) show +302.4 625.8 moveto +12 f0 +(vector) show +367.2 625.8 moveto +12 f0 +( ) show +46.1 616.1 moveto +3.3 3.3 c stroke +56 612 moveto +12 f0 +(Real) show +77.9 612 moveto +12 f0 +( You say here that you will be handling ) show +270.2 612 moveto +12 f1 +(Real-valued genotypes) show +385.1 612 moveto +12 f0 +(, whose fitness is a) show +56 598.7 moveto +12 f0 +(double. This makes Indi derive from the ) show +251.9 598.7 moveto +12 f0 +(STL class) show +299.5 598.7 moveto +12 f0 +( ) show +302.4 598.7 moveto +12 f0 +(vector) show +378.5 598.7 moveto +12 f0 +( ) show +56 585.4 moveto +12 f0 +( ) show +18.1 575.7 moveto +3.3 3.3 c fill +28 571.6 moveto +12 f1 +(Fitness function:) show +113.6 571.6 moveto +12 f0 +( the code for the fitness function is included in the file. It must take as) show +28 558.3 moveto +12 f0 +(argument a reference to an individual \(at the moment\).) show +290.2 558.3 moveto +12 f0 +( ) show +46.1 549.1 moveto +3.3 3.3 c stroke +56 545 moveto +12 f0 +(Bit) show +70.6 545 moveto +12 f0 +( This function simply computes the number of ones of the bitstring \(it's called the) show +56 531.7 moveto +12 f0 +(OneMax function\). The optimum is of course the all-ones bitstring.) show +378.9 531.7 moveto +12 f0 +( ) show +46.1 522.5 moveto +3.3 3.3 c stroke +56 518.4 moveto +12 f0 +(Real) show +77.9 518.4 moveto +12 f0 +( This function simply computes the inverse of the sum of the squares of all) show +56 505.1 moveto +12 f0 +(variables \(also called the sphere function\). The optimum is of course the all-zeroes) show +56 491.8 moveto +12 f0 +(vector.) show +88.9 491.8 moveto +12 f0 +( ) show +56 478.5 moveto +12 f0 +( ) show +18.1 468.8 moveto +3.3 3.3 c fill +28 464.7 moveto +12 f1 +(Parameters) show +87.3 464.7 moveto +12 f0 +(: all parameters of the algorithm are declared here \() show +333.5 464.7 moveto +12 f0 +(Bit) show +348.1 464.7 moveto +12 f0 +( - ) show +358 464.7 moveto +12 f0 +(Real) show +379.9 464.7 moveto +12 f0 +(\), and their values) show +28 451.4 moveto +12 f0 +(and assigned. Of course, this means that you will need to recompile to change these values -) show +28 438.1 moveto +12 f0 +(see Lesson 3 to get rid of that heavy requirement.) show +265.2 438.1 moveto +12 f0 +( ) show +28 424.8 moveto +12 f0 +( ) show +18.1 415.1 moveto +3.3 3.3 c fill +28 411 moveto +12 f1 +(Random seeding:) show +117 411 moveto +12 f0 +( Random numbers play an important role in Evolutionary Algorithms. See) show +28 397.7 moveto +12 f0 +(in ) show +40.3 397.7 moveto +12 f0 +(EO programming hints) show +150.9 397.7 moveto +12 f0 +( more details about how this is simulated in EO - but as far as you) show +28 384.4 moveto +12 f0 +(are concerned now, remember that the ) show +214.9 384.4 moveto +12 f0 +(global Random Number Generator) show +381.8 384.4 moveto +12 f0 +( is called ) show +427.4 384.4 moveto +12 f5 +(rng) show +448.9 384.4 moveto +12 f0 +( and) show +28 370.8 moveto +12 f0 +(should be used everywhere you need a realization of a random variable of known law.) show +28 357.5 moveto +12 f0 +(Moreover, this RNG requires a ) show +179.9 357.5 moveto +12 f5 +(seed) show +208.6 357.5 moveto +12 f0 +(, which is set here \() show +301.9 357.5 moveto +12 f0 +(Bit) show +316.5 357.5 moveto +12 f0 +( - ) show +326.4 357.5 moveto +12 f0 +(Real) show +348.3 357.5 moveto +12 f0 +(\): everytime you run the) show +28 343.9 moveto +12 f0 +(algorithm with the ) show +119.6 343.9 moveto +12 f0 +(same seed) show +168.5 343.9 moveto +12 f0 +(, you will get the ) show +252.4 343.9 moveto +12 f0 +(same result) show +306.7 343.9 moveto +12 f0 +(. Hence, to test the robustness of) show +28 330.6 moveto +12 f0 +(your algorithm, you should run it with different seeds. This is rather time consuming in the) show +28 317.3 moveto +12 f0 +(present programs, so we suggest that you wait until Lesson 3 to do so.) show +364.3 317.3 moveto +12 f0 +( ) show +28 304 moveto +12 f0 +( ) show +18.1 294.3 moveto +3.3 3.3 c fill +28 290.2 moveto +12 f1 +(Fitness function encapsulation: ) show +190.3 290.2 moveto +12 f0 +(EO is based on the notion of ) show +329.9 290.2 moveto +12 f0 +(functors) show +369.2 290.2 moveto +12 f0 +( - hence you now need) show +28 276.9 moveto +12 f0 +(to encapsulate your fitness function into a functor object. This is what is done here \() show +431.2 276.9 moveto +12 f0 +(Bit) show +445.8 276.9 moveto +12 f0 +( -) show +28 263.6 moveto +12 f0 +(Real) show +49.9 263.6 moveto +12 f0 +(\).) show +56.8 263.6 moveto +12 f0 +( ) show +28 250.3 moveto +12 f0 +( ) show +18.1 240.6 moveto +3.3 3.3 c fill +28 236.5 moveto +12 f1 +(Initialization) show +94 236.5 moveto +12 f0 +(: to initialize the population, first declare an empty object of class ) show +411.6 236.5 moveto +10 f5 +(eoPop) show +477.6 236.5 moveto +12 f0 +(,) show +28 223.2 moveto +12 f0 +(which is basically an ) show +131.3 223.2 moveto +12 f0 +(STL) show +152.6 223.2 moveto +12 f0 +( ) show +155.5 223.2 moveto +10 f5 +(vector) show +227.5 223.2 moveto +12 f0 +(, then fill it with Indi's.) show +339.1 223.2 moveto +12 f0 +( And remember that) show +28 209.9 moveto +10 f5 +(v.push_back) show +94 209.9 moveto +12 f0 +( simply appends its argument at the end of ) show +300.3 209.9 moveto +12 f0 +(STL) show +321.6 209.9 moveto +12 f0 +( vector ) show +357.5 209.9 moveto +10 f5 +(v.) show +369.5 209.9 moveto +12 f0 +( ) show +46.1 198.9 moveto +3.3 3.3 c stroke +56 194.8 moveto +12 f0 +(Bit) show +70.6 194.8 moveto +12 f0 +( ) show +73.5 194.8 moveto +10 f5 +(rng.flip\(\)) show +133.5 194.8 moveto +12 f0 +( return a ) show +176.4 194.8 moveto +14 f0 +(random boolean) show +266.9 194.8 moveto +12 f0 +( ) show +46.1 183.4 moveto +3.3 3.3 c stroke +56 179.3 moveto +12 f0 +(Real) show +77.9 179.3 moveto +12 f0 +( ) show +80.8 179.3 moveto +10 f5 +(rng.uniform\(\)) show +158.8 179.3 moveto +12 f0 +( returns a ) show +206.4 179.3 moveto +14 f0 +(real value uniformly drawn in [0,1].) show +407.4 179.3 moveto +12 f0 +( ) show +56 165.6 moveto +12 f0 +( ) show +18.1 155.9 moveto +3.3 3.3 c fill +28 151.8 moveto +12 f1 +(Output) show +65.3 151.8 moveto +12 f0 +(: take a snapshot at the initial population \() show +266.6 151.8 moveto +12 f0 +(Bit) show +281.2 151.8 moveto +12 f0 +( - ) show +291.1 151.8 moveto +12 f0 +(Real) show +313 151.8 moveto +12 f0 +(\). Sort it first, so the best) show +28 138.5 moveto +12 f0 +(individuals are first, and display it. Note that an eoPop has a ) show +319.9 138.5 moveto +10 f5 +(<<) show +331.9 138.5 moveto +12 f0 +( method, which means that a) show +28 125.2 moveto +12 f0 +(simple ) show +63 125.2 moveto +10 f5 +(os << pop ) show +123 125.2 moveto +12 f0 +(streams the ) show +180.3 125.2 moveto +10 f5 +(pop) show +198.3 125.2 moveto +12 f0 +( onto the ostream ) show +284.2 125.2 moveto +10 f5 +(os) show +296.2 125.2 moveto +12 f0 +(. This is true for ) show +376.1 125.2 moveto +12 f0 +(all objects of of class) show +28 109.6 moveto +14 f1 +(eoPrintable) show +97.2 109.6 moveto +12 f0 +( \(most EO objects\) through the method ) show +287.5 109.6 moveto +10 f5 +(printOn) show +329.5 109.6 moveto +12 f0 +( \(which is then called by the ) show +467.8 109.6 moveto +10 f5 +(<<) show +28 95.9 moveto +12 f0 +(operator\).) show +74.9 95.9 moveto +12 f0 +( ) show +28 82.6 moveto +12 f0 +( ) show +pagelevel restore +showpage +%%Page: 3 3 +%%BeginPageSetup +/pagelevel save def +54 0 translate +%%EndPageSetup +newpath 0 72 moveto 481 0 rlineto 0 689 rlineto -481 0 rlineto closepath clip newpath +18.1 753.8 moveto +3.3 3.3 c fill +28 749.7 moveto +12 f1 +(Evolution engine:) show +118.3 749.7 moveto +12 f0 +( The selection/replacement mechanism \() show +312.2 749.7 moveto +12 f0 +(Bit) show +326.8 749.7 moveto +12 f0 +( - ) show +336.7 749.7 moveto +12 f0 +(Real) show +358.6 749.7 moveto +12 f0 +(\) is a simple generational) show +28 734.1 moveto +12 f0 +(GA here: a simple selector, and a generational replacement. The ) show +339.2 734.1 moveto +14 f1 +(eoDetTournament) show +448.8 734.1 moveto +12 f0 +( has) show +28 720.4 moveto +12 f0 +(been chosen as a robust selection, and the generational replacement \(all parents are replaced) show +28 707.1 moveto +12 f0 +(by the offspring\) is hard-coded in the eoSGA ) show +247.9 707.1 moveto +12 f0 +(algorithm) show +294.5 707.1 moveto +12 f0 +(.) show +297.4 707.1 moveto +12 f0 +( ) show +28 693.8 moveto +12 f0 +( ) show +18.1 684.1 moveto +3.3 3.3 c fill +28 680 moveto +12 f1 +(Variation operators) show +129.6 680 moveto +12 f0 +(:) show +132.9 680 moveto +12 f0 +( in the simple algorithm considered here, individuals undergo ) show +430.8 680 moveto +12 f0 +(crossover) show +28 664.4 moveto +12 f0 +(and ) show +48.3 664.4 moveto +12 f0 +(mutation) show +90.9 664.4 moveto +12 f0 +(. In EO, these operators are \() show +228.8 664.4 moveto +12 f0 +(functor) show +263.4 664.4 moveto +12 f0 +(\) objects of class) show +343.7 664.4 moveto +12 f0 +( ) show +346.6 664.4 moveto +14 f1 +(eoQuadOp) show +411.9 664.4 moveto +12 f0 +( \(binary) show +28 648.4 moveto +12 f0 +(operator that modifies both its arguments\) and ) show +252.9 648.4 moveto +14 f1 +(eoMonOp) show +312.7 648.4 moveto +12 f0 +( \(unary operator\). These operators) show +28 634.7 moveto +12 f0 +(are applied in turn to all selected parents, according to user-defined probabilities. These) show +28 621.4 moveto +12 f0 +(probabilities are defined with all other ) show +214.6 621.4 moveto +12 f0 +(parameters) show +267.2 621.4 moveto +12 f0 +(, and will be passed to the ) show +394.5 621.4 moveto +12 f5 +(eoSGA) show +430.6 621.4 moveto +12 f0 +(algorithm) show +477.2 621.4 moveto +12 f0 +(.) show +46.1 609.6 moveto +3.3 3.3 c stroke +56 605.5 moveto +12 f0 +(Bit) show +70.6 605.5 moveto +12 f0 +( The crossover ) show +144.2 605.5 moveto +14 f1 +(eoBinCrossover) show +239 605.5 moveto +12 f0 +( is the standard ) show +314.3 605.5 moveto +12 f0 +(1-point crossover) show +397.9 605.5 moveto +12 f0 +(, and) show +56 589.5 moveto +14 f1 +(eoBinMutation) show +146.2 589.5 moveto +12 f0 +( is the standard ) show +221.5 589.5 moveto +12 f0 +(bit-flip mutation) show +300.5 589.5 moveto +12 f0 +( that randomly flips all bits with a) show +56 575.8 moveto +12 f0 +(given probability ) show +141.3 575.8 moveto +10 f5 +(P_MUT_PER_BIT.) show +225.3 575.8 moveto +12 f0 +( ) show +56 562 moveto +12 f1 +(Warning) show +102 562 moveto +12 f0 +(: the ) show +125.9 562 moveto +10 f5 +(P_MUT_PER_BIT) show +203.9 562 moveto +12 f0 +( probability is an ) show +287.8 562 moveto +12 f0 +(internal parameter) show +375.4 562 moveto +12 f0 +( of the) show +56 548.2 moveto +12 f5 +(eoBinMutation) show +149.5 548.2 moveto +12 f0 +(, it is ) show +176.1 548.2 moveto +12 f1 +(NOT) show +202.1 548.2 moveto +12 f0 +( the probability of mutation at the individual level. EO) show +56 534.6 moveto +12 f0 +(corrects what can be viewed as an inconsistency in Holland's original work, further) show +56 521.3 moveto +12 f0 +(used in Goldberg's book by separating the probability of mutation for each individual) show +56 508 moveto +12 f0 +(\(independent of the type of mutation that will be applied\) from the probability of) show +56 494.7 moveto +12 f0 +(flipping each bit, which is specific of the bit-flip mutation. Hence, to run the same) show +56 481.4 moveto +12 f0 +(algorithm as Goldberg's SGA, the mutation probability \(at individual level\) is 1, and the) show +56 468.1 moveto +12 f0 +(probability of flipping each bit is ) show +217.3 468.1 moveto +10 f5 +(P_MUT_PER_BIT.) show +301.3 468.1 moveto +12 f0 +( ) show +46.1 456.6 moveto +3.3 3.3 c stroke +56 452.5 moveto +12 f0 +(Real) show +77.9 452.5 moveto +12 f0 +( The crossover ) show +151.5 452.5 moveto +14 f1 +(eoArithmeticCrossover) show +290.6 452.5 moveto +12 f0 +( is the standard ) show +365.9 452.5 moveto +12 f0 +(arithmetic crossover) show +463.5 452.5 moveto +12 f0 +( for) show +56 438.8 moveto +12 f0 +(real-valued vectors, that chooses a point randomly on the segment between both parents) show +56 423.2 moveto +12 f0 +(\(also termed ) show +118.6 423.2 moveto +12 f0 +(BLX-0) show +152.5 423.2 moveto +12 f0 +(\). ) show +162.4 423.2 moveto +14 f1 +(eoUniformMutation) show +282.9 423.2 moveto +12 f0 +( is the ) show +314.5 423.2 moveto +12 f0 +(uniform mutation) show +398.8 423.2 moveto +12 f0 +( for real-valued) show +56 409.5 moveto +12 f0 +(vectors that choses a new value for each variable uniformly on an interval centered on) show +56 396.2 moveto +12 f0 +(the parent value. The width of the interval is an ) show +286.2 396.2 moveto +12 f0 +(internal parameter) show +373.8 396.2 moveto +12 f0 +( of the object, here) show +56 382.9 moveto +12 f0 +(called ) show +87.6 382.9 moveto +10 f5 +(EPSILON) show +129.7 382.9 moveto +12 f0 +(.) show +132.6 382.9 moveto +12 f0 +( ) show +56 369.6 moveto +12 f0 +( ) show +18.1 359.9 moveto +3.3 3.3 c fill +28 355.8 moveto +12 f1 +(Stopping criterion:) show +125.6 355.8 moveto +12 f0 +( Specify a ) show +176.5 355.8 moveto +12 f0 +(maximum number of generations) show +336.1 355.8 moveto +12 f0 +( to run \() show +374.4 355.8 moveto +12 f0 +(Bit) show +389 355.8 moveto +12 f0 +( - ) show +398.9 355.8 moveto +12 f0 +(Real) show +420.8 355.8 moveto +12 f0 +(\): the) show +28 342.5 moveto +12 f0 +(simplest of all stopping criteria at the moment, using an object of a sub-class of class) show +28 326.9 moveto +14 f1 +(eoContinue) show +96.4 326.9 moveto +12 f0 +(.) show +99.3 326.9 moveto +12 f0 +( ) show +28 313.2 moveto +12 f0 +( ) show +18.1 303.5 moveto +3.3 3.3 c fill +28 299.4 moveto +12 f1 +(The algorithm: ) show +108.6 299.4 moveto +12 f0 +(the simple algorithm that is used here, called ) show +329.2 299.4 moveto +12 f5 +(eoSGA ) show +372.3 299.4 moveto +12 f0 +(requires as parameters) show +28 285.8 moveto +12 f0 +(a ) show +36.3 285.8 moveto +12 f0 +(selector) show +73.6 285.8 moveto +12 f0 +(, a ) show +87.9 285.8 moveto +12 f0 +(crossover) show +133.9 285.8 moveto +12 f0 +( and the associated ) show +227.2 285.8 moveto +12 f0 +(crossover rate) show +294.1 285.8 moveto +12 f0 +(, a ) show +308.4 285.8 moveto +12 f0 +(mutation) show +351 285.8 moveto +12 f0 +( and the associated) show +28 272.5 moveto +12 f0 +(mutation rate,) show +94.6 272.5 moveto +12 f0 +( and a ) show +126.2 272.5 moveto +12 f0 +(stopping criterion) show +211.1 272.5 moveto +12 f0 +(. Take a look at the corresponding ) show +377.7 272.5 moveto +12 f0 +(constructor) show +431.7 272.5 moveto +12 f0 +( of the) show +28 259.2 moveto +12 f0 +(class ) show +54.3 259.2 moveto +12 f5 +(eoSGA) show +90.4 259.2 moveto +12 f0 +(: it only initializes its ) show +194.7 259.2 moveto +12 f0 +(private data) show +251 259.2 moveto +12 f0 +( with the parameters. Now look at the) show +28 245.6 moveto +12 f0 +(operator\(\)) show +75.9 245.6 moveto +12 f0 +( method - the one that is called in the code for ) show +299.1 245.6 moveto +12 f0 +(FirstBitGA) show +353.1 245.6 moveto +12 f0 +( or ) show +369 245.6 moveto +12 f0 +(FirstRealGA) show +430.3 245.6 moveto +12 f0 +( - and) show +28 232.3 moveto +12 f0 +(you'll find out that is is as simple as it sounds.) show +250.3 232.3 moveto +12 f0 +( ) show +28 219 moveto +12 f0 +( ) show +18.1 209.3 moveto +3.3 3.3 c fill +28 205.2 moveto +12 f1 +(Output) show +65.3 205.2 moveto +12 f0 +(: After running the algorithm, output the sorted final population \() show +377.6 205.2 moveto +12 f0 +(Bit) show +392.2 205.2 moveto +12 f0 +( - ) show +402.1 205.2 moveto +12 f0 +(Real) show +424 205.2 moveto +12 f0 +(\) - and look) show +28 191.9 moveto +12 f0 +(at the best individual: this is the result of the algorithm.) show +293.6 191.9 moveto +12 f0 +( ) show +28 178.6 moveto +12 f0 +( ) show +18.1 168.9 moveto +3.3 3.3 c fill +28 164.8 moveto +12 f1 +(Main body:) show +87.6 164.8 moveto +12 f0 +( for technical reasons \(intercepting the exceptions\), we need a main like this one) show +28 151.5 moveto +12 f0 +(\() show +31.9 151.5 moveto +12 f0 +(Bit) show +46.5 151.5 moveto +12 f0 +( - ) show +56.4 151.5 moveto +12 f0 +(Real) show +78.3 151.5 moveto +12 f0 +(\)., and you should not touch it unless you know what you are doing. Simply note) show +28 138.2 moveto +12 f0 +(that this main calls the function main_function, which we have been discussing up to now!) show +463.6 138.2 moveto +12 f0 +( ) show +0 109.3 moveto +14 f1 +(Exercise 1: maximize your own function) show +0 82.3 moveto +12 f0 +(This is very easy - if your search space is that of bitstring or of unbounded real numbers. ) show +pagelevel restore +showpage +%%Page: 4 4 +%%BeginPageSetup +/pagelevel save def +54 0 translate +%%EndPageSetup +newpath 0 72 moveto 481 0 rlineto 0 689 rlineto -481 0 rlineto closepath clip newpath +18.1 748.8 moveto +3.3 3.3 c fill +28 744.7 moveto +12 f0 +(Go to the tutorial directory, and ) show +182.9 744.7 moveto +12 f0 +(copy) show +206.2 744.7 moveto +12 f0 +( the program you want to modify onto ) show +392.3 744.7 moveto +10 f5 +(mytest.cpp.) show +458.3 744.7 moveto +12 f0 +( ) show +18.1 735.5 moveto +3.3 3.3 c fill +28 731.4 moveto +12 f0 +(Edit) show +48 731.4 moveto +12 f0 +( ) show +51.1 731.4 moveto +10 f5 +(mytest.cpp) show +111.1 731.4 moveto +12 f0 +( with any text editor:) show +211 731.4 moveto +12 f0 +( ) show +18.1 722.2 moveto +3.3 3.3 c fill +28 718.1 moveto +12 f0 +(Modify) show +63.9 718.1 moveto +12 f0 +( the fitness function itself \() show +192.8 718.1 moveto +12 f0 +(binary_value) show +255.4 718.1 moveto +12 f0 +( in ) show +270.7 718.1 moveto +10 f5 +(FirstBitGA) show +330.7 718.1 moveto +12 f0 +(,) show +333.6 718.1 moveto +12 f0 +(real_value) show +383.5 718.1 moveto +12 f0 +( in ) show +401.8 718.1 moveto +10 f5 +(FirstRealGA) show +467.8 718.1 moveto +12 f0 +(\)) show +471.7 718.1 moveto +12 f0 +( ) show +18.1 708.9 moveto +3.3 3.3 c fill +28 704.8 moveto +12 f0 +(Compile) show +69.3 704.8 moveto +12 f0 +( the program by typing ) show +182.4 704.8 moveto +10 f5 +(make mytest) show +248.4 704.8 moveto +12 f0 +( at system prompt) show +334 704.8 moveto +12 f0 +( ) show +18.1 695.6 moveto +3.3 3.3 c fill +28 691.5 moveto +12 f0 +(Run) show +48 691.5 moveto +12 f0 +( the new program by entering the command ) show +260.8 691.5 moveto +10 f5 +(mytest) show +296.8 691.5 moveto +12 f0 +( at system prompt.) show +385.4 691.5 moveto +12 f0 +( ) show +0 662.6 moveto +14 f1 +(Exercise 2: check the differences between both programs) show +0 635.6 moveto +12 f0 +(Go and take a look at the code for these programs ) show +241.6 635.6 moveto +12 f0 +(\() show +245.5 635.6 moveto +12 f0 +(Bit) show +260.1 635.6 moveto +12 f0 +( - ) show +270 635.6 moveto +12 f0 +(Real) show +291.9 635.6 moveto +12 f0 +(\)) show +295.8 635.6 moveto +12 f0 +(. Use the symbolic representation of) show +0 622.3 moveto +12 f0 +(an Evolutionary Algorithm \(you should understand that figure now, otherwise go ) show +392.6 622.3 moveto +12 f0 +(there) show +416.5 622.3 moveto +12 f0 +( and come) show +0 609 moveto +12 f0 +(back\) to understand how each part of the EA is coded. Try to spot the differences between both) show +0 595.7 moveto +12 f0 +(codes: there are not so many! ) show +0 582.4 moveto +12 f0 +(After you've tried that alone, take a look at the ) show +227.6 582.4 moveto +12 f0 +(solution) show +266.2 582.4 moveto +12 f0 +( :-\) ) show +0 569.1 moveto +12 f0 +( ) show +0 540.2 moveto +14 f1 +(Exercise 3: change the selection procedure) show +0 513.2 moveto +12 f0 +(This is rather straightforward ... if you know what other types of selection are available! ) show +0 499.9 moveto +12 f0 +(At the moment, let's only consider only the following simple ones: ) show +18.1 477.4 moveto +3.3 3.3 c fill +28 473.3 moveto +12 f0 +(You already know the ) show +137.3 473.3 moveto +12 f0 +(tournament selection) show +237.6 473.3 moveto +12 f0 +( ) show +28 460 moveto +12 f0 +(Syntax: ) show +70.6 460 moveto +10 f5 +(eoDetTournament select\(T_SIZE\); ) show +310.6 460 moveto +10 f4 +(// T_SIZE in [2,POP_SIZE\)) show +460.6 460 moveto +12 f0 +( ) show +18.1 450.8 moveto +3.3 3.3 c fill +28 446.7 moveto +12 f0 +(Try the well-known ) show +126.3 446.7 moveto +12 f0 +(roulette wheel) show +194.6 446.7 moveto +12 f0 +( ) show +28 433.4 moveto +12 f0 +( ) show +30.9 433.4 moveto +12 f0 +(Syntax: ) show +70.5 433.4 moveto +12 f0 +( ) show +79.4 433.4 moveto +10 f5 +(eoProportional select;) show +247.4 433.4 moveto +12 f0 +( ) show +18.1 424.2 moveto +3.3 3.3 c fill +28 420.1 moveto +12 f0 +(Or the ) show +61.3 420.1 moveto +12 f0 +(stochastic binary tournament) show +199.9 420.1 moveto +12 f0 +( ) show +28 406.8 moveto +12 f0 +(Syntax: ) show +70.6 406.8 moveto +10 f5 +(eoStochTournament select\(RATE\); ) show +298.6 406.8 moveto +10 f4 +( // RATE in ]0.5,1]) show +430.6 406.8 moveto +12 f0 +( ) show +18.1 397.6 moveto +3.3 3.3 c fill +28 393.5 moveto +12 f0 +(and of course the ) show +113.3 393.5 moveto +12 f0 +(random) show +149.9 393.5 moveto +12 f0 +( selection should give bad results!) show +312.8 393.5 moveto +12 f0 +( ) show +28 380.2 moveto +12 f0 +(Syntax: ) show +70.6 380.2 moveto +10 f5 +(eoSelectRandom select;) show +0 354.1 moveto +12 f0 +(Note that all these classes of eoObjects are derived from the abstract class ) show +357.5 354.1 moveto +14 f1 +(eoSelectOne.) show +434 354.1 moveto +12 f0 +( ) show +0 321 moveto +18 f1 +(Lessons:) show +66 321 moveto +12 f0 +( ) show +18.1 297.2 moveto +3.3 3.3 c fill +28 293.1 moveto +12 f0 +(in EO, all actions are performed by ) show +199.9 293.1 moveto +12 f0 +(functor objects) show +271.5 293.1 moveto +12 f0 +( \(this section is the last time in this tutorial) show +28 279.8 moveto +12 f0 +(that there is a direct link to the ) show +177.3 279.8 moveto +12 f0 +(EO Programming hints) show +288.6 279.8 moveto +12 f0 +( page - though the link at top and) show +28 266.5 moveto +12 f0 +(bottom of all pages will remain there\).) show +212.3 266.5 moveto +12 f0 +( ) show +18.1 256.8 moveto +3.3 3.3 c fill +28 252.7 moveto +12 f0 +(in EO, all object you will usually need to manipulate are ) show +301.9 252.7 moveto +12 f0 +(templatized) show +357.8 252.7 moveto +12 f1 +( w.r.t. the type of the) show +28 238.9 moveto +12 f1 +(individual) show +80 238.9 moveto +12 f0 +( you are handling.) show +166.6 238.9 moveto +12 f0 +( ) show +18.1 229.2 moveto +3.3 3.3 c fill +28 225.1 moveto +12 f0 +(The type of the individual is itself ) show +193.6 225.1 moveto +12 f0 +(templatized) show +249.5 225.1 moveto +12 f1 +( w.r.t. the type of fitness) show +372.1 225.1 moveto +12 f0 +( \(double by default\).) show +470 225.1 moveto +12 f0 +( ) show +18.1 215.9 moveto +3.3 3.3 c fill +28 211.8 moveto +12 f0 +(In EO \(actually, in EC!\) ) show +146.6 211.8 moveto +12 f0 +(initialization and variation) show +273.2 211.8 moveto +12 f0 +( operators are ) show +341.5 211.8 moveto +12 f0 +(representation) show +409.4 211.8 moveto +12 f0 +(-dependent,) show +28 198.5 moveto +12 f0 +(while the ) show +75.3 198.5 moveto +12 f0 +(evolution engine) show +155.6 198.5 moveto +12 f0 +( is ) show +169.6 198.5 moveto +12 f0 +(representation) show +237.5 198.5 moveto +12 f0 +(-independent \(well, like any rule, this one does) show +28 185.2 moveto +12 f0 +(have some exceptions\).) show +140.3 185.2 moveto +12 f0 +( ) show +18.1 176 moveto +3.3 3.3 c fill +28 171.9 moveto +12 f0 +(Changing the ) show +95.3 171.9 moveto +12 f0 +(fitness function) show +169.6 171.9 moveto +12 f0 +(, or the ) show +206.2 171.9 moveto +12 f0 +(selection procedure) show +299.8 171.9 moveto +12 f0 +( inside the generational GA evolution) show +28 158.6 moveto +12 f0 +(engine is straightforward.) show +150.9 158.6 moveto +12 f0 +( ) show +18.1 149.4 moveto +3.3 3.3 c fill +28 145.3 moveto +12 f0 +(remember, all ) show +97.6 145.3 moveto +12 f0 +(solutions) show +140.9 145.3 moveto +12 f0 +( to exercises are in the same sub-dir of dir Tutorial than the lesson) show +28 132 moveto +12 f0 +(itself \(see ) show +77.3 132 moveto +12 f0 +(here) show +97.9 132 moveto +12 f0 +(\).) show +104.8 132 moveto +12 f0 +( ) show +0 109.6 moveto +481 0 rlineto 0 -1.4 rlineto -481 0 rlineto closepath fill +0 90.3 moveto +12 f0 +(Lesson 2) show +42.9 90.3 moveto +12 f0 +( - ) show +52.8 90.3 moveto +12 f0 +(Tutorial main page) show +144.1 90.3 moveto +12 f0 +( - ) show +154 90.3 moveto +12 f0 +(Top-Down page) show +232.3 90.3 moveto +12 f0 +( - ) show +242.2 90.3 moveto +12 f0 +(Bottom-up page) show +319.8 90.3 moveto +12 f0 +( - ) show +329.7 90.3 moveto +12 f0 +(Programming hints) show +422 90.3 moveto +12 f0 +( - ) show +431.9 90.3 moveto +14 f0 +(EO) show +pagelevel restore +showpage +%%Page: 5 5 +%%BeginPageSetup +/pagelevel save def +54 0 translate +%%EndPageSetup +newpath 0 72 moveto 481 0 rlineto 0 689 rlineto -481 0 rlineto closepath clip newpath +0 748.4 moveto +14 f0 +(documentation) show +83.2 748.4 moveto +12 f0 +( ) show +0 738.9 moveto +481 0 rlineto 0 -1.4 rlineto -481 0 rlineto closepath fill +0.2 721.6 moveto +12 f2 +(Marc Schoenauer) show +0 695 moveto +12 f0 +(Last modified: Fri Nov 3 18:49:12 CET 2000) show +218.3 695 moveto +12 f0 +( ) show +pagelevel restore +showpage +%%EOF diff --git a/eo/tutorial/html/real_value.html b/eo/tutorial/html/real_value.html new file mode 100644 index 00000000..c45ffd05 --- /dev/null +++ b/eo/tutorial/html/real_value.html @@ -0,0 +1,56 @@ + + + + + + real_value.h + + +Back to Lesson 2 - Tutorial +main page - Top-Down page - Bottom-up +page - Programming hints - +EO documentation +
+
+
+ + + + +
#include <vector> +
//----------------------------------------------------------------------------- +
/** Just a simple function that takes an +vector<double> and sets the fitnes  +
     to the sphere function. +Please use doubles not float!!! +
     @param _ind A floatingpoint +vector  +
*/
+ + + + + +
double real_value(const std::vector<double>& +_ind) +
{ +
 double sum = 0; +
 for (unsigned i = 0; i < _ind.size(); +i++) +
         +sum += _ind[i] * _ind[i]; +
 return -sum; +
}
+ +
Back to Lesson 2 - Tutorial +main page - Top-Down page - Bottom-up +page - Programming hints - EO +documentation +
+
+Marc Schoenauer
+ +
Last modified: Wed Nov +29 08:58:50 2000 + +