diff --git a/eo/tutorial/Lesson3/SecondBitEA.cpp b/eo/tutorial/Lesson3/SecondBitEA.cpp index 457346a2..9126710c 100644 --- a/eo/tutorial/Lesson3/SecondBitEA.cpp +++ b/eo/tutorial/Lesson3/SecondBitEA.cpp @@ -32,7 +32,7 @@ typedef eoBin Indi; // 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[], +void read_param(eoParser & _parser, uint32 & _seed, unsigned int & _vecSize, unsigned int & _popSize, @@ -51,74 +51,71 @@ void read_param(int argc, char *argv[], 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'); + eoValueParam& seedParam = _parser.createParam(time(0), "seed", "Random number seed", 'S'); _seed = seedParam.value(); - eoValueParam& vecSizeParam = parser.createParam(8, "vecSize", "Genotype size",'V', "Representation"); + eoValueParam& vecSizeParam = _parser.createParam(8, "vecSize", "Genotype size",'V', "Representation"); _vecSize = vecSizeParam.value(); - eoValueParam& popSizeParam = parser.createParam(10, "popSize", "Population size",'P', "Evolution"); + eoValueParam& popSizeParam = _parser.createParam(10, "popSize", "Population size",'P', "Evolution"); _popSize = popSizeParam.value(); - eoValueParam& tSizeParam = parser.createParam(10, "tSize", "Tournament size",'T', "Evolution"); + 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"); + 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"); + 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"); + 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"); + 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"); + 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"); + 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"); + 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"); + 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"); + 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"); + 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"); + 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"); + 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"); + 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()) + if (_parser.userNeedsHelp()) { - parser.printHelp(cout); + _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 + os << _parser; // and you can use that file as parameter file } } @@ -150,8 +147,11 @@ void main_function(int argc, char **argv) double bitFlipRate; double oneBitRate; + // define a parser from the command-line arguments + eoParser parser(argc, argv); + // Now read the parameters of the program - read_param(argc, argv, seed, vecSize, popSize, tSize, + read_param(parser, seed, vecSize, popSize, tSize, pCross, pMut, load_name, maxGen, minGen, steadyGen, onePointRate, twoPointsRate, URate, pMutPerBit, bitFlipRate, oneBitRate ); @@ -319,12 +319,13 @@ void main_function(int argc, char **argv) // 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(parser); outState.registerObject(pop); + outState.registerObject(rng); // and feed the state to state savers // save state every 100th generation - eoCountedStateSaver stateSaver1(100, outState, "generation"); + eoCountedStateSaver stateSaver1(20, outState, "generation"); // save state every 1 seconds eoTimedStateSaver stateSaver2(1, outState, "time");