diff --git a/eo/tutorial/Templates/Makefile.tmpl b/eo/tutorial/Templates/Makefile.tmpl index cfc870722..053c3a99b 100644 --- a/eo/tutorial/Templates/Makefile.tmpl +++ b/eo/tutorial/Templates/Makefile.tmpl @@ -1,12 +1,11 @@ # sample makefile for building an EA evolving a new genotype -.cpp: ; c++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\" -I. -I../../src -Wall -g -o $@ $*.cpp ../../src/libeo.a ../../src/utils/libeoutils.a +.cpp: ; c++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.3\" -I. -I../../src -Wall -g -o $@ $*.cpp ../../src/libeo.a ../../src/utils/libeoutils.a -.cpp.o: ; c++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\" -DF2C -I. -I../../src -I./util -Wall -g -c $*.cpp +.cpp.o: ; c++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.3\" -I. -I../../src -I./util -Wall -g -c $*.cpp # local sources -LOCAL_SOURCES = MyStructEA.cpp \ - eoMyStruct.h \ +COMMON_SOURCES = eoMyStruct.h \ eoMyStructEvalFunc.h \ eoMyStructInit.h \ eoMyStructMutation.h \ @@ -14,7 +13,12 @@ LOCAL_SOURCES = MyStructEA.cpp \ make_genotype_MyStruct.h \ make_op_MyStruct.h +NO_LIB_SOURCES = MyStructEA.cpp +LIB_SOURCES = MyStructLibEA.cpp make_MyStruct.cpp + + +SOURCES = $(COMMON_SOURCES) MyStructEA.cpp MyStructLibEA.cpp make_MyStruct.cpp # START eventually modify the name of EO dir DIR_EO = ../../src @@ -22,11 +26,14 @@ DIR_EO = ../../src LIB_EO = $(DIR_EO)/utils/libeoutils.a $(DIR_EO)/libeo.a -ALL = MyStructEA +ALL = MyStructEA MyStructLibEA MyStructEA : MyStructEA.o c++ -g -o $@ MyStructEA.o ../../src/utils/libeoutils.a ../../src/libeo.a -lm +MyStructLibEA : MyStructLibEA.o make_MyStruct.o + c++ -g -o $@ MyStructLibEA.o make_MyStruct.o ../../src/utils/libeoutils.a ../../src/libeo.a -lm + tar : ; tar czvf MyStruct.tgz *.h *.cpp Makefile all : $(ALL) @@ -34,5 +41,6 @@ all : $(ALL) clean : ; /bin/rm *.o $(ALL) ########## local dependencies -AppliEA.o : $(LOCAL_SOURCES) - +MyStructEA.o : $(COMMON_SOURCES) MyStructEA.cpp +MyStructLibEA.o : $(COMMON_SOURCES) MyStructLibEA.cpp +make_MyStruct.o : make_MyStruct.cpp eoMyStruct.h diff --git a/eo/tutorial/Templates/MyStructLibEA.cpp b/eo/tutorial/Templates/MyStructLibEA.cpp new file mode 100644 index 000000000..24fed341d --- /dev/null +++ b/eo/tutorial/Templates/MyStructLibEA.cpp @@ -0,0 +1,162 @@ +/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +The above line is usefulin Emacs-like editors + */ + +/* +Template for creating a new representation in EO +================================================ + +This is the template main file for compiling after creating a +"library", i.e. putting everything but the fitness in a separate file +(make_MyStruct.cpp) and compiling it once and for all. +*/ + +// Miscilaneous include and declaration +#include +using namespace std; + +// eo general include +#include "eo" +// the real bounds (not yet in general eo include) +#include "utils/eoRealVectorBounds.h" + +// include here whatever specific files for your representation +// Basically, this should include at least the following + +/** definition of representation: + * class eoMyStruct MUST derive from EO for some fitness + */ +#include "eoMyStruct.h" + +/** definition of initilizqtion: + * class eoMyStructInit MUST derive from eoInit + */ +#include "eoMyStructInit.h" + +/** definition of evaluation: + * class eoMyStructEvalFunc MUST derive from eoEvalFunc + * and should test for validity before doing any computation + * see tutorial/Templates/evalFunc.tmpl + */ +#include "eoMyStructEvalFunc.h" + +// GENOTYPE eoMyStruct ***MUST*** be templatized over the fitness + +// +// START fitness type: double or eoMaximizingFitness if you are maximizing +// eoMinimizingFitness if you are minimizing +typedef eoMinimizingFitness MyFitT ; // type of fitness +// END fitness type +// + +// Then define your EO objects using that fitness type +typedef eoMyStruct Indi; // ***MUST*** derive from EO + +// create an initializer - done here and NOT in make_MyStruct.cpp +// because it is NOT representation independent +#include "make_genotype_MyStruct.h" +eoInit & make_genotype(eoParser& _parser, eoState&_state, Indi _eo) +{ + return do_make_genotype(_parser, _state, _eo); +} + +// same thing for the variation operaotrs +#include "make_op_MyStruct.h" +eoGenOp& make_op(eoParser& _parser, eoState& _state, eoInit& _init) +{ + return do_make_op(_parser, _state, _init); +} + +// The representation independent routines are simply declared here + +// how to initialize the population +// it IS representation independent if an eoInit is given +eoPop& make_pop(eoParser& _parser, eoState& _state, eoInit & _init); + +// the stopping criterion +eoContinue& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter & _eval); + +// outputs (stats, population dumps, ...) +eoCheckPoint& make_checkpoint(eoParser& _parser, eoState& _state, eoEvalFuncCounter& _eval, eoContinue& _continue); + +// evolution engine (selection and replacement) +eoAlgo& make_algo_scalar(eoParser& _parser, eoState& _state, eoEvalFunc& _eval, eoContinue& _continue, eoGenOp& _op); + +// simple call to the algo. stays there for consistency reasons +// no template for that one +void run_ea(eoAlgo& _ga, eoPop& _pop); + +// checks for help demand, and writes the status file +// and make_help; in libutils - just a declaration, code in libeoutils.a +void make_help(eoParser & _parser); + +// now use all of the above, + representation dependent things +// from here on, no difference with eoMyStruct.cpp +int main(int argc, char* argv[]) +{ + + try + { + eoParser parser(argc, argv); // for user-parameter reading + + eoState state; // keeps all things allocated + + // The fitness + ////////////// + eoMyStructEvalFunc plainEval/* (varType _anyVariable) */; + // turn that object into an evaluation counter + eoEvalFuncCounter eval(plainEval); + + // the genotype - through a genotype initializer + eoInit& init = make_genotype(parser, state, Indi()); + + // Build the variation operator (any seq/prop construct) + eoGenOp& op = make_op(parser, state, init); + + + //// Now the representation-independent things + // + // YOU SHOULD NOT NEED TO MODIFY ANYTHING BEYOND THIS POINT + // unless you want to add specific statistics to the checkpoint + ////////////////////////////////////////////// + + // initialize the population + // yes, this is representation indepedent once you have an eoInit + eoPop& pop = make_pop(parser, state, init); + + // stopping criteria + eoContinue & term = make_continue(parser, state, eval); + // output + eoCheckPoint & checkpoint = make_checkpoint(parser, state, eval, term); + // algorithm (need the operator!) + eoAlgo& ga = make_algo_scalar(parser, state, eval, checkpoint, op); + + ///// End of construction of the algorithm + + ///////////////////////////////////////// + // to be called AFTER all parameters have been read!!! + make_help(parser); + + //// GO + /////// + // evaluate intial population AFTER help and status in case it takes time + apply(eval, pop); + // if you want to print it out + cout << "Initial Population\n"; + pop.sortedPrintOn(cout); + cout << endl; + + run_ea(ga, pop); // run the ga + + cout << "Final Population\n"; + pop.sortedPrintOn(cout); + cout << endl; + + } + catch(exception& e) + { + cout << e.what() << endl; + } + return 0; +} diff --git a/eo/tutorial/Templates/create.sh b/eo/tutorial/Templates/create.sh index 0a458b556..25aa1bd3d 100755 --- a/eo/tutorial/Templates/create.sh +++ b/eo/tutorial/Templates/create.sh @@ -54,6 +54,8 @@ sed s/MyStruct/$1/g quadCrossover.tmpl > $TargetDir/eo$1QuadCrossover.h sed s/MyStruct/$1/g MyStructEA.cpp > $TargetDir/$1EA.cpp sed s/MyStruct/$1/g make_genotype_MyStruct.h > $TargetDir/make_genotype_$1.h sed s/MyStruct/$1/g make_op_MyStruct.h > $TargetDir/make_op_$1.h +sed s/MyStruct/$1/g make_MyStruct.cpp > $TargetDir/make_$1.cpp +sed s/MyStruct/$1/g MyStructLibEA.cpp > $TargetDir/$1LibEA.cpp sed s/MyStruct/$1/g Makefile.tmpl > $TargetDir/$MakeName echo Done! diff --git a/eo/tutorial/Templates/make_MyStruct.cpp b/eo/tutorial/Templates/make_MyStruct.cpp new file mode 100644 index 000000000..fff16f4a6 --- /dev/null +++ b/eo/tutorial/Templates/make_MyStruct.cpp @@ -0,0 +1,129 @@ +/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +The above line is usefulin Emacs-like editors + */ + +/* +Template for creating a new representation in EO +================================================ + +This is the template file that allows separate compilation of +everything that is representation independant (evolution engine and +general output) for an Evolutionary Algorithm with scalar fitness. + +It includes of course the definition of the genotype (eoMyStruct.h) and +is written like the make_xxx.cpp files in dirs src/ga (for bitstrings) +and src/es (for real vectors). + +*/ + +// Miscilaneous include and declaration +#include +using namespace std; + +// eo general include +#include "eo" +// the real bounds (not yet in general eo include) +#include "utils/eoRealVectorBounds.h" + +// include here whatever specific files for your representation +// Basically, this should include at least the following + +/** definition of representation: + * class eoMyStruct MUST derive from EO for some fitness + */ +#include "eoMyStruct.h" + +// create an initializer: this is NOT representation-independent +// and will be done in the main file +// However, should you decide to freeze that part, you could use the +// following (and remove it from the main file, of course!!!) +//------------------------------------------------------------------ +// #include "make_genotype_MyStruct.h" +// eoInit> & make_genotype(eoParser& _parser, eoState&_state, eoMyStruct _eo) +// { +// return do_make_genotype(_parser, _state, _eo); +// } + +// eoInit> & make_genotype(eoParser& _parser, eoState&_state, eoMyStruct _eo) +// { +// return do_make_genotype(_parser, _state, _eo); +// } + +// same thing for the variation operaotrs +//--------------------------------------- +// #include "make_op_MyStruct.h" +// eoGenOp>& make_op(eoParser& _parser, eoState& _state, eoInit>& _init) +// { +// return do_make_op(_parser, _state, _init); +// } + +// eoGenOp>& make_op(eoParser& _parser, eoState& _state, eoInit>& _init) +// { +// return do_make_op(_parser, _state, _init); +// } + +// The following modules use ***representation independent*** routines + +// how to initialize the population +// it IS representation independent if an eoInit is given +#include +eoPop >& make_pop(eoParser& _parser, eoState& _state, eoInit > & _init) +{ + return do_make_pop(_parser, _state, _init); +} + +eoPop >& make_pop(eoParser& _parser, eoState& _state, eoInit > & _init) +{ + return do_make_pop(_parser, _state, _init); +} + +// the stopping criterion +#include +eoContinue >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter > & _eval) +{ + return do_make_continue(_parser, _state, _eval); +} + +eoContinue >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter > & _eval) +{ + return do_make_continue(_parser, _state, _eval); +} + +// outputs (stats, population dumps, ...) +#include +eoCheckPoint >& make_checkpoint(eoParser& _parser, eoState& _state, eoEvalFuncCounter >& _eval, eoContinue >& _continue) +{ + return do_make_checkpoint(_parser, _state, _eval, _continue); +} + +eoCheckPoint >& make_checkpoint(eoParser& _parser, eoState& _state, eoEvalFuncCounter >& _eval, eoContinue >& _continue) +{ + return do_make_checkpoint(_parser, _state, _eval, _continue); +} + +// evolution engine (selection and replacement) +#include +eoAlgo >& make_algo_scalar(eoParser& _parser, eoState& _state, eoEvalFunc >& _eval, eoContinue >& _continue, eoGenOp >& _op) +{ + return do_make_algo_scalar(_parser, _state, _eval, _continue, _op); +} + +eoAlgo >& make_algo_scalar(eoParser& _parser, eoState& _state, eoEvalFunc >& _eval, eoContinue >& _continue, eoGenOp >& _op) +{ + return do_make_algo_scalar(_parser, _state, _eval, _continue, _op); +} + +// simple call to the algo. stays there for consistency reasons +// no template for that one +#include +void run_ea(eoAlgo >& _ga, eoPop >& _pop) +{ + do_run(_ga, _pop); +} + +void run_ea(eoAlgo >& _ga, eoPop >& _pop) +{ + do_run(_ga, _pop); +} +