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