# t-eoStateAndParser.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
    00022 //-----------------------------------------------------------------------------
    00023
    00024 // include package checkpointing
    00025 #include <utils/checkpointing>
    00026 // and provisions for Bounds reading
    00027 #include <utils/eoRealVectorBounds.h>
    00028
    00029 struct Dummy : public EO<double>
    00030 {
    00031     typedef double Type;
    00032 };
    00033
    00034
    00035 //-----------------------------------------------------------------------------
    00036
    00037 int the_main(int argc, char **argv)
    00038 { // ok, we have a command line parser and a state
    00039
    00040     typedef eoBit<float> Chrom;
    00041
    00042     eoParser parser(argc, argv);
    00043
    00044     // Define Parameters
    00045     eoValueParam<unsigned int> dimParam((unsigned int)(5), "dimension", "dimension");
    00046     eoValueParam<double> rate(0.01, "mutationRatePerBit", "Initial value for mutation rate per bit");
    00047     eoValueParam<double> factor(0.99, "mutationFactor", "Decrease factor for mutation rate");
    00048     eoValueParam<uint32_t> seed(time(0), "seed", "Random number seed");
    00049     // test if user entered or if default value used
    00050     if (parser.isItThere(seed))
    00051       std::cout << "YES\n";
    00052     else
    00053       std::cout << "NO\n";
    00054
    00055     eoValueParam<std::string> load_name("", "Load","Load",'L');
    00056     eoValueParam<std::string> save_name("", "Save","Save",'S');
    00057
    00058
    00059     // Register them
    00060     parser.processParam(dimParam,   "Genetic Operators");
    00061     parser.processParam(rate,       "Genetic Operators");
    00062     parser.processParam(factor,     "Genetic Operators");
    00063     parser.processParam(load_name,  "Persistence");
    00064     parser.processParam(save_name,  "Persistence");
    00065     parser.processParam(seed,       "Rng seeding");
    00066
    00067     // a bound param (need dim)
    00068     eoValueParam<eoRealVectorBounds> boundParam(eoRealVectorBounds(dimParam.value(),eoDummyRealNoBounds), "bounds","bounds",'b');
    00069
    00070     parser.processParam(boundParam, "Genetic Operators");
    00071
    00072     std::cout << "Bounds: " << boundParam.value() << std::endl;
    00073
    00074    eoState state;
    00075    state.registerObject(parser);
    00076
    00077
    00078    if (load_name.value() != "")
    00079    { // load the parser. This is only neccessary when the user wants to
    00080      // be able to change the parameters in the state file by hand.
    00081        state.load(load_name.value()); // load the parser
    00082    }
    00083
    00084     // Create the algorithm here
    00085
    00086     // Register the algorithm
    00087     state.registerObject(rng);
    00088     //state.registerObject(pop);
    00089
    00090     if (parser.userNeedsHelp())
    00091     {
    00092         parser.printHelp(std::cout);
    00093         return 0;
    00094     }
    00095
    00096     // Either load or initialize
    00097     if (load_name.value() != "")
    00098     {
    00099         state.load(load_name.value()); // load the rest
    00100     }
    00101     else
    00102     {
    00103         // else
    00104
    00105         // initialize rng and population
    00106
    00107         rng.reseed(seed.value());
    00108     }
    00109
    00110     // run the algorithm
    00111
    00112     // Save when needed
    00113     if (save_name.value() != "")
    00114     {
    00115         std::string file_name = save_name.value();
    00116         save_name.value() = ""; // so that it does not appear in the parser section of the state file
    00117         state.save(file_name);
    00118     }
    00119
    00120     for (int i = 0; i < 100; ++i)
    00121         rng.rand();
    00122
    00123     std::cout << "a random number is " << rng.random(1024) << std::endl;;
    00124
    00125     return 1;
    00126 }
    00127
    00128 int main(int argc, char **argv)
    00129 {
    00130     try
    00131     {
    00132         the_main(argc, argv);
    00133     }
    00134     catch(std::exception& e)
    00135     {
    00136         std::cout << "Exception: " << e.what() << std::endl;
    00137     }
    00138
    00139 }