From f4d2630516f7bd973590a18ea9ce98c90e059bbd Mon Sep 17 00:00:00 2001 From: evomarc Date: Thu, 3 May 2001 17:20:27 +0000 Subject: [PATCH] Corrected a problem in eoEsChromInit (it refused plain eoReal) + some other minor modications (e.g. a dir in make_checkpoint where ALL file will go). --- eo/src/do/make_algo_scalar.h | 18 +++++--- eo/src/do/make_checkpoint.h | 33 +++++++++------ eo/src/do/make_pop.h | 7 +++- eo/src/es/eoEsChromInit.h | 6 +-- eo/src/es/make_genotype_real.h | 6 +-- eo/src/ga/make_genotype_ga.h | 2 +- eo/src/utils/eoRNG.cpp | 15 ++++++- eo/src/utils/eoRNG.h | 2 + eo/tutorial/html/eoLesson4.html | 73 ++++++++++++++++++--------------- 9 files changed, 98 insertions(+), 64 deletions(-) diff --git a/eo/src/do/make_algo_scalar.h b/eo/src/do/make_algo_scalar.h index 9e726354c..8673a80e5 100644 --- a/eo/src/do/make_algo_scalar.h +++ b/eo/src/do/make_algo_scalar.h @@ -73,7 +73,7 @@ template eoAlgo & do_make_algo_scalar(eoParameterLoader& _parser, eoState& _state, eoEvalFunc& _eval, eoContinue& _ccontinue, eoGenOp& _op) { // the selection - eoValueParam& selectionParam = _parser.createParam(eoParamParamType("DetTour(2)"), "selection", "Selection: Roulette, DetTour(T), StochTour(t) or Sequential(ordered/unordered)", 'S', "engine"); + eoValueParam& selectionParam = _parser.createParam(eoParamParamType("DetTour(2)"), "selection", "Selection: Roulette, DetTour(T), StochTour(t) or Sequential(ordered/unordered)", 'S', "Evolution Engine"); eoParamParamType & ppSelect = selectionParam.value(); // pair > @@ -109,18 +109,21 @@ eoAlgo & do_make_algo_scalar(eoParameterLoader& _parser, eoState& _state, e select = new eoProportionalSelect; } else - throw runtime_error("Invalid selection"); + { + string stmp = string("Invalid selection: ") + ppSelect.first; + throw runtime_error(stmp.c_str()); + } _state.storeFunctor(select); // the number of offspring - eoValueParam& offspringRateParam = _parser.createParam(eoRateParamType("100%"), "nbOffspring", "Nb of offspring (percentage or absolute)", 'O', "engine"); + eoValueParam& offspringRateParam = _parser.createParam(eoRateParamType("100%"), "nbOffspring", "Nb of offspring (percentage or absolute)", 'O', "Evolution Engine"); // an eoRateParamType is simply a pair double offRate=offspringRateParam.value().first; bool offInterpret_as_rate = offspringRateParam.value().second; // the replacement - eoValueParam& replacementParam = _parser.createParam(eoParamParamType("Comma"), "replacement", "Replacement: Comma, Plus or EPTour(T), SSGAWorst, SSGADet(T), SSGAStoch(t)", 'R', "engine"); + eoValueParam& replacementParam = _parser.createParam(eoParamParamType("Comma"), "replacement", "Replacement: Comma, Plus or EPTour(T), SSGAWorst, SSGADet(T), SSGAStoch(t)", 'R', "Evolution Engine"); eoParamParamType & ppReplace = replacementParam.value(); // pair > @@ -159,12 +162,15 @@ eoAlgo & do_make_algo_scalar(eoParameterLoader& _parser, eoState& _state, e replace = new eoSSGAStochTournamentReplacement(p); } else - throw runtime_error("Invalid replacement"); + { + string stmp = string("Invalid replacement: ") + ppReplace.first; + throw runtime_error(stmp.c_str()); + } _state.storeFunctor(replace); // adding weak elitism - eoValueParam& weakElitismParam = _parser.createParam(false, "weakElitism", "Old best parent replaces new worst offspring *if necessary*", 'w', "engine"); + eoValueParam& weakElitismParam = _parser.createParam(false, "weakElitism", "Old best parent replaces new worst offspring *if necessary*", 'w', "Evolution Engine"); if (weakElitismParam.value()) { eoReplacement *replaceTmp = replace; diff --git a/eo/src/do/make_checkpoint.h b/eo/src/do/make_checkpoint.h index 0efd02b37..a08447d29 100644 --- a/eo/src/do/make_checkpoint.h +++ b/eo/src/do/make_checkpoint.h @@ -77,12 +77,15 @@ eoCheckPoint& do_make_checkpoint(eoParameterLoader& _parser, eoState& _stat // Best fitness in population //--------------------------- eoValueParam& printBestParam = _parser.createParam(true, "printBestStat", "Print Best/avg/stdev every gen.", '\0', "Output"); - eoValueParam& plotBestParam = _parser.createParam(false, "plotBestStat", "Plot Best/avg Stat", '\0', "Output"); - eoValueParam& bestFileNameParam = _parser.createParam(string("best.xg"), "BestFileName", "Name of file for Best/avg/stdev", '\0', "Output"); - bool fileBestParam = _parser.isItThere(bestFileNameParam); + eoValueParam& plotBestParam = _parser.createParam(false, "plotBestStat", "Plot Best/avg Stat", '\0', "Output - Graphical"); + + // dir for DISK output + eoValueParam& dirNameParam = _parser.createParam(string("Res"), "resDir", "Directory to store DISK outputs", '\0', "Output - Disk"); + eoValueParam& fileBestParam = _parser.createParam(false, "fileBestStat", "Output bes/avg/std to file", '\0', "Output - Disk"); + eoBestFitnessStat *bestStat = NULL; - if ( printBestParam.value() || plotBestParam.value() || fileBestParam ) + if ( printBestParam.value() || plotBestParam.value() || fileBestParam.value() ) // we need the bestStat for at least one of the 3 above { bestStat = new eoBestFitnessStat; @@ -134,7 +137,7 @@ eoCheckPoint& do_make_checkpoint(eoParameterLoader& _parser, eoState& _stat // the Fitness Distance Correlation //--------------------------------- eoValueParam& printFDCParam = _parser.createParam(true, "printFDC", "Print FDC coeff. every gen.", '\0', "Output"); - eoValueParam& plotFDCParam = _parser.createParam(false, "plotFDCStat", "Plot FDC scatter plot", '\0', "Output"); + eoValueParam& plotFDCParam = _parser.createParam(false, "plotFDCStat", "Plot FDC scatter plot", '\0', "Output - Graphical"); eoFDCStat *fdcStat = NULL; if ( printFDCParam.value() || plotFDCParam.value() ) // we need FDCStat @@ -180,9 +183,10 @@ eoCheckPoint& do_make_checkpoint(eoParameterLoader& _parser, eoState& _stat monitor->add(*popStat); } - if (fileBestParam) // A file monitor for best & secondMoment + if (fileBestParam.value()) // A file monitor for best & secondMoment { - eoFileMonitor *fileMonitor = new eoFileMonitor(bestFileNameParam.value()); + string stmp = dirNameParam.value() + "/best.xg"; + eoFileMonitor *fileMonitor = new eoFileMonitor(stmp); // save and give to checkpoint _state.storeFunctor(fileMonitor); checkpoint->add(*fileMonitor); @@ -195,7 +199,8 @@ eoCheckPoint& do_make_checkpoint(eoParameterLoader& _parser, eoState& _stat if (plotBestParam.value()) // an eoGnuplot1DMonitor for best & average { - eoGnuplot1DMonitor *gnuMonitor = new eoGnuplot1DMonitor("_gnu_best.xg",minimizing_fitness()); + string stmp = dirNameParam.value() + "_gnu_best.xg"; + eoGnuplot1DMonitor *gnuMonitor = new eoGnuplot1DMonitor(stmp,minimizing_fitness()); // save and give to checkpoint _state.storeFunctor(gnuMonitor); checkpoint->add(*gnuMonitor); @@ -211,7 +216,7 @@ eoCheckPoint& do_make_checkpoint(eoParameterLoader& _parser, eoState& _stat if (plotFDCParam.value()) // a specific plot monitor for FDC { // first into a file (it adds everything ti itself - eoFDCFileSnapshot *fdcFileSnapshot = new eoFDCFileSnapshot(*fdcStat); + eoFDCFileSnapshot *fdcFileSnapshot = new eoFDCFileSnapshot(*fdcStat, dirNameParam.value()); _state.storeFunctor(fdcFileSnapshot); // then to a Gnuplot monitor eoGnuplot1DSnapshot *fdcGnuplot = new eoGnuplot1DSnapshot(*fdcFileSnapshot); @@ -222,14 +227,14 @@ eoCheckPoint& do_make_checkpoint(eoParameterLoader& _parser, eoState& _stat checkpoint->add(*fdcGnuplot); } - eoValueParam plotHistogramParam = _parser.createParam(false, "plotHisto", "Plot histogram of fitnesses", '\0', "Output"); + eoValueParam plotHistogramParam = _parser.createParam(false, "plotHisto", "Plot histogram of fitnesses", '\0', "Output - Graphical"); if (plotHistogramParam.value()) // want to see how the fitness is spread? { eoScalarFitnessStat *fitStat = new eoScalarFitnessStat; _state.storeFunctor(fitStat); checkpoint->add(*fitStat); // a gnuplot-based monitor for snapshots: needs a dir name - eoGnuplot1DSnapshot *fitSnapshot = new eoGnuplot1DSnapshot("Fitnesses"); + eoGnuplot1DSnapshot *fitSnapshot = new eoGnuplot1DSnapshot(dirNameParam.value()); _state.storeFunctor(fitSnapshot); // add any stat that is a vector to it fitSnapshot->add(*fitStat); @@ -248,7 +253,8 @@ eoCheckPoint& do_make_checkpoint(eoParameterLoader& _parser, eoState& _stat if (_parser.isItThere(saveFrequencyParam)) { unsigned freq = (saveFrequencyParam.value()>0 ? saveFrequencyParam.value() : UINT_MAX ); - eoCountedStateSaver *stateSaver1 = new eoCountedStateSaver(freq, _state, "generation"); + string stmp = dirNameParam.value() + "/generations"; + eoCountedStateSaver *stateSaver1 = new eoCountedStateSaver(freq, _state, stmp); _state.storeFunctor(stateSaver1); checkpoint->add(*stateSaver1); } @@ -257,7 +263,8 @@ eoCheckPoint& do_make_checkpoint(eoParameterLoader& _parser, eoState& _stat eoValueParam& saveTimeIntervalParam = _parser.createParam(unsigned(0), "saveTimeInterval", "Save every T seconds (0 or absent = never)", '\0',"Persistence" ); if (_parser.isItThere(saveTimeIntervalParam) && saveTimeIntervalParam.value()>0) { - eoTimedStateSaver *stateSaver2 = new eoTimedStateSaver(saveTimeIntervalParam.value(), _state, "time"); + string stmp = dirNameParam.value() + "/time"; + eoTimedStateSaver *stateSaver2 = new eoTimedStateSaver(saveTimeIntervalParam.value(), _state, stmp); _state.storeFunctor(stateSaver2); checkpoint->add(*stateSaver2); } diff --git a/eo/src/do/make_pop.h b/eo/src/do/make_pop.h index 0c2ecd75d..ed0d6fc6b 100644 --- a/eo/src/do/make_pop.h +++ b/eo/src/do/make_pop.h @@ -47,8 +47,11 @@ template eoPop& do_make_pop(eoParser & _parser, eoState& _state, eoInit & _init) { - eoValueParam& seedParam = _parser.createParam(uint32(time(0)), "seed", "Random number seed", 'S'); - eoValueParam& popSize = _parser.createParam(unsigned(20), "popSize", "Population Size", 'P', "initialization"); + // random seed + eoValueParam& seedParam = _parser.createParam(uint32(0), "seed", "Random number seed", 'S'); + if (seedParam.value() == 0) + seedParam.value() = random_seed(); + eoValueParam& popSize = _parser.createParam(unsigned(20), "popSize", "Population Size", 'P', "Evolution Engine"); // Either load or initialize // create an empty pop and let the state handle the memory diff --git a/eo/src/es/eoEsChromInit.h b/eo/src/es/eoEsChromInit.h index 90720d3d3..399397405 100644 --- a/eo/src/es/eoEsChromInit.h +++ b/eo/src/es/eoEsChromInit.h @@ -74,10 +74,8 @@ public : private : // No adaptive mutation at all - void create_self_adapt(eoReal& result)// security check :-) - { - throw runtime_error("We should not be in create_self_adapt(eoReal)!"); - } + void create_self_adapt(eoReal& result)// nothing to do here ... + { } // Adaptive mutation through a unique sigma void create_self_adapt(eoEsSimple& result) diff --git a/eo/src/es/make_genotype_real.h b/eo/src/es/make_genotype_real.h index 823b509bc..490071059 100644 --- a/eo/src/es/make_genotype_real.h +++ b/eo/src/es/make_genotype_real.h @@ -64,10 +64,10 @@ eoEsChromInit & do_make_genotype(eoParameterLoader& _parser, eoState& _stat typedef typename EOT::Fitness FitT; // for eoReal, only thing needed is the size - eoValueParam& vecSize = _parser.createParam(unsigned(10), "vecSize", "The number of variables ", 'n',"initialization"); + eoValueParam& vecSize = _parser.createParam(unsigned(10), "vecSize", "The number of variables ", 'n',"Genotype Initialization"); // to build an eoReal Initializer, we need bounds - eoValueParam& boundsParam = _parser.createParam(eoParamParamType("(0,1)"), "initBounds", "Bounds for uniform initialization", 'B', "initialization"); + eoValueParam& boundsParam = _parser.createParam(eoParamParamType("(0,1)"), "initBounds", "Bounds for uniform initialization", 'B', "Genotype Initialization"); eoParamParamType & ppBounds = boundsParam.value(); // pair > // transform into a vector @@ -90,7 +90,7 @@ eoEsChromInit & do_make_genotype(eoParameterLoader& _parser, eoState& _stat // now some initial value for sigmas - even if useless? // shoudl be used in Normal mutation - eoValueParam& sigmaParam = _parser.createParam(0.3, "sigmaInit", "Initial value for Sigma(s)", 's',"initialization"); + eoValueParam& sigmaParam = _parser.createParam(0.3, "sigmaInit", "Initial value for Sigma(s)", 's',"Genotype Initialization"); // minimum check if ( (sigmaParam.value() < 0) ) diff --git a/eo/src/ga/make_genotype_ga.h b/eo/src/ga/make_genotype_ga.h index 9a0fc6a81..93ea00c52 100644 --- a/eo/src/ga/make_genotype_ga.h +++ b/eo/src/ga/make_genotype_ga.h @@ -61,7 +61,7 @@ template eoInit & do_make_genotype(eoParameterLoader& _parser, eoState& _state, EOT) { // for bitstring, only thing needed is the size - eoValueParam& chromSize = _parser.createParam(unsigned(10), "ChromSize", "The length of the bitstrings", 'n',"initialization"); + eoValueParam& chromSize = _parser.createParam(unsigned(10), "ChromSize", "The length of the bitstrings", 'n',"Genotype Initialization"); // Then we can built a bitstring random initializer // based on boolean_generator class (see utils/rnd_generator.h) diff --git a/eo/src/utils/eoRNG.cpp b/eo/src/utils/eoRNG.cpp index b1e592baf..a7de0d17a 100644 --- a/eo/src/utils/eoRNG.cpp +++ b/eo/src/utils/eoRNG.cpp @@ -1,9 +1,22 @@ #include #include "eoRNG.h" +#include +/** this function retunrs a "truly random" seed for RNG + * Replaces the call to time(0) that seems to be non-portable??? + */ +uint32 random_seed() +{ + struct timeval tval; + struct timezone tzp; + + gettimeofday (&tval, &tzp); // milliseconds since midnight January 1, 1970. + return uint32(tval.tv_usec); +} + namespace eo { /// The Global random number generator. -eoRng rng((uint32) time(0)); +eoRng rng(random_seed()); } diff --git a/eo/src/utils/eoRNG.h b/eo/src/utils/eoRNG.h index d92549852..d8b9a6239 100644 --- a/eo/src/utils/eoRNG.h +++ b/eo/src/utils/eoRNG.h @@ -288,6 +288,8 @@ extern eoRng rng; using eo::rng; +/** the random_seed utility in eoRNG.cpp */ +extern uint32 random_seed(); // Implementation of some eoRng members.... Don't mind the mess, it does work. diff --git a/eo/tutorial/html/eoLesson4.html b/eo/tutorial/html/eoLesson4.html index b56f18cbe..61e796641 100644 --- a/eo/tutorial/html/eoLesson4.html +++ b/eo/tutorial/html/eoLesson4.html @@ -2,7 +2,7 @@ - + Tutorial: Lesson 3 @@ -38,7 +38,7 @@ the same objects.



User's -guide +guide

As already said, the behavior of the algorithms will be exactly the same as the previous one as far as optimization is concerned. Only now you will be able to tune every component of the algorithms @@ -55,7 +55,7 @@ the names of the underlying classes (though they should be similar in most cases).

User's guide: Parameter input The way to input parameters -has already be described in Lesson +has already be described in Lesson 3. To get a list of parameters, type the command with option --help (or -h): with both testBit and testReal this will result in

    @@ -88,7 +88,7 @@ General ######
    # --help=0 # -h : Prints this message
    Boolean parameter of -absolutely no interest: tells whether or not help was requested. +absolutely no interest: tells whether or not help was requested.

    # --seed=988700289 # -S : Random number seed
    Unsigned long parameter: @@ -103,44 +103,46 @@ seed).
    Section ###### Output ######
    This section contains parameters related to output -(to screen, to files, graphical, ...). +(to screen, to files, graphical, ...).

    # --useEval=1 # Use nb of eval. as counter (vs nb of gen.)
    Boolean parameter: whether or not you want the nb of evluations to be displayed and used as counter in statistics outputs and plots. Default -is true. +is true.

    # --printBestStat=1 # Print Best/avg/stdev every gen.
    Boolean parameter: toggles screen output of indicated statistics. Default -is true. +is true.

    # --plotBestStat=0 # Plot Best/avg Stat
    Boolean parameter: toggles gnuplot output of best and average plots (Linux only at the moment). -Default is false. +Default +is false.

    # --BestFileName=best.xg # Name of file for Best/avg/stdev
    String parameter: if present, the statistics are stored in that file (no -default) +default)

    # --printPop=0 # Print sorted pop. every gen.
    Boolean parameter: adds a dump of the whole population to the screen every generation. Is likely to generate huge -output! Default is false. +output! Default is false.

    # --printFDC=1 # Print FDC coeff. every gen.
    Boolean parameter: adds Fitness Distance Correlation to output every generation. Default -is false. +is false.

    # --plotFDCStat=0 # Plot FDC scatter plot
    Boolean parameter: toggles the Fitness Distance Correlation plot (Fitness vs distance to best). -Default is false. +Default +is false.

    # --plotHisto=0 # Plot histogram of fitnesses
    Boolean parameter: @@ -152,7 +154,7 @@ is false.
    Section ###### Persistence ######
    This section contains parameters handling job -dump and restart mechanism +dump and restart mechanism

    # --Load= # -L : A save file to restart from
    String parameter: @@ -166,13 +168,13 @@ to re-use the same parameters is to ... use that very save file as parameter file, as it contains all actual parameters in the right format.
    Note that if not enough individuals are read, the remaining are randomly initialized. No -default value. +default value.

    # --recomputeFitness=0 # -r : Recompute the fitness after re-loading the pop.?
    Boolean parameter: in case some individuals are read from a file, their fitness is read too. If this one is true, it is nevertheless recomputed. Default -is false i.e. use fitnes that's in the file. +is false i.e. use fitnes that's in the file.

    # --saveFrequency=0 # Save every F generation (0 = only final state, absent = never)
    Integer parameter: @@ -181,13 +183,13 @@ to disk, in a file named genNN.sav, where NN is the generation number. If this prameter is present (even with 0 or negative value), the final population will always be saved, whatever the reason for stopping. Hence the only way to avoid all saves is to omit the parameter (there is no -default value). +default value).

    # --saveTimeInterval=0 # Save every T seconds (0 or absent = never)
    Integer parameter: time interval between two population (+RNG + parameters) dumps to disks. Files are names timeNN.sav. See pervious parameter description for ore -details. No default value. +details. No default value.

    # --status=t-eoGA.status # Status file
    String parameter: @@ -199,32 +201,34 @@ the name /dev/null (Unix). Default value is Program
    Section ###### Stopping criterion ######
    This section allows to decide when the algorithm -will stop. +will stop.

    # --maxGen=100 # -G : Maximum number of generations (0 = none)
    Integer parameter: maximum number of generations. A value of 0 disables that stopping criterion. Default -is 100. +is 100.

    # --steadyGen=100 # -s : Number of generations with no improvement
    Integer parameter: stops whenever that number of generations is passed without any improvement of the best fitness in the population, provided the following minimum number -of generations has been done. No default value. +of generations has been done. No default value.

    # --minGen=0 # -g : Minimum number of generations
    Integer parameter: the above steadyGen parameter starts its job only after that minimum nuber of generations is passed. -No default value. +No +default value.

    # --maxEval=0 # -E : Maximum number of evaluations (0 = none)
    Integer parameter: maximum number of generations. -No default value. +No +default value.

    # --targetFitness=0 # -T : Stop when fitness reaches
    Real-valued parameter: the algorithm stops whenever the best fitness reaches that target. No default -value. +value.

    # --CtrlC=0 # -C : Terminate current generation upon Ctrl C
    Boolean parameter: if true, Ctrl C only stops @@ -237,7 +241,7 @@ in Unix at the moment. Default is false. engine ######
    In this section, one chooses all components of the Evolution Engine (selection, replacemenet -and the like). +and the like).

    # --selection=DetTour(2) # -S : Selection: Roulette, DetTour(T), StochTour(t) or Sequential(ordered/unordered)
    String parameter: @@ -256,23 +260,23 @@ all individuals in turn), either from best to worst (option unordered); and finally repeated independent uniform choices  (name Random). -Default is DetTour(2). +Default is DetTour(2).

    # --nbOffspring=100% # -O : Nb of offspring (percentage or absolute)
    Integer or real-valued parameter: this parameter indicates the amount of offspring that will be generated from the genitors every generation. However, this amount can be specified either -relative to the -population size, and it should then end with percent character (%), or -as an absolute +relative +to the population size, and it should then end with percent character (%), +or as an absolute integer number (without the percent char).
    Indeed, you can either want, say 7 times more offspring than parents (a rather common situation in Evolution Strategies), in which case you give value 700% to nbOffspring parameter; or you might want a single offspring whatever the population size, like in Steady-State evolution engines, in which case you simply -enter value 1. Default is 100%. +enter value 1. Default is 100%.

    # --replacement=Comma # -R : Replacement: Comma, Plus, EPTour(T), SSGAWorst, SSGADet(T), SSGAStoch(t)
    String parameter: @@ -280,18 +284,19 @@ Name of replacement procedure. Availabable are the deterministic replacement strategies (named respectively Plus and  Comma); -EP stochastic tournament -(name EPTour +EP +stochastic tournament (name EPTour with tournament size in parentheses); and the steady-state -replacement procedures, at the moment -only based on fitnesses, replacement being either deterministic +replacement +procedures, at the moment only based on fitnesses, replacement being either +deterministic (new born replace worst parent, name SSGAWorst),  or based on a tournament  (name SSGADet for deterministic tournament, size in parentheses, and SSGAStoch  for stochastic tournament, probability in parentheses). Default is Comma (which btw is also SGA generational replacement whenever there are as many offspring -as parents). +as parents).

    # --weakElitism=0 # -w : Old best parent replaces new worst offspring *if necessary*
    Boolean parameter: