From 9823af7c093c63d70464e9d0ad41049e7910df74 Mon Sep 17 00:00:00 2001 From: mac Date: Fri, 31 Mar 2000 10:04:39 +0000 Subject: [PATCH] Finalized Checkpointing, renamed t-testSta.... to t-eoStateAndParser added a checkpoint tester, but did not yet update the Makefiles as I don't have automake on my machine --- eo/test/t-eoCheckpointing.cpp | 173 ++++++++++++++ eo/test/t-eoESFull.cpp | 220 +++++++++--------- ...teAndParser.cpp => t-eoStateAndParser.cpp} | 24 +- 3 files changed, 303 insertions(+), 114 deletions(-) create mode 100644 eo/test/t-eoCheckpointing.cpp rename eo/test/{t-testStateAndParser.cpp => t-eoStateAndParser.cpp} (82%) diff --git a/eo/test/t-eoCheckpointing.cpp b/eo/test/t-eoCheckpointing.cpp new file mode 100644 index 00000000..ee4ac0a9 --- /dev/null +++ b/eo/test/t-eoCheckpointing.cpp @@ -0,0 +1,173 @@ +//----------------------------------------------------------------------------- + +// to avoid long name warnings +#ifdef _MSC_VER +#pragma warning(disable:4786) +#endif + +#include // runtime_error + +//----------------------------------------------------------------------------- +// tt.cpp: +// +//----------------------------------------------------------------------------- + + +// general +#include // Random number generators +#include +#include +#include + +//----------------------------------------------------------------------------- + +// include package checkpointing +#include +#include + +struct Dummy : public EO +{ + typedef double Type; +}; + + +struct eoDummyPop : public eoPop +{ +public : + eoDummyPop(int s = 2) { resize(s); } +}; + +//----------------------------------------------------------------------------- + +int the_main(int argc, char **argv) +{ // ok, we have a command line parser and a state + + typedef eoBin Chrom; + + eoParser parser(argc, argv); + + // Define Parameters + eoValueParam& chrom_size = parser.createParam(unsigned(2), "chrom-size", "Chromosome size"); + eoValueParam rate(0.01, "mutationRatePerBit", "Initial value for mutation rate per bit"); + eoValueParam factor(0.99, "mutationFactor", "Decrease factor for mutation rate"); + eoValueParam seed(time(0), "seed", "Random number seed"); + eoValueParam load_name("", "Load","Load",'L'); + eoValueParam save_name("", "Save","Save",'S'); + + // Register them + parser.processParam(rate, "Genetic Operators"); + parser.processParam(factor, "Genetic Operators"); + parser.processParam(load_name, "Persistence"); + parser.processParam(save_name, "Persistence"); + parser.processParam(seed, "Rng seeding"); + + eoState state; + state.registerObject(parser); + + if (load_name.value() != "") + { // load the parser. This is only neccessary when the user wants to + // be able to change the parameters in the state file by hand. + state.load(load_name.value()); // load the parser + } + + // Create the algorithm here + typedef Dummy EoType; + + eoDummyPop pop; + + eoGenTerm genTerm(5); // 5 generations + + eoCheckPoint checkpoint(genTerm); + + eoValueParam generationCounter(0, "Generation"); + eoIncrementor increment(generationCounter.value()); + + checkpoint.add(increment); + + eoFileMonitor monitor("monitor.csv"); + checkpoint.add(monitor); + + monitor.add(generationCounter); + + eoSecondMomentStats stats; + + checkpoint.add(stats); + monitor.add(stats); + + eoCountedStateSaver stateSaver1(3, state, "generation"); // save every third generation + eoTimedStateSaver stateSaver2(2, state, "time"); // save every 2 seconds + + checkpoint.add(stateSaver1); + checkpoint.add(stateSaver2); + + // Register the algorithm + state.registerObject(rng); + state.registerObject(pop); + + if (parser.userNeedsHelp()) + { + parser.printHelp(cout); + return 0; + } + + // Either load or initialize + if (load_name.value() != "") + { + state.load(load_name.value()); // load the rest + } + else + { + // else + + // initialize rng and population + + rng.reseed(seed.value()); + + pop.resize(2); + + pop[0].fitness(1); + pop[1].fitness(2); + } + + while(checkpoint(pop)) + { + pop[0].fitness(pop[0].fitness() + 1); + + time_t now = time(0); + + while (time(0) == now) {} // wait a second to test timed saver + + cout << "gen " << generationCounter.value() << endl; + } + + // run the algorithm + + // Save when needed + if (save_name.value() != "") + { + string file_name = save_name.value(); + save_name.value() = ""; // so that it does not appear in the parser section of the state file + state.save(file_name); + } + + for (int i = 0; i < 100; ++i) + rng.rand(); + + cout << "a random number is " << rng.random(1024) << endl;; + + return 1; +} + +int main(int argc, char **argv) +{ + try + { + the_main(argc, argv); + } + catch(exception& e) + { + cout << "Exception: " << e.what() << endl; + } + + return 1; +} \ No newline at end of file diff --git a/eo/test/t-eoESFull.cpp b/eo/test/t-eoESFull.cpp index 71e57376..71df2f2e 100644 --- a/eo/test/t-eoESFull.cpp +++ b/eo/test/t-eoESFull.cpp @@ -1,107 +1,113 @@ -// Program to test several EO-ES features - -#ifdef _MSC_VER -#pragma warning(disable:4786) -#endif - -#include -#include -#include - -using namespace std; - -// general -#include // though contained in all others! -// evolution specific -#include -//#include -//#include -// representation specific - -#include // though contained in following -//#include -//#include -//#include -// this fitness -#include "real_value.h" // the sphere fitness - -// Now the main -/////////////// -typedef eoESFullChrom Ind; - -main(int argc, char *argv[]) { -// unsigned mu, lambda; -// bool comma; - - // Create the command-line parser - Parser parser( argc, argv, "Basic EA for vector with adaptive mutations"); - - //reproducible random seed - thanks, Maarten - InitRandom(parser); - - // a first Ind, reading its parameters from the parser - // will be used later to inialize the whole population - Ind FirstEO(parser); - - // Evaluation - // here we should call some parser-based constructor, - // as many evaluation function need parameters - // and also have some preliminary stuffs to do - eoEvalFuncPtr eval( real_value ); - - /* - // Evolution and population parameters - eoScheme the_scheme(parser); - - // recombination and mutation operators, reading their parameters from the parser - eoESReco MyReco(parser, FirstEO); - eoESMutate MyMut(parser, FirstEO); - - // termination conditions read by the parser - eoTermVector the_terms(parser); - - // Initialization of the population - // shoudl be called using the parser, in case you want to read from file(s) - eoESRandomize randomize; // an eoESInd randomnizer - eoPop pop(the_scheme.PopSize(), FirstEO, randomize); - // eval(pop); // shoudl we call it from inside the constructor??? - - // ALL parmeters have been read: write them out - // Writing the parameters on arv[0].status - // but of course this can be modified - see the example parser.cpp - parser.outputParam(); - // except the help parameter??? - if( parser.getBool("-h" , "--help" , "Shows this help")) { - parser.printHelp(); - exit(1); - } - - unsigned i, iind; - - - cout << "Initial population: \n" << endl; - for (i = 0; i < pop.size(); ++i) { - eval(pop[i]); - cout << pop[i].fitness() << "\t" << pop[i] << endl; - } - - // the Operators - eoSequentialOpHolder seqholder; - // seqholder.addOp(MyReco, 1.0); - seqholder.addOp(MyMut, 1.0); - - // One generation - eoEvolStep evol_scheme(the_scheme, seqholder, eval); - - // the algorithm: - eoFullEA ea(evol_scheme, the_terms); - - ea(pop); - - cout << "Final population: \n" << endl; - for (i = 0; i < pop.size(); ++i) - cout << pop[i].fitness() << "\t" << pop[i] << endl; - */ - return 0; -} - +// Program to test several EO-ES features + +#ifdef _MSC_VER +#pragma warning(disable:4786) +#endif + +#include +#include +#include + +using namespace std; + +#include + +// evolution specific +#include + +// representation specific +#include // though contained in following +//#include +//#include +//#include +// this fitness +#include "real_value.h" // the sphere fitness + +// Now the main +/////////////// +typedef eoESFullChrom Ind; + +main(int argc, char *argv[]) { +// unsigned mu, lambda; +// bool comma; + + // Create the command-line parser + Parser parser( argc, argv, "Basic EA for vector with adaptive mutations"); + + // Define Parameters and load them + eoValueParam& seed = parser.createParam(time(0), "seed", "Random number seed"); + eoValueParam& load_name = parser.createParam("", "Load","Load a state file",'L'); + eoValueParam& save_name = parser.createParam("", "Save","Saves a state file",'S'); + + eoState state; + state.registerObject(parser); + + if (load_name.value() != "") + { // load the parser. This is only neccessary when the user wants to + // be able to change the parameters in the state file by hand. + state.load(load_name.value()); // load the parser + } + + + // Evaluation + eoEvalFuncPtr eval( real_value ); + + + + /* + // Evolution and population parameters + eoScheme the_scheme(parser); + + // recombination and mutation operators, reading their parameters from the parser + eoESReco MyReco(parser, FirstEO); + eoESMutate MyMut(parser, FirstEO); + + // termination conditions read by the parser + eoTermVector the_terms(parser); + + // Initialization of the population + // shoudl be called using the parser, in case you want to read from file(s) + eoESRandomize randomize; // an eoESInd randomnizer + eoPop pop(the_scheme.PopSize(), FirstEO, randomize); + // eval(pop); // shoudl we call it from inside the constructor??? + + // ALL parmeters have been read: write them out + // Writing the parameters on arv[0].status + // but of course this can be modified - see the example parser.cpp + parser.outputParam(); + // except the help parameter??? + if( parser.getBool("-h" , "--help" , "Shows this help")) { + parser.printHelp(); + exit(1); + } + + unsigned i, iind; + + + cout << "Initial population: \n" << endl; + for (i = 0; i < pop.size(); ++i) { + eval(pop[i]); + cout << pop[i].fitness() << "\t" << pop[i] << endl; + } + + // the Operators + eoSequentialOpHolder seqholder; + // seqholder.addOp(MyReco, 1.0); + seqholder.addOp(MyMut, 1.0); + + // One generation + eoEvolStep evol_scheme(the_scheme, seqholder, eval); + + // the algorithm: + eoFullEA ea(evol_scheme, the_terms); + + ea(pop); + + cout << "Final population: \n" << endl; + for (i = 0; i < pop.size(); ++i) + cout << pop[i].fitness() << "\t" << pop[i] << endl; + */ + return 0; +} + + diff --git a/eo/test/t-testStateAndParser.cpp b/eo/test/t-eoStateAndParser.cpp similarity index 82% rename from eo/test/t-testStateAndParser.cpp rename to eo/test/t-eoStateAndParser.cpp index 87f0a8c9..e058db3a 100644 --- a/eo/test/t-testStateAndParser.cpp +++ b/eo/test/t-eoStateAndParser.cpp @@ -1,7 +1,9 @@ //----------------------------------------------------------------------------- // to avoid long name warnings +#ifdef _MSC_VER #pragma warning(disable:4786) +#endif #include // runtime_error @@ -19,6 +21,15 @@ //----------------------------------------------------------------------------- +// include package checkpointing +#include +#include + +struct Dummy : public EO +{ + typedef double Type; +}; + //----------------------------------------------------------------------------- @@ -30,7 +41,7 @@ int the_main(int argc, char **argv) eoParser parser(argc, argv); // Define Parameters - eoValueParam chrom_size(2, "chrom-size", "Chromosome size"); + eoValueParam& chrom_size = parser.createParam(unsigned(2), "chrom-size", "Chromosome size"); eoValueParam rate(0.01, "mutationRatePerBit", "Initial value for mutation rate per bit"); eoValueParam factor(0.99, "mutationFactor", "Decrease factor for mutation rate"); eoValueParam seed(time(0), "seed", "Random number seed"); @@ -38,12 +49,11 @@ int the_main(int argc, char **argv) eoValueParam save_name("", "Save","Save",'S'); // Register them - parser.processParam(chrom_size, "Representation"); - parser.processParam(rate, "Genetic Operators"); - parser.processParam(factor, "Genetic Operators"); - parser.processParam(load_name, "Persistence"); - parser.processParam(save_name, "Persistence"); - parser.processParam(seed, "Rng seeding"); + parser.processParam(rate, "Genetic Operators"); + parser.processParam(factor, "Genetic Operators"); + parser.processParam(load_name, "Persistence"); + parser.processParam(save_name, "Persistence"); + parser.processParam(seed, "Rng seeding"); eoState state; state.registerObject(parser);