Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | Related Pages | Search for

# t-eoCheckpointing.cpp

    00001 //-----------------------------------------------------------------------------
    00002
    00003 // to avoid long name warnings
    00004 #ifdef _MSC_VER
    00005 #pragma warning(disable:4786)
    00006 #endif
    00007
    00008 #include <stdexcept>  // runtime_error
    00009
    00010 //-----------------------------------------------------------------------------
    00011 // tt.cpp:
    00012 //
    00013 //-----------------------------------------------------------------------------
    00014
    00015
    00016 // general
    00017 #include <utils/eoRNG.h>                // Random number generators
    00018 #include <ga.h>
    00019 #include <utils/eoParser.h>
    00020 #include <utils/eoState.h>
    00021 #include <eoGenContinue.h>
    00022 //-----------------------------------------------------------------------------
    00023
    00024 // include package checkpointing
    00025 #include <utils/checkpointing>
    00026
    00027 struct Dummy : public EO<double>
    00028 {
    00029     typedef double Type;
    00030 };
    00031
    00032
    00033 struct eoDummyPop : public eoPop<Dummy>
    00034 {
    00035 public :
    00036     eoDummyPop(int s = 2) { resize(s); }
    00037 };
    00038
    00039 //-----------------------------------------------------------------------------
    00040
    00041 int the_main(int argc, char **argv)
    00042 { // ok, we have a command line parser and a state
    00043
    00044     typedef eoBit<float> Chrom;
    00045
    00046     eoParser parser(argc, argv);
    00047
    00048     // Define Parameters
    00049     eoValueParam<double> rate(0.01, "mutationRatePerBit", "Initial value for mutation rate per bit");
    00050     eoValueParam<double> factor(0.99, "mutationFactor", "Decrease factor for mutation rate");
    00051     eoValueParam<uint32_t> seed(time(0), "seed", "Random number seed");
    00052     eoValueParam<std::string> load_name("", "Load","Load",'L');
    00053     eoValueParam<std::string> save_name("", "Save","Save",'S');
    00054
    00055     // Register them
    00056     parser.processParam(rate,       "Genetic Operators");
    00057     parser.processParam(factor,     "Genetic Operators");
    00058     parser.processParam(load_name,  "Persistence");
    00059     parser.processParam(save_name,  "Persistence");
    00060     parser.processParam(seed,       "Rng seeding");
    00061
    00062    eoState state;
    00063    state.registerObject(parser);
    00064
    00065    if (load_name.value() != "")
    00066    { // load the parser. This is only neccessary when the user wants to
    00067      // be able to change the parameters in the state file by hand.
    00068        state.load(load_name.value()); // load the parser
    00069    }
    00070
    00071     // Create the algorithm here
    00072     typedef Dummy EoType;
    00073
    00074     eoDummyPop pop;
    00075
    00076     eoGenContinue<EoType> genTerm(5); // run for 5 generations
    00077
    00078     eoCheckPoint<EoType> checkpoint(genTerm);
    00079     // The algorithm will now quit after five generations
    00080
    00081     // Create a counter parameter
    00082     eoValueParam<unsigned> generationCounter(0, "Generation");
    00083
    00084     // Create an incrementor (wich is an eoUpdater). Note that the
    00085     // Parameter's value is passed by reference, so every time the incrementer increments,
    00086     // the data in generationCounter will change.
    00087     eoIncrementor<unsigned> increment(generationCounter.value());
    00088
    00089     // Add it to the checkpoint, this will result in the counter being incremented every generation
    00090     checkpoint.add(increment);
    00091
    00092     // The file monitor will print parameters to a comma seperated file
    00093     eoFileMonitor monitor("monitor.csv");
    00094
    00095     // the checkpoint mechanism can handle multiple monitors
    00096     checkpoint.add(monitor);
    00097
    00098     // the monitor can monitor parameters such as the generationCounter
    00099     monitor.add(generationCounter);
    00100
    00101     // Second moment stats: average and stdev
    00102     eoSecondMomentStats<EoType> stats;
    00103
    00104     // Add it to the checkpoint to get it called at the appropriate time
    00105     checkpoint.add(stats);
    00106
    00107     // Add it to the monitor to get it written to the file
    00108     monitor.add(stats);
    00109
    00110     // save state every third generation
    00111     eoCountedStateSaver stateSaver1(3, state, "generation");
    00112     // save state every 2 seconds
    00113     eoTimedStateSaver   stateSaver2(2, state, "time");
    00114
    00115     // And add the two savers to the checkpoint
    00116     checkpoint.add(stateSaver1);
    00117     checkpoint.add(stateSaver2);
    00118
    00119     // Register the algorithm
    00120     state.registerObject(rng);
    00121     state.registerObject(pop);
    00122
    00123     if (parser.userNeedsHelp())
    00124     {
    00125         parser.printHelp(std::cout);
    00126         return 0;
    00127     }
    00128
    00129     // Either load or initialize
    00130     if (load_name.value() != "")
    00131     {
    00132         state.load(load_name.value()); // load the rest
    00133     }
    00134     else
    00135     {
    00136         // else
    00137
    00138         // initialize rng and population
    00139
    00140         rng.reseed(seed.value());
    00141
    00142         pop.resize(2);
    00143
    00144         pop[0].fitness(1);
    00145         pop[1].fitness(2);
    00146     }
    00147
    00148     while(checkpoint(pop))
    00149     {
    00150         pop[0].fitness(pop[0].fitness() + 1);
    00151
    00152         time_t now = time(0);
    00153
    00154         while (time(0) == now) {} // wait a second to test timed saver
    00155
    00156         std::cout << "gen " << generationCounter.value() << std::endl;
    00157     }
    00158
    00159     // run the algorithm
    00160
    00161     // Save when needed
    00162     if (save_name.value() != "")
    00163     {
    00164         std::string file_name = save_name.value();
    00165         save_name.value() = ""; // so that it does not appear in the parser section of the state file
    00166         state.save(file_name);
    00167     }
    00168
    00169     return 1;
    00170 }
    00171
    00172 int main(int argc, char **argv)
    00173 {
    00174     try
    00175     {
    00176         the_main(argc, argv);
    00177     }
    00178     catch(std::exception& e)
    00179     {
    00180         std::cout << "Exception: " << e.what() << std::endl;
    00181     }
    00182
    00183 }