diff --git a/eo/tutorial/Lesson3/exercise1.cpp b/eo/tutorial/Lesson3/exercise1.cpp index 67b941303..a01226ca9 100644 --- a/eo/tutorial/Lesson3/exercise1.cpp +++ b/eo/tutorial/Lesson3/exercise1.cpp @@ -24,6 +24,9 @@ // define your genotype and fitness types typedef eoBin Indi; +// the main_function: nothing changed(!), except variable initialization +void main_function(int argc, char **argv) +{ // PARAMETRES //----------------------------------------------------------------------------- // instead of having all values of useful parameters as constants, read them: @@ -32,97 +35,84 @@ typedef eoBin Indi; // # = 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 + // First define a parser from the command-line arguments eoParser parser(argc, argv); // For each parameter, define Parameter, read it through the parser, // and assign the value to the variable + eoValueParam seedParam(time(0), "seed", "Random number seed", 'S'); parser.processParam( seedParam ); - _seed = seedParam.value(); + unsigned seed = seedParam.value(); + // description of genotype eoValueParam vecSizeParam(8, "vecSize", "Genotype size",'V'); parser.processParam( vecSizeParam, "Representation" ); - _vecSize = vecSizeParam.value(); + unsigned vecSize = vecSizeParam.value(); + // parameters for evolution engine eoValueParam popSizeParam(10, "popSize", "Population size",'P'); parser.processParam( popSizeParam, "Evolution engine" ); - _popSize = popSizeParam.value(); + unsigned popSize = popSizeParam.value(); eoValueParam tSizeParam(10, "tSize", "Tournament size",'T'); parser.processParam( tSizeParam, "Evolution Engine" ); - _tSize = tSizeParam.value(); + unsigned tSize = tSizeParam.value(); + // init and stop eoValueParam loadNameParam("", "Load","A save file to restart from",'L'); parser.processParam( loadNameParam, "Persistence" ); - _load_name = loadNameParam.value(); + string loadName = loadNameParam.value(); eoValueParam maxGenParam(100, "maxGen", "Maximum number of generations",'G'); parser.processParam( maxGenParam, "Stopping criterion" ); - _maxGen = maxGenParam.value(); + unsigned maxGen = maxGenParam.value(); eoValueParam minGenParam(100, "minGen", "Minimum number of generations",'g'); parser.processParam( minGenParam, "Stopping criterion" ); - _minGen = minGenParam.value(); + unsigned minGen = minGenParam.value(); eoValueParam steadyGenParam(100, "steadyGen", "Number of generations with no improvement",'s'); parser.processParam( steadyGenParam, "Stopping criterion" ); - _steadyGen = steadyGenParam.value(); + unsigned steadyGen = steadyGenParam.value(); + // operators probabilities at the algorithm level eoValueParam pCrossParam(0.6, "pCross", "Probability of Crossover", 'C'); parser.processParam( pCrossParam, "Genetic Operators" ); - _pCross = pCrossParam.value(); + double pCross = pCrossParam.value(); eoValueParam pMutParam(0.1, "pMut", "Probability of Mutation", 'M'); parser.processParam( pMutParam, "Genetic Operators" ); - _pMut = pMutParam.value(); + double pMut = pMutParam.value(); + // relative rates for crossovers eoValueParam onePointRateParam(1, "onePointRate", "Relative rate for one point crossover", '1'); parser.processParam( onePointRateParam, "Genetic Operators" ); - _onePointRate = onePointRateParam.value(); + double onePointRate = onePointRateParam.value(); eoValueParam twoPointsRateParam(1, "twoPointRate", "Relative rate for two point crossover", '2'); parser.processParam( twoPointsRateParam, "Genetic Operators" ); - _twoPointsRate = twoPointsRateParam.value(); + double twoPointsRate = twoPointsRateParam.value(); eoValueParam uRateParam(2, "uRate", "Relative rate for uniform crossover", 'U'); parser.processParam( uRateParam, "Genetic Operators" ); - _uRate = uRateParam.value(); + double URate = uRateParam.value(); + // relative rates and private parameters for mutations; eoValueParam pMutPerBitParam(0.01, "pMutPerBit", "Probability of flipping 1 bit in bit-flip mutation", 'b'); parser.processParam( pMutPerBitParam, "Genetic Operators" ); - _pMutPerBit = pMutPerBitParam.value(); + double pMutPerBit = pMutPerBitParam.value(); eoValueParam bitFlipRateParam(0.01, "bitFlipRate", "Relative rate for bit-flip mutation", 'B'); parser.processParam( bitFlipRateParam, "Genetic Operators" ); - _bitFlipRate = bitFlipRateParam.value(); + double bitFlipRate = bitFlipRateParam.value(); eoValueParam oneBitRateParam(0.01, "oneBitRate", "Relative rate for deterministic bit-flip mutation", 'D'); parser.processParam( oneBitRateParam, "Genetic Operators" ); - _oneBitRate = oneBitRateParam.value(); + double oneBitRate = oneBitRateParam.value(); // the name of the "status" file where all actual parameter values will be saved - string str_status = parser.ProgramName() + ".status"; + string str_status = parser.ProgramName() + ".status"; // default value eoValueParam statusParam(str_status.c_str(), "status","Status file",'S'); parser.processParam( statusParam, "Persistence" ); @@ -138,41 +128,6 @@ void read_param(int argc, char *argv[], ofstream os(statusParam.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 ///////////////////////////// @@ -198,9 +153,9 @@ void main_function(int argc, char **argv) inState.registerObject(rng); inState.registerObject(pop); - if (load_name != "") + if (loadName != "") { - inState.load(load_name); // load the pop and the rng + inState.load(loadName); // load the pop and the rng // the fitness is read in the file: // do only evaluate the pop if the fitness has changed } @@ -237,8 +192,11 @@ void main_function(int argc, char **argv) // REPLACE // And we now have the full slection/replacement - though with - // no replacement (== generational replacement) at the moment :-) + // generational replacement at the moment :-) eoGenerationalReplacement replace; + // want to add (weak) elitism? easy! + // rename the eoGenerationalReplacement replace_main, + // then encapsulate it in the elitist replacement // eoWeakElitistReplacement replace(replace_main); // OPERATORS