Added the MyStructLibEA.cpp+make_MyStruct.cpp that allow separate
compilation of representation-indenepent stuff. Modified create.sh script and Makefile accordingly. See Lesson5 of the tutorial
This commit is contained in:
parent
e2e2f39cc7
commit
b407bf5e81
4 changed files with 308 additions and 7 deletions
|
|
@ -1,12 +1,11 @@
|
||||||
# sample makefile for building an EA evolving a new genotype
|
# 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
|
||||||
LOCAL_SOURCES = MyStructEA.cpp \
|
COMMON_SOURCES = eoMyStruct.h \
|
||||||
eoMyStruct.h \
|
|
||||||
eoMyStructEvalFunc.h \
|
eoMyStructEvalFunc.h \
|
||||||
eoMyStructInit.h \
|
eoMyStructInit.h \
|
||||||
eoMyStructMutation.h \
|
eoMyStructMutation.h \
|
||||||
|
|
@ -14,7 +13,12 @@ LOCAL_SOURCES = MyStructEA.cpp \
|
||||||
make_genotype_MyStruct.h \
|
make_genotype_MyStruct.h \
|
||||||
make_op_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
|
# START eventually modify the name of EO dir
|
||||||
DIR_EO = ../../src
|
DIR_EO = ../../src
|
||||||
|
|
@ -22,11 +26,14 @@ DIR_EO = ../../src
|
||||||
|
|
||||||
LIB_EO = $(DIR_EO)/utils/libeoutils.a $(DIR_EO)/libeo.a
|
LIB_EO = $(DIR_EO)/utils/libeoutils.a $(DIR_EO)/libeo.a
|
||||||
|
|
||||||
ALL = MyStructEA
|
ALL = MyStructEA MyStructLibEA
|
||||||
|
|
||||||
MyStructEA : MyStructEA.o
|
MyStructEA : MyStructEA.o
|
||||||
c++ -g -o $@ MyStructEA.o ../../src/utils/libeoutils.a ../../src/libeo.a -lm
|
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
|
tar : ; tar czvf MyStruct.tgz *.h *.cpp Makefile
|
||||||
|
|
||||||
all : $(ALL)
|
all : $(ALL)
|
||||||
|
|
@ -34,5 +41,6 @@ all : $(ALL)
|
||||||
clean : ; /bin/rm *.o $(ALL)
|
clean : ; /bin/rm *.o $(ALL)
|
||||||
|
|
||||||
########## local dependencies
|
########## 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
|
||||||
|
|
|
||||||
162
eo/tutorial/Templates/MyStructLibEA.cpp
Normal file
162
eo/tutorial/Templates/MyStructLibEA.cpp
Normal file
|
|
@ -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 <iostream>
|
||||||
|
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<FitT> for some fitness
|
||||||
|
*/
|
||||||
|
#include "eoMyStruct.h"
|
||||||
|
|
||||||
|
/** definition of initilizqtion:
|
||||||
|
* class eoMyStructInit MUST derive from eoInit<eoMyStruct>
|
||||||
|
*/
|
||||||
|
#include "eoMyStructInit.h"
|
||||||
|
|
||||||
|
/** definition of evaluation:
|
||||||
|
* class eoMyStructEvalFunc MUST derive from eoEvalFunc<eoMyStruct>
|
||||||
|
* 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<MyFitT> 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<Indi> & 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<Indi>& make_op(eoParser& _parser, eoState& _state, eoInit<Indi>& _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<Indi >& make_pop(eoParser& _parser, eoState& _state, eoInit<Indi> & _init);
|
||||||
|
|
||||||
|
// the stopping criterion
|
||||||
|
eoContinue<Indi>& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter<Indi> & _eval);
|
||||||
|
|
||||||
|
// outputs (stats, population dumps, ...)
|
||||||
|
eoCheckPoint<Indi>& make_checkpoint(eoParser& _parser, eoState& _state, eoEvalFuncCounter<Indi>& _eval, eoContinue<Indi>& _continue);
|
||||||
|
|
||||||
|
// evolution engine (selection and replacement)
|
||||||
|
eoAlgo<Indi>& make_algo_scalar(eoParser& _parser, eoState& _state, eoEvalFunc<Indi>& _eval, eoContinue<Indi>& _continue, eoGenOp<Indi>& _op);
|
||||||
|
|
||||||
|
// simple call to the algo. stays there for consistency reasons
|
||||||
|
// no template for that one
|
||||||
|
void run_ea(eoAlgo<Indi>& _ga, eoPop<Indi>& _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<Indi> plainEval/* (varType _anyVariable) */;
|
||||||
|
// turn that object into an evaluation counter
|
||||||
|
eoEvalFuncCounter<Indi> eval(plainEval);
|
||||||
|
|
||||||
|
// the genotype - through a genotype initializer
|
||||||
|
eoInit<Indi>& init = make_genotype(parser, state, Indi());
|
||||||
|
|
||||||
|
// Build the variation operator (any seq/prop construct)
|
||||||
|
eoGenOp<Indi>& 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<Indi>& pop = make_pop(parser, state, init);
|
||||||
|
|
||||||
|
// stopping criteria
|
||||||
|
eoContinue<Indi> & term = make_continue(parser, state, eval);
|
||||||
|
// output
|
||||||
|
eoCheckPoint<Indi> & checkpoint = make_checkpoint(parser, state, eval, term);
|
||||||
|
// algorithm (need the operator!)
|
||||||
|
eoAlgo<Indi>& 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<Indi>(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;
|
||||||
|
}
|
||||||
|
|
@ -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 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_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_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
|
sed s/MyStruct/$1/g Makefile.tmpl > $TargetDir/$MakeName
|
||||||
|
|
||||||
echo Done!
|
echo Done!
|
||||||
|
|
|
||||||
129
eo/tutorial/Templates/make_MyStruct.cpp
Normal file
129
eo/tutorial/Templates/make_MyStruct.cpp
Normal file
|
|
@ -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 <iostream>
|
||||||
|
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<FitT> 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<eoMyStruct<double>> & make_genotype(eoParser& _parser, eoState&_state, eoMyStruct<double> _eo)
|
||||||
|
// {
|
||||||
|
// return do_make_genotype(_parser, _state, _eo);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// eoInit<eoMyStruct<eoMinimizingFitness>> & make_genotype(eoParser& _parser, eoState&_state, eoMyStruct<eoMinimizingFitness> _eo)
|
||||||
|
// {
|
||||||
|
// return do_make_genotype(_parser, _state, _eo);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// same thing for the variation operaotrs
|
||||||
|
//---------------------------------------
|
||||||
|
// #include "make_op_MyStruct.h"
|
||||||
|
// eoGenOp<eoMyStruct<double>>& make_op(eoParser& _parser, eoState& _state, eoInit<eoMyStruct<double>>& _init)
|
||||||
|
// {
|
||||||
|
// return do_make_op(_parser, _state, _init);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// eoGenOp<eoMyStruct<eoMinimizingFitness>>& make_op(eoParser& _parser, eoState& _state, eoInit<eoMyStruct<eoMinimizingFitness>>& _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 <do/make_pop.h>
|
||||||
|
eoPop<eoMyStruct<double> >& make_pop(eoParser& _parser, eoState& _state, eoInit<eoMyStruct<double> > & _init)
|
||||||
|
{
|
||||||
|
return do_make_pop(_parser, _state, _init);
|
||||||
|
}
|
||||||
|
|
||||||
|
eoPop<eoMyStruct<eoMinimizingFitness> >& make_pop(eoParser& _parser, eoState& _state, eoInit<eoMyStruct<eoMinimizingFitness> > & _init)
|
||||||
|
{
|
||||||
|
return do_make_pop(_parser, _state, _init);
|
||||||
|
}
|
||||||
|
|
||||||
|
// the stopping criterion
|
||||||
|
#include <do/make_continue.h>
|
||||||
|
eoContinue<eoMyStruct<double> >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter<eoMyStruct<double> > & _eval)
|
||||||
|
{
|
||||||
|
return do_make_continue(_parser, _state, _eval);
|
||||||
|
}
|
||||||
|
|
||||||
|
eoContinue<eoMyStruct<eoMinimizingFitness> >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter<eoMyStruct<eoMinimizingFitness> > & _eval)
|
||||||
|
{
|
||||||
|
return do_make_continue(_parser, _state, _eval);
|
||||||
|
}
|
||||||
|
|
||||||
|
// outputs (stats, population dumps, ...)
|
||||||
|
#include <do/make_checkpoint.h>
|
||||||
|
eoCheckPoint<eoMyStruct<double> >& make_checkpoint(eoParser& _parser, eoState& _state, eoEvalFuncCounter<eoMyStruct<double> >& _eval, eoContinue<eoMyStruct<double> >& _continue)
|
||||||
|
{
|
||||||
|
return do_make_checkpoint(_parser, _state, _eval, _continue);
|
||||||
|
}
|
||||||
|
|
||||||
|
eoCheckPoint<eoMyStruct<eoMinimizingFitness> >& make_checkpoint(eoParser& _parser, eoState& _state, eoEvalFuncCounter<eoMyStruct<eoMinimizingFitness> >& _eval, eoContinue<eoMyStruct<eoMinimizingFitness> >& _continue)
|
||||||
|
{
|
||||||
|
return do_make_checkpoint(_parser, _state, _eval, _continue);
|
||||||
|
}
|
||||||
|
|
||||||
|
// evolution engine (selection and replacement)
|
||||||
|
#include <do/make_algo_scalar.h>
|
||||||
|
eoAlgo<eoMyStruct<double> >& make_algo_scalar(eoParser& _parser, eoState& _state, eoEvalFunc<eoMyStruct<double> >& _eval, eoContinue<eoMyStruct<double> >& _continue, eoGenOp<eoMyStruct<double> >& _op)
|
||||||
|
{
|
||||||
|
return do_make_algo_scalar(_parser, _state, _eval, _continue, _op);
|
||||||
|
}
|
||||||
|
|
||||||
|
eoAlgo<eoMyStruct<eoMinimizingFitness> >& make_algo_scalar(eoParser& _parser, eoState& _state, eoEvalFunc<eoMyStruct<eoMinimizingFitness> >& _eval, eoContinue<eoMyStruct<eoMinimizingFitness> >& _continue, eoGenOp<eoMyStruct<eoMinimizingFitness> >& _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 <do/make_run.h>
|
||||||
|
void run_ea(eoAlgo<eoMyStruct<double> >& _ga, eoPop<eoMyStruct<double> >& _pop)
|
||||||
|
{
|
||||||
|
do_run(_ga, _pop);
|
||||||
|
}
|
||||||
|
|
||||||
|
void run_ea(eoAlgo<eoMyStruct<eoMinimizingFitness> >& _ga, eoPop<eoMyStruct<eoMinimizingFitness> >& _pop)
|
||||||
|
{
|
||||||
|
do_run(_ga, _pop);
|
||||||
|
}
|
||||||
|
|
||||||
Reference in a new issue