gprop.cpp

00001 //-----------------------------------------------------------------------------
00002 // gprop
00003 //-----------------------------------------------------------------------------
00004 using namespace std;
00005 
00006 #include <stdlib.h>                // EXIT_SUCCESS EXIT_FAILURE
00007 #include <stdexcept>               // exception 
00008 #include <iostream>                // cerr cout
00009 #include <fstream>                 // ifstream
00010 #include <string>                  // string
00011 #include <eo>                      // all usefull eo stuff
00012 #include "gprop.h"                 // Chrom eoChromInit eoChromMutation eoChromXover eoChromEvaluator
00013 
00014 //-----------------------------------------------------------------------------
00015 // global variables
00016 //-----------------------------------------------------------------------------
00017 
00018 unsigned in, out, hidden;
00019 mlp::set train, validate, test;
00020 
00021 //-----------------------------------------------------------------------------
00022 // parameters
00023 //-----------------------------------------------------------------------------
00024 
00025 eoValueParam<unsigned> pop_size(10, "pop_size", "population size", 'p');
00026 eoValueParam<unsigned> generations(10, "generations", "number of generation", 'g');
00027 eoValueParam<double> mut_rate(0.25, "mut_rate", "mutation rate", 'm');
00028 eoValueParam<double> xover_rate(0.25, "xover_rate", "default crossover rate", 'x');
00029 eoValueParam<string> file("", "file", "common start of patterns filenames *.trn *.val and *.tst", 'f');
00030 eoValueParam<unsigned> hiddenp(0, "hidden", "number of neurons in hidden layer", 'd');
00031 
00032 //-----------------------------------------------------------------------------
00033 // auxiliar functions
00034 //-----------------------------------------------------------------------------
00035 
00036 void arg(int argc, char** argv);
00037 void load_file(mlp::set& s, const string& s);
00038 void ga();
00039 
00040 //-----------------------------------------------------------------------------
00041 // main
00042 //-----------------------------------------------------------------------------
00043 
00044 int main(int argc, char** argv)
00045 {
00046   try
00047     {
00048       arg(argc, argv);
00049       ga();
00050     }
00051   catch (exception& e)
00052     {
00053       cerr << argv[0] << ": " << e.what() << endl;
00054       exit(EXIT_FAILURE);
00055     }
00056 
00057   return 0;
00058 }
00059 
00060 //-----------------------------------------------------------------------------
00061 // implementation
00062 //-----------------------------------------------------------------------------
00063 
00064 void arg(int argc, char** argv)
00065 {
00066   eoParser parser(argc, argv);
00067   
00068   parser.processParam(pop_size,    "genetic operators");
00069   parser.processParam(generations, "genetic operators");
00070   parser.processParam(mut_rate,    "genetic operators");
00071   parser.processParam(xover_rate,  "genetic operators");
00072   parser.processParam(file,        "files");
00073   parser.processParam(hiddenp,     "genetic operators");
00074 
00075   if (parser.userNeedsHelp())
00076     {
00077       parser.printHelp(cout);
00078       exit(EXIT_SUCCESS);
00079     }
00080 
00081   load_file(train, "trn");
00082   load_file(validate, "val");
00083   load_file(test, "tst");
00084     
00085   phenotype::trn_max = train.size();
00086   phenotype::val_max = validate.size();
00087   phenotype::tst_max = test.size();
00088 
00089   in = train.front().input.size();
00090   out = train.front().output.size();
00091   gprop_use_datasets(&train, &validate, &test);
00092   hidden = hiddenp.value();
00093 }
00094 
00095 //-----------------------------------------------------------------------------
00096 
00097 void load_file(mlp::set& set, const string& ext)
00098 {
00099   string filename = file.value(); filename += "." + ext;
00100 
00101   ifstream ifs(filename.c_str());
00102   if (!ifs)
00103     {
00104       cerr << "can't open file \"" << filename << "\"" << endl;
00105       exit(EXIT_FAILURE);
00106     } 
00107   
00108   ifs >> set;
00109 
00110   if (set.size() == 0)
00111     {
00112       cerr << filename << " data file is empty!";
00113       exit(EXIT_FAILURE);
00114     }
00115 }
00116 
00117 //-----------------------------------------------------------------------------
00118 
00119 void ga()
00120 {
00121   // create population
00122   eoInitChrom init;
00123   eoPop<Chrom> pop(pop_size.value(), init);
00124   
00125   // evaluate population
00126   eoEvalFuncPtr<Chrom> evaluator(eoChromEvaluator);
00127   apply<Chrom>(evaluator, pop);
00128   
00129   // selector
00130   eoStochTournamentSelect<Chrom> select;
00131 
00132   // genetic operators
00133   eoChromMutation mutation;
00134   eoChromXover xover;
00135   
00136   // stop condition
00137   eoGenContinue<Chrom> continuator1(generations.value());
00138   phenotype p; p.val_ok = validate.size() - 1; p.mse_error = 0;
00139   eoFitContinue<Chrom> continuator2(p);
00140   eoCombinedContinue<Chrom> continuator(continuator1, continuator2);
00141 
00142   // checkpoint
00143   eoCheckPoint<Chrom> checkpoint(continuator);
00144   
00145   // monitor
00146   eoStdoutMonitor monitor;
00147   checkpoint.add(monitor);
00148   
00149   // statistics
00150   eoBestFitnessStat<Chrom> stats;
00151   checkpoint.add(stats);
00152   monitor.add(stats);
00153   
00154   // genetic algorithm
00155   eoSGA<Chrom> sga(select,
00156                    xover, xover_rate.value(), 
00157                    mutation, mut_rate.value(), 
00158                    evaluator, 
00159                    checkpoint);
00160  
00161   sga(pop);
00162 
00163   cout << "best: " << *max_element(pop.begin(), pop.end()) << endl;
00164 }
00165 
00166 //-----------------------------------------------------------------------------
00167 
00168 // Local Variables: 
00169 // mode:C++
00170 // End:

Generated on Thu Oct 19 05:06:40 2006 for EO by  doxygen 1.3.9.1