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
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable:4786)
|
||||
#endif
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// general
|
||||
#include <eoParser.h> // though contained in all others!
|
||||
// evolution specific
|
||||
#include <eoEvalFuncPtr.h>
|
||||
//#include <eoSequentialOpHolder.h>
|
||||
//#include <eoFullEA.h>
|
||||
// representation specific
|
||||
|
||||
#include <eoESFullChrom.h> // though contained in following
|
||||
//#include <eoESReco.h>
|
||||
//#include <eoESMut.h>
|
||||
//#include <eoESRandomize.h>
|
||||
// this fitness
|
||||
#include "real_value.h" // the sphere fitness
|
||||
|
||||
// Now the main
|
||||
///////////////
|
||||
typedef eoESFullChrom<float> Ind;
|
||||
|
||||
main(int argc, char *argv[]) {
|
||||
// unsigned mu, lambda;
|
||||
// bool comma;
|
||||
|
||||
// Create the command-line parser
|
||||
Parser parser( argc, argv, "Basic EA for vector<float> 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<Ind> eval( real_value );
|
||||
|
||||
/*
|
||||
// Evolution and population parameters
|
||||
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);
|
||||
|
||||
// termination conditions read by the parser
|
||||
eoTermVector<Ind> the_terms(parser);
|
||||
|
||||
// Initialization of the population
|
||||
// shoudl be called using the parser, in case you want to read from file(s)
|
||||
eoESRandomize<float> randomize; // an eoESInd randomnizer
|
||||
eoPop<Ind> 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 <Ind> seqholder;
|
||||
// seqholder.addOp(MyReco, 1.0);
|
||||
seqholder.addOp(MyMut, 1.0);
|
||||
|
||||
// One generation
|
||||
eoEvolStep<Ind> evol_scheme(the_scheme, seqholder, eval);
|
||||
|
||||
// the algorithm:
|
||||
eoFullEA<Ind> 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 <string>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <utils/eoParser.h>
|
||||
|
||||
// evolution specific
|
||||
#include <eoEvalFuncPtr.h>
|
||||
|
||||
// representation specific
|
||||
#include <es/eoESFullChrom.h> // though contained in following
|
||||
//#include <eoESReco.h>
|
||||
//#include <eoESMut.h>
|
||||
//#include <eoESRandomize.h>
|
||||
// this fitness
|
||||
#include "real_value.h" // the sphere fitness
|
||||
|
||||
// Now the main
|
||||
///////////////
|
||||
typedef eoESFullChrom<float> Ind;
|
||||
|
||||
main(int argc, char *argv[]) {
|
||||
// unsigned mu, lambda;
|
||||
// bool comma;
|
||||
|
||||
// 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");
|
||||
eoValueParam<string>& load_name = parser.createParam("", "Load","Load a state file",'L');
|
||||
eoValueParam<string>& 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<Ind> eval( real_value );
|
||||
|
||||
|
||||
|
||||
/*
|
||||
// Evolution and population parameters
|
||||
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);
|
||||
|
||||
// termination conditions read by the parser
|
||||
eoTermVector<Ind> the_terms(parser);
|
||||
|
||||
// Initialization of the population
|
||||
// shoudl be called using the parser, in case you want to read from file(s)
|
||||
eoESRandomize<float> randomize; // an eoESInd randomnizer
|
||||
eoPop<Ind> 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 <Ind> seqholder;
|
||||
// seqholder.addOp(MyReco, 1.0);
|
||||
seqholder.addOp(MyMut, 1.0);
|
||||
|
||||
// One generation
|
||||
eoEvolStep<Ind> evol_scheme(the_scheme, seqholder, eval);
|
||||
|
||||
// the algorithm:
|
||||
eoFullEA<Ind> 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;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
|
||||
// to avoid long name warnings
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(disable:4786)
|
||||
#endif
|
||||
|
||||
#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);
|
||||
|
||||
// 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> factor(0.99, "mutationFactor", "Decrease factor for mutation rate");
|
||||
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');
|
||||
|
||||
// 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);
|
||||
Reference in a new issue