# main.cpp

    00001 /*
    00002     This program is free software; you can redistribute it and/or modify
    00003     it under the terms of the GNU General Public License as published by
    00004     the Free Software Foundation; either version 2 of the License, or
    00005     (at your option) any later version.
    00006
    00007     This library is distributed in the hope that it will be useful,
    00008     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00009     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00010     Lesser General Public License for more details.
    00011
    00012     You should have received a copy of the GNU General Public License
    00013     along with this library; if not, write to the Free Software
    00014     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    00015
    00016     Contact: todos@geneura.ugr.es, http://geneura.ugr.es
    00017              jeggermo@liacs.nl
    00018 */
    00019
    00020 #ifdef _MSC_VER
    00021 #pragma warning(disable:4786)
    00022 #endif
    00023
    00024 #ifdef HAVE_CONFIG_H
    00025 #include <config.h>
    00026 #endif
    00027
    00028 #include <iostream>
    00029 #include "gp/eoParseTree.h"
    00030 #include "eo"
    00031
    00032 using namespace gp_parse_tree;
    00033 using namespace std;
    00034
    00035 //-----------------------------------------------------------------------------
    00036
    00037 #include "node.h"
    00038 #include "parameters.h"
    00039 #include "fitness.h"
    00040
    00041
    00042 // TYPE DECLARATIONS FOR GP
    00043
    00044
    00045 typedef eoParseTree<FitnessType, Node > EoType;
    00046 typedef eoPop<EoType> Pop;
    00047
    00048 //-----------------------------------------------------------------------------
    00049
    00050 int main(int argc, char *argv[])
    00051 {
    00052
    00053         // the vector containing the possible nodes
    00054         vector<Node> initSequence;
    00055
    00056         // initialise parameters
    00057         Parameters parameter(argc, argv);
    00058
    00059         // set the randomseed
    00060         rng.reseed(parameter.randomseed);
    00061
    00062          // Create a generation counter
    00063         eoValueParam<unsigned> generationCounter(0, "Gen.");
    00064
    00065         // Create an incrementor (sub-class of eoUpdater). Note that the
    00066         // parameter's value is passed by reference,
    00067         // so every time the incrementer is updated (every generation),
    00068         // the data in generationCounter will change.
    00069         eoIncrementor<unsigned> increment(generationCounter.value());
    00070
    00071
    00072         // create an instantiation of the fitness/evaluation function
    00073         // it initializes the initSequence vector
    00074         // the parameters are passed on as well
    00075         RegFitness eval(generationCounter, initSequence, parameter);
    00076
    00077         // Depth Initializor, set for Ramped Half and Half Initialization
    00078         eoParseTreeDepthInit<FitnessType, Node> initializer(parameter.InitMaxDepth, initSequence, true, true);
    00079
    00080         // create the initial population
    00081         Pop pop(parameter.population_size, initializer);
    00082
    00083         // and evaluate the individuals
    00084         apply<EoType>(eval, pop);
    00085
    00086         generationCounter.value()++; // set the generationCounter to 1
    00087
    00088
    00089         // define X-OVER
    00090
    00091         eoSubtreeXOver<FitnessType, Node>   xover(parameter.MaxSize);
    00092
    00093         // define MUTATION
    00094       eoBranchMutation<FitnessType, Node> mutation(initializer, parameter.MaxSize);
    00095 //      eoExpansionMutation<FitnessType, Node> mutation(initializer, parameter.MaxSize);
    00096 //      eoCollapseSubtreeMutation<FitnessType, Node> mutation(initializer, parameter.MaxSize);
    00097 //      eoPointMutation<FitnessType, Node> mutation(initSequence);
    00098 //      eoHoistMutation<FitnessType, Node> mutation;
    00099
    00100         // The operators are  encapsulated into an eoTRansform object,
    00101         // that performs sequentially crossover and mutation
    00102         eoSGATransform<EoType> transform(xover, parameter.xover_rate, mutation, parameter.mutation_rate);
    00103
    00104         // The robust tournament selection
    00105         // in our case 5-tournament selection
    00106         eoDetTournamentSelect<EoType> selectOne(parameter.tournamentsize);
    00107         // is now encapsulated in a eoSelectMany
    00108         eoSelectMany<EoType> select(selectOne, parameter.offspring_size, eo_is_an_integer);
    00109
    00110         // and the generational replacement
    00111         //eoGenerationalReplacement<EoType> replace;
    00112         // or the SteadtState replacment
    00113         //eoSSGAWorseReplacement<EoType> replace;
    00114         // or comma selection
    00115         eoCommaReplacement<EoType> replace;
    00116
    00117         // Terminators
    00118         eoGenContinue<EoType> term(parameter.nGenerations);
    00119
    00120         eoCheckPoint<EoType> checkPoint(term);
    00121
    00122         // STATISTICS
    00123         eoAverageStat<EoType>     avg;
    00124         eoBestFitnessStat<EoType> best;
    00125
    00126
    00127         // Add it to the checkpoint,
    00128         // so the counter is updated (here, incremented) every generation
    00129         checkPoint.add(increment);
    00130         checkPoint.add(avg);
    00131         checkPoint.add(best);
    00132
    00133 #ifdef HAVE_GNUPLOT
    00134         eoGnuplot1DMonitor gnuplotmonitor("gnuplotBestStats");
    00135         gnuplotmonitor.add(generationCounter);
    00136         gnuplotmonitor.add(best);
    00137         // we need to add a empty string variable if we want to seed the second fitness value
    00138         eoValueParam<string> dummy1("", "Smallest Tree Size");
    00139         gnuplotmonitor.add(dummy1);
    00140
    00141         eoGnuplot1DMonitor gnuplotAvgmonitor("gnuplotAvgStats");
    00142         gnuplotAvgmonitor.add(generationCounter);
    00143         gnuplotAvgmonitor.add(avg);
    00144         // we need to add a empty string variable if we want to seed the second fitness value
    00145         eoValueParam<string> dummy2("", "Average Tree Size");
    00146         gnuplotAvgmonitor.add(dummy2);
    00147
    00148         checkPoint.add(gnuplotmonitor);
    00149         checkPoint.add(gnuplotAvgmonitor);
    00150 #endif
    00151         // GP Generation
    00152         eoEasyEA<EoType> gp(checkPoint, eval, select, transform, replace);
    00153
    00154         cout << "Initialization done" << endl;
    00155
    00156
    00157         try
    00158         {
    00159                 gp(pop);
    00160         }
    00161         catch (exception& e)
    00162         {
    00163             cout << "exception: " << e.what() << endl;;
    00164             exit(EXIT_FAILURE);
    00165         }
    00166
    00167         return 1;
    00168
    00169 };
    00170
    00171
    00172