mastermind.cpp

00001 //-----------------------------------------------------------------------------
00002 // mastermind
00003 //-----------------------------------------------------------------------------
00004 
00005 #include <stdlib.h>                // EXIT_SUCCESS EXIT_FAILURE
00006 #include <stdexcept>               // exception
00007 #include <iostream>                // cerr cout
00008 #include <fstream>                 // ifstream
00009 #include <string>                  // string
00010 #include <eo>                      // all usefull eo stuff
00011 
00012 #include "mastermind.h"            // Chrom eoChromInit eoChromMutation eoChromXover eoChromEvaluator
00013 
00014 using namespace std;
00015 
00016 //-----------------------------------------------------------------------------
00017 // global variables
00018 //-----------------------------------------------------------------------------
00019 
00020 unsigned in, out, hidden;
00021 
00022 //-----------------------------------------------------------------------------
00023 // parameters
00024 //-----------------------------------------------------------------------------
00025 
00026 eoValueParam<unsigned> pop_size(16, "pop_size", "population size", 'p');
00027 eoValueParam<unsigned> generations(100, "generations", "number of generation", 'g');
00028 eoValueParam<double> mut_rate(0.1, "mut_rate", "mutation rate", 'm');
00029 eoValueParam<double> xover_rate(0.5, "xover_rate", "default crossover rate", 'x');
00030 eoValueParam<unsigned> col_p(default_colors, "colors", "number of colors", 'c');
00031 eoValueParam<unsigned> len_p(default_length, "legth", "solution legth", 'l');
00032 eoValueParam<string> sol_p(default_solution, "solution", "problem solution", 's');
00033 
00034 //-----------------------------------------------------------------------------
00035 // auxiliar functions
00036 //-----------------------------------------------------------------------------
00037 
00038 void arg(int argc, char** argv);
00039 void ga();
00040 
00041 //-----------------------------------------------------------------------------
00042 // main
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 // implementation
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(col_p,       "problem");
00074   parser.processParam(len_p,       "problem");
00075   parser.processParam(sol_p,       "problem");
00076 
00077   if (parser.userNeedsHelp())
00078     {
00079       parser.printHelp(cout);
00080       exit(EXIT_SUCCESS);
00081     }
00082 
00083   init_eoChromEvaluator(col_p.value(), len_p.value(), sol_p.value());
00084 }
00085 
00086 //-----------------------------------------------------------------------------
00087 
00088 void ga()
00089 {
00090   // create population
00091   eoInitChrom init;
00092   eoPop<Chrom> pop(pop_size.value(), init);
00093 
00094   // evaluate population
00095   eoEvalFuncPtr<Chrom> evaluator(eoChromEvaluator);
00096   apply<Chrom>(evaluator, pop);
00097 
00098   // selector
00099   eoProportionalSelect<Chrom> select(pop);
00100 
00101   // genetic operators
00102   eoChromMutation mutation;
00103   eoChromXover xover;
00104 
00105   // stop condition
00106   eoGenContinue<Chrom> continuator1(generations.value());
00107   eoFitContinue<Chrom> continuator2(solution.fitness());
00108   eoCombinedContinue<Chrom> continuator(continuator1, continuator2);
00109 
00110   // checkpoint
00111   eoCheckPoint<Chrom> checkpoint(continuator);
00112 
00113   // monitor
00114   eoStdoutMonitor monitor;
00115   checkpoint.add(monitor);
00116 
00117   // statistics
00118   eoBestFitnessStat<Chrom> stats;
00119   checkpoint.add(stats);
00120   monitor.add(stats);
00121 
00122   // genetic algorithm
00123   eoSGA<Chrom> sga(select,
00124                    xover, xover_rate.value(),
00125                    mutation, mut_rate.value(),
00126                    evaluator,
00127                    checkpoint);
00128   sga(pop);
00129 
00130   cout << "solution = " << solution << endl
00131        << "best     = " << *max_element(pop.begin(), pop.end()) << endl;
00132 }
00133 
00134 //-----------------------------------------------------------------------------
00135 
00136 // Local Variables:
00137 // mode:C++
00138 // End:

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