an example of how to make a new genotype within EO - see HTML doc
This commit is contained in:
parent
f2e20f638e
commit
e2e2f39cc7
11 changed files with 1190 additions and 0 deletions
46
eo/tutorial/Lesson5/Makefile
Normal file
46
eo/tutorial/Lesson5/Makefile
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
# sample makefile for building an EA evolving a new genotype
|
||||
|
||||
.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.3\" -I. -I../../src -I./util -Wall -g -c $*.cpp
|
||||
|
||||
# local sources
|
||||
COMMON_SOURCES = eoOneMax.h \
|
||||
eoOneMaxEvalFunc.h \
|
||||
eoOneMaxInit.h \
|
||||
eoOneMaxMutation.h \
|
||||
eoOneMaxQuadCrossover.h \
|
||||
make_genotype_OneMax.h \
|
||||
make_op_OneMax.h
|
||||
|
||||
NO_LIB_SOURCES = OneMaxEA.cpp
|
||||
|
||||
LIB_SOURCES = OneMaxLibEA.cpp make_OneMax.cpp
|
||||
|
||||
|
||||
SOURCES = $(COMMON_SOURCES) OneMaxEA.cpp OneMaxLibEA.cpp make_OneMax.cpp
|
||||
|
||||
# START eventually modify the name of EO dir
|
||||
DIR_EO = ../../src
|
||||
# END eventually modify the name of EO dir
|
||||
|
||||
LIB_EO = $(DIR_EO)/utils/libeoutils.a $(DIR_EO)/libeo.a
|
||||
|
||||
ALL = OneMaxEA OneMaxLibEA
|
||||
|
||||
OneMaxEA : OneMaxEA.o
|
||||
c++ -g -o $@ OneMaxEA.o ../../src/utils/libeoutils.a ../../src/libeo.a -lm
|
||||
|
||||
OneMaxLibEA : OneMaxLibEA.o make_OneMax.o
|
||||
c++ -g -o $@ OneMaxLibEA.o make_OneMax.o ../../src/utils/libeoutils.a ../../src/libeo.a -lm
|
||||
|
||||
tar : ; tar czvf OneMax.tgz *.h *.cpp Makefile
|
||||
|
||||
all : $(ALL)
|
||||
|
||||
clean : ; /bin/rm *.o $(ALL)
|
||||
|
||||
########## local dependencies
|
||||
OneMaxEA.o : $(COMMON_SOURCES) OneMaxEA.cpp
|
||||
OneMaxLibEA.o : $(COMMON_SOURCES) OneMaxLibEA.cpp
|
||||
make_OneMax.o : make_OneMax.cpp eoOneMax.h
|
||||
188
eo/tutorial/Lesson5/OneMaxEA.cpp
Normal file
188
eo/tutorial/Lesson5/OneMaxEA.cpp
Normal file
|
|
@ -0,0 +1,188 @@
|
|||
/** -*- 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.
|
||||
It includes all other files that have been generated by the script create.sh
|
||||
so it is the only file to compile.
|
||||
|
||||
In case you want to build up a separate library for your new Evolving Object,
|
||||
you'll need some work - follow what's done in the src/ga dir, used in the
|
||||
main file BitEA in tutorial/Lesson4 dir.
|
||||
Or you can wait until we do it :-)
|
||||
*/
|
||||
|
||||
// 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 eoOneMax MUST derive from EO<FitT> for some fitness
|
||||
*/
|
||||
#include "eoOneMax.h"
|
||||
|
||||
/** definition of initilizqtion:
|
||||
* class eoOneMaxInit MUST derive from eoInit<eoOneMax>
|
||||
*/
|
||||
#include "eoOneMaxInit.h"
|
||||
|
||||
/** definition of evaluation:
|
||||
* class eoOneMaxEvalFunc MUST derive from eoEvalFunc<eoOneMax>
|
||||
* and should test for validity before doing any computation
|
||||
* see tutorial/Templates/evalFunc.tmpl
|
||||
*/
|
||||
#include "eoOneMaxEvalFunc.h"
|
||||
|
||||
// GENOTYPE eoOneMax ***MUST*** be templatized over the fitness
|
||||
|
||||
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
|
||||
// START fitness type: double or eoMaximizingFitness if you are maximizing
|
||||
// eoMinimizingFitness if you are minimizing
|
||||
typedef eoMaximizingFitness MyFitT ; // type of fitness
|
||||
// END fitness type
|
||||
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
|
||||
|
||||
// Then define your EO objects using that fitness type
|
||||
typedef eoOneMax<MyFitT> Indi; // ***MUST*** derive from EO
|
||||
|
||||
// create an initializer
|
||||
#include "make_genotype_OneMax.h"
|
||||
eoInit<Indi> & make_genotype(eoParser& _parser, eoState&_state, Indi _eo)
|
||||
{
|
||||
return do_make_genotype(_parser, _state, _eo);
|
||||
}
|
||||
|
||||
// and the variation operaotrs
|
||||
#include "make_op_OneMax.h"
|
||||
eoGenOp<Indi>& make_op(eoParser& _parser, eoState& _state, eoInit<Indi>& _init)
|
||||
{
|
||||
return do_make_op(_parser, _state, _init);
|
||||
}
|
||||
|
||||
// Use existing modules to define representation independent routines
|
||||
// These are parser-based definitions of objects
|
||||
|
||||
// how to initialize the population
|
||||
// it IS representation independent if an eoInit is given
|
||||
#include <do/make_pop.h>
|
||||
eoPop<Indi >& make_pop(eoParser& _parser, eoState& _state, eoInit<Indi> & _init)
|
||||
{
|
||||
return do_make_pop(_parser, _state, _init);
|
||||
}
|
||||
|
||||
// the stopping criterion
|
||||
#include <do/make_continue.h>
|
||||
eoContinue<Indi>& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter<Indi> & _eval)
|
||||
{
|
||||
return do_make_continue(_parser, _state, _eval);
|
||||
}
|
||||
|
||||
// outputs (stats, population dumps, ...)
|
||||
#include <do/make_checkpoint.h>
|
||||
eoCheckPoint<Indi>& make_checkpoint(eoParser& _parser, eoState& _state, eoEvalFuncCounter<Indi>& _eval, eoContinue<Indi>& _continue)
|
||||
{
|
||||
return do_make_checkpoint(_parser, _state, _eval, _continue);
|
||||
}
|
||||
|
||||
// evolution engine (selection and replacement)
|
||||
#include <do/make_algo_scalar.h>
|
||||
eoAlgo<Indi>& make_algo_scalar(eoParser& _parser, eoState& _state, eoEvalFunc<Indi>& _eval, eoContinue<Indi>& _continue, eoGenOp<Indi>& _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>
|
||||
// the instanciating fitnesses
|
||||
#include <eoScalarFitness.h>
|
||||
void run_ea(eoAlgo<Indi>& _ga, eoPop<Indi>& _pop)
|
||||
{
|
||||
do_run(_ga, _pop);
|
||||
}
|
||||
|
||||
// checks for help demand, and writes the status file
|
||||
// and make_help; in libutils
|
||||
void make_help(eoParser & _parser);
|
||||
|
||||
// now use all of the above, + representation dependent things
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
eoParser parser(argc, argv); // for user-parameter reading
|
||||
|
||||
eoState state; // keeps all things allocated
|
||||
|
||||
// The fitness
|
||||
//////////////
|
||||
eoOneMaxEvalFunc<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;
|
||||
}
|
||||
162
eo/tutorial/Lesson5/OneMaxLibEA.cpp
Normal file
162
eo/tutorial/Lesson5/OneMaxLibEA.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.
|
||||
See make_OneMax.cpp file.
|
||||
*/
|
||||
|
||||
// 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 eoOneMax MUST derive from EO<FitT> for some fitness
|
||||
*/
|
||||
#include "eoOneMax.h"
|
||||
|
||||
/** definition of initilizqtion:
|
||||
* class eoOneMaxInit MUST derive from eoInit<eoOneMax>
|
||||
*/
|
||||
#include "eoOneMaxInit.h"
|
||||
|
||||
/** definition of evaluation:
|
||||
* class eoOneMaxEvalFunc MUST derive from eoEvalFunc<eoOneMax>
|
||||
* and should test for validity before doing any computation
|
||||
* see tutorial/Templates/evalFunc.tmpl
|
||||
*/
|
||||
#include "eoOneMaxEvalFunc.h"
|
||||
|
||||
// GENOTYPE eoOneMax ***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 eoOneMax<MyFitT> Indi; // ***MUST*** derive from EO
|
||||
|
||||
// create an initializer - done here and NOT in make_OneMax.cpp
|
||||
// because it is NOT representation independent
|
||||
#include "make_genotype_OneMax.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_OneMax.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 eoOneMax.cpp
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
eoParser parser(argc, argv); // for user-parameter reading
|
||||
|
||||
eoState state; // keeps all things allocated
|
||||
|
||||
// The fitness
|
||||
//////////////
|
||||
eoOneMaxEvalFunc<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;
|
||||
}
|
||||
111
eo/tutorial/Lesson5/eoOneMax.h
Normal file
111
eo/tutorial/Lesson5/eoOneMax.h
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
/** -*- 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
|
||||
================================================
|
||||
*/
|
||||
|
||||
#ifndef _eoOneMax_h
|
||||
#define _eoOneMax_h
|
||||
|
||||
/**
|
||||
* Always write a comment in this format before class definition
|
||||
* if you want the class to be documented by Doxygen
|
||||
|
||||
* Note that you MUST derive your structure from EO<fitT>
|
||||
* but you MAY use some other already prepared class in the hierarchy
|
||||
* like eoVector for instance, if you handle a vector of something....
|
||||
|
||||
* If you create a structure from scratch,
|
||||
* the only thing you need to provide are
|
||||
* a default constructor
|
||||
* IO routines printOn and readFrom
|
||||
*
|
||||
* Note that operator<< and operator>> are defined at EO level
|
||||
* using these routines
|
||||
*/
|
||||
template< class FitT>
|
||||
class eoOneMax: public EO<FitT> {
|
||||
public:
|
||||
/** Ctor: you MUST provide a default ctor.
|
||||
* though such individuals will generally be processed
|
||||
* by some eoInit object
|
||||
*/
|
||||
eoOneMax()
|
||||
{
|
||||
// START Code of default Ctor of an eoOneMax object
|
||||
// END Code of default Ctor of an eoOneMax object
|
||||
}
|
||||
|
||||
virtual ~eoOneMax()
|
||||
{
|
||||
// START Code of Destructor of an eoEASEAGenome object
|
||||
// END Code of Destructor of an eoEASEAGenome object
|
||||
}
|
||||
|
||||
virtual string className() const { return "eoOneMax"; }
|
||||
|
||||
/** printing... */
|
||||
void printOn(ostream& _os) const
|
||||
{
|
||||
// First write the fitness
|
||||
EO<FitT>::printOn(_os);
|
||||
_os << ' ';
|
||||
// START Code of default output
|
||||
|
||||
/** HINTS
|
||||
* in EO we systematically write the sizes of things before the things
|
||||
* so readFrom is easier to code (see below)
|
||||
*/
|
||||
_os << b.size() << ' ' ;
|
||||
for (unsigned i=0; i<b.size(); i++)
|
||||
_os << b[i] << ' ' ;
|
||||
// END Code of default output
|
||||
}
|
||||
|
||||
/** reading...
|
||||
* of course, your readFrom must be able to read what printOn writes!!!
|
||||
*/
|
||||
void readFrom(istream& _is)
|
||||
{
|
||||
// of course you should read the fitness first!
|
||||
EO<FitT>::readFrom(_is);
|
||||
// START Code of input
|
||||
|
||||
/** HINTS
|
||||
* remember the eoOneMax object will come from the default ctor
|
||||
* this is why having the sizes written out is useful
|
||||
*/
|
||||
unsigned s;
|
||||
_is >> s;
|
||||
b.resize(s);
|
||||
for (unsigned i=0; i<s; i++)
|
||||
{
|
||||
bool bTmp;
|
||||
_is >> bTmp;
|
||||
b[i] = bTmp;
|
||||
}
|
||||
// END Code of input
|
||||
}
|
||||
|
||||
// accessing and setting values
|
||||
void setB(vector<bool> & _b)
|
||||
{
|
||||
b=_b;
|
||||
}
|
||||
const vector<bool> & B()
|
||||
{
|
||||
return b;
|
||||
}
|
||||
|
||||
private: // put all data here
|
||||
// START Private data of an eoOneMax object
|
||||
std::vector<bool> b;
|
||||
// END Private data of an eoOneMax object
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
68
eo/tutorial/Lesson5/eoOneMaxEvalFunc.h
Normal file
68
eo/tutorial/Lesson5/eoOneMaxEvalFunc.h
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
The above line is usefulin Emacs-like editors
|
||||
*/
|
||||
|
||||
/*
|
||||
Template for evaluator in EO, a functor that computes the fitness of an EO
|
||||
==========================================================================
|
||||
*/
|
||||
|
||||
#ifndef _eoOneMaxEvalFunc_h
|
||||
#define _eoOneMaxEvalFunc_h
|
||||
|
||||
// include whatever general include you need
|
||||
#include <stdexcept>
|
||||
#include <fstream>
|
||||
|
||||
// include the base definition of eoEvalFunc
|
||||
#include "eoEvalFunc.h"
|
||||
|
||||
/**
|
||||
Always write a comment in this format before class definition
|
||||
if you want the class to be documented by Doxygen
|
||||
*/
|
||||
template <class EOT>
|
||||
class eoOneMaxEvalFunc : public eoEvalFunc<EOT>
|
||||
{
|
||||
public:
|
||||
/// Ctor - no requirement
|
||||
// START eventually add or modify the anyVariable argument
|
||||
eoOneMaxEvalFunc()
|
||||
// eoOneMaxEvalFunc( varType _anyVariable) : anyVariable(_anyVariable)
|
||||
// END eventually add or modify the anyVariable argument
|
||||
{
|
||||
// START Code of Ctor of an eoOneMaxEvalFunc object
|
||||
// END Code of Ctor of an eoOneMaxEvalFunc object
|
||||
}
|
||||
|
||||
/** Actually compute the fitness
|
||||
*
|
||||
* @param EOT & _eo the EO object to evaluate
|
||||
* it should stay templatized to be usable
|
||||
* with any fitness type
|
||||
*/
|
||||
void operator()(EOT & _eo)
|
||||
{
|
||||
// test for invalid to avoid recomputing fitness of unmodified individuals
|
||||
if (_eo.invalid())
|
||||
{
|
||||
double fit; // to hold fitness value
|
||||
// START Code of computation of fitness of the eoOneMax object
|
||||
const vector<bool> & b = _eo.B();
|
||||
fit = 0;
|
||||
for (unsigned i=0; i<b.size(); i++)
|
||||
fit += (b[i]?0:1);
|
||||
// END Code of computation of fitness of the eoOneMax object
|
||||
_eo.fitness(fit);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
// START Private data of an eoOneMaxEvalFunc object
|
||||
// varType anyVariable; // for example ...
|
||||
// END Private data of an eoOneMaxEvalFunc object
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
63
eo/tutorial/Lesson5/eoOneMaxInit.h
Normal file
63
eo/tutorial/Lesson5/eoOneMaxInit.h
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
The above line is usefulin Emacs-like editors
|
||||
*/
|
||||
|
||||
/*
|
||||
Template for EO objects initialization in EO
|
||||
============================================
|
||||
*/
|
||||
|
||||
#ifndef _eoOneMaxInit_h
|
||||
#define _eoOneMaxInit_h
|
||||
|
||||
// include the base definition of eoInit
|
||||
#include <eoInit.h>
|
||||
|
||||
/**
|
||||
* Always write a comment in this format before class definition
|
||||
* if you want the class to be documented by Doxygen
|
||||
*
|
||||
* There is NO ASSUMPTION on the class GenoypeT.
|
||||
* In particular, it does not need to derive from EO (e.g. to initialize
|
||||
* atoms of an eoVector you will need an eoInit<AtomType>)
|
||||
*/
|
||||
template <class GenotypeT>
|
||||
class eoOneMaxInit: public eoInit<GenotypeT> {
|
||||
public:
|
||||
/// Ctor - no requirement
|
||||
// START eventually add or modify the anyVariable argument
|
||||
// eoOneMaxInit()
|
||||
eoOneMaxInit( unsigned _vecSize) : vecSize(_vecSize)
|
||||
// END eventually add or modify the anyVariable argument
|
||||
{
|
||||
// START Code of Ctor of an eoOneMaxInit object
|
||||
// END Code of Ctor of an eoOneMaxInit object
|
||||
}
|
||||
|
||||
|
||||
/** initialize a genotype
|
||||
*
|
||||
* @param _genotype generally a genotype that has been default-constructed
|
||||
* whatever it contains will be lost
|
||||
*/
|
||||
void operator()(GenotypeT & _genotype)
|
||||
{
|
||||
// START Code of random initialization of an eoOneMax object
|
||||
vector<bool> b(vecSize);
|
||||
for (unsigned i=0; i<vecSize; i++)
|
||||
b[i]=rng.flip();
|
||||
_genotype.setB(b);
|
||||
// END Code of random initialization of an eoOneMax object
|
||||
_genotype.invalidate(); // IMPORTANT in case the _genotype is old
|
||||
}
|
||||
|
||||
private:
|
||||
// START Private data of an eoOneMaxInit object
|
||||
unsigned vecSize; // size of all bitstrings that this eoInit randomize
|
||||
// varType anyVariable; // for example ...
|
||||
// END Private data of an eoOneMaxInit object
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
68
eo/tutorial/Lesson5/eoOneMaxMutation.h
Normal file
68
eo/tutorial/Lesson5/eoOneMaxMutation.h
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
The above line is useful in Emacs-like editors
|
||||
*/
|
||||
|
||||
/*
|
||||
Template for simple mutation operators
|
||||
======================================
|
||||
*/
|
||||
|
||||
#ifndef eoOneMaxMutation_H
|
||||
#define eoOneMaxMutation_H
|
||||
|
||||
|
||||
#include <eoOp.h>
|
||||
|
||||
/**
|
||||
* Always write a comment in this format before class definition
|
||||
* if you want the class to be documented by Doxygen
|
||||
*
|
||||
* THere is NO ASSUMPTION on the class GenoypeT.
|
||||
* In particular, it does not need to derive from EO
|
||||
*/
|
||||
template<class GenotypeT>
|
||||
class eoOneMaxMutation: public eoMonOp<GenotypeT>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Ctor - no requirement
|
||||
*/
|
||||
// START eventually add or modify the anyVariable argument
|
||||
eoOneMaxMutation()
|
||||
// eoOneMaxMutation( varType _anyVariable) : anyVariable(_anyVariable)
|
||||
// END eventually add or modify the anyVariable argument
|
||||
{
|
||||
// START Code of Ctor of an eoOneMaxEvalFunc object
|
||||
// END Code of Ctor of an eoOneMaxEvalFunc object
|
||||
}
|
||||
|
||||
/// The class name. Used to display statistics
|
||||
string className() const { return "eoOneMaxMutation"; }
|
||||
|
||||
/**
|
||||
* modifies the parent
|
||||
* @param _genotype The parent genotype (will be modified)
|
||||
*/
|
||||
bool operator()(GenotypeT & _genotype)
|
||||
{
|
||||
bool isModified;
|
||||
// START code for mutation of the _genotype object
|
||||
|
||||
/** Requirement
|
||||
* if (_genotype has been modified)
|
||||
* isModified = true;
|
||||
* else
|
||||
* isModified = false;
|
||||
*/
|
||||
return isModified;
|
||||
// END code for mutation of the _genotype object
|
||||
}
|
||||
|
||||
private:
|
||||
// START Private data of an eoOneMaxMutation object
|
||||
// varType anyVariable; // for example ...
|
||||
// END Private data of an eoOneMaxMutation object
|
||||
};
|
||||
|
||||
#endif
|
||||
70
eo/tutorial/Lesson5/eoOneMaxQuadCrossover.h
Normal file
70
eo/tutorial/Lesson5/eoOneMaxQuadCrossover.h
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
The above line is usefulin Emacs-like editors
|
||||
*/
|
||||
|
||||
/*
|
||||
Template for simple quadratic crossover operators
|
||||
=================================================
|
||||
|
||||
Quadratic crossover operators modify the both genotypes
|
||||
*/
|
||||
|
||||
#ifndef eoOneMaxQuadCrossover_H
|
||||
#define eoOneMaxQuadCrossover_H
|
||||
|
||||
#include <eoOp.h>
|
||||
|
||||
/**
|
||||
* Always write a comment in this format before class definition
|
||||
* if you want the class to be documented by Doxygen
|
||||
*
|
||||
* THere is NO ASSUMPTION on the class GenoypeT.
|
||||
* In particular, it does not need to derive from EO
|
||||
*/
|
||||
template<class GenotypeT>
|
||||
class eoOneMaxQuadCrossover: public eoQuadOp<GenotypeT>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Ctor - no requirement
|
||||
*/
|
||||
// START eventually add or modify the anyVariable argument
|
||||
eoOneMaxQuadCrossover()
|
||||
// eoOneMaxQuadCrossover( varType _anyVariable) : anyVariable(_anyVariable)
|
||||
// END eventually add or modify the anyVariable argument
|
||||
{
|
||||
// START Code of Ctor of an eoOneMaxEvalFunc object
|
||||
// END Code of Ctor of an eoOneMaxEvalFunc object
|
||||
}
|
||||
|
||||
/// The class name. Used to display statistics
|
||||
string className() const { return "eoOneMaxQuadCrossover"; }
|
||||
|
||||
/**
|
||||
* eoQuad crossover - modifies both parents
|
||||
* @param _genotype1 The first parent
|
||||
* @param _genotype2 The second parent
|
||||
*/
|
||||
bool operator()(GenotypeT& _genotype1, GenotypeT & _genotype2)
|
||||
{
|
||||
bool oneAtLeastIsModified;
|
||||
// START code for crossover of _genotype1 and _genotype2 objects
|
||||
|
||||
/** Requirement
|
||||
* if (at least one genotype has been modified) // no way to distinguish
|
||||
* oneAtLeastIsModified = true;
|
||||
* else
|
||||
* oneAtLeastIsModified = false;
|
||||
*/
|
||||
return oneAtLeastIsModified;
|
||||
// END code for crossover of _genotype1 and _genotype2 objects
|
||||
}
|
||||
|
||||
private:
|
||||
// START Private data of an eoOneMaxQuadCrossover object
|
||||
// varType anyVariable; // for example ...
|
||||
// END Private data of an eoOneMaxQuadCrossover object
|
||||
};
|
||||
|
||||
#endif
|
||||
129
eo/tutorial/Lesson5/make_OneMax.cpp
Normal file
129
eo/tutorial/Lesson5/make_OneMax.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 llows 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 (eoOneMax.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 eoOneMax MUST derive from EO<FitT> for some fitness
|
||||
*/
|
||||
#include "eoOneMax.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_OneMax.h"
|
||||
// eoInit<eoOneMax<double>> & make_genotype(eoParser& _parser, eoState&_state, eoOneMax<double> _eo)
|
||||
// {
|
||||
// return do_make_genotype(_parser, _state, _eo);
|
||||
// }
|
||||
|
||||
// eoInit<eoOneMax<eoMinimizingFitness>> & make_genotype(eoParser& _parser, eoState&_state, eoOneMax<eoMinimizingFitness> _eo)
|
||||
// {
|
||||
// return do_make_genotype(_parser, _state, _eo);
|
||||
// }
|
||||
|
||||
// same thing for the variation operaotrs
|
||||
//---------------------------------------
|
||||
// #include "make_op_OneMax.h"
|
||||
// eoGenOp<eoOneMax<double>>& make_op(eoParser& _parser, eoState& _state, eoInit<eoOneMax<double>>& _init)
|
||||
// {
|
||||
// return do_make_op(_parser, _state, _init);
|
||||
// }
|
||||
|
||||
// eoGenOp<eoOneMax<eoMinimizingFitness>>& make_op(eoParser& _parser, eoState& _state, eoInit<eoOneMax<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<eoOneMax<double> >& make_pop(eoParser& _parser, eoState& _state, eoInit<eoOneMax<double> > & _init)
|
||||
{
|
||||
return do_make_pop(_parser, _state, _init);
|
||||
}
|
||||
|
||||
eoPop<eoOneMax<eoMinimizingFitness> >& make_pop(eoParser& _parser, eoState& _state, eoInit<eoOneMax<eoMinimizingFitness> > & _init)
|
||||
{
|
||||
return do_make_pop(_parser, _state, _init);
|
||||
}
|
||||
|
||||
// the stopping criterion
|
||||
#include <do/make_continue.h>
|
||||
eoContinue<eoOneMax<double> >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter<eoOneMax<double> > & _eval)
|
||||
{
|
||||
return do_make_continue(_parser, _state, _eval);
|
||||
}
|
||||
|
||||
eoContinue<eoOneMax<eoMinimizingFitness> >& make_continue(eoParser& _parser, eoState& _state, eoEvalFuncCounter<eoOneMax<eoMinimizingFitness> > & _eval)
|
||||
{
|
||||
return do_make_continue(_parser, _state, _eval);
|
||||
}
|
||||
|
||||
// outputs (stats, population dumps, ...)
|
||||
#include <do/make_checkpoint.h>
|
||||
eoCheckPoint<eoOneMax<double> >& make_checkpoint(eoParser& _parser, eoState& _state, eoEvalFuncCounter<eoOneMax<double> >& _eval, eoContinue<eoOneMax<double> >& _continue)
|
||||
{
|
||||
return do_make_checkpoint(_parser, _state, _eval, _continue);
|
||||
}
|
||||
|
||||
eoCheckPoint<eoOneMax<eoMinimizingFitness> >& make_checkpoint(eoParser& _parser, eoState& _state, eoEvalFuncCounter<eoOneMax<eoMinimizingFitness> >& _eval, eoContinue<eoOneMax<eoMinimizingFitness> >& _continue)
|
||||
{
|
||||
return do_make_checkpoint(_parser, _state, _eval, _continue);
|
||||
}
|
||||
|
||||
// evolution engine (selection and replacement)
|
||||
#include <do/make_algo_scalar.h>
|
||||
eoAlgo<eoOneMax<double> >& make_algo_scalar(eoParser& _parser, eoState& _state, eoEvalFunc<eoOneMax<double> >& _eval, eoContinue<eoOneMax<double> >& _continue, eoGenOp<eoOneMax<double> >& _op)
|
||||
{
|
||||
return do_make_algo_scalar(_parser, _state, _eval, _continue, _op);
|
||||
}
|
||||
|
||||
eoAlgo<eoOneMax<eoMinimizingFitness> >& make_algo_scalar(eoParser& _parser, eoState& _state, eoEvalFunc<eoOneMax<eoMinimizingFitness> >& _eval, eoContinue<eoOneMax<eoMinimizingFitness> >& _continue, eoGenOp<eoOneMax<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<eoOneMax<double> >& _ga, eoPop<eoOneMax<double> >& _pop)
|
||||
{
|
||||
do_run(_ga, _pop);
|
||||
}
|
||||
|
||||
void run_ea(eoAlgo<eoOneMax<eoMinimizingFitness> >& _ga, eoPop<eoOneMax<eoMinimizingFitness> >& _pop)
|
||||
{
|
||||
do_run(_ga, _pop);
|
||||
}
|
||||
|
||||
75
eo/tutorial/Lesson5/make_genotype_OneMax.h
Normal file
75
eo/tutorial/Lesson5/make_genotype_OneMax.h
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// make_genotype.h
|
||||
// (c) Maarten Keijzer, Marc Schoenauer and GeNeura Team, 2001
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
Marc.Schoenauer@polytechnique.fr
|
||||
mkeijzer@dhi.dk
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _make_genotype_h
|
||||
#define _make_genotype_h
|
||||
|
||||
#include <eoOneMax.h>
|
||||
#include <eoOneMaxInit.h>
|
||||
// also need the parser and param includes
|
||||
#include <utils/eoParser.h>
|
||||
#include <utils/eoState.h>
|
||||
|
||||
|
||||
/*
|
||||
* This fuction does the create an eoInit<eoOneMax>
|
||||
*
|
||||
* It could be here tempatized only on the fitness, as it can be used
|
||||
* to evolve structures with any fitness.
|
||||
* However, for consistency reasons, it was finally chosen, as in
|
||||
* the rest of EO, to templatize by the full EOT, as this eventually
|
||||
* allows to choose the type of genotype at run time (see in es dir)
|
||||
*
|
||||
* It returns an eoInit<EOT> that can later be used to initialize
|
||||
* the population (see make_pop.h).
|
||||
*
|
||||
* It uses a parser (to get user parameters) and a state (to store the memory)
|
||||
* the last argument is to disambiguate the call upon different instanciations.
|
||||
*
|
||||
* WARNING: that last argument will generally be the result of calling
|
||||
* the default ctor of EOT, resulting in most cases in an EOT
|
||||
* that is ***not properly initialized***
|
||||
*/
|
||||
|
||||
template <class EOT>
|
||||
eoInit<EOT> & do_make_genotype(eoParameterLoader& _parser, eoState& _state, EOT)
|
||||
{
|
||||
// read any useful parameter here from the parser
|
||||
// the param itself will belong to the parser (as far as memory is concerned)
|
||||
|
||||
// paramType & param = _parser.createParam(deafultValue, "Keyword", "Comment to appear in help and status", 'c',"Section of status file").value();
|
||||
|
||||
unsigned vecSize = _parser.createParam(unsigned(8), "VecSize", "Size of the bitstrings", 'v',"Representation").value();
|
||||
|
||||
// Then built the initializer - a pointer, stored in the eoState
|
||||
eoInit<EOT>* init = new eoOneMaxInit<EOT>(vecSize);
|
||||
// store in state
|
||||
_state.storeFunctor(init);
|
||||
// and return a reference
|
||||
return *init;
|
||||
}
|
||||
|
||||
#endif
|
||||
210
eo/tutorial/Lesson5/make_op_OneMax.h
Normal file
210
eo/tutorial/Lesson5/make_op_OneMax.h
Normal file
|
|
@ -0,0 +1,210 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// make_op_OneMax.h
|
||||
// (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2001
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
Marc.Schoenauer@polytechnique.fr
|
||||
mkeijzer@dhi.dk
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _make_op_OneMax_h
|
||||
#define _make_op_OneMax_h
|
||||
|
||||
// the operators
|
||||
#include <eoOp.h>
|
||||
#include <eoGenOp.h>
|
||||
#include <eoCloneOps.h>
|
||||
#include <eoOpContainer.h>
|
||||
// combinations of simple eoOps (eoMonOp and eoQuadOp)
|
||||
#include <eoProportionalCombinedOp.h>
|
||||
|
||||
/** definition of mutation:
|
||||
* class eoOneMaxMonop MUST derive from eoMonOp<eoOneMax>
|
||||
*/
|
||||
#include "eoOneMaxMutation.h"
|
||||
|
||||
/** definition of crossover (either as eoBinOp (2->1) or eoQuadOp (2->2):
|
||||
* class eoOneMaxBinCrossover MUST derive from eoBinOp<eoOneMax>
|
||||
* OR
|
||||
* class eoOneMaxQuadCrossover MUST derive from eoQuadOp<eoOneMax>
|
||||
*/
|
||||
// #include "eoOneMaxBinOp.h"
|
||||
// OR
|
||||
#include "eoOneMaxQuadCrossover.h"
|
||||
|
||||
// also need the parser and state includes
|
||||
#include <utils/eoParser.h>
|
||||
#include <utils/eoState.h>
|
||||
|
||||
|
||||
/////////////////// variation operators ///////////////
|
||||
// canonical (crossover + mutation) only at the moment //
|
||||
|
||||
/*
|
||||
* This function builds the operators that will be applied to the eoOneMax
|
||||
*
|
||||
* It uses a parser (to get user parameters), a state (to store the memory)
|
||||
* the last parameter is an eoInit: if some operator needs some info
|
||||
* about the genotypes, the init has it all (e.g. bounds, ...)
|
||||
* Simply do
|
||||
* EOT myEO;
|
||||
* _init(myEO);
|
||||
* and myEO is then an ACTUAL object
|
||||
*
|
||||
* As usual, the template is the complete EOT even though only the fitness
|
||||
* is actually templatized here: the following only applies to eoOneMax
|
||||
*/
|
||||
|
||||
template <class EOT>
|
||||
eoGenOp<EOT> & do_make_op(eoParameterLoader& _parser, eoState& _state, eoInit<EOT>& _init)
|
||||
{
|
||||
// this is a temporary version, while Maarten codes the full tree-structured
|
||||
// general operator input
|
||||
// BTW we must leave that simple version available somehow, as it is the one
|
||||
// that 90% people use!
|
||||
|
||||
|
||||
/////////////////////////////
|
||||
// Variation operators
|
||||
////////////////////////////
|
||||
// read crossover and mutations, combine each in a proportional Op
|
||||
// and create the eoGenOp that calls crossover at rate pCross
|
||||
// then mutation with rate pMut
|
||||
|
||||
// the crossovers
|
||||
/////////////////
|
||||
|
||||
// here we can have eoQuadOp (2->2) only - no time for the eoBinOp case
|
||||
|
||||
// you can have more than one - combined in a proportional way
|
||||
|
||||
// first, define the crossover objects and read their rates from the parser
|
||||
|
||||
// A first crossover
|
||||
eoQuadOp<Indi> *cross = new eoOneMaxQuadCrossover<Indi> /* (varType _anyVariable) */;
|
||||
// store in the state
|
||||
_state.storeFunctor(cross);
|
||||
|
||||
// read its relative rate in the combination
|
||||
double cross1Rate = _parser.createParam(1.0, "cross1Rate", "Relative rate for crossover 1", '1', "Variation Operators").value();
|
||||
|
||||
// and create the combined operator with this one
|
||||
eoPropCombinedQuadOp<Indi> *propXover =
|
||||
new eoPropCombinedQuadOp<Indi>(*cross, cross1Rate);
|
||||
// and of course stor it in the state
|
||||
_state.storeFunctor(propXover);
|
||||
|
||||
|
||||
// Optional: A second(and third, and ...) crossover
|
||||
// of course you must create the corresponding classes
|
||||
// and all ***MUST*** derive from eoQuadOp<Indi>
|
||||
|
||||
/* Uncomment if necessary - and replicate as many time as you need
|
||||
cross = new eoOneMaxSecondCrossover<Indi>(varType _anyVariable);
|
||||
_state.storeFunctor(cross);
|
||||
double cross2Rate = _parser.createParam(1.0, "cross2Rate", "Relative rate for crossover 2", '2', "Variation Operators").value();
|
||||
propXover.add(*cross, cross2Rate);
|
||||
*/
|
||||
// if you want some gentle output, the last one shoudl be like
|
||||
// propXover.add(*cross, crossXXXRate, true);
|
||||
|
||||
|
||||
// the mutation: same story
|
||||
////////////////
|
||||
// you can have more than one - combined in a proportional way
|
||||
|
||||
// for each mutation,
|
||||
// - define the mutator object
|
||||
// - read its rate from the parser
|
||||
// - add it to the proportional combination
|
||||
|
||||
// a first mutation
|
||||
eoMonOp<Indi> *mut = new eoOneMaxMutation<Indi>/* (varType _anyVariable) */;
|
||||
_state.storeFunctor(mut);
|
||||
// its relative rate in the combination
|
||||
double mut1Rate = _parser.createParam(1.0, "mut1Rate", "Relative rate for mutation 1", '1', "Variation Operators").value();
|
||||
// and the creation of the combined operator with this one
|
||||
eoPropCombinedMonOp<Indi> *propMutation = new eoPropCombinedMonOp<Indi>(*mut, mut1Rate);
|
||||
_state.storeFunctor(propMutation);
|
||||
|
||||
// Optional: A second(and third, and ...) mutation with their rates
|
||||
// of course you must create the corresponding classes
|
||||
// and all ***MUST*** derive from eoMonOp<Indi>
|
||||
|
||||
/* Uncomment if necessary - and replicate as many time as you need
|
||||
mut = new eoOneMaxSecondMutation<Indi>(varType _anyVariable);
|
||||
_state.storeFunctor(mut);
|
||||
double mut2Rate = _parser.createParam(1.0, "mut2Rate", "Relative rate for mutation 2", '2', "Variation Operators").value();
|
||||
propMutation.add(*mut, mut2Rate);
|
||||
*/
|
||||
// if you want some gentle output, the last one shoudl be like
|
||||
// propMutation.add(*mut, mutXXXRate, true);
|
||||
|
||||
// end of crossover and mutation definitions
|
||||
////////////////////////////////////////////
|
||||
|
||||
// END Modify definitions of objects by eventually add parameters
|
||||
//*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
|
||||
|
||||
// from now on, you do not need to modify anything
|
||||
// though you CAN add things to the checkpointing (see tutorial)
|
||||
|
||||
// now build the eoGenOp:
|
||||
// to simulate SGA (crossover with proba pCross + mutation with proba pMut
|
||||
// we must construct
|
||||
// a sequential combination of
|
||||
// with proba 1, a proportional combination of
|
||||
// a QuadCopy and our crossover
|
||||
// with proba pMut, our mutation
|
||||
|
||||
// but of course you're free to use any smart combination you could think of
|
||||
// especially, if you have to use eoBinOp rather than eoQuad Op youùll have
|
||||
// to modify that part
|
||||
|
||||
// First read the individual level parameters
|
||||
eoValueParam<double>& pCrossParam = _parser.createParam(0.6, "pCross", "Probability of Crossover", 'C', "Variation Operators" );
|
||||
// minimum check
|
||||
if ( (pCrossParam.value() < 0) || (pCrossParam.value() > 1) )
|
||||
throw runtime_error("Invalid pCross");
|
||||
|
||||
eoValueParam<double>& pMutParam = _parser.createParam(0.1, "pMut", "Probability of Mutation", 'M', "Variation Operators" );
|
||||
// minimum check
|
||||
if ( (pMutParam.value() < 0) || (pMutParam.value() > 1) )
|
||||
throw runtime_error("Invalid pMut");
|
||||
|
||||
|
||||
// the crossover - with probability pCross
|
||||
eoProportionalOp<Indi> * propOp = new eoProportionalOp<Indi> ;
|
||||
_state.storeFunctor(propOp);
|
||||
eoQuadOp<Indi> *ptQuad = new eoQuadCloneOp<Indi>;
|
||||
_state.storeFunctor(ptQuad);
|
||||
propOp->add(*propXover, pCrossParam.value()); // crossover, with proba pcross
|
||||
propOp->add(*ptQuad, 1-pCrossParam.value()); // nothing, with proba 1-pcross
|
||||
|
||||
// now the sequential
|
||||
eoSequentialOp<Indi> *op = new eoSequentialOp<Indi>;
|
||||
_state.storeFunctor(op);
|
||||
op->add(*propOp, 1.0); // always do combined crossover
|
||||
op->add(*propMutation, pMutParam.value()); // then mutation, with proba pmut
|
||||
|
||||
// that's it - return a reference
|
||||
return *op;
|
||||
}
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue