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
This commit is contained in:
parent
9bcf9d95f8
commit
9823af7c09
3 changed files with 303 additions and 114 deletions
173
eo/test/t-eoCheckpointing.cpp
Normal file
173
eo/test/t-eoCheckpointing.cpp
Normal file
|
|
@ -0,0 +1,173 @@
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// to avoid long name warnings
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
#pragma warning(disable:4786)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdexcept> // runtime_error
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
// tt.cpp:
|
||||||
|
//
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
// general
|
||||||
|
#include <utils/eoRNG.h> // Random number generators
|
||||||
|
#include <ga/eoBin.h>
|
||||||
|
#include <utils/eoParser.h>
|
||||||
|
#include <utils/eoState.h>
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// include package checkpointing
|
||||||
|
#include <utils/checkpointing>
|
||||||
|
#include <eoGenTerm.h>
|
||||||
|
|
||||||
|
struct Dummy : public EO<double>
|
||||||
|
{
|
||||||
|
typedef double Type;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
struct eoDummyPop : public eoPop<Dummy>
|
||||||
|
{
|
||||||
|
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<float> Chrom;
|
||||||
|
|
||||||
|
eoParser parser(argc, argv);
|
||||||
|
|
||||||
|
// Define Parameters
|
||||||
|
eoValueParam<unsigned>& chrom_size = parser.createParam(unsigned(2), "chrom-size", "Chromosome size");
|
||||||
|
eoValueParam<double> rate(0.01, "mutationRatePerBit", "Initial value for mutation rate per bit");
|
||||||
|
eoValueParam<double> factor(0.99, "mutationFactor", "Decrease factor for mutation rate");
|
||||||
|
eoValueParam<uint32> seed(time(0), "seed", "Random number seed");
|
||||||
|
eoValueParam<string> load_name("", "Load","Load",'L');
|
||||||
|
eoValueParam<string> 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<EoType> genTerm(5); // 5 generations
|
||||||
|
|
||||||
|
eoCheckPoint<EoType> checkpoint(genTerm);
|
||||||
|
|
||||||
|
eoValueParam<unsigned> generationCounter(0, "Generation");
|
||||||
|
eoIncrementor<unsigned> increment(generationCounter.value());
|
||||||
|
|
||||||
|
checkpoint.add(increment);
|
||||||
|
|
||||||
|
eoFileMonitor monitor("monitor.csv");
|
||||||
|
checkpoint.add(monitor);
|
||||||
|
|
||||||
|
monitor.add(generationCounter);
|
||||||
|
|
||||||
|
eoSecondMomentStats<EoType> 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;
|
||||||
|
}
|
||||||
|
|
@ -1,107 +1,113 @@
|
||||||
// Program to test several EO-ES features
|
// Program to test several EO-ES features
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#pragma warning(disable:4786)
|
#pragma warning(disable:4786)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
// general
|
#include <utils/eoParser.h>
|
||||||
#include <eoParser.h> // though contained in all others!
|
|
||||||
// evolution specific
|
// evolution specific
|
||||||
#include <eoEvalFuncPtr.h>
|
#include <eoEvalFuncPtr.h>
|
||||||
//#include <eoSequentialOpHolder.h>
|
|
||||||
//#include <eoFullEA.h>
|
// representation specific
|
||||||
// representation specific
|
#include <es/eoESFullChrom.h> // though contained in following
|
||||||
|
//#include <eoESReco.h>
|
||||||
#include <eoESFullChrom.h> // though contained in following
|
//#include <eoESMut.h>
|
||||||
//#include <eoESReco.h>
|
//#include <eoESRandomize.h>
|
||||||
//#include <eoESMut.h>
|
// this fitness
|
||||||
//#include <eoESRandomize.h>
|
#include "real_value.h" // the sphere fitness
|
||||||
// this fitness
|
|
||||||
#include "real_value.h" // the sphere fitness
|
// Now the main
|
||||||
|
///////////////
|
||||||
// Now the main
|
typedef eoESFullChrom<float> Ind;
|
||||||
///////////////
|
|
||||||
typedef eoESFullChrom<float> Ind;
|
main(int argc, char *argv[]) {
|
||||||
|
// unsigned mu, lambda;
|
||||||
main(int argc, char *argv[]) {
|
// bool comma;
|
||||||
// unsigned mu, lambda;
|
|
||||||
// bool comma;
|
// Create the command-line parser
|
||||||
|
Parser parser( argc, argv, "Basic EA for vector<float> with adaptive mutations");
|
||||||
// Create the command-line parser
|
|
||||||
Parser parser( argc, argv, "Basic EA for vector<float> with adaptive mutations");
|
// Define Parameters and load them
|
||||||
|
eoValueParam<uint32>& seed = parser.createParam(time(0), "seed", "Random number seed");
|
||||||
//reproducible random seed - thanks, Maarten
|
eoValueParam<string>& load_name = parser.createParam("", "Load","Load a state file",'L');
|
||||||
InitRandom(parser);
|
eoValueParam<string>& save_name = parser.createParam("", "Save","Saves a state file",'S');
|
||||||
|
|
||||||
// a first Ind, reading its parameters from the parser
|
eoState state;
|
||||||
// will be used later to inialize the whole population
|
state.registerObject(parser);
|
||||||
Ind FirstEO(parser);
|
|
||||||
|
if (load_name.value() != "")
|
||||||
// Evaluation
|
{ // load the parser. This is only neccessary when the user wants to
|
||||||
// here we should call some parser-based constructor,
|
// be able to change the parameters in the state file by hand.
|
||||||
// as many evaluation function need parameters
|
state.load(load_name.value()); // load the parser
|
||||||
// and also have some preliminary stuffs to do
|
}
|
||||||
eoEvalFuncPtr<Ind> eval( real_value );
|
|
||||||
|
|
||||||
/*
|
// Evaluation
|
||||||
// Evolution and population parameters
|
eoEvalFuncPtr<Ind> eval( real_value );
|
||||||
eoScheme<Ind> the_scheme(parser);
|
|
||||||
|
|
||||||
// recombination and mutation operators, reading their parameters from the parser
|
|
||||||
eoESReco<float> MyReco(parser, FirstEO);
|
/*
|
||||||
eoESMutate<float> MyMut(parser, FirstEO);
|
// Evolution and population parameters
|
||||||
|
eoScheme<Ind> the_scheme(parser);
|
||||||
// termination conditions read by the parser
|
|
||||||
eoTermVector<Ind> the_terms(parser);
|
// recombination and mutation operators, reading their parameters from the parser
|
||||||
|
eoESReco<float> MyReco(parser, FirstEO);
|
||||||
// Initialization of the population
|
eoESMutate<float> MyMut(parser, FirstEO);
|
||||||
// shoudl be called using the parser, in case you want to read from file(s)
|
|
||||||
eoESRandomize<float> randomize; // an eoESInd randomnizer
|
// termination conditions read by the parser
|
||||||
eoPop<Ind> pop(the_scheme.PopSize(), FirstEO, randomize);
|
eoTermVector<Ind> the_terms(parser);
|
||||||
// eval(pop); // shoudl we call it from inside the constructor???
|
|
||||||
|
// Initialization of the population
|
||||||
// ALL parmeters have been read: write them out
|
// shoudl be called using the parser, in case you want to read from file(s)
|
||||||
// Writing the parameters on arv[0].status
|
eoESRandomize<float> randomize; // an eoESInd randomnizer
|
||||||
// but of course this can be modified - see the example parser.cpp
|
eoPop<Ind> pop(the_scheme.PopSize(), FirstEO, randomize);
|
||||||
parser.outputParam();
|
// eval(pop); // shoudl we call it from inside the constructor???
|
||||||
// except the help parameter???
|
|
||||||
if( parser.getBool("-h" , "--help" , "Shows this help")) {
|
// ALL parmeters have been read: write them out
|
||||||
parser.printHelp();
|
// Writing the parameters on arv[0].status
|
||||||
exit(1);
|
// but of course this can be modified - see the example parser.cpp
|
||||||
}
|
parser.outputParam();
|
||||||
|
// except the help parameter???
|
||||||
unsigned i, iind;
|
if( parser.getBool("-h" , "--help" , "Shows this help")) {
|
||||||
|
parser.printHelp();
|
||||||
|
exit(1);
|
||||||
cout << "Initial population: \n" << endl;
|
}
|
||||||
for (i = 0; i < pop.size(); ++i) {
|
|
||||||
eval(pop[i]);
|
unsigned i, iind;
|
||||||
cout << pop[i].fitness() << "\t" << pop[i] << endl;
|
|
||||||
}
|
|
||||||
|
cout << "Initial population: \n" << endl;
|
||||||
// the Operators
|
for (i = 0; i < pop.size(); ++i) {
|
||||||
eoSequentialOpHolder <Ind> seqholder;
|
eval(pop[i]);
|
||||||
// seqholder.addOp(MyReco, 1.0);
|
cout << pop[i].fitness() << "\t" << pop[i] << endl;
|
||||||
seqholder.addOp(MyMut, 1.0);
|
}
|
||||||
|
|
||||||
// One generation
|
// the Operators
|
||||||
eoEvolStep<Ind> evol_scheme(the_scheme, seqholder, eval);
|
eoSequentialOpHolder <Ind> seqholder;
|
||||||
|
// seqholder.addOp(MyReco, 1.0);
|
||||||
// the algorithm:
|
seqholder.addOp(MyMut, 1.0);
|
||||||
eoFullEA<Ind> ea(evol_scheme, the_terms);
|
|
||||||
|
// One generation
|
||||||
ea(pop);
|
eoEvolStep<Ind> evol_scheme(the_scheme, seqholder, eval);
|
||||||
|
|
||||||
cout << "Final population: \n" << endl;
|
// the algorithm:
|
||||||
for (i = 0; i < pop.size(); ++i)
|
eoFullEA<Ind> ea(evol_scheme, the_terms);
|
||||||
cout << pop[i].fitness() << "\t" << pop[i] << endl;
|
|
||||||
*/
|
ea(pop);
|
||||||
return 0;
|
|
||||||
}
|
cout << "Final population: \n" << endl;
|
||||||
|
for (i = 0; i < pop.size(); ++i)
|
||||||
|
cout << pop[i].fitness() << "\t" << pop[i] << endl;
|
||||||
|
*/
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,9 @@
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
// to avoid long name warnings
|
// to avoid long name warnings
|
||||||
|
#ifdef _MSC_VER
|
||||||
#pragma warning(disable:4786)
|
#pragma warning(disable:4786)
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <stdexcept> // runtime_error
|
#include <stdexcept> // runtime_error
|
||||||
|
|
||||||
|
|
@ -19,6 +21,15 @@
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
// include package checkpointing
|
||||||
|
#include <utils/checkpointing>
|
||||||
|
#include <eoGenTerm.h>
|
||||||
|
|
||||||
|
struct Dummy : public EO<double>
|
||||||
|
{
|
||||||
|
typedef double Type;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
@ -30,7 +41,7 @@ int the_main(int argc, char **argv)
|
||||||
eoParser parser(argc, argv);
|
eoParser parser(argc, argv);
|
||||||
|
|
||||||
// Define Parameters
|
// Define Parameters
|
||||||
eoValueParam<unsigned> chrom_size(2, "chrom-size", "Chromosome size");
|
eoValueParam<unsigned>& chrom_size = parser.createParam(unsigned(2), "chrom-size", "Chromosome size");
|
||||||
eoValueParam<double> rate(0.01, "mutationRatePerBit", "Initial value for mutation rate per bit");
|
eoValueParam<double> rate(0.01, "mutationRatePerBit", "Initial value for mutation rate per bit");
|
||||||
eoValueParam<double> factor(0.99, "mutationFactor", "Decrease factor for mutation rate");
|
eoValueParam<double> factor(0.99, "mutationFactor", "Decrease factor for mutation rate");
|
||||||
eoValueParam<uint32> seed(time(0), "seed", "Random number seed");
|
eoValueParam<uint32> seed(time(0), "seed", "Random number seed");
|
||||||
|
|
@ -38,12 +49,11 @@ int the_main(int argc, char **argv)
|
||||||
eoValueParam<string> save_name("", "Save","Save",'S');
|
eoValueParam<string> save_name("", "Save","Save",'S');
|
||||||
|
|
||||||
// Register them
|
// Register them
|
||||||
parser.processParam(chrom_size, "Representation");
|
parser.processParam(rate, "Genetic Operators");
|
||||||
parser.processParam(rate, "Genetic Operators");
|
parser.processParam(factor, "Genetic Operators");
|
||||||
parser.processParam(factor, "Genetic Operators");
|
parser.processParam(load_name, "Persistence");
|
||||||
parser.processParam(load_name, "Persistence");
|
parser.processParam(save_name, "Persistence");
|
||||||
parser.processParam(save_name, "Persistence");
|
parser.processParam(seed, "Rng seeding");
|
||||||
parser.processParam(seed, "Rng seeding");
|
|
||||||
|
|
||||||
eoState state;
|
eoState state;
|
||||||
state.registerObject(parser);
|
state.registerObject(parser);
|
||||||
Reference in a new issue