Initial version of the tutorial.
Warning: all Makefile's are hand-made, and will only work in Linux
This commit is contained in:
parent
a27dc53ef0
commit
b8d4e7faef
48 changed files with 9174 additions and 0 deletions
164
eo/tutorial/Lesson1/FirstBitGA.cpp
Normal file
164
eo/tutorial/Lesson1/FirstBitGA.cpp
Normal file
|
|
@ -0,0 +1,164 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// FirstBitGA.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
//*
|
||||
// An instance of a VERY simple Bitstring Genetic Algorithm
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
// standard includes
|
||||
#include <stdexcept> // runtime_error
|
||||
#include <iostream> // cout
|
||||
#include <strstream> // ostrstream, istrstream
|
||||
|
||||
// the general include for eo
|
||||
|
||||
#include <eo>
|
||||
|
||||
// REPRESENTATION
|
||||
//-----------------------------------------------------------------------------
|
||||
// define your individuals
|
||||
typedef eoBin<double> Indi; // A bitstring with fitness double
|
||||
|
||||
// EVAL
|
||||
//-----------------------------------------------------------------------------
|
||||
// a simple fitness function that computes the number of ones of a bitstring
|
||||
// @param _indi A biststring individual
|
||||
|
||||
double binary_value(const Indi & _indi)
|
||||
{
|
||||
double sum = 0;
|
||||
for (unsigned i = 0; i < _indi.size(); i++)
|
||||
sum += _indi[i];
|
||||
return sum;
|
||||
}
|
||||
// GENERAL
|
||||
//-----------------------------------------------------------------------------
|
||||
void main_function(int argc, char **argv)
|
||||
{
|
||||
// PARAMETRES
|
||||
// all parameters are hard-coded!
|
||||
const unsigned int SEED = 42; // seed for random number generator
|
||||
const unsigned int T_SIZE = 3; // size for tournament selection
|
||||
const unsigned int VEC_SIZE = 8; // Number of bits in genotypes
|
||||
const unsigned int POP_SIZE = 20; // Size of population
|
||||
const unsigned int MAX_GEN = 100; // Maximum number of generation before STOP
|
||||
const float CROSS_RATE = 0.8; // Crossover rate
|
||||
const double P_MUT_PER_BIT = 0.01; // probability of bit-flip mutation
|
||||
const float MUT_RATE = 1.0; // mutation rate
|
||||
|
||||
// GENERAL
|
||||
//////////////////////////
|
||||
// Random seed
|
||||
//////////////////////////
|
||||
//reproducible random seed: if you don't change SEED above,
|
||||
// you'll aways get the same result, NOT a random run
|
||||
rng.reseed(SEED);
|
||||
|
||||
// EVAL
|
||||
/////////////////////////////
|
||||
// Fitness function
|
||||
////////////////////////////
|
||||
// Evaluation: from a plain C++ fn to an EvalFunc Object
|
||||
eoEvalFuncPtr<Indi> eval( binary_value );
|
||||
|
||||
// INIT
|
||||
////////////////////////////////
|
||||
// Initilisation of population
|
||||
////////////////////////////////
|
||||
|
||||
// declare the population
|
||||
eoPop<Indi> pop;
|
||||
// fill it!
|
||||
for (unsigned int igeno=0; igeno<POP_SIZE; igeno++)
|
||||
{
|
||||
Indi v; // void individual, to be filled
|
||||
for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)
|
||||
{
|
||||
bool r = rng.flip(); // new value, random in {0,1}
|
||||
v.push_back(r); // append that random value to v
|
||||
}
|
||||
eval(v); // evaluate it
|
||||
pop.push_back(v); // and put it in the population
|
||||
}
|
||||
|
||||
// OUTPUT
|
||||
// sort pop before printing it!
|
||||
pop.sort();
|
||||
// Print (sorted) intial population (raw printout)
|
||||
cout << "Initial Population" << endl;
|
||||
cout << pop;
|
||||
|
||||
// ENGINE
|
||||
/////////////////////////////////////
|
||||
// selection and replacement
|
||||
////////////////////////////////////
|
||||
// SELECT
|
||||
// The robust tournament selection
|
||||
eoDetTournament<Indi> select(T_SIZE); // T_SIZE in [2,POP_SIZE]
|
||||
|
||||
// REPLACE
|
||||
// The simple GA evolution engine uses generational replacement
|
||||
// so no replacement procedure is needed
|
||||
|
||||
// OPERATORS
|
||||
//////////////////////////////////////
|
||||
// The variation operators
|
||||
//////////////////////////////////////
|
||||
// CROSSOVER
|
||||
// 1-point mutation for bitstring
|
||||
eoBinCrossover<Indi> xover;
|
||||
// MUTATION
|
||||
// standard bit-flip mutation for bitstring
|
||||
eoBinMutation<Indi> mutation(P_MUT_PER_BIT);
|
||||
|
||||
// STOP
|
||||
// CHECKPOINT
|
||||
//////////////////////////////////////
|
||||
// termination condition
|
||||
/////////////////////////////////////
|
||||
// stop after MAX_GEN generations
|
||||
eoGenContinue<Indi> continuator(MAX_GEN);
|
||||
|
||||
// GENERATION
|
||||
/////////////////////////////////////////
|
||||
// the algorithm
|
||||
////////////////////////////////////////
|
||||
// standard Generational GA requires as parameters
|
||||
// selection, evaluation, crossover and mutation, stopping criterion
|
||||
|
||||
|
||||
eoSGA<Indi> gga(select, xover, CROSS_RATE, mutation, MUT_RATE,
|
||||
eval, continuator);
|
||||
|
||||
// Apply algo to pop - that's it!
|
||||
gga(pop);
|
||||
|
||||
// OUTPUT
|
||||
// Print (sorted) intial population
|
||||
pop.sort();
|
||||
cout << "FINAL Population\n" << pop << endl;
|
||||
// GENERAL
|
||||
}
|
||||
// A main that catches the exceptions
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
// rng.reseed(42);
|
||||
int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
|
||||
flag |= _CRTDBG_LEAK_CHECK_DF;
|
||||
_CrtSetDbgFlag(flag);
|
||||
// _CrtSetBreakAlloc(100);
|
||||
#endif
|
||||
|
||||
try
|
||||
{
|
||||
main_function(argc, argv);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
165
eo/tutorial/Lesson1/FirstRealGA.cpp
Normal file
165
eo/tutorial/Lesson1/FirstRealGA.cpp
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// FirstRealGA.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
//*
|
||||
// An instance of a VERY simple Real-coded Genetic Algorithm
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
// standard includes
|
||||
#include <stdexcept> // runtime_error
|
||||
#include <iostream> // cout
|
||||
#include <strstream> // ostrstream, istrstream
|
||||
|
||||
// the general include for eo
|
||||
|
||||
#include <eo>
|
||||
|
||||
// REPRESENTATION
|
||||
//-----------------------------------------------------------------------------
|
||||
// define your individuals
|
||||
typedef eoReal<double> Indi;
|
||||
|
||||
// EVAL
|
||||
//-----------------------------------------------------------------------------
|
||||
// a simple fitness function that computes the euclidian norm of a real vector
|
||||
// @param _indi A real-valued individual
|
||||
|
||||
double real_value(const Indi & _indi)
|
||||
{
|
||||
double sum = 0;
|
||||
for (unsigned i = 0; i < _indi.size(); i++)
|
||||
sum += _indi[i]*_indi[i];
|
||||
return (-sum); // maximizing only
|
||||
}
|
||||
// GENERAL
|
||||
//-----------------------------------------------------------------------------
|
||||
void main_function(int argc, char **argv)
|
||||
{
|
||||
// PARAMETRES
|
||||
// all parameters are hard-coded!
|
||||
const unsigned int SEED = 42; // seed for random number generator
|
||||
const unsigned int VEC_SIZE = 8; // Number of object variables in genotypes
|
||||
const unsigned int POP_SIZE = 20; // Size of population
|
||||
const unsigned int T_SIZE = 3; // size for tournament selection
|
||||
const unsigned int MAX_GEN = 500; // Maximum number of generation before STOP
|
||||
const float CROSS_RATE = 0.8; // Crossover rate
|
||||
const double EPSILON = 0.01; // range for real uniform mutation
|
||||
const float MUT_RATE = 0.5; // mutation rate
|
||||
|
||||
// GENERAL
|
||||
//////////////////////////
|
||||
// Random seed
|
||||
//////////////////////////
|
||||
//reproducible random seed: if you don't change SEED above,
|
||||
// you'll aways get the same result, NOT a random run
|
||||
rng.reseed(SEED);
|
||||
|
||||
// EVAL
|
||||
/////////////////////////////
|
||||
// Fitness function
|
||||
////////////////////////////
|
||||
// Evaluation: from a plain C++ fn to an EvalFunc Object
|
||||
eoEvalFuncPtr<Indi> eval( real_value );
|
||||
|
||||
// INIT
|
||||
////////////////////////////////
|
||||
// Initilisation of population
|
||||
////////////////////////////////
|
||||
|
||||
// declare the population
|
||||
eoPop<Indi> pop;
|
||||
// fill it!
|
||||
for (unsigned int igeno=0; igeno<POP_SIZE; igeno++)
|
||||
{
|
||||
Indi v; // void individual, to be filled
|
||||
for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)
|
||||
{
|
||||
double r = 2*rng.uniform() - 1; // new value, random in [-1,1)
|
||||
v.push_back(r); // append that random value to v
|
||||
}
|
||||
eval(v); // evaluate it
|
||||
pop.push_back(v); // and put it in the population
|
||||
}
|
||||
|
||||
// OUTPUT
|
||||
// sort pop before printing it!
|
||||
pop.sort();
|
||||
// Print (sorted) intial population (raw printout)
|
||||
cout << "Initial Population" << endl;
|
||||
cout << pop;
|
||||
|
||||
// ENGINE
|
||||
/////////////////////////////////////
|
||||
// selection and replacement
|
||||
////////////////////////////////////
|
||||
// SELECT
|
||||
// The robust tournament selection
|
||||
eoDetTournament<Indi> select(T_SIZE); // T_SIZE in [2,POP_SIZE]
|
||||
|
||||
// REPLACE
|
||||
// eoSGA uses generational replacement by default
|
||||
// so no replacement procedure has to be given
|
||||
|
||||
// OPERATORS
|
||||
//////////////////////////////////////
|
||||
// The variation operators
|
||||
//////////////////////////////////////
|
||||
// CROSSOVER
|
||||
// offspring(i) is a linear combination of parent(i)
|
||||
eoArithmeticCrossover<Indi> xover;
|
||||
// MUTATION
|
||||
// offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]
|
||||
eoUniformMutation<Indi> mutation(EPSILON);
|
||||
|
||||
// STOP
|
||||
// CHECKPOINT
|
||||
//////////////////////////////////////
|
||||
// termination condition
|
||||
/////////////////////////////////////
|
||||
// stop after MAX_GEN generations
|
||||
eoGenContinue<Indi> continuator(MAX_GEN);
|
||||
|
||||
// GENERATION
|
||||
/////////////////////////////////////////
|
||||
// the algorithm
|
||||
////////////////////////////////////////
|
||||
// standard Generational GA requires
|
||||
// selection, evaluation, crossover and mutation, stopping criterion
|
||||
|
||||
|
||||
eoSGA<Indi> gga(select, xover, CROSS_RATE, mutation, MUT_RATE,
|
||||
eval, continuator);
|
||||
|
||||
// Apply algo to pop - that's it!
|
||||
gga(pop);
|
||||
|
||||
// OUTPUT
|
||||
// Print (sorted) intial population
|
||||
pop.sort();
|
||||
cout << "FINAL Population\n" << pop << endl;
|
||||
// GENERAL
|
||||
}
|
||||
|
||||
// A main that catches the exceptions
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
// rng.reseed(42);
|
||||
int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
|
||||
flag |= _CRTDBG_LEAK_CHECK_DF;
|
||||
_CrtSetDbgFlag(flag);
|
||||
// _CrtSetBreakAlloc(100);
|
||||
#endif
|
||||
|
||||
try
|
||||
{
|
||||
main_function(argc, argv);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
12
eo/tutorial/Lesson1/Makefile
Normal file
12
eo/tutorial/Lesson1/Makefile
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
.cpp: ; g++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\" -I. -I../../src -Wall -g -o $@ $*.cpp ../../src/libeo.a ../../src/utils/libeoutils.a
|
||||
|
||||
.cpp.o: ; g++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\" -I. -I../../src -Wall -g -c $*.cpp
|
||||
|
||||
firstGA = FirstRealGA FirstBitGA
|
||||
|
||||
ALL = $(firstGA) exercise3
|
||||
|
||||
lesson1 : $(firstGA)
|
||||
|
||||
clean :
|
||||
@/bin/rm $(ALL) *.o *~
|
||||
165
eo/tutorial/Lesson1/exercise3.cpp
Normal file
165
eo/tutorial/Lesson1/exercise3.cpp
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
#include <stdexcept> // runtime_error
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// FirstBitGA.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
//*
|
||||
// An instance of a VERY simple Bitstring Genetic Algorithm
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
// standard includes
|
||||
|
||||
#include <iostream> // cout
|
||||
#include <strstream> // ostrstream, istrstream
|
||||
|
||||
// the general include for eo
|
||||
|
||||
#include <eo>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// define your individuals
|
||||
typedef eoBin<double> Indi; // A bitstring with fitness double
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/** a simple fitness function that computes the number of ones of a bitstring
|
||||
@param _indi A biststring individual
|
||||
*/
|
||||
|
||||
double binary_value(const Indi & _indi)
|
||||
{
|
||||
double sum = 0;
|
||||
for (unsigned i = 0; i < _indi.size(); i++)
|
||||
sum += _indi[i];
|
||||
return sum;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void main_function(int argc, char **argv)
|
||||
{
|
||||
const unsigned int SEED = 42; // seed for random number generator
|
||||
const unsigned int VEC_SIZE = 8; // Number of bits in genotypes
|
||||
const unsigned int POP_SIZE = 20; // Size of population
|
||||
const unsigned int MAX_GEN = 500; // Maximum number of generation before STOP
|
||||
const float CROSS_RATE = 0.8; // Crossover rate
|
||||
const double P_MUT_PER_BIT = 0.01; // probability of bit-flip mutation
|
||||
const float MUT_RATE = 1.0; // mutation rate
|
||||
|
||||
//////////////////////////
|
||||
// Random seed
|
||||
//////////////////////////
|
||||
//reproducible random seed: if you don't change SEED above,
|
||||
// you'll aways get the same result, NOT a random run
|
||||
rng.reseed(SEED);
|
||||
|
||||
/////////////////////////////
|
||||
// Fitness function
|
||||
////////////////////////////
|
||||
// Evaluation: from a plain C++ fn to an EvalFunc Object
|
||||
eoEvalFuncPtr<Indi> eval( binary_value );
|
||||
|
||||
////////////////////////////////
|
||||
// Initilisation of population
|
||||
////////////////////////////////
|
||||
|
||||
// declare the population
|
||||
eoPop<Indi> pop;
|
||||
// fill it!
|
||||
for (unsigned int igeno=0; igeno<POP_SIZE; igeno++)
|
||||
{
|
||||
Indi v; // void individual, to be filled
|
||||
for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)
|
||||
{
|
||||
bool r = rng.flip(); // new value, random in {0,1}
|
||||
v.push_back(r); // append that random value to v
|
||||
}
|
||||
eval(v); // evaluate it
|
||||
pop.push_back(v); // and put it in the population
|
||||
}
|
||||
|
||||
// sort pop before printing it!
|
||||
pop.sort();
|
||||
// Print (sorted) intial population (raw printout)
|
||||
cout << "Initial Population" << endl;
|
||||
cout << pop;
|
||||
|
||||
/////////////////////////////////////
|
||||
// selection and replacement
|
||||
////////////////////////////////////
|
||||
|
||||
// solution solution solution: uncomment one of the following,
|
||||
// comment out the eoDetTournament lines
|
||||
|
||||
// The well-known roulette
|
||||
// eoProportional<Indi> select;
|
||||
|
||||
// could also use stochastic binary tournament selection
|
||||
//
|
||||
// const double RATE = 0.75;
|
||||
// eoStochTournament<Indi> select(RATE); // RATE in ]0.5,1]
|
||||
// The robust tournament selection
|
||||
const unsigned int T_SIZE = 3; // size for tournament selection
|
||||
eoDetTournament<Indi> select(T_SIZE); // T_SIZE in [2,POP_SIZE]
|
||||
|
||||
// and of course the random selection
|
||||
// eoSelectRandom<Indi> select;
|
||||
|
||||
// The simple GA evolution engine uses generational replacement
|
||||
// so no replacement procedure is needed
|
||||
|
||||
//////////////////////////////////////
|
||||
// termination condition
|
||||
/////////////////////////////////////
|
||||
// stop after MAX_GEN generations
|
||||
eoGenContinue<Indi> continuator(MAX_GEN);
|
||||
|
||||
|
||||
//////////////////////////////////////
|
||||
// The variation operators
|
||||
//////////////////////////////////////
|
||||
// standard bit-flip mutation for bitstring
|
||||
eoBinMutation<Indi> mutation(P_MUT_PER_BIT);
|
||||
// 1-point mutation for bitstring
|
||||
eoBinCrossover<Indi> xover;
|
||||
|
||||
/////////////////////////////////////////
|
||||
// the algorithm
|
||||
////////////////////////////////////////
|
||||
// standard Generational GA requires as parameters
|
||||
// selection, evaluation, crossover and mutation, stopping criterion
|
||||
|
||||
|
||||
eoSGA<Indi> gga(select, xover, CROSS_RATE, mutation, MUT_RATE,
|
||||
eval, continuator);
|
||||
|
||||
// Apply algo to pop - that's it!
|
||||
gga(pop);
|
||||
|
||||
// Print (sorted) intial population
|
||||
pop.sort();
|
||||
cout << "FINAL Population\n" << pop << endl;
|
||||
}
|
||||
|
||||
// A main that catches the exceptions
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
// rng.reseed(42);
|
||||
int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
|
||||
flag |= _CRTDBG_LEAK_CHECK_DF;
|
||||
_CrtSetDbgFlag(flag);
|
||||
// _CrtSetBreakAlloc(100);
|
||||
#endif
|
||||
|
||||
try
|
||||
{
|
||||
main_function(argc, argv);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
193
eo/tutorial/Lesson2/FirstBitEA.cpp
Normal file
193
eo/tutorial/Lesson2/FirstBitEA.cpp
Normal file
|
|
@ -0,0 +1,193 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// FirstBitEA.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
//*
|
||||
// Still an instance of a VERY simple Bitstring Genetic Algorithm
|
||||
// (see FirstBitGA.cpp) but now with Breeder - and Combined Ops
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
// standard includes
|
||||
#include <stdexcept> // runtime_error
|
||||
#include <iostream> // cout
|
||||
#include <strstream> // ostrstream, istrstream
|
||||
|
||||
// the general include for eo
|
||||
#include <eo>
|
||||
|
||||
// REPRESENTATION
|
||||
//-----------------------------------------------------------------------------
|
||||
// define your individuals
|
||||
typedef eoBin<double> Indi; // A bitstring with fitness double
|
||||
|
||||
// EVALFUNC
|
||||
//-----------------------------------------------------------------------------
|
||||
// a simple fitness function that computes the number of ones of a bitstring
|
||||
// Now in a separate file, and declared as binary_value(const vector<bool> &)
|
||||
|
||||
#include "binary_value.h"
|
||||
|
||||
// GENERAL
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void main_function(int argc, char **argv)
|
||||
{
|
||||
// PARAMETRES
|
||||
const unsigned int SEED = 42; // seed for random number generator
|
||||
const unsigned int T_SIZE = 3; // size for tournament selection
|
||||
const unsigned int VEC_SIZE = 8; // Number of bits in genotypes
|
||||
const unsigned int POP_SIZE = 20; // Size of population
|
||||
|
||||
const unsigned int MAX_GEN = 500; // Maximum number of generation before STOP
|
||||
const unsigned int MIN_GEN = 10; // Minimum number of generation before ...
|
||||
const unsigned int STEADY_GEN = 50; // stop after STEADY_GEN gen. without improvelent
|
||||
|
||||
const double P_CROSS = 0.8; // Crossover probability
|
||||
const double P_MUT = 1.0; // mutation probability
|
||||
|
||||
const double P_MUT_PER_BIT = 0.01; // internal probability for bit-flip mutation
|
||||
// some parameters for chosing among different operators
|
||||
const double onePointRate = 0.5; // rate for 1-pt Xover
|
||||
const double twoPointsRate = 0.5; // rate for 2-pt Xover
|
||||
const double URate = 0.5; // rate for Uniform Xover
|
||||
const double bitFlipRate = 0.5; // rate for bit-flip mutation
|
||||
const double oneBitRate = 0.5; // rate for one-bit mutation
|
||||
|
||||
// GENERAL
|
||||
//////////////////////////
|
||||
// Random seed
|
||||
//////////////////////////
|
||||
//reproducible random seed: if you don't change SEED above,
|
||||
// you'll aways get the same result, NOT a random run
|
||||
rng.reseed(SEED);
|
||||
|
||||
// EVAL
|
||||
/////////////////////////////
|
||||
// Fitness function
|
||||
////////////////////////////
|
||||
// Evaluation: from a plain C++ fn to an EvalFunc Object
|
||||
// you need to give the full description of the function
|
||||
eoEvalFuncPtr<Indi, double, const vector<bool>& > eval( binary_value );
|
||||
|
||||
// INIT
|
||||
////////////////////////////////
|
||||
// Initilisation of population
|
||||
////////////////////////////////
|
||||
|
||||
// based on boolean_generator class (see utils/rnd_generator.h)
|
||||
eoInitFixedLength<Indi, boolean_generator>
|
||||
random(VEC_SIZE, boolean_generator());
|
||||
// Initialization of the population
|
||||
eoPop<Indi> pop(POP_SIZE, random);
|
||||
|
||||
// and evaluate it in one loop
|
||||
apply<Indi>(eval, pop); // STL syntax
|
||||
|
||||
// OUTPUT
|
||||
// sort pop before printing it!
|
||||
pop.sort();
|
||||
// Print (sorted) intial population (raw printout)
|
||||
cout << "Initial Population" << endl;
|
||||
cout << pop;
|
||||
|
||||
// ENGINE
|
||||
/////////////////////////////////////
|
||||
// selection and replacement
|
||||
////////////////////////////////////
|
||||
// SELECT
|
||||
// The robust tournament selection
|
||||
eoDetTournament<Indi> selectOne(T_SIZE); // T_SIZE in [2,POP_SIZE]
|
||||
// is now encapsulated in a eoSelectPerc (entage)
|
||||
eoSelectPerc<Indi> select(selectOne);// by default rate==1
|
||||
|
||||
// REPLACE
|
||||
// And we now have the full slection/replacement - though with
|
||||
// no replacement (== generational replacement) at the moment :-)
|
||||
eoNoReplacement<Indi> replace;
|
||||
|
||||
// OPERATORS
|
||||
//////////////////////////////////////
|
||||
// The variation operators
|
||||
//////////////////////////////////////
|
||||
// CROSSOVER
|
||||
// 1-point crossover for bitstring
|
||||
eoBinCrossover<Indi> xover1;
|
||||
// uniform crossover for bitstring
|
||||
eoBinUxOver<Indi> xoverU;
|
||||
// 2-pots xover
|
||||
eoBinNxOver<Indi> xover2(2);
|
||||
// Combine them with relative rates
|
||||
eoPropCombinedQuadOp<Indi> xover(xover1, onePointRate);
|
||||
xover.add(xoverU, URate);
|
||||
xover.add(xover2, twoPointsRate, true);
|
||||
|
||||
// MUTATION
|
||||
// standard bit-flip mutation for bitstring
|
||||
eoBinMutation<Indi> mutationBitFlip(P_MUT_PER_BIT);
|
||||
// mutate exactly 1 bit per individual
|
||||
eoDetBitFlip<Indi> mutationOneBit;
|
||||
// Combine them with relative rates
|
||||
eoPropCombinedMonOp<Indi> mutation(mutationBitFlip, bitFlipRate);
|
||||
mutation.add(mutationOneBit, oneBitRate, true);
|
||||
|
||||
// The operators are encapsulated into an eoTRansform object
|
||||
eoSGATransform<Indi> transform(xover, P_CROSS, mutation, P_MUT);
|
||||
|
||||
// STOP
|
||||
// CHECKPOINT
|
||||
//////////////////////////////////////
|
||||
// termination conditions: use more than one
|
||||
/////////////////////////////////////
|
||||
// stop after MAX_GEN generations
|
||||
eoGenContinue<Indi> genCont(MAX_GEN);
|
||||
// do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement
|
||||
eoSteadyFitContinue<Indi> steadyCont(MIN_GEN, STEADY_GEN);
|
||||
// stop when fitness reaches a target (here VEC_SIZE)
|
||||
eoFitContinue<Indi> fitCont(VEC_SIZE);
|
||||
// do stop when one of the above says so
|
||||
eoCombinedContinue<Indi> continuator(genCont);
|
||||
continuator.add(steadyCont);
|
||||
continuator.add(fitCont);
|
||||
|
||||
// GENERATION
|
||||
/////////////////////////////////////////
|
||||
// the algorithm
|
||||
////////////////////////////////////////
|
||||
|
||||
// Easy EA requires
|
||||
// selection, transformation, eval, replacement, and stopping criterion
|
||||
eoEasyEA<Indi> gga(continuator, eval, select, transform, replace);
|
||||
|
||||
// Apply algo to pop - that's it!
|
||||
cout << "\n Here we go\n\n";
|
||||
gga(pop);
|
||||
|
||||
// OUTPUT
|
||||
// Print (sorted) intial population
|
||||
pop.sort();
|
||||
cout << "FINAL Population\n" << pop << endl;
|
||||
// GENERAL
|
||||
}
|
||||
|
||||
// A main that catches the exceptions
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
// rng.reseed(42);
|
||||
int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
|
||||
flag |= _CRTDBG_LEAK_CHECK_DF;
|
||||
_CrtSetDbgFlag(flag);
|
||||
// _CrtSetBreakAlloc(100);
|
||||
#endif
|
||||
|
||||
try
|
||||
{
|
||||
main_function(argc, argv);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
188
eo/tutorial/Lesson2/FirstRealEA.cpp
Normal file
188
eo/tutorial/Lesson2/FirstRealEA.cpp
Normal file
|
|
@ -0,0 +1,188 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// FirstRealEA.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
//*
|
||||
// Still an instance of a VERY simple Real-coded Genetic Algorithm
|
||||
// (see FirstBitGA.cpp) but now with Breeder - and Combined Ops
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
// standard includes
|
||||
#include <stdexcept> // runtime_error
|
||||
#include <iostream> // cout
|
||||
#include <strstream> // ostrstream, istrstream
|
||||
|
||||
// the general include for eo
|
||||
#include <eo>
|
||||
|
||||
// REPRESENTATION
|
||||
//-----------------------------------------------------------------------------
|
||||
// define your individuals
|
||||
typedef eoReal<double> Indi;
|
||||
|
||||
// EVALFUNC
|
||||
//-----------------------------------------------------------------------------
|
||||
// a simple fitness function that computes the euclidian norm of a real vector
|
||||
// Now in a separate file, and declared as binary_value(const vector<bool> &)
|
||||
|
||||
#include "real_value.h"
|
||||
|
||||
// GENERAL
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void main_function(int argc, char **argv)
|
||||
{
|
||||
// PARAMETRES
|
||||
const unsigned int SEED = 42; // seed for random number generator
|
||||
const unsigned int T_SIZE = 3; // size for tournament selection
|
||||
const unsigned int VEC_SIZE = 8; // Number of object variables in genotypes
|
||||
const unsigned int POP_SIZE = 20; // Size of population
|
||||
|
||||
const unsigned int MAX_GEN = 500; // Maximum number of generation before STOP
|
||||
const unsigned int MIN_GEN = 10; // Minimum number of generation before ...
|
||||
const unsigned int STEADY_GEN = 50; // stop after STEADY_GEN gen. without improvelent
|
||||
|
||||
const float P_CROSS = 0.8; // Crossover probability
|
||||
const float P_MUT = 0.5; // mutation probability
|
||||
|
||||
const double EPSILON = 0.01; // range for real uniform mutation
|
||||
// some parameters for chosing among different operators
|
||||
const double segmentRate = 0.5; // rate for 1-pt Xover
|
||||
const double arithmeticRate = 0.5; // rate for 2-pt Xover
|
||||
const double uniformMutRate = 0.5; // rate for bit-flip mutation
|
||||
const double detMutRate = 0.5; // rate for one-bit mutation
|
||||
|
||||
// GENERAL
|
||||
//////////////////////////
|
||||
// Random seed
|
||||
//////////////////////////
|
||||
//reproducible random seed: if you don't change SEED above,
|
||||
// you'll aways get the same result, NOT a random run
|
||||
rng.reseed(SEED);
|
||||
|
||||
// EVAL
|
||||
/////////////////////////////
|
||||
// Fitness function
|
||||
////////////////////////////
|
||||
// Evaluation: from a plain C++ fn to an EvalFunc Object
|
||||
// you need to give the full description of the function
|
||||
eoEvalFuncPtr<Indi, double, const vector<double>& > eval( real_value );
|
||||
|
||||
// INIT
|
||||
////////////////////////////////
|
||||
// Initilisation of population
|
||||
////////////////////////////////
|
||||
// based on a uniform generator
|
||||
eoInitFixedLength<Indi, uniform_generator<double> >
|
||||
random(VEC_SIZE, uniform_generator<double>(-1.0, 1.0));
|
||||
// Initialization of the population
|
||||
eoPop<Indi> pop(POP_SIZE, random);
|
||||
|
||||
// and evaluate it in one loop
|
||||
apply<Indi>(eval, pop); // STL syntax
|
||||
|
||||
// OUTPUT
|
||||
// sort pop before printing it!
|
||||
pop.sort();
|
||||
// Print (sorted) intial population (raw printout)
|
||||
cout << "Initial Population" << endl;
|
||||
cout << pop;
|
||||
|
||||
// ENGINE
|
||||
/////////////////////////////////////
|
||||
// selection and replacement
|
||||
////////////////////////////////////
|
||||
// SELECT
|
||||
// The robust tournament selection
|
||||
eoDetTournament<Indi> selectOne(T_SIZE);
|
||||
// is now encapsulated in a eoSelectPerc (entage)
|
||||
eoSelectPerc<Indi> select(selectOne);// by default rate==1
|
||||
|
||||
// REPLACE
|
||||
// And we now have the full slection/replacement - though with
|
||||
// no replacement (== generational replacement) at the moment :-)
|
||||
eoNoReplacement<Indi> replace;
|
||||
|
||||
// OPERATORS
|
||||
//////////////////////////////////////
|
||||
// The variation operators
|
||||
//////////////////////////////////////
|
||||
// CROSSOVER
|
||||
// uniform chooce on segment made by the parents
|
||||
eoSegmentCrossover<Indi> xoverS;
|
||||
// uniform choice in hypercube built by the parents
|
||||
eoArithmeticCrossover<Indi> xoverA;
|
||||
// Combine them with relative rates
|
||||
eoPropCombinedQuadOp<Indi> xover(xoverS, segmentRate);
|
||||
xover.add(xoverA, arithmeticRate, true);
|
||||
|
||||
// MUTATION
|
||||
// offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]
|
||||
eoUniformMutation<Indi> mutationU(EPSILON);
|
||||
// k (=1) coordinates of parents are uniformly modified
|
||||
eoDetUniformMutation<Indi> mutationD(EPSILON);
|
||||
// Combine them with relative rates
|
||||
eoPropCombinedMonOp<Indi> mutation(mutationU, uniformMutRate);
|
||||
mutation.add(mutationD, detMutRate, true);
|
||||
|
||||
// STOP
|
||||
// CHECKPOINT
|
||||
//////////////////////////////////////
|
||||
// termination conditions: use more than one
|
||||
/////////////////////////////////////
|
||||
// stop after MAX_GEN generations
|
||||
eoGenContinue<Indi> genCont(MAX_GEN);
|
||||
// do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement
|
||||
eoSteadyFitContinue<Indi> steadyCont(MIN_GEN, STEADY_GEN);
|
||||
// stop when fitness reaches a target (here VEC_SIZE)
|
||||
eoFitContinue<Indi> fitCont(0);
|
||||
// do stop when one of the above says so
|
||||
eoCombinedContinue<Indi> continuator(genCont);
|
||||
continuator.add(steadyCont);
|
||||
continuator.add(fitCont);
|
||||
|
||||
// The operators are encapsulated into an eoTRansform object
|
||||
eoSGATransform<Indi> transform(xover, P_CROSS, mutation, P_MUT);
|
||||
|
||||
// GENERATION
|
||||
/////////////////////////////////////////
|
||||
// the algorithm
|
||||
////////////////////////////////////////
|
||||
|
||||
// Easy EA requires
|
||||
// selection, transformation, eval, replacement, and stopping criterion
|
||||
eoEasyEA<Indi> gga(continuator, eval, select, transform, replace);
|
||||
|
||||
// Apply algo to pop - that's it!
|
||||
cout << "\n Here we go\n\n";
|
||||
gga(pop);
|
||||
|
||||
// OUTPUT
|
||||
// Print (sorted) intial population
|
||||
pop.sort();
|
||||
cout << "FINAL Population\n" << pop << endl;
|
||||
// GENERAL
|
||||
}
|
||||
|
||||
// A main that catches the exceptions
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
// rng.reseed(42);
|
||||
int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
|
||||
flag |= _CRTDBG_LEAK_CHECK_DF;
|
||||
_CrtSetDbgFlag(flag);
|
||||
// _CrtSetBreakAlloc(100);
|
||||
#endif
|
||||
|
||||
try
|
||||
{
|
||||
main_function(argc, argv);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
12
eo/tutorial/Lesson2/Makefile
Normal file
12
eo/tutorial/Lesson2/Makefile
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
.cpp: ; g++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\" -I. -I../../src -Wall -g -o $@ $*.cpp ../../src/libeo.a ../../src/utils/libeoutils.a
|
||||
|
||||
.cpp.o: ; g++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\" -I. -I../../src -Wall -g -c $*.cpp
|
||||
|
||||
firstEA = FirstRealEA FirstBitEA
|
||||
|
||||
ALL = $(firstEA) exercise1 exercise2 exercise3
|
||||
|
||||
lesson2 : $(firstEA)
|
||||
|
||||
clean :
|
||||
@/bin/rm $(ALL) *.o *~
|
||||
17
eo/tutorial/Lesson2/binary_value.h
Normal file
17
eo/tutorial/Lesson2/binary_value.h
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#include <eo>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/** Just a simple function that takes binary value of a chromosome and sets
|
||||
the fitnes.
|
||||
@param _chrom A binary chromosome
|
||||
*/
|
||||
// INIT
|
||||
double binary_value(const vector<bool>& _chrom)
|
||||
{
|
||||
double sum = 0;
|
||||
for (unsigned i = 0; i < _chrom.size(); i++)
|
||||
sum += _chrom[i];
|
||||
return sum;
|
||||
}
|
||||
|
||||
195
eo/tutorial/Lesson2/exercise3.cpp
Normal file
195
eo/tutorial/Lesson2/exercise3.cpp
Normal file
|
|
@ -0,0 +1,195 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// FirstBitEA.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
//*
|
||||
// Still an instance of a VERY simple Bitstring Genetic Algorithm
|
||||
// (see FirstBitGA.cpp) but now with Breeder - and Combined Ops
|
||||
//
|
||||
//-----------------------------------------------------------------------------
|
||||
// standard includes
|
||||
#include <stdexcept> // runtime_error
|
||||
#include <iostream> // cout
|
||||
#include <strstream> // ostrstream, istrstream
|
||||
|
||||
// the general include for eo
|
||||
#include <eo>
|
||||
|
||||
// REPRESENTATION
|
||||
//-----------------------------------------------------------------------------
|
||||
// define your individuals
|
||||
typedef eoBin<double> Indi; // A bitstring with fitness double
|
||||
|
||||
// EVAL
|
||||
//-----------------------------------------------------------------------------
|
||||
// a simple fitness function that computes the number of ones of a bitstring
|
||||
// Now in a separate file, and declared as binary_value(const vector<bool> &)
|
||||
|
||||
#include "binary_value.h"
|
||||
|
||||
// GENERAL
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
void main_function(int argc, char **argv)
|
||||
{
|
||||
// PARAMETRES
|
||||
const unsigned int SEED = 42; // seed for random number generator
|
||||
const unsigned int T_SIZE = 3; // size for tournament selection
|
||||
const unsigned int VEC_SIZE = 8; // Number of bits in genotypes
|
||||
const unsigned int POP_SIZE = 20; // Size of population
|
||||
|
||||
const unsigned int MAX_GEN = 500; // Maximum number of generation before STOP
|
||||
const unsigned int MIN_GEN = 10; // Minimum number of generation before ...
|
||||
const unsigned int STEADY_GEN = 50; // stop after STEADY_GEN gen. without improvelent
|
||||
|
||||
const double P_CROSS = 0.8; // Crossover probability
|
||||
const double P_MUT = 1.0; // mutation probability
|
||||
|
||||
const double P_MUT_PER_BIT = 0.01; // internal probability for bit-flip mutation
|
||||
// some parameters for chosing among different operators
|
||||
const double onePointRate = 0.5; // rate for 1-pt Xover
|
||||
const double twoPointsRate = 0.5; // rate for 2-pt Xover
|
||||
const double URate = 0.5; // rate for Uniform Xover
|
||||
const double bitFlipRate = 0.5; // rate for bit-flip mutation
|
||||
const double oneBitRate = 0.5; // rate for one-bit mutation
|
||||
|
||||
// GENERAL
|
||||
//////////////////////////
|
||||
// Random seed
|
||||
//////////////////////////
|
||||
//reproducible random seed: if you don't change SEED above,
|
||||
// you'll aways get the same result, NOT a random run
|
||||
rng.reseed(SEED);
|
||||
|
||||
// EVAL
|
||||
/////////////////////////////
|
||||
// Fitness function
|
||||
////////////////////////////
|
||||
// Evaluation: from a plain C++ fn to an EvalFunc Object
|
||||
// you need to give the full description of the function
|
||||
eoEvalFuncPtr<Indi, double, const vector<bool>& > eval( binary_value );
|
||||
|
||||
// INIT
|
||||
////////////////////////////////
|
||||
// Initilisation of population
|
||||
////////////////////////////////
|
||||
|
||||
// based on boolean_generator class (see utils/rnd_generator.h)
|
||||
eoInitFixedLength<Indi, boolean_generator>
|
||||
random(VEC_SIZE, boolean_generator());
|
||||
// Initialization of the population
|
||||
eoPop<Indi> pop(POP_SIZE, random);
|
||||
|
||||
// and evaluate it in one loop
|
||||
apply<Indi>(eval, pop); // STL syntax
|
||||
|
||||
// OUTPUT
|
||||
// sort pop before printing it!
|
||||
pop.sort();
|
||||
// Print (sorted) intial population (raw printout)
|
||||
cout << "Initial Population" << endl;
|
||||
cout << pop;
|
||||
|
||||
// ENGINE
|
||||
/////////////////////////////////////
|
||||
// selection and replacement
|
||||
////////////////////////////////////
|
||||
// SELECT
|
||||
// The robust tournament selection
|
||||
eoDetTournament<Indi> selectOne(T_SIZE); // T_SIZE in [2,POP_SIZE]
|
||||
// solution solution solution solution solution solution solution
|
||||
// modify the rate in the constructor
|
||||
eoSelectPerc<Indi> select(selectOne,2.0);// rate is second arg.
|
||||
|
||||
// REPLACE
|
||||
// solution solution solution solution solution solution solution
|
||||
// eoCommaReplacement keeps the best among offspring
|
||||
// eoPlusReplacement keeps the best among parents + offspring
|
||||
// eoCommaReplacement<Indi> replace;
|
||||
eoPlusReplacement<Indi> replace;
|
||||
|
||||
// OPERATORS
|
||||
//////////////////////////////////////
|
||||
// The variation operators
|
||||
//////////////////////////////////////
|
||||
// CROSSOVER
|
||||
// 1-point crossover for bitstring
|
||||
eoBinCrossover<Indi> xover1;
|
||||
// uniform crossover for bitstring
|
||||
eoBinUxOver<Indi> xoverU;
|
||||
// 2-pots xover
|
||||
eoBinNxOver<Indi> xover2(2);
|
||||
// Combine them with relative rates
|
||||
eoPropCombinedQuadOp<Indi> xover(xover1, onePointRate);
|
||||
xover.add(xoverU, URate);
|
||||
xover.add(xover2, twoPointsRate, true);
|
||||
|
||||
// MUTATION
|
||||
// standard bit-flip mutation for bitstring
|
||||
eoBinMutation<Indi> mutationBitFlip(P_MUT_PER_BIT);
|
||||
// mutate exactly 1 bit per individual
|
||||
eoDetBitFlip<Indi> mutationOneBit;
|
||||
// Combine them with relative rates
|
||||
eoPropCombinedMonOp<Indi> mutation(mutationBitFlip, bitFlipRate);
|
||||
mutation.add(mutationOneBit, oneBitRate, true);
|
||||
|
||||
// The operators are encapsulated into an eoTRansform object
|
||||
eoSGATransform<Indi> transform(xover, P_CROSS, mutation, P_MUT);
|
||||
|
||||
// STOP
|
||||
// CHECKPOINT
|
||||
//////////////////////////////////////
|
||||
// termination conditions: use more than one
|
||||
/////////////////////////////////////
|
||||
// stop after MAX_GEN generations
|
||||
eoGenContinue<Indi> genCont(MAX_GEN);
|
||||
// do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement
|
||||
eoSteadyFitContinue<Indi> steadyCont(MIN_GEN, STEADY_GEN);
|
||||
// stop when fitness reaches a target (here VEC_SIZE)
|
||||
eoFitContinue<Indi> fitCont(VEC_SIZE);
|
||||
// do stop when one of the above says so
|
||||
eoCombinedContinue<Indi> continuator(genCont);
|
||||
continuator.add(steadyCont);
|
||||
continuator.add(fitCont);
|
||||
|
||||
// GENERATION
|
||||
/////////////////////////////////////////
|
||||
// the algorithm
|
||||
////////////////////////////////////////
|
||||
|
||||
// Easy EA requires
|
||||
// selection, transformation, eval, replacement, and stopping criterion
|
||||
eoEasyEA<Indi> gga(continuator, eval, select, transform, replace);
|
||||
|
||||
// Apply algo to pop - that's it!
|
||||
gga(pop);
|
||||
|
||||
// OUTPUT
|
||||
// Print (sorted) intial population
|
||||
pop.sort();
|
||||
cout << "FINAL Population\n" << pop << endl;
|
||||
// GENERAL
|
||||
}
|
||||
|
||||
// A main that catches the exceptions
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
// rng.reseed(42);
|
||||
int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
|
||||
flag |= _CRTDBG_LEAK_CHECK_DF;
|
||||
_CrtSetDbgFlag(flag);
|
||||
// _CrtSetBreakAlloc(100);
|
||||
#endif
|
||||
|
||||
try
|
||||
{
|
||||
main_function(argc, argv);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
18
eo/tutorial/Lesson2/real_value.h
Normal file
18
eo/tutorial/Lesson2/real_value.h
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#include <vector>
|
||||
//-----------------------------------------------------------------------------
|
||||
/** Just a simple function that takes an vector<double> and sets the fitnes
|
||||
to the sphere function. Please use doubles not float!!!
|
||||
@param _ind A floatingpoint vector
|
||||
*/
|
||||
|
||||
// INIT
|
||||
double real_value(const std::vector<double>& _ind)
|
||||
{
|
||||
double sum = 0;
|
||||
for (unsigned i = 0; i < _ind.size(); i++)
|
||||
sum += _ind[i] * _ind[i];
|
||||
return -sum;
|
||||
}
|
||||
|
||||
|
||||
|
||||
12
eo/tutorial/Lesson3/Makefile
Normal file
12
eo/tutorial/Lesson3/Makefile
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
.cpp: ; g++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\" -I. -I../../src -Wall -g -o $@ $*.cpp ../../src/libeo.a ../../src/utils/libeoutils.a
|
||||
|
||||
.cpp.o: ; g++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\" -I. -I../../src -Wall -g -c $*.cpp
|
||||
|
||||
secondEA = SecondBitEA
|
||||
|
||||
ALL = $(secondEA) exercise1 exercise2 exercise3
|
||||
|
||||
lesson3 : $(secondEA)
|
||||
|
||||
clean :
|
||||
@/bin/rm $(ALL) *.o *.sav *.xg *.status *~
|
||||
375
eo/tutorial/Lesson3/SecondBitEA.cpp
Normal file
375
eo/tutorial/Lesson3/SecondBitEA.cpp
Normal file
|
|
@ -0,0 +1,375 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// SecondGA.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
//*
|
||||
// Same code than FirstBitEA as far as Evolutionary Computation is concerned
|
||||
// but now you learn to enter the parameters in a more flexible way
|
||||
// and to twidle the output to your preferences!
|
||||
//-----------------------------------------------------------------------------
|
||||
// standard includes
|
||||
#include <stdexcept> // runtime_error
|
||||
#include <iostream> // cout
|
||||
#include <strstream> // ostrstream, istrstream
|
||||
#include <fstream>
|
||||
|
||||
// the general include for eo
|
||||
#include <eo>
|
||||
|
||||
// EVAL
|
||||
#include "binary_value.h"
|
||||
|
||||
// REPRESENTATION
|
||||
//-----------------------------------------------------------------------------
|
||||
// define your genotype and fitness types
|
||||
typedef eoBin<double> Indi;
|
||||
|
||||
// PARAMETRES
|
||||
//-----------------------------------------------------------------------------
|
||||
// instead of having all values of useful parameters as constants, read them:
|
||||
// either on the command line (--option=value or -o=value)
|
||||
// or in a parameter file (same syntax, order independent,
|
||||
// # = usual comment character
|
||||
// or in the environment (TODO)
|
||||
|
||||
// note that the parameters are passed by reference so they can be updated
|
||||
void read_param(int argc, char *argv[],
|
||||
uint32 & _seed,
|
||||
unsigned int & _vecSize,
|
||||
unsigned int & _popSize,
|
||||
unsigned int & _tSize,
|
||||
double & _pCross,
|
||||
double & _pMut,
|
||||
string & _load_name,
|
||||
unsigned int & _maxGen,
|
||||
unsigned int & _minGen,
|
||||
unsigned int & _steadyGen,
|
||||
double & _onePointRate,
|
||||
double & _twoPointsRate,
|
||||
double & _uRate,
|
||||
double & _pMutPerBit,
|
||||
double & _bitFlipRate,
|
||||
double & _oneBitRate
|
||||
)
|
||||
{
|
||||
// define a parser from the command-line arguments
|
||||
eoParser parser(argc, argv);
|
||||
|
||||
// For each parameter, define Parameters directly in the parser,
|
||||
// and assign the value to the variable
|
||||
eoValueParam<uint32>& seedParam = parser.createParam<uint32>(time(0), "seed", "Random number seed", 'S');
|
||||
_seed = seedParam.value();
|
||||
|
||||
eoValueParam<unsigned int>& vecSizeParam = parser.createParam<unsigned int>(8, "vecSize", "Genotype size",'V', "Representation");
|
||||
_vecSize = vecSizeParam.value();
|
||||
|
||||
eoValueParam<unsigned int>& popSizeParam = parser.createParam<unsigned int>(10, "popSize", "Population size",'P', "Evolution");
|
||||
_popSize = popSizeParam.value();
|
||||
|
||||
eoValueParam<unsigned int>& tSizeParam = parser.createParam<unsigned int>(10, "tSize", "Tournament size",'T', "Evolution");
|
||||
_tSize = tSizeParam.value();
|
||||
|
||||
eoValueParam<string>& load_nameParam = parser.createParam<string>("", "Load","A save file to restart from",'L', "Persistence");
|
||||
_load_name = load_nameParam.value();
|
||||
|
||||
eoValueParam<unsigned int>& maxGenParam = parser.createParam<unsigned int>(100, "maxGen", "Maximum number of generations",'G', "Stopping criterion");
|
||||
_maxGen = maxGenParam.value();
|
||||
|
||||
eoValueParam<unsigned int>& minGenParam = parser.createParam<unsigned int>(100, "minGen", "Minimum number of generations",'g', "Stopping criterion");
|
||||
_minGen = minGenParam.value();
|
||||
|
||||
eoValueParam<unsigned int>& steadyGenParam = parser.createParam<unsigned int>(100, "steadyGen", "Number of generations with no improvement",'s', "Stopping criterion");
|
||||
_steadyGen = steadyGenParam.value();
|
||||
|
||||
eoValueParam<double>& pCrossParam = parser.createParam<double>(0.6, "pCross", "Probability of Crossover", 'C', "Genetic Operators");
|
||||
_pCross = pCrossParam.value();
|
||||
|
||||
eoValueParam<double>& pMutParam = parser.createParam<double>(0.1, "pMut", "Probability of Mutation", 'M', "Genetic Operators");
|
||||
_pMut = pMutParam.value();
|
||||
|
||||
eoValueParam<double>& onePointRateParam = parser.createParam<double>(1, "onePointRate", "Relative rate for one point crossover", '1', "Genetic Operators");
|
||||
_onePointRate = onePointRateParam.value();
|
||||
|
||||
eoValueParam<double>& twoPointsRateParam = parser.createParam<double>(1, "twoPointRate", "Relative rate for two point crossover", '2', "Genetic Operators");
|
||||
_twoPointsRate = twoPointsRateParam.value();
|
||||
|
||||
eoValueParam<double>& uRateParam = parser.createParam<double>(2, "uRate", "Relative rate for uniform crossover", 'U', "Genetic Operators");
|
||||
_uRate = uRateParam.value();
|
||||
|
||||
eoValueParam<double>& pMutPerBitParam = parser.createParam<double>(0.01, "pMutPerBit", "Probability of flipping 1 bit in bit-flip mutation", 'b', "Genetic Operators");
|
||||
_pMutPerBit = pMutPerBitParam.value();
|
||||
|
||||
eoValueParam<double>& bitFlipRateParam = parser.createParam<double>(0.01, "bitFlipRate", "Relative rate for bit-flip mutation", 'B', "Genetic Operators");
|
||||
_bitFlipRate = bitFlipRateParam.value();
|
||||
|
||||
eoValueParam<double>& oneBitRateParam = parser.createParam<double>(0.01, "oneBitRate", "Relative rate for deterministic bit-flip mutation", 'D', "Genetic Operators");
|
||||
_oneBitRate = oneBitRateParam.value();
|
||||
|
||||
// the name of the "status" file where all actual parameter values will be saved
|
||||
string str_status = parser.ProgramName() + ".status";
|
||||
eoValueParam<string>& status_nameParam = parser.createParam<string>(str_status.c_str(), "status","Status file",'S', "Persistence");
|
||||
|
||||
// do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED
|
||||
// i.e. in case you need parameters somewhere else, postpone these
|
||||
if (parser.userNeedsHelp())
|
||||
{
|
||||
parser.printHelp(cout);
|
||||
exit(1);
|
||||
}
|
||||
if (status_nameParam.value() != "")
|
||||
{
|
||||
ofstream os(status_nameParam.value().c_str());
|
||||
os << parser; // and you can use that file as parameter file
|
||||
}
|
||||
}
|
||||
|
||||
// GENERAL
|
||||
// now the main_function: nothing changed, except input/output
|
||||
void main_function(int argc, char **argv)
|
||||
{
|
||||
// PARAMETRES
|
||||
uint32 seed;
|
||||
// decription of genotype
|
||||
unsigned int vecSize;
|
||||
// parameters for evolution engine
|
||||
unsigned int popSize;
|
||||
unsigned int tSize;
|
||||
// operators probabilities at the algorithm level
|
||||
double pCross;
|
||||
double pMut;
|
||||
// init and stop
|
||||
string load_name;
|
||||
unsigned int maxGen;
|
||||
unsigned int minGen;
|
||||
unsigned int steadyGen;
|
||||
// rates for crossovers
|
||||
double onePointRate;
|
||||
double twoPointsRate;
|
||||
double URate;
|
||||
// rates and private parameters for mutations;
|
||||
double pMutPerBit;
|
||||
double bitFlipRate;
|
||||
double oneBitRate;
|
||||
|
||||
// Now read the parameters of the program
|
||||
read_param(argc, argv, seed, vecSize, popSize, tSize,
|
||||
pCross, pMut, load_name, maxGen, minGen, steadyGen,
|
||||
onePointRate, twoPointsRate, URate,
|
||||
pMutPerBit, bitFlipRate, oneBitRate );
|
||||
|
||||
// EVAL
|
||||
/////////////////////////////
|
||||
// Fitness function
|
||||
////////////////////////////
|
||||
// Evaluation: from a plain C++ fn to an EvalFunc Object ...
|
||||
eoEvalFuncPtr<Indi, double, const vector<bool>& > plainEval( binary_value );
|
||||
// ... to an object that counts the nb of actual evaluations
|
||||
eoEvalFuncCounter<Indi> eval(plainEval);
|
||||
|
||||
// INIT
|
||||
////////////////////////////////
|
||||
// Initilisation of population
|
||||
////////////////////////////////
|
||||
// Either load or initialize
|
||||
// create an empty pop
|
||||
eoPop<Indi> pop;
|
||||
// create a state for reading
|
||||
eoState inState; // a state for loading - WITHOUT the parser
|
||||
// register the rng and the pop in the state, so they can be loaded,
|
||||
// and the present run will be the exact conitnuation of the saved run
|
||||
// eventually with different parameters
|
||||
inState.registerObject(rng);
|
||||
inState.registerObject(pop);
|
||||
|
||||
if (load_name != "")
|
||||
{
|
||||
inState.load(load_name); // load the pop and the rng
|
||||
// the fitness is read in the file:
|
||||
// do only evaluate the pop if the fitness has changed
|
||||
}
|
||||
else
|
||||
{
|
||||
rng.reseed(seed);
|
||||
// a Indi random initializer
|
||||
// based on boolean_generator class (see utils/rnd_generator.h)
|
||||
eoInitFixedLength<Indi, boolean_generator>
|
||||
random(vecSize, boolean_generator());
|
||||
|
||||
// Init pop from the randomizer: need to use the append function
|
||||
pop.append(popSize, random);
|
||||
// and evaluate pop (STL syntax)
|
||||
apply<Indi>(eval, pop);
|
||||
} // end of initializatio of the population
|
||||
|
||||
// OUTPUT
|
||||
// sort pop for pretty printout
|
||||
pop.sort();
|
||||
// Print (sorted) intial population (raw printout)
|
||||
cout << "Initial Population" << endl << pop << endl;
|
||||
|
||||
// ENGINE
|
||||
/////////////////////////////////////
|
||||
// selection and replacement
|
||||
////////////////////////////////////
|
||||
// SELECT
|
||||
// The robust tournament selection
|
||||
eoDetTournament<Indi> selectOne(tSize); // tSize in [2,POPSIZE]
|
||||
// is now encapsulated in a eoSelectPerc (entage)
|
||||
eoSelectPerc<Indi> select(selectOne);// by default rate==1
|
||||
|
||||
// REPLACE
|
||||
// And we now have the full slection/replacement - though with
|
||||
// no replacement (== generational replacement) at the moment :-)
|
||||
eoNoReplacement<Indi> replace;
|
||||
|
||||
// OPERATORS
|
||||
//////////////////////////////////////
|
||||
// The variation operators
|
||||
//////////////////////////////////////
|
||||
// CROSSOVER
|
||||
// 1-point crossover for bitstring
|
||||
eoBinCrossover<Indi> xover1;
|
||||
// uniform crossover for bitstring
|
||||
eoBinUxOver<Indi> xoverU;
|
||||
// 2-pots xover
|
||||
eoBinNxOver<Indi> xover2(2);
|
||||
// Combine them with relative rates
|
||||
eoPropCombinedQuadOp<Indi> xover(xover1, onePointRate);
|
||||
xover.add(xoverU, URate);
|
||||
xover.add(xover2, twoPointsRate, true);
|
||||
|
||||
// MUTATION
|
||||
// standard bit-flip mutation for bitstring
|
||||
eoBinMutation<Indi> mutationBitFlip(pMutPerBit);
|
||||
// mutate exactly 1 bit per individual
|
||||
eoDetBitFlip<Indi> mutationOneBit;
|
||||
// Combine them with relative rates
|
||||
eoPropCombinedMonOp<Indi> mutation(mutationBitFlip, bitFlipRate);
|
||||
mutation.add(mutationOneBit, oneBitRate, true);
|
||||
|
||||
// The operators are encapsulated into an eoTRansform object
|
||||
eoSGATransform<Indi> transform(xover, pCross, mutation, pMut);
|
||||
|
||||
// STOP
|
||||
//////////////////////////////////////
|
||||
// termination condition see FirstBitEA.cpp
|
||||
/////////////////////////////////////
|
||||
eoGenContinue<Indi> genCont(maxGen);
|
||||
eoSteadyFitContinue<Indi> steadyCont(minGen, steadyGen);
|
||||
eoFitContinue<Indi> fitCont(vecSize);
|
||||
eoCombinedContinue<Indi> continuator(genCont);
|
||||
continuator.add(steadyCont);
|
||||
continuator.add(fitCont);
|
||||
|
||||
|
||||
// CHECKPOINT
|
||||
// but now you want to make many different things every generation
|
||||
// (e.g. statistics, plots, ...).
|
||||
// the class eoCheckPoint is dedicated to just that:
|
||||
|
||||
// Declare a checkpoint (from a continuator: an eoCheckPoint
|
||||
// IS AN eoContinue and will be called in the loop of all algorithms)
|
||||
eoCheckPoint<Indi> checkpoint(continuator);
|
||||
|
||||
// Create a counter parameter
|
||||
eoValueParam<unsigned> generationCounter(0, "Gen.");
|
||||
|
||||
// Create an incrementor (sub-class of eoUpdater). Note that the
|
||||
// parameter's value is passed by reference,
|
||||
// so every time the incrementer is updated (every generation),
|
||||
// the data in generationCounter will change.
|
||||
eoIncrementor<unsigned> increment(generationCounter.value());
|
||||
|
||||
// Add it to the checkpoint,
|
||||
// so the counter is updated (here, incremented) every generation
|
||||
checkpoint.add(increment);
|
||||
|
||||
// now some statistics on the population:
|
||||
// Best fitness in population
|
||||
eoBestFitnessStat<Indi> bestStat;
|
||||
// Second moment stats: average and stdev
|
||||
eoSecondMomentStats<Indi> SecondStat;
|
||||
|
||||
// Add them to the checkpoint to get them called at the appropriate time
|
||||
checkpoint.add(bestStat);
|
||||
checkpoint.add(SecondStat);
|
||||
|
||||
// The Stdout monitor will print parameters to the screen ...
|
||||
eoStdoutMonitor monitor(false);
|
||||
|
||||
// when called by the checkpoint (i.e. at every generation)
|
||||
checkpoint.add(monitor);
|
||||
|
||||
// the monitor will output a series of parameters: add them
|
||||
monitor.add(generationCounter);
|
||||
monitor.add(eval); // because now eval is an eoEvalFuncCounter!
|
||||
monitor.add(bestStat);
|
||||
monitor.add(SecondStat);
|
||||
|
||||
// A file monitor: will print parameters to ... a File, yes, you got it!
|
||||
eoFileMonitor fileMonitor("stats.xg", " ");
|
||||
|
||||
// the checkpoint mechanism can handle multiple monitors
|
||||
checkpoint.add(fileMonitor);
|
||||
|
||||
// the fileMonitor can monitor parameters, too, but you must tell it!
|
||||
fileMonitor.add(generationCounter);
|
||||
fileMonitor.add(bestStat);
|
||||
fileMonitor.add(SecondStat);
|
||||
|
||||
// Last type of item the eoCheckpoint can handle: state savers:
|
||||
eoState outState;
|
||||
// Register the algorithm into the state (so it has something to save!!)
|
||||
outState.registerObject(rng);
|
||||
outState.registerObject(pop);
|
||||
|
||||
// and feed the state to state savers
|
||||
// save state every 100th generation
|
||||
eoCountedStateSaver stateSaver1(100, outState, "generation");
|
||||
// save state every 1 seconds
|
||||
eoTimedStateSaver stateSaver2(1, outState, "time");
|
||||
|
||||
// Don't forget to add the two savers to the checkpoint
|
||||
checkpoint.add(stateSaver1);
|
||||
checkpoint.add(stateSaver2);
|
||||
// and that's it for the (control and) output
|
||||
|
||||
// GENERATION
|
||||
/////////////////////////////////////////
|
||||
// the algorithm
|
||||
////////////////////////////////////////
|
||||
|
||||
// Easy EA requires
|
||||
// selection, transformation, eval, replacement, and stopping criterion
|
||||
eoEasyEA<Indi> gga(checkpoint, eval, select, transform, replace);
|
||||
|
||||
// Apply algo to pop - that's it!
|
||||
gga(pop);
|
||||
|
||||
// OUTPUT
|
||||
// Print (sorted) intial population
|
||||
pop.sort();
|
||||
cout << "FINAL Population\n" << pop << endl;
|
||||
// GENERAL
|
||||
}
|
||||
|
||||
// A main that catches the exceptions
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
|
||||
flag |= _CRTDBG_LEAK_CHECK_DF;
|
||||
_CrtSetDbgFlag(flag);
|
||||
// _CrtSetBreakAlloc(100);
|
||||
#endif
|
||||
|
||||
try
|
||||
{
|
||||
main_function(argc, argv);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
17
eo/tutorial/Lesson3/binary_value.h
Normal file
17
eo/tutorial/Lesson3/binary_value.h
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#include <eo>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/** Just a simple function that takes binary value of a chromosome and sets
|
||||
the fitnes.
|
||||
@param _chrom A binary chromosome
|
||||
*/
|
||||
// INIT
|
||||
double binary_value(const vector<bool>& _chrom)
|
||||
{
|
||||
double sum = 0;
|
||||
for (unsigned i = 0; i < _chrom.size(); i++)
|
||||
sum += _chrom[i];
|
||||
return sum;
|
||||
}
|
||||
|
||||
384
eo/tutorial/Lesson3/exercise1.cpp
Normal file
384
eo/tutorial/Lesson3/exercise1.cpp
Normal file
|
|
@ -0,0 +1,384 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// SecondBitGA.cpp
|
||||
//-----------------------------------------------------------------------------
|
||||
//*
|
||||
// Same code than FirstBitEA as far as Evolutionary Computation is concerned
|
||||
// but now you learn to enter the parameters in a more flexible way
|
||||
// and to twidle the output to your preferences!
|
||||
//-----------------------------------------------------------------------------
|
||||
// standard includes
|
||||
#include <stdexcept> // runtime_error
|
||||
#include <iostream> // cout
|
||||
#include <strstream> // ostrstream, istrstream
|
||||
#include <fstream>
|
||||
|
||||
// the general include for eo
|
||||
#include <eo>
|
||||
#include <utils/eoGnuplot1DMonitor.h>
|
||||
|
||||
// EVAL
|
||||
#include "binary_value.h"
|
||||
|
||||
// REPRESENTATION
|
||||
//-----------------------------------------------------------------------------
|
||||
// define your genotype and fitness types
|
||||
typedef eoBin<double> Indi;
|
||||
|
||||
// PARAMETRES
|
||||
//-----------------------------------------------------------------------------
|
||||
// instead of having all values of useful parameters as constants, read them:
|
||||
// either on the command line (--option=value or -o=value)
|
||||
// or in a parameter file (same syntax, order independent,
|
||||
// # = usual comment character
|
||||
// or in the environment (TODO)
|
||||
|
||||
// note that the parameters are passed by reference so they can be updated
|
||||
void read_param(int argc, char *argv[],
|
||||
uint32 & _seed,
|
||||
unsigned int & _vecSize,
|
||||
unsigned int & _popSize,
|
||||
unsigned int & _tSize,
|
||||
double & _pCross,
|
||||
double & _pMut,
|
||||
string & _load_name,
|
||||
unsigned int & _maxGen,
|
||||
unsigned int & _minGen,
|
||||
unsigned int & _steadyGen,
|
||||
double & _onePointRate,
|
||||
double & _twoPointsRate,
|
||||
double & _uRate,
|
||||
double & _pMutPerBit,
|
||||
double & _bitFlipRate,
|
||||
double & _oneBitRate
|
||||
)
|
||||
{
|
||||
// define a parser from the command-line arguments
|
||||
eoParser parser(argc, argv);
|
||||
|
||||
// For each parameter, define Parameters directly in the parser,
|
||||
// and assign the value to the variable
|
||||
eoValueParam<uint32>& seedParam = parser.createParam<uint32>(time(0), "seed", "Random number seed", 'S');
|
||||
_seed = seedParam.value();
|
||||
|
||||
eoValueParam<unsigned int>& vecSizeParam = parser.createParam<unsigned int>(8, "vecSize", "Genotype size",'V', "Representation");
|
||||
_vecSize = vecSizeParam.value();
|
||||
|
||||
eoValueParam<unsigned int>& popSizeParam = parser.createParam<unsigned int>(10, "popSize", "Population size",'P', "Evolution");
|
||||
_popSize = popSizeParam.value();
|
||||
|
||||
eoValueParam<unsigned int>& tSizeParam = parser.createParam<unsigned int>(10, "tSize", "Tournament size",'T', "Evolution");
|
||||
_tSize = tSizeParam.value();
|
||||
|
||||
eoValueParam<string>& load_nameParam = parser.createParam<string>("", "Load","A save file to restart from",'L', "Persistence");
|
||||
_load_name = load_nameParam.value();
|
||||
|
||||
eoValueParam<unsigned int>& maxGenParam = parser.createParam<unsigned int>(100, "maxGen", "Maximum number of generations",'G', "Stopping criterion");
|
||||
_maxGen = maxGenParam.value();
|
||||
|
||||
eoValueParam<unsigned int>& minGenParam = parser.createParam<unsigned int>(100, "minGen", "Minimum number of generations",'g', "Stopping criterion");
|
||||
_minGen = minGenParam.value();
|
||||
|
||||
eoValueParam<unsigned int>& steadyGenParam = parser.createParam<unsigned int>(100, "steadyGen", "Number of generations with no improvement",'s', "Stopping criterion");
|
||||
_steadyGen = steadyGenParam.value();
|
||||
|
||||
eoValueParam<double>& pCrossParam = parser.createParam<double>(0.6, "pCross", "Probability of Crossover", 'C', "Genetic Operators");
|
||||
_pCross = pCrossParam.value();
|
||||
|
||||
eoValueParam<double>& pMutParam = parser.createParam<double>(0.1, "pMut", "Probability of Mutation", 'M', "Genetic Operators");
|
||||
_pMut = pMutParam.value();
|
||||
|
||||
eoValueParam<double>& onePointRateParam = parser.createParam<double>(1, "onePointRate", "Relative rate for one point crossover", '1', "Genetic Operators");
|
||||
_onePointRate = onePointRateParam.value();
|
||||
|
||||
eoValueParam<double>& twoPointsRateParam = parser.createParam<double>(1, "twoPointRate", "Relative rate for two point crossover", '2', "Genetic Operators");
|
||||
_twoPointsRate = twoPointsRateParam.value();
|
||||
|
||||
eoValueParam<double>& uRateParam = parser.createParam<double>(2, "uRate", "Relative rate for uniform crossover", 'U', "Genetic Operators");
|
||||
_uRate = uRateParam.value();
|
||||
|
||||
eoValueParam<double>& pMutPerBitParam = parser.createParam<double>(0.01, "pMutPerBit", "Probability of flipping 1 bit in bit-flip mutation", 'b', "Genetic Operators");
|
||||
_pMutPerBit = pMutPerBitParam.value();
|
||||
|
||||
eoValueParam<double>& bitFlipRateParam = parser.createParam<double>(0.01, "bitFlipRate", "Relative rate for bit-flip mutation", 'B', "Genetic Operators");
|
||||
_bitFlipRate = bitFlipRateParam.value();
|
||||
|
||||
eoValueParam<double>& oneBitRateParam = parser.createParam<double>(0.01, "oneBitRate", "Relative rate for deterministic bit-flip mutation", 'D', "Genetic Operators");
|
||||
_oneBitRate = oneBitRateParam.value();
|
||||
|
||||
// the name of the "status" file where all actual parameter values will be saved
|
||||
string str_status = parser.ProgramName() + ".status";
|
||||
eoValueParam<string>& status_nameParam = parser.createParam<string>(str_status.c_str(), "status","Status file",'S', "Persistence");
|
||||
|
||||
// do the following AFTER ALL PARAMETERS HAVE BEEN PROCESSED
|
||||
// i.e. in case you need parameters somewhere else, postpone these
|
||||
if (parser.userNeedsHelp())
|
||||
{
|
||||
parser.printHelp(cout);
|
||||
exit(1);
|
||||
}
|
||||
if (status_nameParam.value() != "")
|
||||
{
|
||||
ofstream os(status_nameParam.value().c_str());
|
||||
os << parser; // and you can use that file as parameter file
|
||||
}
|
||||
}
|
||||
|
||||
// GENERAL
|
||||
// now the main_function: nothing changed, except input/output
|
||||
void main_function(int argc, char **argv)
|
||||
{
|
||||
// PARAMETRES
|
||||
uint32 seed;
|
||||
// decription of genotype
|
||||
unsigned int vecSize;
|
||||
// parameters for evolution engine
|
||||
unsigned int popSize;
|
||||
unsigned int tSize;
|
||||
// operators probabilities at the algorithm level
|
||||
double pCross;
|
||||
double pMut;
|
||||
// init and stop
|
||||
string load_name;
|
||||
unsigned int maxGen;
|
||||
unsigned int minGen;
|
||||
unsigned int steadyGen;
|
||||
// rates for crossovers
|
||||
double onePointRate;
|
||||
double twoPointsRate;
|
||||
double URate;
|
||||
// rates and private parameters for mutations;
|
||||
double pMutPerBit;
|
||||
double bitFlipRate;
|
||||
double oneBitRate;
|
||||
|
||||
// Now read the parameters of the program
|
||||
read_param(argc, argv, seed, vecSize, popSize, tSize,
|
||||
pCross, pMut, load_name, maxGen, minGen, steadyGen,
|
||||
onePointRate, twoPointsRate, URate,
|
||||
pMutPerBit, bitFlipRate, oneBitRate );
|
||||
|
||||
// EVAL
|
||||
/////////////////////////////
|
||||
// Fitness function
|
||||
////////////////////////////
|
||||
// Evaluation: from a plain C++ fn to an EvalFunc Object ...
|
||||
eoEvalFuncPtr<Indi, double, const vector<bool>& > plainEval( binary_value );
|
||||
// ... to an object that counts the nb of actual evaluations
|
||||
eoEvalFuncCounter<Indi> eval(plainEval);
|
||||
|
||||
// INIT
|
||||
////////////////////////////////
|
||||
// Initilisation of population
|
||||
////////////////////////////////
|
||||
// Either load or initialize
|
||||
// create an empty pop
|
||||
eoPop<Indi> pop;
|
||||
// create a state for reading
|
||||
eoState inState; // a state for loading - WITHOUT the parser
|
||||
// register the rng and the pop in the state, so they can be loaded,
|
||||
// and the present run will be the exact conitnuation of the saved run
|
||||
// eventually with different parameters
|
||||
inState.registerObject(rng);
|
||||
inState.registerObject(pop);
|
||||
|
||||
if (load_name != "")
|
||||
{
|
||||
inState.load(load_name); // load the pop and the rng
|
||||
// the fitness is read in the file:
|
||||
// do only evaluate the pop if the fitness has changed
|
||||
}
|
||||
else
|
||||
{
|
||||
rng.reseed(seed);
|
||||
// a Indi random initializer
|
||||
// based on boolean_generator class (see utils/rnd_generator.h)
|
||||
eoInitFixedLength<Indi, boolean_generator>
|
||||
random(vecSize, boolean_generator());
|
||||
|
||||
// Init pop from the randomizer: need to use the append function
|
||||
pop.append(popSize, random);
|
||||
// and evaluate pop (STL syntax)
|
||||
apply<Indi>(eval, pop);
|
||||
} // end of initializatio of the population
|
||||
|
||||
// OUTPUT
|
||||
// sort pop for pretty printout
|
||||
pop.sort();
|
||||
// Print (sorted) intial population (raw printout)
|
||||
cout << "Initial Population" << endl << pop << endl;
|
||||
|
||||
// ENGINE
|
||||
/////////////////////////////////////
|
||||
// selection and replacement
|
||||
////////////////////////////////////
|
||||
// SELECT
|
||||
// The robust tournament selection
|
||||
eoDetTournament<Indi> selectOne(tSize); // tSize in [2,POPSIZE]
|
||||
// is now encapsulated in a eoSelectPerc (entage)
|
||||
eoSelectPerc<Indi> select(selectOne);// by default rate==1
|
||||
|
||||
// REPLACE
|
||||
// And we now have the full slection/replacement - though with
|
||||
// no replacement (== generational replacement) at the moment :-)
|
||||
eoNoReplacement<Indi> replace;
|
||||
|
||||
// OPERATORS
|
||||
//////////////////////////////////////
|
||||
// The variation operators
|
||||
//////////////////////////////////////
|
||||
// CROSSOVER
|
||||
// 1-point crossover for bitstring
|
||||
eoBinCrossover<Indi> xover1;
|
||||
// uniform crossover for bitstring
|
||||
eoBinUxOver<Indi> xoverU;
|
||||
// 2-pots xover
|
||||
eoBinNxOver<Indi> xover2(2);
|
||||
// Combine them with relative rates
|
||||
eoPropCombinedQuadOp<Indi> xover(xover1, onePointRate);
|
||||
xover.add(xoverU, URate);
|
||||
xover.add(xover2, twoPointsRate, true);
|
||||
|
||||
// MUTATION
|
||||
// standard bit-flip mutation for bitstring
|
||||
eoBinMutation<Indi> mutationBitFlip(pMutPerBit);
|
||||
// mutate exactly 1 bit per individual
|
||||
eoDetBitFlip<Indi> mutationOneBit;
|
||||
// Combine them with relative rates
|
||||
eoPropCombinedMonOp<Indi> mutation(mutationBitFlip, bitFlipRate);
|
||||
mutation.add(mutationOneBit, oneBitRate, true);
|
||||
|
||||
// The operators are encapsulated into an eoTRansform object
|
||||
eoSGATransform<Indi> transform(xover, pCross, mutation, pMut);
|
||||
|
||||
// STOP
|
||||
//////////////////////////////////////
|
||||
// termination condition see FirstBitEA.cpp
|
||||
/////////////////////////////////////
|
||||
eoGenContinue<Indi> genCont(maxGen);
|
||||
eoSteadyFitContinue<Indi> steadyCont(minGen, steadyGen);
|
||||
eoFitContinue<Indi> fitCont(vecSize);
|
||||
eoCombinedContinue<Indi> continuator(genCont);
|
||||
continuator.add(steadyCont);
|
||||
continuator.add(fitCont);
|
||||
|
||||
|
||||
// CHECKPOINT
|
||||
// but now you want to make many different things every generation
|
||||
// (e.g. statistics, plots, ...).
|
||||
// the class eoCheckPoint is dedicated to just that:
|
||||
|
||||
// Declare a checkpoint (from a continuator: an eoCheckPoint
|
||||
// IS AN eoContinue and will be called in the loop of all algorithms)
|
||||
eoCheckPoint<Indi> checkpoint(continuator);
|
||||
|
||||
// Create a counter parameter
|
||||
eoValueParam<unsigned> generationCounter(0, "Gen.");
|
||||
|
||||
// Create an incrementor (sub-class of eoUpdater). Note that the
|
||||
// parameter's value is passed by reference,
|
||||
// so every time the incrementer is updated (every generation),
|
||||
// the data in generationCounter will change.
|
||||
eoIncrementor<unsigned> increment(generationCounter.value());
|
||||
|
||||
// Add it to the checkpoint,
|
||||
// so the counter is updated (here, incremented) every generation
|
||||
checkpoint.add(increment);
|
||||
|
||||
// now some statistics on the population:
|
||||
// Best fitness in population
|
||||
eoBestFitnessStat<Indi> bestStat;
|
||||
eoAverageStat<Indi> averageStat;
|
||||
// Second moment stats: average and stdev
|
||||
eoSecondMomentStats<Indi> SecondStat;
|
||||
|
||||
// Add them to the checkpoint to get them called at the appropriate time
|
||||
checkpoint.add(bestStat);
|
||||
checkpoint.add(averageStat);
|
||||
checkpoint.add(SecondStat);
|
||||
|
||||
// The Stdout monitor will print parameters to the screen ...
|
||||
eoStdoutMonitor monitor(false);
|
||||
|
||||
// when called by the checkpoint (i.e. at every generation)
|
||||
checkpoint.add(monitor);
|
||||
|
||||
// the monitor will output a series of parameters: add them
|
||||
monitor.add(generationCounter);
|
||||
monitor.add(eval); // because now eval is an eoEvalFuncCounter!
|
||||
monitor.add(bestStat);
|
||||
monitor.add(SecondStat);
|
||||
|
||||
// A file monitor: will print parameters to ... a File, yes, you got it!
|
||||
eoFileMonitor fileMonitor("stats.xg", " ");
|
||||
eoGnuplot1DMonitor gnuMonitor("best_average.xg");
|
||||
|
||||
// the checkpoint mechanism can handle multiple monitors
|
||||
checkpoint.add(fileMonitor);
|
||||
checkpoint.add(gnuMonitor);
|
||||
|
||||
// the fileMonitor can monitor parameters, too, but you must tell it!
|
||||
fileMonitor.add(generationCounter);
|
||||
fileMonitor.add(bestStat);
|
||||
fileMonitor.add(SecondStat);
|
||||
// the fileMonitor can monitor parameters, too, but you must tell it!
|
||||
gnuMonitor.add(eval);
|
||||
gnuMonitor.add(bestStat);
|
||||
gnuMonitor.add(averageStat);
|
||||
|
||||
// Last type of item the eoCheckpoint can handle: state savers:
|
||||
eoState outState;
|
||||
// Register the algorithm into the state (so it has something to save!!)
|
||||
outState.registerObject(rng);
|
||||
outState.registerObject(pop);
|
||||
|
||||
// and feed the state to state savers
|
||||
// save state every 100th generation
|
||||
eoCountedStateSaver stateSaver1(100, outState, "generation");
|
||||
// save state every 1 seconds
|
||||
eoTimedStateSaver stateSaver2(1, outState, "time");
|
||||
|
||||
// Don't forget to add the two savers to the checkpoint
|
||||
checkpoint.add(stateSaver1);
|
||||
checkpoint.add(stateSaver2);
|
||||
// and that's it for the (control and) output
|
||||
|
||||
// GENERATION
|
||||
/////////////////////////////////////////
|
||||
// the algorithm
|
||||
////////////////////////////////////////
|
||||
|
||||
// Easy EA requires
|
||||
// selection, transformation, eval, replacement, and stopping criterion
|
||||
eoEasyEA<Indi> gga(checkpoint, eval, select, transform, replace);
|
||||
|
||||
// Apply algo to pop - that's it!
|
||||
gga(pop);
|
||||
|
||||
// OUTPUT
|
||||
// Print (sorted) intial population
|
||||
pop.sort();
|
||||
cout << "FINAL Population\n" << pop << endl;
|
||||
// GENERAL
|
||||
}
|
||||
|
||||
// A main that catches the exceptions
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
|
||||
flag |= _CRTDBG_LEAK_CHECK_DF;
|
||||
_CrtSetDbgFlag(flag);
|
||||
// _CrtSetBreakAlloc(100);
|
||||
#endif
|
||||
|
||||
try
|
||||
{
|
||||
main_function(argc, argv);
|
||||
}
|
||||
catch(exception& e)
|
||||
{
|
||||
cout << "Exception: " << e.what() << '\n';
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
BIN
eo/tutorial/html/EA_tutorial.jpg
Normal file
BIN
eo/tutorial/html/EA_tutorial.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 52 KiB |
368
eo/tutorial/html/FirstBitEA.html
Normal file
368
eo/tutorial/html/FirstBitEA.html
Normal file
|
|
@ -0,0 +1,368 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="cpp2html Marc Schoenauer">
|
||||
<title>FirstBitEA.cpp</title>
|
||||
<!-- Changed by: Marc Schoenauer, 28-Nov-2000 -->
|
||||
</head>
|
||||
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">FirstBitEA.cpp</font></h1></center>
|
||||
Click on the figure to see the corresponding code.<br>
|
||||
In the code, the <a href="eoTutorial.html#colors">colors are meaningfull</a><br>
|
||||
The actual code is in boldface and the comment in normal face.
|
||||
<br><img SRC="EA_tutorial.jpg" USEMAP="#Map" >
|
||||
<map NAME="Map">
|
||||
<!-- Init -->
|
||||
<area SHAPE="rect" HREF="#init" COORDS="14,31,135,70">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="14,110,135,150">
|
||||
<!-- main loop -->
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="170,110,295,150">
|
||||
<area SHAPE="rect" HREF="#output" COORDS="280,45,480,70">
|
||||
<area SHAPE="rect" HREF="#stop" COORDS="348,110,430,150">
|
||||
<area SHAPE="rect" HREF="#select" COORDS="495,110,615,150">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="495,190,615,230">
|
||||
<area SHAPE="rect" HREF="#crossover" COORDS="495,265,625,287">
|
||||
<area SHAPE="rect" HREF="#mutation" COORDS="495,287,625,305">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="240,270,465,310">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="170,270,295,310">
|
||||
<area SHAPE="rect" HREF="#replace" COORDS="170,190,295,230">
|
||||
<!-- Center of loop -->
|
||||
<area SHAPE="rect" HREF="#generation" COORDS="310,160,485,260">
|
||||
<!-- 4 bottom lines -->
|
||||
<area SHAPE="rect" HREF="#operators" COORDS="15,350,260,370">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="270,350,460,370">
|
||||
<area SHAPE="rect" HREF="#engine" COORDS="15,377,400,397">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="15,403,230,423">
|
||||
<area SHAPE="rect" HREF="#checkpoint" COORDS="15,430,221,450">
|
||||
<area SHAPE="rect" HREF="#stop" COORDS="221,430,345,450">
|
||||
<area SHAPE="rect" HREF="#stat" COORDS="375,430,445,450">
|
||||
<area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378">
|
||||
</map>
|
||||
<br>
|
||||
<A NAME="start"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr NOSAVE>
|
||||
<td NOSAVE><tt><font color="#993300">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// FirstBitEA.cpp<br>
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
//*<br>
|
||||
// Still an instance of a VERY simple Bitstring Genetic Algorithm <br>
|
||||
// (see FirstBitGA.cpp) but now with Breeder - and Combined Ops<br>
|
||||
//<br>
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// standard includes<br>
|
||||
<b>#include <stdexcept> </b>// runtime_error <br>
|
||||
<b>#include <iostream> </b>// cout<br>
|
||||
<b>#include <strstream> </b>// ostrstream, istrstream<br>
|
||||
// the general include for eo<br>
|
||||
<b>#include <eo></b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="representation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFFFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#999900">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// define your individuals<br>
|
||||
<b>typedef eoBin<double> Indi; </b>// A bitstring with fitness double<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="evalfunc"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#CC0000">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// a simple fitness function that computes the number of ones of a bitstring<br>
|
||||
// Now in a separate file, and declared as binary_value(const vector<bool> &)<br>
|
||||
<b>#include "binary_value.h"</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993300">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
<b>void main_function(int argc, char **argv)</b><br>
|
||||
<b>{</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="parametres"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> const unsigned int SEED = 42; </b>// seed for random number generator<br>
|
||||
<b> const unsigned int T_SIZE = 3; </b>// size for tournament selection<br>
|
||||
<b> const unsigned int VEC_SIZE = 8; </b>// Number of bits in genotypes<br>
|
||||
<b> const unsigned int POP_SIZE = 20; </b>// Size of population<br>
|
||||
<b> const unsigned int MAX_GEN = 500; </b>// Maximum number of generation before STOP<br>
|
||||
<b> const float CROSS_RATE = 0.8; </b>// Crossover rate<br>
|
||||
<b> const double P_MUT_PER_BIT = 0.01; </b>// probability of bit-flip mutation<br>
|
||||
<b> const float MUT_RATE = 1.0; </b>// mutation rate<br>
|
||||
<b> </b>// some parameters for chosing among different operators<br>
|
||||
<b> const double onePointRate = 0.5; </b>// rate for 1-pt Xover<br>
|
||||
<b> const double twoPointsRate = 0.5; </b>// rate for 2-pt Xover<br>
|
||||
<b> const double URate = 0.5; </b>// rate for Uniform Xover<br>
|
||||
<b> const double bitFlipRate = 0.5; </b>// rate for bit-flip mutation<br>
|
||||
<b> const double oneBitRate = 0.5; </b>// rate for one-bit mutation<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993300">
|
||||
<b> </b>//////////////////////////<br>
|
||||
<b> </b>// Random seed<br>
|
||||
<b> </b>//////////////////////////<br>
|
||||
<b> </b>//reproducible random seed: if you don't change SEED above, <br>
|
||||
<b> </b>// you'll aways get the same result, NOT a random run<br>
|
||||
<b> rng.reseed(SEED);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="eval"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#CC0000">
|
||||
<b> </b>/////////////////////////////<br>
|
||||
<b> </b>// Fitness function<br>
|
||||
<b> </b>////////////////////////////<br>
|
||||
<b> </b>// Evaluation: from a plain C++ fn to an EvalFunc Object<br>
|
||||
<b> </b>// you need to give the full description of the function<br>
|
||||
<b> eoEvalFuncPtr<Indi, double, const vector<bool>& > eval( binary_value );</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="init"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>////////////////////////////////<br>
|
||||
<b> </b>// Initilisation of population<br>
|
||||
<b> </b>////////////////////////////////<br>
|
||||
<b> </b>// based on boolean_generator class (see utils/rnd_generator.h)<br>
|
||||
<b> eoInitFixedLength<Indi, boolean_generator> </b><br>
|
||||
<b> random(VEC_SIZE, boolean_generator());</b><br>
|
||||
<b> </b>// Initialization of the population<br>
|
||||
<b> eoPop<Indi> pop(POP_SIZE, random);</b><br>
|
||||
<b> </b>// and evaluate it in one loop<br>
|
||||
<b> apply<Indi>(eval, pop); </b>// STL syntax<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="output"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>// sort pop before printing it!<br>
|
||||
<b> pop.sort();</b><br>
|
||||
<b> </b>// Print (sorted) intial population (raw printout)<br>
|
||||
<b> cout << "Initial Population" << endl;</b><br>
|
||||
<b> cout << pop;</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="engine"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#009900">
|
||||
<b> </b>/////////////////////////////////////<br>
|
||||
<b> </b>// selection and replacement<br>
|
||||
<b> </b>////////////////////////////////////<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="select"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#009900">
|
||||
<b> </b>// The robust tournament selection<br>
|
||||
<b> eoDetTournament<Indi> selectOne(T_SIZE); </b>// T_SIZE in [2,POP_SIZE]<br>
|
||||
<a NAME="select_encapsulate"></a>
|
||||
<b> </b>// is now encapsulated in a eoSelectPerc (entage)<br>
|
||||
<b> eoSelectPerc<Indi> select(selectOne);</b>// by default rate==1<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="replace"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#009900">
|
||||
<b> </b>// And we now have the full slection/replacement - though with <br>
|
||||
<b> </b>// no replacement (== generational replacement) at the moment :-)<br>
|
||||
<b> eoNoReplacement<Indi> replace; </b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="operators"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
<b> </b>// The variation operators<br>
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="crossover"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>// 1-point crossover for bitstring<br>
|
||||
<b> eoBinCrossover<Indi> xover1;</b><br>
|
||||
<b> </b>// uniform crossover for bitstring<br>
|
||||
<b> eoBinUxOver<Indi> xoverU;</b><br>
|
||||
<b> </b>// 2-pots xover<br>
|
||||
<b> eoBinNxOver<Indi> xover2(2);</b><br>
|
||||
<b> </b>// Combine them with relative rates<br>
|
||||
<b> eoPropCombinedQuadOp<Indi> xover(xover1, onePointRate);</b><br>
|
||||
<b> xover.add(xoverU, URate);</b><br>
|
||||
<b> xover.add(xover2, twoPointsRate, true);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="mutation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b><br>
|
||||
<b> </b>// standard bit-flip mutation for bitstring<br>
|
||||
<b> eoBinMutation<Indi> mutationBitFlip(P_MUT_PER_BIT);</b><br>
|
||||
<b> </b>// mutate exactly 1 bit per individual<br>
|
||||
<b> eoDetBitFlip<Indi> mutationOneBit; </b><br>
|
||||
<b> </b>// Combine them with relative rates<br>
|
||||
<b> eoPropCombinedMonOp<Indi> mutation(mutationBitFlip, bitFlipRate);</b><br>
|
||||
<b> mutation.add(mutationOneBit, oneBitRate, true);</b><br>
|
||||
<b> </b><br>
|
||||
<a NAME="transform"></a>
|
||||
<b> </b>// The operators are encapsulated into an eoTRansform object<br>
|
||||
<b> eoSGATransform<Indi> transform(xover, CROSS_RATE, mutation, MUT_RATE);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="stop"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="checkpoint"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
<b> </b>// termination conditions: use more than one<br>
|
||||
<b> </b>/////////////////////////////////////<br>
|
||||
<b> </b>// stop after MAX_GEN generations<br>
|
||||
<b> eoGenContinue<Indi> genCont(MAX_GEN);</b><br>
|
||||
<b> </b>// do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement<br>
|
||||
<b> eoSteadyFitContinue<Indi> steadyCont(MIN_GEN, STEADY_GEN);</b><br>
|
||||
<b> </b>// stop when fitness reaches a target (here VEC_SIZE)<br>
|
||||
<b> eoFitContinue<Indi> fitCont(0);</b><br>
|
||||
<b> </b>// do stop when one of the above says so<br>
|
||||
<b> eoCombinedContinue<Indi> continuator(genCont);</b><br>
|
||||
<b> continuator.add(steadyCont);</b><br>
|
||||
<b> continuator.add(fitCont);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="generation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#FF6666">
|
||||
<b> </b>/////////////////////////////////////////<br>
|
||||
<b> </b>// the algorithm<br>
|
||||
<b> </b>////////////////////////////////////////<br>
|
||||
<b> </b>// Easy EA requires <br>
|
||||
<b> </b>// selection, transformation, eval, replacement, and stopping criterion<br>
|
||||
<b> eoEasyEA<Indi> gga(continuator, eval, select, transform, replace);</b><br>
|
||||
<b> </b>// Apply algo to pop - that's it!<br>
|
||||
<b> gga(pop);</b><br>
|
||||
<b> </b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="output"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>// Print (sorted) intial population<br>
|
||||
<b> pop.sort();</b><br>
|
||||
<b> cout << "FINAL Population\n" << pop << endl;</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993300">
|
||||
<b>}</b><br>
|
||||
// A main that catches the exceptions<br>
|
||||
<b>int main(int argc, char **argv)</b><br>
|
||||
<b>{</b><br>
|
||||
<b>#ifdef _MSC_VER</b><br>
|
||||
<b> </b>// rng.reseed(42);<br>
|
||||
<b> int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</b><br>
|
||||
<b> flag |= _CRTDBG_LEAK_CHECK_DF;</b><br>
|
||||
<b> _CrtSetDbgFlag(flag);</b><br>
|
||||
// _CrtSetBreakAlloc(100);<br>
|
||||
<b>#endif</b><br>
|
||||
<b> try</b><br>
|
||||
<b> {</b><br>
|
||||
<b> main_function(argc, argv);</b><br>
|
||||
<b> }</b><br>
|
||||
<b> catch(exception& e)</b><br>
|
||||
<b> {</b><br>
|
||||
<b> cout << "Exception: " << e.what() << '\n';</b><br>
|
||||
<b> }</b><br>
|
||||
<b> return 1;</b><br>
|
||||
<b>}</b><br>
|
||||
</font></font></font></td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr WIDTH="100%"><a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last
|
||||
modified: Sun Nov 19 22:26:27 2000
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
350
eo/tutorial/html/FirstBitGA.html
Normal file
350
eo/tutorial/html/FirstBitGA.html
Normal file
|
|
@ -0,0 +1,350 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="cpp2html Marc Schoenauer">
|
||||
<title>FirstBitGA.html</title>
|
||||
<!-- Changed by: Marc Schoenauer, 29-Nov-2000 -->
|
||||
</head>
|
||||
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">FirstBitGA.html</font></h1></center>
|
||||
Click on the figure to see the corresponding code.<br>
|
||||
In the code, the <a href="eoTutorial.html#colors">colors are meaningfull</a><br>
|
||||
The actual code is in boldface and the comment in normal face.
|
||||
<br><img SRC="EA_tutorial.jpg" USEMAP="#Map" >
|
||||
<map NAME="Map">
|
||||
<!-- Init -->
|
||||
<area SHAPE="rect" HREF="#init" COORDS="14,31,135,70">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="14,110,135,150">
|
||||
<!-- main loop -->
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="170,110,295,150">
|
||||
<area SHAPE="rect" HREF="#output" COORDS="280,45,480,70">
|
||||
<area SHAPE="rect" HREF="#stop" COORDS="348,110,430,150">
|
||||
<area SHAPE="rect" HREF="#select" COORDS="495,110,615,150">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="495,190,615,230">
|
||||
<area SHAPE="rect" HREF="#crossover" COORDS="495,265,625,287">
|
||||
<area SHAPE="rect" HREF="#mutation" COORDS="495,287,625,305">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="240,270,465,310">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="170,270,295,310">
|
||||
<area SHAPE="rect" HREF="#replace" COORDS="170,190,295,230">
|
||||
<!-- Center of loop -->
|
||||
<area SHAPE="rect" HREF="#generation" COORDS="310,160,485,260">
|
||||
<!-- 4 bottom lines -->
|
||||
<area SHAPE="rect" HREF="#operators" COORDS="15,350,260,370">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="270,350,460,370">
|
||||
<area SHAPE="rect" HREF="#engine" COORDS="15,377,400,397">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="15,403,230,423">
|
||||
<area SHAPE="rect" HREF="#checkpoint" COORDS="15,430,221,450">
|
||||
<area SHAPE="rect" HREF="#stop" COORDS="221,430,345,450">
|
||||
<area SHAPE="rect" HREF="#stat" COORDS="375,430,445,450">
|
||||
<area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378">
|
||||
</map>
|
||||
<br>
|
||||
<A NAME="start"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr NOSAVE>
|
||||
<td NOSAVE><tt><font color="#993300">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// FirstBitGA.cpp<br>
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
//*<br>
|
||||
// An instance of a VERY simple Bitstring Genetic Algorithm<br>
|
||||
//<br>
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// standard includes<br>
|
||||
<b>#include <stdexcept> </b>// runtime_error <br>
|
||||
<b>#include <iostream> </b>// cout<br>
|
||||
<b>#include <strstream> </b>// ostrstream, istrstream<br>
|
||||
// the general include for eo<br>
|
||||
<b>#include <eo></b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="representation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFFFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#999900">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// define your individuals<br>
|
||||
<b>typedef eoBin<double> Indi; </b>// A bitstring with fitness double<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#CC0000">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// a simple fitness function that computes the number of ones of a bitstring<br>
|
||||
// @param _indi A biststring individual<br>
|
||||
<a name="evalfunc"></a>
|
||||
<b>double binary_value(const Indi & _indi)</b><br>
|
||||
<b>{</b><br>
|
||||
<b> double sum = 0;</b><br>
|
||||
<b> for (unsigned i = 0; i < _indi.size(); i++)</b><br>
|
||||
<b> sum += _indi[i];</b><br>
|
||||
<b> return sum;</b><br>
|
||||
<b>}</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993300">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
<b>void main_function(int argc, char **argv)</b><br>
|
||||
<b>{</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="parametres"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>// all parameters are hard-coded!<br>
|
||||
<b> const unsigned int SEED = 42; </b>// seed for random number generator<br>
|
||||
<b> const unsigned int T_SIZE = 3; </b>// size for tournament selection<br>
|
||||
<b> const unsigned int VEC_SIZE = 8; </b>// Number of bits in genotypes<br>
|
||||
<b> const unsigned int POP_SIZE = 20; </b>// Size of population<br>
|
||||
<b> const unsigned int MAX_GEN = 100; </b>// Maximum number of generation before STOP<br>
|
||||
<b> const float CROSS_RATE = 0.8; </b>// Crossover rate<br>
|
||||
<b> const double P_MUT_PER_BIT = 0.01; </b>// probability of bit-flip mutation<br>
|
||||
<b> const float MUT_RATE = 1.0; </b>// mutation rate<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993300">
|
||||
<a NAME="random"></a>
|
||||
<b> </b>//////////////////////////<br>
|
||||
<b> </b>// Random seed<br>
|
||||
<b> </b>//////////////////////////<br>
|
||||
<b> </b>//reproducible random seed: if you don't change SEED above, <br>
|
||||
<b> </b>// you'll aways get the same result, NOT a random run<br>
|
||||
<b> rng.reseed(SEED);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="eval"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#CC0000">
|
||||
<b> </b>/////////////////////////////<br>
|
||||
<b> </b>// Fitness function<br>
|
||||
<b> </b>////////////////////////////<br>
|
||||
<b> </b>// Evaluation: from a plain C++ fn to an EvalFunc Object<br>
|
||||
<b> eoEvalFuncPtr<Indi> eval( binary_value );</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="init"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>////////////////////////////////<br>
|
||||
<b> </b>// Initilisation of population<br>
|
||||
<b> </b>////////////////////////////////<br>
|
||||
<b> </b>// declare the population<br>
|
||||
<b> eoPop<Indi> pop;</b><br>
|
||||
<b> </b>// fill it!<br>
|
||||
<b> for (unsigned int igeno=0; igeno<POP_SIZE; igeno++)</b><br>
|
||||
<b> {</b><br>
|
||||
<b> Indi v; </b>// void individual, to be filled<br>
|
||||
<b> for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)</b><br>
|
||||
<b> {</b><br>
|
||||
<b> bool r = rng.flip(); </b>// new value, random in {0,1}<br>
|
||||
<b> v.push_back(r); </b>// append that random value to v<br>
|
||||
<b> }</b><br>
|
||||
<b> eval(v); </b>// evaluate it<br>
|
||||
<b> pop.push_back(v); </b>// and put it in the population<br>
|
||||
<b> }</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="output"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>// sort pop before printing it!<br>
|
||||
<b> pop.sort();</b><br>
|
||||
<b> </b>// Print (sorted) intial population (raw printout)<br>
|
||||
<b> cout << "Initial Population" << endl;</b><br>
|
||||
<b> cout << pop;</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="engine"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#009900">
|
||||
<b> </b>/////////////////////////////////////<br>
|
||||
<b> </b>// selection and replacement<br>
|
||||
<b> </b>////////////////////////////////////<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="select"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#009900">
|
||||
<b> </b>// The robust tournament selection<br>
|
||||
<b> eoDetTournament<Indi> select(T_SIZE); </b>// T_SIZE in [2,POP_SIZE]<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="replace"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#009900">
|
||||
<b> </b>// The simple GA evolution engine uses generational replacement<br>
|
||||
<b> </b>// so no replacement procedure is needed<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="stop"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="operators"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
<b> </b>// The variation operators<br>
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="crossover"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>// 1-point mutation for bitstring<br>
|
||||
<b> eoBinCrossover<Indi> xover;</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="mutation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b><br>
|
||||
<b> </b>// standard bit-flip mutation for bitstring<br>
|
||||
<b> eoBinMutation<Indi> mutation(P_MUT_PER_BIT);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="checkpoint"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
<b> </b>// termination condition<br>
|
||||
<b> </b>/////////////////////////////////////<br>
|
||||
<b> </b>// stop after MAX_GEN generations<br>
|
||||
<b> eoGenContinue<Indi> continuator(MAX_GEN);</b><br>
|
||||
<b> </b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="generation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#FF6666">
|
||||
<b> </b>/////////////////////////////////////////<br>
|
||||
<b> </b>// the algorithm<br>
|
||||
<b> </b>////////////////////////////////////////<br>
|
||||
<b> </b>// standard Generational GA requires as parameters<br>
|
||||
<b> </b>// selection, evaluation, crossover and mutation, stopping criterion<br>
|
||||
<b> </b><br>
|
||||
<b> eoSGA<Indi> gga(select, xover, CROSS_RATE, mutation, MUT_RATE, </b><br>
|
||||
<b> eval, continuator);</b><br>
|
||||
<b> </b>// Apply algo to pop - that's it!<br>
|
||||
<b> gga(pop);</b><br>
|
||||
<b> </b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="final_output"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>// Print (sorted) intial population<br>
|
||||
<b> pop.sort();</b><br>
|
||||
<b> cout << "FINAL Population\n" << pop << endl;</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="main"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993300">
|
||||
<b>}</b><br>
|
||||
<b> </b>// A main that catches the exceptions<br>
|
||||
<b>int main(int argc, char **argv)</b><br>
|
||||
<b>{</b><br>
|
||||
<b>#ifdef _MSC_VER</b><br>
|
||||
<b> </b>// rng.reseed(42);<br>
|
||||
<b> int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</b><br>
|
||||
<b> flag |= _CRTDBG_LEAK_CHECK_DF;</b><br>
|
||||
<b> _CrtSetDbgFlag(flag);</b><br>
|
||||
// _CrtSetBreakAlloc(100);<br>
|
||||
<b>#endif</b><br>
|
||||
<b> try</b><br>
|
||||
<b> {</b><br>
|
||||
<b> main_function(argc, argv);</b><br>
|
||||
<b> }</b><br>
|
||||
<b> catch(exception& e)</b><br>
|
||||
<b> {</b><br>
|
||||
<b> cout << "Exception: " << e.what() << '\n';</b><br>
|
||||
<b> }</b><br>
|
||||
<b> return 1;</b><br>
|
||||
<b>}</b><br>
|
||||
</font></font></font></td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr WIDTH="100%"><a href="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last
|
||||
modified: Sun Nov 19 08:31:26 2000
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
366
eo/tutorial/html/FirstRealEA.html
Normal file
366
eo/tutorial/html/FirstRealEA.html
Normal file
|
|
@ -0,0 +1,366 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="cpp2html Marc Schoenauer">
|
||||
<title>FirstRealEA.cpp</title>
|
||||
</head>
|
||||
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">FirstRealEA.cpp</font></h1></center>
|
||||
Click on the figure to see the corresponding code.<br>
|
||||
In the code, the <a href="eoTutorial.html#colors">colors are meaningfull</a><br>
|
||||
The actual code is in boldface and the comment in normal face.
|
||||
<br><img SRC="EA_tutorial.jpg" USEMAP="#Map" >
|
||||
<map NAME="Map">
|
||||
<!-- Init -->
|
||||
<area SHAPE="rect" HREF="#init" COORDS="14,31,135,70">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="14,110,135,150">
|
||||
<!-- main loop -->
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="170,110,295,150">
|
||||
<area SHAPE="rect" HREF="#output" COORDS="280,45,480,70">
|
||||
<area SHAPE="rect" HREF="#stop" COORDS="348,110,430,150">
|
||||
<area SHAPE="rect" HREF="#select" COORDS="495,110,615,150">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="495,190,615,230">
|
||||
<area SHAPE="rect" HREF="#crossover" COORDS="495,265,625,287">
|
||||
<area SHAPE="rect" HREF="#mutation" COORDS="495,287,625,305">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="240,270,465,310">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="170,270,295,310">
|
||||
<area SHAPE="rect" HREF="#replace" COORDS="170,190,295,230">
|
||||
<!-- Center of loop -->
|
||||
<area SHAPE="rect" HREF="#generation" COORDS="310,160,485,260">
|
||||
<!-- 4 bottom lines -->
|
||||
<area SHAPE="rect" HREF="#operators" COORDS="15,350,260,370">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="270,350,460,370">
|
||||
<area SHAPE="rect" HREF="#engine" COORDS="15,377,400,397">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="15,403,230,423">
|
||||
<area SHAPE="rect" HREF="#checkpoint" COORDS="15,430,221,450">
|
||||
<area SHAPE="rect" HREF="#stop" COORDS="221,430,345,450">
|
||||
<area SHAPE="rect" HREF="#stat" COORDS="375,430,445,450">
|
||||
<area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378">
|
||||
</map>
|
||||
<br>
|
||||
<A NAME="start"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr NOSAVE>
|
||||
<td NOSAVE><tt><font color="#993300">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// FirstRealEA.cpp<br>
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
//*<br>
|
||||
// Still an instance of a VERY simple Real-coded Genetic Algorithm <br>
|
||||
// (see FirstBitGA.cpp) but now with Breeder - and Combined Ops<br>
|
||||
//<br>
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// standard includes<br>
|
||||
<b>#include <stdexcept> </b>// runtime_error <br>
|
||||
<b>#include <iostream> </b>// cout<br>
|
||||
<b>#include <strstream> </b>// ostrstream, istrstream<br>
|
||||
// the general include for eo<br>
|
||||
<b>#include <eo></b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="representation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFFFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#999900">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// define your individuals<br>
|
||||
<b>typedef eoReal<double> Indi; </b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="evalfunc"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#CC0000">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// a simple fitness function that computes the euclidian norm of a real vector<br>
|
||||
// Now in a separate file, and declared as binary_value(const vector<bool> &)<br>
|
||||
<b>#include "real_value.h"</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993300">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
<b>void main_function(int argc, char **argv)</b><br>
|
||||
<b>{</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="parametres"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> const unsigned int SEED = 42; </b>// seed for random number generator<br>
|
||||
<b> const unsigned int T_SIZE = 3; </b>// size for tournament selection<br>
|
||||
<b> const unsigned int VEC_SIZE = 8; </b>// Number of object variables in genotypes<br>
|
||||
<b> const unsigned int POP_SIZE = 20; </b>// Size of population<br>
|
||||
<b> const unsigned int MAX_GEN = 500; </b>// Maximum number of generation before STOP<br>
|
||||
<b> const unsigned int MIN_GEN = 10; </b>// Minimum number of generation before ...<br>
|
||||
<b> const unsigned int STEADY_GEN = 50; </b>// stop after STEADY_GEN gen. without improvelent<br>
|
||||
<b> const float P_CROSS = 0.8; </b>// Crossover probability<br>
|
||||
<b> const float P_MUT = 0.5; </b>// mutation probability<br>
|
||||
<b> const double EPSILON = 0.01; </b>// range for real uniform mutation<br>
|
||||
<b> </b>// some parameters for chosing among different operators<br>
|
||||
<b> const double segmentRate = 0.5; </b>// rate for 1-pt Xover<br>
|
||||
<b> const double arithmeticRate = 0.5; </b>// rate for 2-pt Xover<br>
|
||||
<b> const double uniformMutRate = 0.5; </b>// rate for bit-flip mutation<br>
|
||||
<b> const double detMutRate = 0.5; </b>// rate for one-bit mutation<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993300">
|
||||
<b> </b>//////////////////////////<br>
|
||||
<b> </b>// Random seed<br>
|
||||
<b> </b>//////////////////////////<br>
|
||||
<b> </b>//reproducible random seed: if you don't change SEED above, <br>
|
||||
<b> </b>// you'll aways get the same result, NOT a random run<br>
|
||||
<b> rng.reseed(SEED);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="eval"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#CC0000">
|
||||
<b> </b>/////////////////////////////<br>
|
||||
<b> </b>// Fitness function<br>
|
||||
<b> </b>////////////////////////////<br>
|
||||
<b> </b>// Evaluation: from a plain C++ fn to an EvalFunc Object<br>
|
||||
<b> </b>// you need to give the full description of the function<br>
|
||||
<b> eoEvalFuncPtr<Indi, double, const vector<double>& > eval( real_value );</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="init"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>////////////////////////////////<br>
|
||||
<b> </b>// Initilisation of population<br>
|
||||
<b> </b>////////////////////////////////<br>
|
||||
<b> </b>// based on a uniform generator<br>
|
||||
<b> eoInitFixedLength<Indi, uniform_generator<double> ></b><br>
|
||||
<b> random(VEC_SIZE, uniform_generator<double>(-1.0, 1.0));</b><br>
|
||||
<b> </b>// Initialization of the population<br>
|
||||
<b> eoPop<Indi> pop(POP_SIZE, random);</b><br>
|
||||
<b> </b><br>
|
||||
<b> </b>// and evaluate it in one loop<br>
|
||||
<b> apply<Indi>(eval, pop); </b>// STL syntax<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="output"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>// sort pop before printing it!<br>
|
||||
<b> pop.sort();</b><br>
|
||||
<b> </b>// Print (sorted) intial population (raw printout)<br>
|
||||
<b> cout << "Initial Population" << endl;</b><br>
|
||||
<b> cout << pop;</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="engine"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#009900">
|
||||
<b> </b>/////////////////////////////////////<br>
|
||||
<b> </b>// selection and replacement<br>
|
||||
<b> </b>////////////////////////////////////<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="select"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#009900">
|
||||
<b> </b>// The robust tournament selection<br>
|
||||
<b> eoDetTournament<Indi> selectOne(T_SIZE);</b><br>
|
||||
<a NAME="select_encapsulate"></a>
|
||||
<b> </b>// is now encapsulated in a eoSelectPerc (entage)<br>
|
||||
<b> eoSelectPerc<Indi> select(selectOne);</b>// by default rate==1<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="replace"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#009900">
|
||||
<b> </b>// And we now have the full slection/replacement - though with <br>
|
||||
<b> </b>// no replacement (== generational replacement) at the moment :-)<br>
|
||||
<b> eoNoReplacement<Indi> replace; </b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="operators"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
<b> </b>// The variation operators<br>
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="crossover"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>// uniform chooce on segment made by the parents<br>
|
||||
<b> eoSegmentCrossover<Indi> xoverS;</b><br>
|
||||
<b> </b>// uniform choice in hypercube built by the parents<br>
|
||||
<b> eoArithmeticCrossover<Indi> xoverA;</b><br>
|
||||
<b> </b>// Combine them with relative rates<br>
|
||||
<b> eoPropCombinedQuadOp<Indi> xover(xoverS, segmentRate);</b><br>
|
||||
<b> xover.add(xoverA, arithmeticRate, true);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="mutation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b><br>
|
||||
<b> </b>// offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]<br>
|
||||
<b> eoUniformMutation<Indi> mutationU(EPSILON); </b><br>
|
||||
<b> </b>// k (=1) coordinates of parents are uniformly modified<br>
|
||||
<b> eoDetUniformMutation<Indi> mutationD(EPSILON); </b><br>
|
||||
<b> </b>// Combine them with relative rates<br>
|
||||
<b> eoPropCombinedMonOp<Indi> mutation(mutationU, uniformMutRate);</b><br>
|
||||
<b> mutation.add(mutationD, detMutRate, true);</b><br>
|
||||
<b> </b><br>
|
||||
<b> </b>// The operators are encapsulated into an eoTRansform object<br>
|
||||
<b> eoSGATransform<Indi> transform(xover, P_CROSS, mutation, P_MUT);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="stop"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="checkpoint"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
<b> </b>// termination conditions: use more than one<br>
|
||||
<b> </b>/////////////////////////////////////<br>
|
||||
<b> </b>// stop after MAX_GEN generations<br>
|
||||
<b> eoGenContinue<Indi> genCont(MAX_GEN);</b><br>
|
||||
<b> </b>// do MIN_GEN gen., then stop after STEADY_GEN gen. without improvement<br>
|
||||
<b> eoSteadyFitContinue<Indi> steadyCont(MIN_GEN, STEADY_GEN);</b><br>
|
||||
<b> </b>// stop when fitness reaches a target (here VEC_SIZE)<br>
|
||||
<b> eoFitContinue<Indi> fitCont(0);</b><br>
|
||||
<b> </b>// do stop when one of the above says so<br>
|
||||
<b> eoCombinedContinue<Indi> continuator(genCont);</b><br>
|
||||
<b> continuator.add(steadyCont);</b><br>
|
||||
<b> continuator.add(fitCont);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="generation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#FF6666">
|
||||
<b> </b>/////////////////////////////////////////<br>
|
||||
<b> </b>// the algorithm<br>
|
||||
<b> </b>////////////////////////////////////////<br>
|
||||
<b> </b>// Easy EA requires <br>
|
||||
<b> </b>// selection, transformation, eval, replacement, and stopping criterion<br>
|
||||
<b> eoEasyEA<Indi> gga(continuator, eval, select, transform, replace);</b><br>
|
||||
<b> </b>// Apply algo to pop - that's it!<br>
|
||||
<b> cout << "\n Here we go\n\n";</b><br>
|
||||
<b> gga(pop);</b><br>
|
||||
<b> </b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="output"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>// Print (sorted) intial population<br>
|
||||
<b> pop.sort();</b><br>
|
||||
<b> cout << "FINAL Population\n" << pop << endl;</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993300">
|
||||
<b>}</b><br>
|
||||
// A main that catches the exceptions<br>
|
||||
<b>int main(int argc, char **argv)</b><br>
|
||||
<b>{</b><br>
|
||||
<b>#ifdef _MSC_VER</b><br>
|
||||
<b> </b>// rng.reseed(42);<br>
|
||||
<b> int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</b><br>
|
||||
<b> flag |= _CRTDBG_LEAK_CHECK_DF;</b><br>
|
||||
<b> _CrtSetDbgFlag(flag);</b><br>
|
||||
// _CrtSetBreakAlloc(100);<br>
|
||||
<b>#endif</b><br>
|
||||
<b> try</b><br>
|
||||
<b> {</b><br>
|
||||
<b> main_function(argc, argv);</b><br>
|
||||
<b> }</b><br>
|
||||
<b> catch(exception& e)</b><br>
|
||||
<b> {</b><br>
|
||||
<b> cout << "Exception: " << e.what() << '\n';</b><br>
|
||||
<b> }</b><br>
|
||||
<b> return 1;</b><br>
|
||||
<b>}</b><br>
|
||||
</font></font></font></td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr WIDTH="100%"><a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last
|
||||
modified: Wed Nov 29 07:38:36 2000
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
349
eo/tutorial/html/FirstRealGA.html
Normal file
349
eo/tutorial/html/FirstRealGA.html
Normal file
|
|
@ -0,0 +1,349 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="cpp2html Marc Schoenauer">
|
||||
<title>../FirstRealGA.html</title>
|
||||
<!-- Changed by: Marc Schoenauer, 29-Nov-2000 -->
|
||||
</head>
|
||||
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">../FirstRealGA.html</font></h1></center>
|
||||
Click on the figure to see the corresponding code.<br>
|
||||
In the code, the <a href="eoTutorial.html#colors">colors are meaningfull</a><br>
|
||||
The actual code is in boldface and the comment in normal face.
|
||||
<br><img SRC="EA_tutorial.jpg" USEMAP="#Map" >
|
||||
<map NAME="Map">
|
||||
<!-- Init -->
|
||||
<area SHAPE="rect" HREF="#init" COORDS="14,31,135,70">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="14,110,135,150">
|
||||
<!-- main loop -->
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="170,110,295,150">
|
||||
<area SHAPE="rect" HREF="#output" COORDS="280,45,480,70">
|
||||
<area SHAPE="rect" HREF="#stop" COORDS="348,110,430,150">
|
||||
<area SHAPE="rect" HREF="#select" COORDS="495,110,615,150">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="495,190,615,230">
|
||||
<area SHAPE="rect" HREF="#crossover" COORDS="495,265,625,287">
|
||||
<area SHAPE="rect" HREF="#mutation" COORDS="495,287,625,305">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="240,270,465,310">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="170,270,295,310">
|
||||
<area SHAPE="rect" HREF="#replace" COORDS="170,190,295,230">
|
||||
<!-- Center of loop -->
|
||||
<area SHAPE="rect" HREF="#generation" COORDS="310,160,485,260">
|
||||
<!-- 4 bottom lines -->
|
||||
<area SHAPE="rect" HREF="#operators" COORDS="15,350,260,370">
|
||||
<area SHAPE="rect" HREF="#representation" COORDS="270,350,460,370">
|
||||
<area SHAPE="rect" HREF="#engine" COORDS="15,377,400,397">
|
||||
<area SHAPE="rect" HREF="#eval" COORDS="15,403,230,423">
|
||||
<area SHAPE="rect" HREF="#checkpoint" COORDS="15,430,221,450">
|
||||
<area SHAPE="rect" HREF="#stop" COORDS="221,430,345,450">
|
||||
<area SHAPE="rect" HREF="#stat" COORDS="375,430,445,450">
|
||||
<area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378">
|
||||
</map>
|
||||
<br>
|
||||
<A NAME="start"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr NOSAVE>
|
||||
<td NOSAVE><tt><font color="#993300">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// FirstRealGA.cpp<br>
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
//*<br>
|
||||
// An instance of a VERY simple Real-coded Genetic Algorithm<br>
|
||||
//<br>
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// standard includes<br>
|
||||
<b>#include <stdexcept> </b>// runtime_error <br>
|
||||
<b>#include <iostream> </b>// cout<br>
|
||||
<b>#include <strstream> </b>// ostrstream, istrstream<br>
|
||||
// the general include for eo<br>
|
||||
<b>#include <eo></b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="representation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFFFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#999900">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// define your individuals<br>
|
||||
<b> typedef eoReal<double> Indi;</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#CC0000">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
// a simple fitness function that computes the euclidian norm of a real vector<br>
|
||||
// @param _indi A real-valued individual <br>
|
||||
<a name="evalfunc"></a>
|
||||
<b>double real_value(const Indi & _indi)</b><br>
|
||||
<b>{</b><br>
|
||||
<b> double sum = 0;</b><br>
|
||||
<b> for (unsigned i = 0; i < _indi.size(); i++)</b><br>
|
||||
<b> sum += _indi[i]*_indi[i];</b><br>
|
||||
<b> return (-sum); </b>// maximizing only<br>
|
||||
<b>}</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993300">
|
||||
//-----------------------------------------------------------------------------<br>
|
||||
<b>void main_function(int argc, char **argv)</b><br>
|
||||
<b>{</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="parametres"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>// all parameters are hard-coded!<br>
|
||||
<b> const unsigned int SEED = 42; </b>// seed for random number generator<br>
|
||||
<b> const unsigned int VEC_SIZE = 8; </b>// Number of object variables in genotypes<br>
|
||||
<b> const unsigned int POP_SIZE = 20; </b>// Size of population<br>
|
||||
<b> const unsigned int T_SIZE = 3; </b>// size for tournament selection<br>
|
||||
<b> const unsigned int MAX_GEN = 500; </b>// Maximum number of generation before STOP<br>
|
||||
<b> const float CROSS_RATE = 0.8; </b>// Crossover rate<br>
|
||||
<b> const double EPSILON = 0.01; </b>// range for real uniform mutation<br>
|
||||
<b> const float MUT_RATE = 0.5; </b>// mutation rate<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993300">
|
||||
<a NAME="random"></a>
|
||||
<b> </b>//////////////////////////<br>
|
||||
<b> </b>// Random seed<br>
|
||||
<b> </b>//////////////////////////<br>
|
||||
<b> </b>//reproducible random seed: if you don't change SEED above, <br>
|
||||
<b> </b>// you'll aways get the same result, NOT a random run<br>
|
||||
<b> rng.reseed(SEED);</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="eval"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#CC0000">
|
||||
<b> </b>/////////////////////////////<br>
|
||||
<b> </b>// Fitness function<br>
|
||||
<b> </b>////////////////////////////<br>
|
||||
<b> </b>// Evaluation: from a plain C++ fn to an EvalFunc Object<br>
|
||||
<b> eoEvalFuncPtr<Indi> eval( real_value );</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="init"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>////////////////////////////////<br>
|
||||
<b> </b>// Initilisation of population<br>
|
||||
<b> </b>////////////////////////////////<br>
|
||||
<b> </b>// declare the population<br>
|
||||
<b> eoPop<Indi> pop;</b><br>
|
||||
<b> </b>// fill it!<br>
|
||||
<b> for (unsigned int igeno=0; igeno<POP_SIZE; igeno++)</b><br>
|
||||
<b> {</b><br>
|
||||
<b> Indi v; </b>// void individual, to be filled<br>
|
||||
<b> for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)</b><br>
|
||||
<b> {</b><br>
|
||||
<b> double r = 2*rng.uniform() - 1; </b>// new value, random in [-1,1)<br>
|
||||
<b> v.push_back(r); </b>// append that random value to v<br>
|
||||
<b> }</b><br>
|
||||
<b> eval(v); </b>// evaluate it<br>
|
||||
<b> pop.push_back(v); </b>// and put it in the population<br>
|
||||
<b> }</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="output"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>// sort pop before printing it!<br>
|
||||
<b> pop.sort();</b><br>
|
||||
<b> </b>// Print (sorted) intial population (raw printout)<br>
|
||||
<b> cout << "Initial Population" << endl;</b><br>
|
||||
<b> cout << pop;</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="engine"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#009900">
|
||||
<b> </b>/////////////////////////////////////<br>
|
||||
<b> </b>// selection and replacement<br>
|
||||
<b> </b>////////////////////////////////////<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="select"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#009900">
|
||||
<b> </b>// The robust tournament selection<br>
|
||||
<b> eoDetTournament<Indi> select(T_SIZE); </b>// T_SIZE in [2,POP_SIZE]<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="replace"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#009900">
|
||||
<b> </b>// eoSGA uses generational replacement by default<br>
|
||||
<b> </b>// so no replacement procedure has to be given<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="stop"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="checkpoint"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
<b> </b>// termination condition<br>
|
||||
<b> </b>/////////////////////////////////////<br>
|
||||
<b> </b>// stop after MAX_GEN generations<br>
|
||||
<b> eoGenContinue<Indi> continuator(MAX_GEN);</b><br>
|
||||
<b> </b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="operators"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
<b> </b>// The variation operators<br>
|
||||
<b> </b>//////////////////////////////////////<br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="mutation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>// offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]<br>
|
||||
<b> eoUniformMutation<Indi> mutation(EPSILON); </b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="crossover"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993399">
|
||||
<b> </b>// offspring(i) is a linear combination of parent(i)<br>
|
||||
<b> eoArithmeticCrossover<Indi> xover;</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="generation"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#FF6666">
|
||||
<b> </b>/////////////////////////////////////////<br>
|
||||
<b> </b>// the algorithm<br>
|
||||
<b> </b>////////////////////////////////////////<br>
|
||||
<b> </b>// standard Generational GA requires<br>
|
||||
<b> </b>// selection, evaluation, crossover and mutation, stopping criterion<br>
|
||||
<b> </b><br>
|
||||
<b> eoSGA<Indi> gga(select, xover, CROSS_RATE, mutation, MUT_RATE, </b><br>
|
||||
<b> eval, continuator);</b><br>
|
||||
<b> </b>// Apply algo to pop - that's it!<br>
|
||||
<b> gga(pop);</b><br>
|
||||
<b> </b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="final_output"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#3366FF">
|
||||
<b> </b>// Print (sorted) intial population<br>
|
||||
<b> pop.sort();</b><br>
|
||||
<b> cout << "FINAL Population\n" << pop << endl;</b><br>
|
||||
</font></tt>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="main"></a><table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td>
|
||||
<tt><font color="#993300">
|
||||
<b>}</b><br>
|
||||
// A main that catches the exceptions<br>
|
||||
<b>int main(int argc, char **argv)</b><br>
|
||||
<b>{</b><br>
|
||||
<b>#ifdef _MSC_VER</b><br>
|
||||
<b> </b>// rng.reseed(42);<br>
|
||||
<b> int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</b><br>
|
||||
<b> flag |= _CRTDBG_LEAK_CHECK_DF;</b><br>
|
||||
<b> _CrtSetDbgFlag(flag);</b><br>
|
||||
// _CrtSetBreakAlloc(100);<br>
|
||||
<b>#endif</b><br>
|
||||
<b> try</b><br>
|
||||
<b> {</b><br>
|
||||
<b> main_function(argc, argv);</b><br>
|
||||
<b> }</b><br>
|
||||
<b> catch(exception& e)</b><br>
|
||||
<b> {</b><br>
|
||||
<b> cout << "Exception: " << e.what() << '\n';</b><br>
|
||||
<b> }</b><br>
|
||||
<b> return 1;</b><br>
|
||||
<b>}</b><br>
|
||||
</font></font></font></td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr WIDTH="100%"><a href="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last
|
||||
modified: Sun Nov 19 08:31:29 2000
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
278
eo/tutorial/html/FirstRealGA_old.html
Normal file
278
eo/tutorial/html/FirstRealGA_old.html
Normal file
|
|
@ -0,0 +1,278 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.51 [en] (X11; I; Linux 2.2.5-15 i686) [Netscape]">
|
||||
<title>First Real GA</title>
|
||||
</head>
|
||||
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">A First Real GA</font></h1></center>
|
||||
Click on the figure to see the corresponding code. Tutorial comments are
|
||||
in variable length fonts, after the code.
|
||||
<center>
|
||||
<p><img SRC="EA_tutorial.jpg" USEMAP="#Map" ></center>
|
||||
<map NAME="Map"><area SHAPE="rect" HREF="#init" COORDS="0,0,112,40"><area SHAPE="rect" HREF="#eval" COORDS="0,78,112,112"><area SHAPE="rect" HREF="#select" COORDS="395,70,507,107"><area SHAPE="rect" HREF="#replace" COORDS="145,144,253,182"><area SHAPE="rect" HREF="#crossover" COORDS="433,145,543,164"><area SHAPE="rect" HREF="#mutation" COORDS="433,164,543,182"><area SHAPE="rect" HREF="#eval" COORDS="218,217,325,252"><area SHAPE="rect" HREF="#output" COORDS="235,14,415,38"><area SHAPE="rect" HREF="#representation" COORDS="143,68,253,114"><area SHAPE="rect" HREF="#representation" COORDS="394,214,515,255"><area SHAPE="rect" HREF="#stop" COORDS="278,74,370,109"><area SHAPE="rect" HREF="#generation" COORDS="257,114,431,213"><!-- 4 lignes en bas --><area SHAPE="rect" HREF="#eval" COORDS="0,334,207,352"><area SHAPE="rect" HREF="#engine" COORDS="0,310,356,334"><area SHAPE="rect" HREF="#operators" COORDS="0,288,230,310"><area SHAPE="rect" HREF="#representation" COORDS="230,288,450,310"><area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378"></map>
|
||||
<p><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
|
||||
<p><font face="Courier New,Courier"><font color="#993300"><font size=+1>#include
|
||||
<stdexcept > // runtime_error</font></font></font>
|
||||
<p><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// FirstRealGA.cpp</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>//*</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// An instance of a
|
||||
VERY simple Real-coded Genetic Algorithm</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>//</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// standard includes</font></font>
|
||||
<p><font face="Courier New,Courier"><font color="#993300"><font size=+1>#include
|
||||
<iostream>// cout</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>#include
|
||||
<strstream>// ostrstream, istrstream</font></font></font>
|
||||
<p><font face="Courier New,Courier"><font size=+1>// the general include
|
||||
for eo</font></font>
|
||||
<p><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>#include
|
||||
<eo></font></font></font>
|
||||
<p><font face="Courier New,Courier"><font size=+1>// specific incluse,
|
||||
as Real is not (yet) in the standard eo source dir</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>#include
|
||||
"eoeal.h"</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>#include
|
||||
"eoRealOp.h"</font></font></font>
|
||||
<p><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// define your individual:</font></font>
|
||||
<p><a NAME="representation"></a><font face="Courier New,Courier"><font color="#FFFF00"><font size=+1>typedef
|
||||
eoReal<double> Indi;</font></font></font>
|
||||
<p>You say here that you will be handling <font face="Arial,Helvetica">arrays
|
||||
of doubles</font>, whose fitness is a double
|
||||
<br>Note that this makes Indi derive from <a href="eoProgramming.html#STL">STL
|
||||
class</a> <font color="#993300">vector<double></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>/** a simple fitness
|
||||
function that computes the euclidian norm of a real vector</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1> @param
|
||||
_indi A real-valued individual</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>*/</font></font>
|
||||
<p><a NAME="evalfunc"></a><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>double
|
||||
real_value(const Indi & _indi)</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>{</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>
|
||||
double sum = 0;</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>
|
||||
for (unsigned i = 0; i < _indi.size(); i++)</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>
|
||||
sum += _indi[i]*_indi[i];</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>
|
||||
return (-sum); // maximizing</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>}</font></font></font>
|
||||
<p>This simple function computes the sum of the squares of the variables
|
||||
(remember Indi is here a vector of doubles)
|
||||
<br><font face="Courier New,Courier"><font size=+1>//-----------------------------------------------------------------------------</font></font>
|
||||
<p><font face="Courier New,Courier"><font color="#993300"><font size=+1>void
|
||||
main_function(int argc, char **argv)</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>{</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
|
||||
unsigned int SEED = 42;// seed for random number generator</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
|
||||
unsigned int VEC_SIZE = 8; // Number of object variables in genotypes</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
|
||||
unsigned int POP_SIZE = 20; // Size of population</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
|
||||
unsigned int T_SIZE = 3; // size for tournament selection</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
|
||||
unsigned int MAX_GEN = 500; // Maximum number of generation before STOP</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
|
||||
float CROSS_RATE = 0.8; // Crossover rate</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
|
||||
double EPSILON = 0.01; // range for real uniform mutation</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>const
|
||||
float MUT_RATE = 0.5; // mutation rate</font></font></font>
|
||||
<br>
|
||||
<p><font face="Courier New,Courier"><font size=+1>//////////////////////////</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// Random seed</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>//////////////////////////</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>//reproducible random
|
||||
seed: if you don't change SEED above,</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// you'll aways get
|
||||
the same result, NOT a random run</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>rng.reseed(SEED);</font></font></font>
|
||||
<p><font face="Courier New,Courier"><font size=+1>/////////////////////////////</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// Fitness function</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>////////////////////////////</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// Evaluation: from
|
||||
a plain C++ fn to an EvalFunc Object</font></font>
|
||||
<br><a NAME="eval"></a><font face="Courier New,Courier"><font color="#CC0000"><font size=+1>eoEvalFuncPtr<Indi>
|
||||
eval( <a href="#evalfunc">real_value</a> );</font></font></font>
|
||||
<p>This encapsulate the C++ function real_value into a <a href="eoProgramming.html#functors">functor
|
||||
object</a>
|
||||
<p><font face="Courier New,Courier"><font size=+1>////////////////////////////////</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// Initilisation of
|
||||
population</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>////////////////////////////////</font></font>
|
||||
<p><font face="Courier New,Courier"><font size=+1>// declare the population</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>eoPop<Indi>
|
||||
pop;</font></font></font>
|
||||
<p>Declares a <font face="Arial,Helvetica"><a href="doc/html/class_eopop.html">population
|
||||
object</a>, </font>which is basically an <a href="eoProgramming.html#STL">STL</a>
|
||||
<tt><font color="#993300"><font size=+1>vector<Indi></font></font></tt>
|
||||
<p><font face="Courier New,Courier"><font size=+1>// fill it!</font></font>
|
||||
<br><a NAME="init"></a><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>for
|
||||
(unsigned int igeno=0; igeno<POP_SIZE; igeno++)</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>
|
||||
{</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>
|
||||
Indi v; // generate a random individual</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>
|
||||
for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>
|
||||
{</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>
|
||||
double r = 2*rng.uniform() - 1; // new value, random in [-1,1)</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>
|
||||
v.push_back(r); //</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>
|
||||
}</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>
|
||||
eval(v); // evaluate it</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>
|
||||
pop.push_back(v); // and put it in the population</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>
|
||||
}</font></font></font>
|
||||
<p>This initialization of the population is straightforward
|
||||
<br><font color="#CC33CC">rng.uniform()</font> generates a double <font face="Arial,Helvetica"><a href="doc/html/class_eorng.html">uniformly
|
||||
in [0,1)</a>,</font>
|
||||
<br><font color="#CC33CC">v.push_back</font> simply appends its argument
|
||||
at the end of <a href="eoProgramming.html#STL">STL</a> vector <font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>v</font></font></font>
|
||||
<br><font color="#CC33CC">eval(v)</font>evaluates individal <font color="#CC33CC">v</font>
|
||||
the <a href="eoProgramming.html#functors">functor way</a>, calling <font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>eval.operator()(v)</font></font></font>
|
||||
<p><font face="Courier New,Courier"><font size=+1>// sort pop before printing
|
||||
it!</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>pop.sort();</font></font></font>
|
||||
<p><font face="Courier New,Courier"><font size=+1>// Print (sorted) intial
|
||||
population (raw printout)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>cout
|
||||
<< "Initial Population" << endl;</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>cout
|
||||
<< pop;</font></font></font>
|
||||
<p>If you looked at <font face="Arial,Helvetica"><a href="doc/html/class_eopop.html">eoPop</a></font>
|
||||
inheritance diagram, you noticed that it derives from <font face="Arial,Helvetica"><a href="doc/html/class_eoprintable.html">eoPrintable</a>:</font>
|
||||
hence you can stream them using the << operator. Of course, Indis
|
||||
are <font face="Arial,Helvetica"><a href="doc/html/class_eo.html">EO objects</a>
|
||||
</font>, which also are eoPrintable, and the << operator for eoPop
|
||||
uses the << operator for Indi.
|
||||
<p><font face="Courier New,Courier"><font size=+1>/////////////////////////////////////</font></font>
|
||||
<br><a NAME="engine"></a><font face="Courier New,Courier"><font size=+1>//
|
||||
selection and replacement</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>////////////////////////////////////</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// The well-known roulette
|
||||
selection</font></font>
|
||||
<br><a NAME="select"></a><font face="Courier New,Courier"><font size=+1>//
|
||||
The robust tournament selection</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#009900"><font size=+1>eoDetTournament<Indi>
|
||||
select(T_SIZE); // SIZE in [2,POP_SIZE]</font></font></font>
|
||||
<p><a NAME="replace"></a><font face="Courier New,Courier"><font size=+1>//
|
||||
eoSGA uses <font color="#009900">generational replacement</font> by default</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// so no replacement
|
||||
procedure has to be given</font></font>
|
||||
<p><font face="Courier New,Courier"><font size=+1>//////////////////////////////////////</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// termination condition</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>/////////////////////////////////////</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// stop after MAX_GEN
|
||||
generations</font></font>
|
||||
<br><a NAME="stop"></a><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>eoGenContinue<Indi>
|
||||
continuator(MAX_GEN);</font></font></font>
|
||||
<p>This class is called <font face="Arial,Helvetica"><a href="doc/html/class_eogencontinue.html">eoGenContinue</a>
|
||||
</font>because the main loop of all <font face="Arial,Helvetica"><a href="doc/html/class_eoalgo.html">eoAlgo</a>
|
||||
</font>says ... while continuator(pop)
|
||||
<p><font face="Courier New,Courier"><font size=+1>//////////////////////////////////////</font></font>
|
||||
<br><a NAME="operators"></a><font face="Courier New,Courier"><font size=+1>//
|
||||
The variation operators</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>//////////////////////////////////////</font></font>
|
||||
<p><a NAME="mutation"></a><font face="Courier New,Courier"><font size=+1>//
|
||||
offspring(i) uniformly chosen in [parent(i)-epsilon, parent(i)+epsilon]</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>eoUniformMutation<Indi>
|
||||
mutation(EPSILON);</font></font></font>
|
||||
<br><a NAME="crossover"></a><font face="Courier New,Courier"><font size=+1>//
|
||||
offspring(i) is a linear combination of parent(i)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"><font size=+1>eoArithmeticCrossover<Indi>
|
||||
xover;</font></font></font>
|
||||
<p>The <a href="eoOperators.html">variation operators</a> are respectively
|
||||
an <font face="Arial,Helvetica"><a href="doc/html/class_eomonop.html">eoMonOp</a></font>
|
||||
(unary operator) and an <font face="Arial,Helvetica"><a href="doc/html/class_eoquadraticop.html">eoQuadOp</a></font>
|
||||
(binary operator that modifies both its arguments).
|
||||
<p><font face="Courier New,Courier"><font size=+1>/////////////////////////////////////////</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// the algorithm</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>////////////////////////////////////////</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// standard Generational
|
||||
GA requires</font></font>
|
||||
<br><font face="Courier New,Courier"><font size=+1>// selection, evaluation,
|
||||
crossover and mutation, stopping criterion</font></font>
|
||||
<br>
|
||||
<p><a NAME="generation"></a><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>eoSGA<Indi>
|
||||
gga(select, xover, CROSS_RATE, mutation, MUT_RATE,</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>eval,
|
||||
continuator);</font></font></font>
|
||||
<p><font face="Courier New,Courier"><font size=+1>// Apply algo to pop
|
||||
- that's it!</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666"><font size=+1>gga(pop);</font></font></font>
|
||||
<p>This simple algorithm of class eoSGA is a <a href="eoProgramming.html#functors">functor
|
||||
object</a>, and running the algorithm amounts to call the <font color="#993300">operator()</font>
|
||||
on the initial population!
|
||||
<p><font face="Courier New,Courier"><font size=+1>// Print (sorted) intial
|
||||
population (raw printout)</font></font>
|
||||
<br><a NAME="output"></a><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>pop.sort();</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#66FFFF"><font size=+1>cout
|
||||
<< "FINAL Population\n" << pop << endl;</font></font></font>
|
||||
<p>that's it - just print the final population and you're done!!!
|
||||
<p><font face="Courier New,Courier"><font size=+1>}</font></font>
|
||||
<p><font face="Courier New,Courier"><font color="#000000"><font size=+1>//
|
||||
A main that catches the exceptions - do not modify!</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>int
|
||||
main(int argc, char **argv)</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>{</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>#ifdef
|
||||
_MSC_VER</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>//
|
||||
rng.reseed(42);</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>int
|
||||
flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>flag
|
||||
|= _CRTDBG_LEAK_CHECK_DF;</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>_CrtSetDbgFlag(flag);</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>//
|
||||
_CrtSetBreakAlloc(100);</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>#endif</font></font></font>
|
||||
<p><font face="Courier New,Courier"><font color="#993300"><font size=+1>try</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>{</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>main_function(argc,
|
||||
argv);</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>}</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>catch(exception&
|
||||
e)</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>{</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>cout
|
||||
<< "Exception: " << e.what() << '\n';</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>}</font></font></font>
|
||||
<p><font face="Courier New,Courier"><font color="#993300"><font size=+1>return
|
||||
1;</font></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300"><font size=+1>}</font></font></font>
|
||||
<p>
|
||||
<hr WIDTH="100%"><a href="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Wed Nov 1 07:18:21 CET 2000 --><!-- hhmts start -->Last
|
||||
modified: Wed Nov 6 17:22:43 CET 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
287
eo/tutorial/html/Firstmerge.html
Normal file
287
eo/tutorial/html/Firstmerge.html
Normal file
|
|
@ -0,0 +1,287 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.75 [en] (X11; U; Linux 2.2.17-21mdksmp i686) [Netscape]">
|
||||
<title>Differences</title>
|
||||
</head>
|
||||
<body text="#000000" link="#0000EE" vlink="#551A8B" alink="#FF0000" background="beige009.jpg">
|
||||
<a href="eoLesson1.html">Back to Lesson 1</a> -
|
||||
<a href="eoTutorial.html">Tutorial
|
||||
main page</a> -
|
||||
<a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <a href="doc/html/index.html">EO
|
||||
documentation</a>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">FirstBitGA and FirstRealGA: differences</font></h1></center>
|
||||
Below is a comparison of the codes for both algorithms (comments have been
|
||||
removed).
|
||||
<br><b><font color="#FF6600">Warning</font></b>: the <font color="#FF6666">pink
|
||||
background</font> here denotes the differences, not the section of
|
||||
the algorithm, which is only recalled by the color of the text!
|
||||
<br>These differences are limited to
|
||||
<ul>
|
||||
<li>
|
||||
the declaration of <font color="#666600">the type of the genotypes,</font></li>
|
||||
|
||||
<li>
|
||||
the <font color="#990000">fitness function</font> (what did you expect
|
||||
:-),</li>
|
||||
|
||||
<li>
|
||||
the <font color="#CC33CC">initialization</font> (and if you look carefully,
|
||||
you'll find out that only a small part of the initialization is different,
|
||||
as both genotypes are eoFixedLength objects) and of course</li>
|
||||
|
||||
<li>
|
||||
the choice of <font color="#CC33CC">variation operators </font>(including
|
||||
the parameter for the mutation).</li>
|
||||
</ul>
|
||||
|
||||
<table BORDER=0 CELLPADDING=5 COLS=2 WIDTH="100%" BGCOLOR="#C3C2B4" >
|
||||
<tr>
|
||||
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#993300">#include
|
||||
<stdexcept ></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">#include <iostream></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">#include <strstream></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666">#include <eo></font></font></td>
|
||||
|
||||
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#993300">#include
|
||||
<stdexcept ></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">#include <iostream></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">#include <strstream></font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666">#include <eo></font></font></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#FFFF00">typedef
|
||||
eoReal<double> Indi;</font></font></td>
|
||||
|
||||
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#FFFF00">typedef
|
||||
eoBin<double> Indi;</font></font></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC0000">double
|
||||
real_value(const Indi & _indi)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000">{</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"> double
|
||||
sum = 0;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"> for (unsigned
|
||||
i = 0; i < _indi.size(); i++)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000">
|
||||
sum += _indi[i]*_indi[i];</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"> return
|
||||
(-sum); // maximizing</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000">}</font></font></td>
|
||||
|
||||
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC0000">double
|
||||
binary_value(const Indi & _indi)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000">{</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"> double
|
||||
sum = 0;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"> for (unsigned
|
||||
i = 0; i < _indi.size(); i++)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000">
|
||||
sum += _indi[i];</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000"> return
|
||||
(sum);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC0000">}</font></font></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#993300">void
|
||||
main_function(int argc, char **argv)</font></font>
|
||||
<br><font face="Courier New,Courier">{</font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">const unsigned
|
||||
int SEED = 42;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">const unsigned
|
||||
int VEC_SIZE = 8;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">const unsigned
|
||||
int POP_SIZE = 20;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">const unsigned
|
||||
int T_SIZE = 3;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">const unsigned
|
||||
int MAX_GEN = 500;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">const float
|
||||
CROSS_RATE = 0.8;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">const float
|
||||
MUT_RATE = 0.5;</font></font></td>
|
||||
|
||||
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#993300">void
|
||||
main_function(int argc, char **argv)</font></font>
|
||||
<br><font face="Courier New,Courier">{</font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">const unsigned
|
||||
int SEED = 42;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">const unsigned
|
||||
int VEC_SIZE = 8;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">const unsigned
|
||||
int POP_SIZE = 20;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">const unsigned
|
||||
int T_SIZE = 3;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">const unsigned
|
||||
int MAX_GEN = 500;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">const float
|
||||
CROSS_RATE = 0.8;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">const float
|
||||
MUT_RATE = 0.5;</font></font></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#3366FF">const
|
||||
double EPSILON = 0.01;</font></font></td>
|
||||
|
||||
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#3366FF">const
|
||||
double P_MUT_PER_BIT = 0.01;</font></font></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC0000">eoEvalFuncPtr<Indi>
|
||||
eval(real_value);</font></font></td>
|
||||
|
||||
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC0000">eoEvalFuncPtr<Indi>
|
||||
eval(binary_value);</font></font></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#CC33CC">eoPop<Indi>
|
||||
pop;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">for (unsigned
|
||||
int igeno=0; igeno<POP_SIZE; igeno++)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"> {</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
Indi v;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
{</font></font></td>
|
||||
|
||||
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#CC33CC">eoPop<Indi>
|
||||
pop;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">for (unsigned
|
||||
int igeno=0; igeno<POP_SIZE; igeno++)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"> {</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
Indi v;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
for (unsigned ivar=0; ivar<VEC_SIZE; ivar++)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
{</font></font></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
double r = 2*rng.uniform() - 1;</font></font></td>
|
||||
|
||||
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
bool r = rng.flip(); </font></font></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
v.push_back(r); //</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
}</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
eval(v);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
pop.push_back(v);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"> }</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">pop.sort();</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">cout <<
|
||||
"Initial Population" << endl;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">cout <<
|
||||
pop;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#009900">eoDetTournament<Indi>
|
||||
select(T_SIZE);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">eoGenContinue<Indi>
|
||||
continuator(MAX_GEN);</font></font></td>
|
||||
|
||||
<td BGCOLOR="#C3C2B4"><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
v.push_back(r); //</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
}</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
eval(v);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">
|
||||
pop.push_back(v);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC"> }</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">pop.sort();</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">cout <<
|
||||
"Initial Population" << endl;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">cout <<
|
||||
pop;</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#009900">eoDetTournament<Indi>
|
||||
select(T_SIZE);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">eoGenContinue<Indi>
|
||||
continuator(MAX_GEN);</font></font></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC33CC">eoUniformMutation<Indi>
|
||||
mutation(EPSILON);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">eoArithmeticCrossover<Indi>
|
||||
xover;</font></font></td>
|
||||
|
||||
<td BGCOLOR="#F0C6B7"><font face="Courier New,Courier"><font color="#CC33CC">eoBinMutation<Indi>
|
||||
mutation(P_MUT_PER_BIT);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#CC33CC">eoBinCrossover<Indi>
|
||||
xover;</font></font></td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td BGCOLOR="#C3C2B4">
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666">eoSGA<Indi>
|
||||
gga(select, xover, CROSS_RATE,</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666">
|
||||
mutation, MUT_RATE, eval, continuator);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666">gga(pop);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">pop.sort();</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">cout <<
|
||||
"FINAL Population\n" << pop << endl;</font></font>
|
||||
<br><font face="Courier New,Courier">}</font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">int main(int
|
||||
argc, char **argv)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">{</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">... [technical
|
||||
code removed]</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">}</font></font></td>
|
||||
|
||||
<td BGCOLOR="#C3C2B4">
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666">eoSGA<Indi>
|
||||
gga(select, xover, CROSS_RATE,</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666">
|
||||
mutation, MUT_RATE,</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666">eval, continuator);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#FF6666">gga(pop);</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">pop.sort();</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#3366FF">cout <<
|
||||
"FINAL Population\n" << pop << endl;</font></font>
|
||||
<br><font face="Courier New,Courier">}</font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">int main(int
|
||||
argc, char **argv)</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">{</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">[... technical
|
||||
code removed ]</font></font>
|
||||
<br><font face="Courier New,Courier"><font color="#993300">}</font></font></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<hr WIDTH="100%"><a href="eoLesson1.html">Back to Lesson 1</a> -
|
||||
<a href="eoTutorial.html">Tutorial
|
||||
main page</a> -
|
||||
<a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <a href="doc/html/index.html">EO
|
||||
documentation</a>
|
||||
<br>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Tue Nov 7 07:04:15 CET 2000 --><!-- hhmts start -->Last
|
||||
modified: Tue Nov 7 07:49:47 CET 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
74
eo/tutorial/html/NoWay.html
Normal file
74
eo/tutorial/html/NoWay.html
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.75 [en] (X11; U; Linux 2.2.17-21mdksmp i686) [Netscape]">
|
||||
<title>Tutorial: Solutions</title>
|
||||
</head>
|
||||
<body text="#000000" link="#0000EE" vlink="#551A8B" alink="#FF0000" background="beige009.jpg">
|
||||
<a href="eoTutorial.html">Tutorial main page</a> -
|
||||
<a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> -<font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
|
||||
documentation</a></font></font>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">Tutorial:</font></h1></center>
|
||||
|
||||
<center><b><font color="#FF0000"><font size=+2>Solutions of exercises</font></font></b></center>
|
||||
|
||||
<p>No, <b><font color="#33CC00">w</font><font color="#FF9900">e </font><font color="#CC66CC">w</font><font color="#33CCFF">o</font><font color="#FFFF00">n</font><font color="#FF0000">'t</font><font color="#999999">
|
||||
p</font><font color="#999900">r</font><font color="#CC6600">o</font><font color="#993399">v</font><font color="#006600">i</font><font color="#990000">d</font><font color="#867CE9">e
|
||||
</font></b><font color="#000000">any
|
||||
hypertext link directly to the correct code, so</font> you get a chance
|
||||
to find out the solution by yourself without immediately going there
|
||||
:-)))
|
||||
<p><b><font color="#000099"><font size=+2>What you should do:</font></font></b>
|
||||
<ul>
|
||||
<li>
|
||||
copy the code of an example onto another name (e.g. <b><tt><font color="#660000">mytest.cpp</font></tt></b>)</li>
|
||||
|
||||
<li>
|
||||
edit and modify <b><tt><font color="#660000">mytest.cpp</font></tt></b>
|
||||
according to the corresponding instructions of the exercise</li>
|
||||
|
||||
<li>
|
||||
compile <b><tt><font color="#660000">mytest.cpp</font></tt></b> by typing
|
||||
<b><tt><font color="#FF6666">make mytest</font></tt></b> at system prompt,
|
||||
and you will hopefully get an executable file named <b><tt><font color="#660000">mytest</font></tt></b>
|
||||
(for Unix systems: if eventually someone tells me how to do that in Windows,
|
||||
apart that you'll probably end up with an executable file named <b><tt><font color="#660000">mytest.exe...</font></tt></b>).</li>
|
||||
</ul>
|
||||
|
||||
<hr WIDTH="100%">
|
||||
<br><b><font color="#000099"><font size=+2>What you may do later:</font></font></b>
|
||||
<p>All solutions to exercises are in the same<b><font color="#FF6600">
|
||||
sub-dir</font></b> of the Tutorial directory than the example files (i.e.
|
||||
Lesson1 for Lesson1, yes!). Hence you may browse through the code, eventually
|
||||
modifying it, and then simply type, in the corresponding directory,
|
||||
<br><b><tt><font color="#FF6666">... % make exerciseN</font></tt></b>
|
||||
<br>which will compile file <b><tt><font color="#660000">exerciseN.cpp</font></tt></b>
|
||||
into executable <b><tt><font color="#660000">exerciseN</font></tt></b>
|
||||
(for Unix systems: if eventually someone tells me how to do that in Windows,
|
||||
you'll probably end up with an executable file named <b><tt><font color="#660000">exerciseN.exe</font></tt></b>).
|
||||
<p>
|
||||
<hr WIDTH="100%">
|
||||
<br><b><font color="#000099"><font size=+2>What you may not do:</font></font></b>
|
||||
<center>Complain that it does not work under Windows :-)</center>
|
||||
|
||||
<hr WIDTH="100%"><a href="eoTutorial.html">Tutorial main page</a> -
|
||||
<a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> - <font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
|
||||
documentation</a></font></font>
|
||||
<br>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:Marc.Schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Fri Nov 3 18:49:12 CET 2000 --><!-- hhmts start -->Last
|
||||
modified: Fri Nov 3 18:49:12 CET 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
612
eo/tutorial/html/SecondBitEA.html
Normal file
612
eo/tutorial/html/SecondBitEA.html
Normal file
|
|
@ -0,0 +1,612 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.75 [en] (X11; U; Linux 2.2.17-21mdk i686) [Netscape]">
|
||||
<title>SecondBitEA.cpp</title>
|
||||
<!-- Changed by: Marc Schoenauer, 29-Nov-2000 -->
|
||||
</head>
|
||||
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoLesson3.html">Back to Lesson 3</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">SecondBitEA.cpp</font></h1></center>
|
||||
Click on the figure to see the corresponding code.
|
||||
<br>In the code, the <a href="eoTutorial.html#colors">colors are meaningfull</a>
|
||||
<br>The actual code is in boldface and the comment in normal face.
|
||||
<p>For this particular program, as all new lines are concerned with program
|
||||
parameter input and information output, the <font color="#FF6600">font
|
||||
color</font> for program-parameter-related sections will <font color="#FF6600">refer
|
||||
to the section where the parameters are used</font> whereas the background
|
||||
color will remain blue.
|
||||
<p><img SRC="EA_tutorial.jpg" USEMAP="#Map" ><map NAME="Map"><!-- Init --><area SHAPE="rect" HREF="#init" COORDS="14,31,135,70"><area SHAPE="rect" HREF="#eval" COORDS="14,110,135,150"><!-- main loop --><area SHAPE="rect" HREF="#representation" COORDS="170,110,295,150"><area SHAPE="rect" HREF="#output" COORDS="280,45,480,70"><area SHAPE="rect" HREF="#stop" COORDS="348,110,430,150"><area SHAPE="rect" HREF="#select" COORDS="495,110,615,150"><area SHAPE="rect" HREF="#representation" COORDS="495,190,615,230"><area SHAPE="rect" HREF="#crossover" COORDS="495,265,625,287"><area SHAPE="rect" HREF="#mutation" COORDS="495,287,625,305"><area SHAPE="rect" HREF="#representation" COORDS="240,270,465,310"><area SHAPE="rect" HREF="#eval" COORDS="170,270,295,310"><area SHAPE="rect" HREF="#replace" COORDS="170,190,295,230"><!-- Center of loop --><area SHAPE="rect" HREF="#generation" COORDS="310,160,485,260"><!-- 4 bottom lines --><area SHAPE="rect" HREF="#operators" COORDS="15,350,260,370"><area SHAPE="rect" HREF="#representation" COORDS="270,350,460,370"><area SHAPE="rect" HREF="#engine" COORDS="15,377,400,397"><area SHAPE="rect" HREF="#eval" COORDS="15,403,230,423"><area SHAPE="rect" HREF="#checkpoint" COORDS="15,430,221,450"><area SHAPE="rect" HREF="#stop" COORDS="221,430,345,450"><area SHAPE="rect" HREF="#stat" COORDS="375,430,445,450"><area SHAPE="rect" HREF="#parametres" COORDS="0,358,278,378"></map>
|
||||
<br><a NAME="start"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr NOSAVE>
|
||||
<td NOSAVE><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
|
||||
<br><tt><font color="#993300">// SecondGA.cpp</font></tt>
|
||||
<br><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
|
||||
<br><tt><font color="#993300">//*</font></tt>
|
||||
<br><tt><font color="#993300">// Same code than FirstBitEA as far as Evolutionary
|
||||
Computation is concerned</font></tt>
|
||||
<br><tt><font color="#993300">// but now you learn to enter the parameters
|
||||
in a more flexible way</font></tt>
|
||||
<br><tt><font color="#993300">// and to twidle the output to your preferences!</font></tt>
|
||||
<br><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
|
||||
<br><tt><font color="#993300">// standard includes</font></tt>
|
||||
<br><tt><font color="#993300"><b>#include <stdexcept> </b>// runtime_error </font></tt>
|
||||
<br><tt><font color="#993300"><b>#include <iostream> </b>//
|
||||
cout</font></tt>
|
||||
<br><tt><font color="#993300"><b>#include <strstream> </b>// ostrstream,
|
||||
istrstream</font></tt>
|
||||
<br><b><tt><font color="#993300">#include <fstream></font></tt></b>
|
||||
<br><tt><font color="#993300">// the general include for eo</font></tt>
|
||||
<br><b><tt><font color="#993300">#include <eo></font></tt></b>
|
||||
<br><tt><font color="#993300">// specific includes</font></tt>
|
||||
<br><b><tt><font color="#993300">#include "eoSGATransform.h"</font></tt></b>
|
||||
<br><b><tt><font color="#993300">#include "eoPropCombinedOp.h"</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="evalfunc"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
||||
<tr>
|
||||
<td><b><tt><font color="#CC0000">#include "binary_value.h"</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="representation"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFFFCC" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#999900">//-----------------------------------------------------------------------------</font></tt>
|
||||
<br><tt><font color="#999900">// define your genotype and fitness types</font></tt>
|
||||
<br><b><tt><font color="#999900">typedef eoBin<double> Indi;</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="parametres"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#3366FF">//-----------------------------------------------------------------------------</font></tt>
|
||||
<br><tt><font color="#3366FF">// instead of having all values of useful
|
||||
parameters as constants, read them:</font></tt>
|
||||
<br><tt><font color="#3366FF">// either on the command line (--option=value
|
||||
or -o=value)</font></tt>
|
||||
<br><tt><font color="#3366FF">//
|
||||
or in a parameter file (same syntax, order independent, </font></tt>
|
||||
<br><tt><font color="#3366FF">//
|
||||
# = usual comment character </font></tt>
|
||||
<br><tt><font color="#3366FF">//
|
||||
or in the environment (TODO)</font></tt>
|
||||
<br><tt><font color="#3366FF">// note that the parameters are passed by
|
||||
reference so they can be updated</font></tt>
|
||||
<br><b><tt><font color="#3366FF">void read_param(int argc, char *argv[], </font></tt></b>
|
||||
<br><a NAME="seed_passing"></a><b><tt><font color="#660000">uint32 &
|
||||
_seed</font><font color="#3366FF">,</font></tt></b>
|
||||
<br><b><tt><font color="#999900">unsigned int & _vecSize</font><font color="#3366FF">,</font></tt></b>
|
||||
<br><b><tt><font color="#009900">unsigned int & _popSize,</font></tt></b>
|
||||
<br><b><tt><font color="#009900">unsigned int & _tSize</font><font color="#3366FF">,</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC">double & _pCross,</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC">double & _pMut</font><font color="#3366FF">,</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">string & _load_name,</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">unsigned int & _maxGen,</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">unsigned int & _minGen,</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">unsigned int & _steadyGen,</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC">double & _onePointRate, </font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC">double & _twoPointsRate, </font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC">double & _uRate, </font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC">double & _pMutPerBit, </font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC">double & _bitFlipRate, </font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC">double & _oneBitRate</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">)</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">{ </font></tt></b>
|
||||
<br><a NAME="parser_declare"></a><tt><font color="#3366FF">// define a
|
||||
parser from the command-line arguments</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoParser parser(argc,
|
||||
argv);</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// For each
|
||||
parameter, define Parameters directly in the parser, </font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// and assign
|
||||
the value to the variable</font></tt>
|
||||
<br><a NAME="random"></a><a NAME="_seed_declare"></a><b><tt><font color="#3366FF">
|
||||
</font><font color="#660000">eoValueParam<uint32>& seedParam = parser.createParam<uint32>(time(0),
|
||||
"seed", "Random number seed", 'S');</font></tt></b>
|
||||
<br><a NAME="seed_assign"></a><b><tt><font color="#660000">
|
||||
_seed = seedParam.value();</font></tt></b>
|
||||
<p><b><tt><font color="#3366FF"> </font><font color="#999900">eoValueParam<unsigned
|
||||
int>& vecSizeParam = parser.createParam<unsigned int>(8, "vecSize",
|
||||
"Genotype size",'V', "Representation");</font></tt></b>
|
||||
<br><b><tt><font color="#999900"> _vecSize = vecSizeParam.value();</font></tt></b>
|
||||
<p><b><tt><font color="#3366FF"> </font><font color="#009900">eoValueParam<unsigned
|
||||
int>& popSizeParam = parser.createParam<unsigned int>(10, "popSize",
|
||||
"Population size",'P', "Evolution");</font></tt></b>
|
||||
<br><b><tt><font color="#009900"> _popSize = popSizeParam.value();</font></tt></b>
|
||||
<br><b><tt><font color="#009900"> eoValueParam<unsigned
|
||||
int>& tSizeParam = parser.createParam<unsigned int>(10, "tSize",
|
||||
"Tournament size",'T', "Evolution");</font></tt></b>
|
||||
<br><b><tt><font color="#009900"> _tSize = tSizeParam.value();</font></tt></b>
|
||||
<br><a NAME="_load_name"></a>
|
||||
<br><b><tt><font color="#3366FF"> eoValueParam<string>&
|
||||
load_nameParam = parser.createParam<string>("", "Load","A save file
|
||||
to restart from",'L', "Persistence");</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> _load_name =
|
||||
load_nameParam.value();</font></tt></b>
|
||||
<p><b><tt><font color="#3366FF"> eoValueParam<unsigned
|
||||
int>& maxGenParam = parser.createParam<unsigned int>(100, "maxGen",
|
||||
"Maximum number of generations",'G', "Stopping criterion");</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> _maxGen = maxGenParam.value();</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> eoValueParam<unsigned
|
||||
int>& minGenParam = parser.createParam<unsigned int>(100, "minGen",
|
||||
"Minimum number of generations",'g', "Stopping criterion");</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> _minGen = minGenParam.value();</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> eoValueParam<unsigned
|
||||
int>& steadyGenParam = parser.createParam<unsigned int>(100, "steadyGen",
|
||||
"Number of generations with no improvement",'s', "Stopping criterion");</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> _steadyGen =
|
||||
steadyGenParam.value();</font></tt></b>
|
||||
<p><b><tt><font color="#CC33CC"> eoValueParam<double>&
|
||||
pCrossParam = parser.createParam<double>(0.6, "pCross", "Probability
|
||||
of Crossover", 'C', "Genetic Operators"); </font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> _pCross = pCrossParam.value();</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> eoValueParam<double>&
|
||||
pMutParam = parser.createParam<double>(0.1, "pMut", "Probability of
|
||||
Mutation", 'M', "Genetic Operators");</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> _pMut = pMutParam.value();</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> eoValueParam<double>&
|
||||
onePointRateParam = parser.createParam<double>(1, "onePointRate", "Relative
|
||||
rate for one point crossover", '1', "Genetic Operators");</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> _onePointRate
|
||||
= onePointRateParam.value();</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> eoValueParam<double>&
|
||||
twoPointsRateParam = parser.createParam<double>(1, "twoPointRate", "Relative
|
||||
rate for two point crossover", '2', "Genetic Operators");</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> _twoPointsRate
|
||||
= twoPointsRateParam.value();</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> eoValueParam<double>&
|
||||
uRateParam = parser.createParam<double>(2, "uRate", "Relative rate for
|
||||
uniform crossover", 'U', "Genetic Operators");</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> _uRate =
|
||||
uRateParam.value();</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> eoValueParam<double>&
|
||||
pMutPerBitParam = parser.createParam<double>(0.01, "pMutPerBit", "Probability
|
||||
of flipping 1 bit in bit-flip mutation", 'b', "Genetic Operators");</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> _pMutPerBit =
|
||||
pMutPerBitParam.value();</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> eoValueParam<double>&
|
||||
bitFlipRateParam = parser.createParam<double>(0.01, "bitFlipRate", "Relative
|
||||
rate for bit-flip mutation", 'B', "Genetic Operators");</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> _bitFlipRate
|
||||
= bitFlipRateParam.value();</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> eoValueParam<double>&
|
||||
oneBitRateParam = parser.createParam<double>(0.01, "oneBitRate", "Relative
|
||||
rate for deterministic bit-flip mutation", 'D', "Genetic Operators");</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC">
|
||||
_oneBitRate = oneBitRateParam.value();</font></tt></b>
|
||||
<p><tt><font color="#3366FF"><b> </b>// the name
|
||||
of the "status" file where all actual parameter values will be saved</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> string str_status
|
||||
= parser.ProgramName() + ".status";</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> eoValueParam<string>&
|
||||
status_nameParam = parser.createParam<string>(str_status.c_str(), "status","Status
|
||||
file",'S', "Persistence");</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// do the following AFTER
|
||||
ALL PARAMETERS HAVE BEEN PROCESSED</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// i.e. in case you need
|
||||
parameters somewhere else, postpone these</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> if (parser.userNeedsHelp())</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">
|
||||
{</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">
|
||||
parser.printHelp(cout);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">
|
||||
exit(1);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">
|
||||
}</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> if (status_nameParam.value()
|
||||
!= "")</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">
|
||||
{</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">ofstream os(status_nameParam.value().c_str());</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b>os << parser; </b>// and you can
|
||||
use that file as parameter file</font></tt>
|
||||
<br><b><tt><font color="#3366FF">
|
||||
}</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">}</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#993300">// now the main_function: nothing changed,
|
||||
except input/output</font></tt>
|
||||
<br><b><tt><font color="#993300">void main_function(int argc, char **argv)</font></tt></b>
|
||||
<br><b><tt><font color="#993300">{</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="parametres"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td><a NAME="seed_declare"></a><b><tt><font color="#660000">
|
||||
uint32 seed;</font></tt></b>
|
||||
<br><tt><font color="#999900"><b> </b>// decription of genotype</font></tt>
|
||||
<br><b><tt><font color="#999900"> unsigned int vecSize;</font></tt></b>
|
||||
<br><tt><font color="#33CC00"><b> </b>// parameters for evolution
|
||||
engine</font></tt>
|
||||
<br><b><tt><font color="#33CC00"> unsigned int popSize;</font></tt></b>
|
||||
<br><b><tt><font color="#33CC00"> unsigned int tSize;</font></tt></b>
|
||||
<br><tt><font color="#CC33CC"><b> </b>// operators probabilities
|
||||
at the algorithm level</font></tt>
|
||||
<br><b><tt><font color="#CC33CC"> double pCross;</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> double pMut;</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// init and stop</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> string load_name;</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> unsigned int maxGen;</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> unsigned int minGen;</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> unsigned int steadyGen;</font></tt></b>
|
||||
<br><tt><font color="#CC33CC"><b> </b>// rates for crossovers</font></tt>
|
||||
<br><b><tt><font color="#CC33CC"> double onePointRate;</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> double twoPointsRate;</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> double URate;</font></tt></b>
|
||||
<br><tt><font color="#CC33CC"><b> </b>// rates and private
|
||||
parameters for mutations;</font></tt>
|
||||
<br><b><tt><font color="#CC33CC"> double pMutPerBit;</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> double bitFlipRate;</font></tt></b>
|
||||
<br><b><tt><font color="#CC33CC"> double oneBitRate;</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// Now read the parameters
|
||||
of the program</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> read_param(argc,
|
||||
argv, seed, vecSize, popSize, tSize,</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">
|
||||
pCross, pMut, load_name, maxGen, minGen, steadyGen,</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">
|
||||
onePointRate, twoPointsRate, URate, </font></tt></b>
|
||||
<br><b><tt><font color="#3366FF">
|
||||
pMutPerBit, bitFlipRate, oneBitRate );</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="eval"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCCC" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#CC0000"><b> </b>/////////////////////////////</font></tt>
|
||||
<br><tt><font color="#CC0000"><b> </b>// Fitness function</font></tt>
|
||||
<br><tt><font color="#CC0000"><b> </b>////////////////////////////</font></tt>
|
||||
<br><tt><font color="#CC0000"><b> </b>// Evaluation: from a plain
|
||||
C++ fn to an EvalFunc Object ...</font></tt>
|
||||
<br><b><tt><font color="#CC0000"> eoEvalFuncPtr<Indi, double, const
|
||||
vector<bool>& > plainEval( binary_value );</font></tt></b>
|
||||
<br><a NAME="evalcounter"></a><tt><font color="#CC0000">// ... to an object
|
||||
that counts the nb of actual evaluations</font></tt>
|
||||
<br><b><tt><font color="#CC0000"> eoEvalFuncCounter<Indi> eval(plainEval);</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="init"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#993399"><b> </b>////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#993399"><b> </b>// Initilisation of population</font></tt>
|
||||
<br><tt><font color="#993399"><b> </b>////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#993399"><b> </b>// Either load or initialize</font></tt>
|
||||
<br><tt><font color="#993399"><b> </b>// create an empty pop</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoPop<Indi> pop;</font></tt></b>
|
||||
<br><tt><font color="#993399"><b> </b>// create a state for reading</font></tt>
|
||||
<br><tt><font color="#993399"><b> eoState inState; </b>// a state
|
||||
for loading - WITHOUT the parser</font></tt>
|
||||
<br><a NAME="register"></a><tt><font color="#993399">// register the rng
|
||||
and the pop in the state, so they can be loaded,</font></tt>
|
||||
<br><tt><font color="#993399"><b> </b>// and the present run will
|
||||
be the exact conitnuation of the saved run</font></tt>
|
||||
<br><tt><font color="#993399"><b> </b>// eventually with different
|
||||
parameters</font></tt>
|
||||
<br><b><tt><font color="#993399"> inState.registerObject(rng);</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> inState.registerObject(pop);</font></tt></b>
|
||||
<p><a NAME="instate.load"></a><b><tt><font color="#993399">if (load_name
|
||||
!= "")</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> {</font></tt></b>
|
||||
<br><a NAME="loadstate"></a><tt><font color="#993399"><b>
|
||||
inState.load(load_name); </b>// load the pop and the rng</font></tt>
|
||||
<br><tt><font color="#993399"><b>
|
||||
</b>// the fitness is read in the file: </font></tt>
|
||||
<br><tt><font color="#993399"><b>
|
||||
</b>// do only evaluate the pop if the fitness has changed</font></tt>
|
||||
<br><b><tt><font color="#993399"> }</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> else</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> {</font></tt></b>
|
||||
<br><b><tt><font color="#993399">
|
||||
rng.reseed(seed);</font></tt></b>
|
||||
<br><tt><font color="#993399"><b>
|
||||
</b>// a Indi random initializer</font></tt>
|
||||
<br><tt><font color="#993399"><b>
|
||||
</b>// based on boolean_generator class (see utils/rnd_generator.h)</font></tt>
|
||||
<br><b><tt><font color="#993399">
|
||||
eoInitFixedLength<Indi, boolean_generator> </font></tt></b>
|
||||
<br><b><tt><font color="#993399">random(vecSize, boolean_generator());</font></tt></b>
|
||||
<br><tt><font color="#993399"><b>
|
||||
</b>// Init pop from the randomizer: need to use the append function</font></tt>
|
||||
<br><b><tt><font color="#993399">
|
||||
pop.append(popSize, random); </font></tt></b>
|
||||
<br><tt><font color="#993399"><b>
|
||||
</b>// and evaluate pop (STL syntax) </font></tt>
|
||||
<br><b><tt><font color="#993399">
|
||||
apply<Indi>(eval, pop);</font></tt></b>
|
||||
<br><tt><font color="#993399"><b> } </b>// end
|
||||
of initializatio of the population</font></tt></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="output"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#3366FF"><b> </b>// sort pop for pretty printout</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> pop.sort();</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// Print (sorted) intial population
|
||||
(raw printout)</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> cout << "Initial Population"
|
||||
<< endl << pop << endl;</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="engine"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#009900"><b> </b>/////////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#009900"><b> </b>// selection and replacement</font></tt>
|
||||
<br><tt><font color="#009900"><b> </b>////////////////////////////////////</font></tt></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="select"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#009900"><b> </b>// The robust tournament selection</font></tt>
|
||||
<br><tt><font color="#009900"><b> eoDetTournament<Indi> selectOne(tSize);
|
||||
</b>// tSize in [2,POPSIZE]</font></tt>
|
||||
<br><tt><font color="#009900"><b> </b>// is now encapsulated in a
|
||||
eoSelectPerc (entage)</font></tt>
|
||||
<br><tt><font color="#009900"><b> eoSelectPerc<Indi> select(selectOne);</b>//
|
||||
by default rate==1</font></tt></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="replace"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#99FFCC" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#009900"><b> </b>// And we now have the full
|
||||
slection/replacement - though with </font></tt>
|
||||
<br><tt><font color="#009900"><b> </b>// no replacement (== generational
|
||||
replacement) at the moment :-)</font></tt>
|
||||
<br><b><tt><font color="#009900"> eoNoReplacement<Indi> replace; </font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="operators"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#993399"><b> </b>//////////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#993399"><b> </b>// The variation operators</font></tt>
|
||||
<br><tt><font color="#993399"><b> </b>//////////////////////////////////////</font></tt></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="crossover"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#993399"><b> </b>// 1-point crossover for bitstring</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoBinCrossover<Indi> xover1;</font></tt></b>
|
||||
<br><tt><font color="#993399"><b> </b>// uniform crossover for bitstring</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoBinUxOver<Indi> xoverU;</font></tt></b>
|
||||
<br><tt><font color="#993399"><b> </b>// 2-pots xover</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoBinNxOver<Indi> xover2(2);</font></tt></b>
|
||||
<br><tt><font color="#993399"><b> </b>// Combine them with relative
|
||||
rates</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoPropCombinedQuadOp<Indi> xover(xover1,
|
||||
onePointRate);</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> xover.add(xoverU, URate);</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> xover.add(xover2, twoPointsRate,
|
||||
true);</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="mutation"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#993399"><b> </b>// standard bit-flip mutation
|
||||
for bitstring</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoBinMutation<Indi> mutationBitFlip(pMutPerBit);</font></tt></b>
|
||||
<br><tt><font color="#993399"><b> </b>// mutate exactly 1 bit per
|
||||
individual</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoDetBitFlip<Indi> mutationOneBit; </font></tt></b>
|
||||
<br><tt><font color="#993399"><b> </b>// Combine them with relative
|
||||
rates</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoPropCombinedMonOp<Indi> mutation(mutationBitFlip,
|
||||
bitFlipRate);</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> mutation.add(mutationOneBit, oneBitRate,
|
||||
true);</font></tt></b>
|
||||
<br><tt><font color="#993399"><b> </b>// The operators are encapsulated
|
||||
into an eoTRansform object</font></tt>
|
||||
<br><b><tt><font color="#993399"> eoSGATransform<Indi> transform(xover,
|
||||
pCross, mutation, pMut);</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="stop"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#3366FF"><b> </b>//////////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// termination condition see
|
||||
FirstBitEA.cpp</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>/////////////////////////////////////</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoGenContinue<Indi> genCont(maxGen);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> eoSteadyFitContinue<Indi> steadyCont(minGen,
|
||||
steadyGen);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> eoFitContinue<Indi> fitCont(vecSize);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> eoCombinedContinue<Indi> continuator(genCont);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> continuator.add(steadyCont);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> continuator.add(fitCont);</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="checkpoint"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#3366FF"><b> </b>// but now you want to make
|
||||
many different things every generation </font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// (e.g. statistics, plots,
|
||||
...).</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// the class eoCheckPoint is
|
||||
dedicated to just that:</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// Declare a checkpoint (from
|
||||
a continuator: an eoCheckPoint </font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// IS AN eoContinue and will
|
||||
be called in the loop of all algorithms)</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoCheckPoint<Indi> checkpoint(continuator);</font></tt></b>
|
||||
<p><a NAME="param_declare"></a><tt><font color="#3366FF"><b>
|
||||
</b>// Create a counter parameter</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoValueParam<unsigned>
|
||||
generationCounter(0, "Gen.");</font></tt></b>
|
||||
<p><a NAME="param_pass"></a><tt><font color="#3366FF"><b>
|
||||
</b>// Create an incrementor (sub-class of eoUpdater). Note that the </font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// parameter's
|
||||
value is passed by reference, </font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// so every
|
||||
time the incrementer is updated (every generation),</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// the data
|
||||
in generationCounter will change.</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoIncrementor<unsigned>
|
||||
increment(generationCounter.value());</font></tt></b>
|
||||
<br><a NAME="updater_pass"></a><tt><font color="#3366FF"><b>
|
||||
</b>// Add it to the checkpoint, </font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// so the
|
||||
counter is updated (here, incremented) every generation</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> checkpoint.add(increment);</font></tt></b>
|
||||
<br><a NAME="stat_declare"></a><tt><font color="#3366FF"><b>
|
||||
</b>// now some statistics on the population:</font></tt>
|
||||
<br><tt><font color="#3366FF"><b> </b>// Best fitness
|
||||
in population</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoBestFitnessStat<Indi>
|
||||
bestStat;</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// Second
|
||||
moment stats: average and stdev</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoSecondMomentStats<Indi>
|
||||
SecondStat;</font></tt></b>
|
||||
<br><a NAME="stat_pass"></a><tt><font color="#3366FF"><b>
|
||||
</b>// Add them to the checkpoint to get them called at the appropriate
|
||||
time</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> checkpoint.add(bestStat);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> checkpoint.add(SecondStat);</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// The Stdout
|
||||
monitor will print parameters to the screen ...</font></tt>
|
||||
<br><a NAME="monitor_declare"></a><b><tt><font color="#3366FF">
|
||||
eoStdoutMonitor monitor(false);</font></tt></b>
|
||||
<p><tt><font color="#3366FF"><b> </b>// when called
|
||||
by the checkpoint (i.e. at every generation)</font></tt>
|
||||
<br><a NAME="monitor_pass"></a><b><tt><font color="#3366FF">
|
||||
checkpoint.add(monitor);</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// the monitor
|
||||
will output a series of parameters: add them </font></tt>
|
||||
<br><a NAME="monitor_add"></a><b><tt><font color="#3366FF">
|
||||
monitor.add(generationCounter);</font></tt></b>
|
||||
<a name="eval_monitor"></a>
|
||||
<br><tt><font color="#3366FF"><b> monitor.add(eval);
|
||||
</b>//
|
||||
because now eval is an eoEvalFuncCounter!</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> monitor.add(bestStat);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> monitor.add(SecondStat);</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// A file
|
||||
monitor: will print parameters to ... a File, yes, you got it!</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoFileMonitor
|
||||
fileMonitor("stats.xg", " ");</font></tt></b>
|
||||
<p><tt><font color="#3366FF"><b> </b>// the checkpoint
|
||||
mechanism can handle multiple monitors</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> checkpoint.add(fileMonitor);</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// the fileMonitor
|
||||
can monitor parameters, too, but you must tell it!</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> fileMonitor.add(generationCounter);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> fileMonitor.add(bestStat);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> fileMonitor.add(SecondStat);</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// Last type
|
||||
of item the eoCheckpoint can handle: state savers:</font></tt>
|
||||
<br><a NAME="outstate_declare"></a><b><tt><font color="#3366FF">
|
||||
eoState outState;</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// Register
|
||||
the algorithm into the state (so it has something to save!!)</font></tt>
|
||||
<br><a NAME="outstate_register"></a><b><tt><font color="#3366FF">
|
||||
outState.registerObject(rng);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> outState.registerObject(pop);</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// and feed
|
||||
the state to state savers</font></tt>
|
||||
<br><a NAME="statesaver_declare"></a><tt><font color="#3366FF"><b>
|
||||
</b>// save state every 100th generation</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoCountedStateSaver
|
||||
stateSaver1(100, outState, "generation"); </font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// save state
|
||||
every 1 seconds </font></tt>
|
||||
<br><b><tt><font color="#3366FF"> eoTimedStateSaver
|
||||
stateSaver2(1, outState, "time"); </font></tt></b>
|
||||
<br><a NAME="statesaver_pass"></a><tt><font color="#3366FF"><b>
|
||||
</b>// Don't forget to add the two savers to the checkpoint</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> checkpoint.add(stateSaver1);</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> checkpoint.add(stateSaver2);</font></tt></b>
|
||||
<br><tt><font color="#3366FF"><b> </b>// and that's
|
||||
it for the (control and) output</font></tt></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="generation"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#FF6666"><b> </b>/////////////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#FF6666"><b> </b>// the algorithm</font></tt>
|
||||
<br><tt><font color="#FF6666"><b> </b>////////////////////////////////////////</font></tt>
|
||||
<br><tt><font color="#FF6666"><b> </b>// Easy EA requires </font></tt>
|
||||
<br><tt><font color="#FF6666"><b> </b>// selection, transformation,
|
||||
eval, replacement, and stopping criterion</font></tt>
|
||||
<br><b><tt><font color="#FF6666"> eoEasyEA<Indi> gga(checkpoint,
|
||||
eval, select, transform, replace);</font></tt></b>
|
||||
<br><tt><font color="#FF6666"><b> </b>// Apply algo to pop - that's
|
||||
it!</font></tt>
|
||||
<br><b><tt><font color="#FF6666"> gga(pop);</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="output"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td><tt><font color="#3366FF"><b> </b>// Print (sorted) final population</font></tt>
|
||||
<br><b><tt><font color="#3366FF"> pop.sort();</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> cout << "FINAL Population\n"
|
||||
<< pop << endl;</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td><b><tt><font color="#993300">}</font></tt></b>
|
||||
<br><tt><font color="#993300">// A main that catches the exceptions</font></tt>
|
||||
<br><b><tt><font color="#993300">int main(int argc, char **argv)</font></tt></b>
|
||||
<br><b><tt><font color="#993300">{</font></tt></b>
|
||||
<br><b><tt><font color="#993300">#ifdef _MSC_VER</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> int flag = _CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> flag
|
||||
|= _CRTDBG_LEAK_CHECK_DF;</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> _CrtSetDbgFlag(flag);</font></tt></b>
|
||||
<br><tt><font color="#993300">// _CrtSetBreakAlloc(100);</font></tt>
|
||||
<br><b><tt><font color="#993300">#endif</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> try</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> {</font></tt></b>
|
||||
<br><b><tt><font color="#993300">
|
||||
main_function(argc, argv);</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> }</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> catch(exception&
|
||||
e)</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> {</font></tt></b>
|
||||
<br><b><tt><font color="#993300">
|
||||
cout << "Exception: " << e.what() << '\n';</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> }</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> return 1;</font></tt></b>
|
||||
<br><b><tt><font color="#993300">}</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<hr WIDTH="100%"><a href="eoLesson3.html">Back to Lesson 3</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <b><font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font></b>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last modified: Sun Nov
|
||||
26 09:31:04 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
BIN
eo/tutorial/html/beige009.jpg
Normal file
BIN
eo/tutorial/html/beige009.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 6.5 KiB |
57
eo/tutorial/html/binary_value.html
Normal file
57
eo/tutorial/html/binary_value.html
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.75 [en] (X11; U; Linux 2.2.17-21mdk i686) [Netscape]">
|
||||
<title>binary_value.h</title>
|
||||
</head>
|
||||
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
|
||||
documentation</a></font></font></b>
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">binary_value.h</font></h1></center>
|
||||
<a NAME="start"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr NOSAVE>
|
||||
<td NOSAVE><b><tt><font color="#993300">#include <eo></font></tt></b>
|
||||
<br><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
|
||||
<br><tt><font color="#993300">/** Just a simple function that takes binary
|
||||
value of a chromosome and sets</font></tt>
|
||||
<br><tt><font color="#993300"> the fitnes.</font></tt>
|
||||
<br><tt><font color="#993300"> @param _chrom A
|
||||
binary chromosome </font></tt>
|
||||
<br><tt><font color="#993300">*/</font></tt></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="init"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td><b><tt><font color="#993399">double binary_value(const vector<bool>&
|
||||
_chrom)</font></tt></b>
|
||||
<br><b><tt><font color="#993399">{</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> double sum = 0;</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> for (unsigned i = 0; i < _chrom.size();
|
||||
i++)</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> sum += _chrom[i];</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> return sum;</font></tt></b>
|
||||
<br><b><tt><font color="#993399">}</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<hr WIDTH="100%"><a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
|
||||
documentation</a></font></font></b>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last modified: Wed Nov
|
||||
29 09:03:09 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
BIN
eo/tutorial/html/costume.jpg
Normal file
BIN
eo/tutorial/html/costume.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 209 KiB |
21
eo/tutorial/html/debut.html
Normal file
21
eo/tutorial/html/debut.html
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>TITRE</title>
|
||||
</head>
|
||||
|
||||
<body text="#000000" background="beige009.jpg" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoTutorial.html">Tutorial main page </a>-
|
||||
<a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO documentation</a></font>
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<!-- -------------- End of header ------------------ -->
|
||||
<!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1><font color="#FF0000">TITRE</font></h1>
|
||||
</center>
|
||||
|
||||
<font face="Courier New,Courier"><font size=+1>
|
||||
<p>
|
||||
74
eo/tutorial/html/eoBottomUp.html
Normal file
74
eo/tutorial/html/eoBottomUp.html
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.72 [en] (X11; U; Linux 2.2.16 i686) [Netscape]">
|
||||
<title>EO - The Bottom-Up approach</title>
|
||||
</head>
|
||||
<body text="#000000" bgcolor="#CCCCCC" link="#0000EF" vlink="#51188E" alink="#FF0000">
|
||||
<a href="eoTutorial.html">Tutorial main page </a>-
|
||||
<a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> - <a href="doc/html/index.html">EO documentation</a>
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">EO - Bottom-Up approach</font></h1></center>
|
||||
|
||||
<p><br>Congratualtions - You have chosen the bottom-up approach!
|
||||
From here you will be allowed to browse into the different components of
|
||||
an Evolutionary Algorithm, and to see how to program your favorite using
|
||||
the EO library.
|
||||
|
||||
<h3>
|
||||
<font color="#000099">Table of Content</font></h3>
|
||||
|
||||
<center>
|
||||
<p><img SRC="EA_tutorial.jpg" USEMAP="#Map" ></center>
|
||||
|
||||
<p>Yes, this is the table of content of this part
|
||||
of the EO tutorial. If you don't already know what this symbolic
|
||||
representation of an EA means, you should try <a
|
||||
href="eoIntroEA.html">here</a>. Otherwise, <font color="#FF6600">click on the figure</font>
|
||||
to go directly to the corresponding section of the tutorial.
|
||||
<br>
|
||||
<map NAME="Map">
|
||||
<area SHAPE="rect" HREF="eoInit.html" COORDS="14,31,135,70">
|
||||
<area SHAPE="rect" HREF="eoEval.html" COORDS="14,110,135,150">
|
||||
<area SHAPE="rect" HREF="eoRepresentation.html" COORDS="240,270,465,310">
|
||||
<area SHAPE="rect" HREF="eoIo.html#output" COORDS="280,45,480,70">
|
||||
<area SHAPE="rect" HREF="eoIo.html#stop" COORDS="348,110,430,150">
|
||||
<area SHAPE="rect" HREF="eoEngine.html#selection" COORDS="495,110,615,150">
|
||||
<area SHAPE="rect" HREF="eoRepresentation.html" COORDS="495,190,615,230">
|
||||
<area SHAPE="rect" HREF="eoOperators.html#crossover" COORDS="495,265,625,287">
|
||||
<area SHAPE="rect" HREF="eoOperators.html#mutation" COORDS="495,287,625,305">
|
||||
<area SHAPE="rect" HREF="eoRepresentation.html" COORDS="170,110,295,150">
|
||||
<area SHAPE="rect" HREF="eoEval.html" COORDS="170,270,295,310">
|
||||
<area SHAPE="rect" HREF="eoEngine.html#replacement" COORDS="170,190,295,230">
|
||||
|
||||
<area SHAPE="rect" HREF="eoGeneration.html" COORDS="310,160,485,260">
|
||||
|
||||
|
||||
<area SHAPE="rect" HREF="eoOperators.html" COORDS="15,350,260,370">
|
||||
<area SHAPE="rect" HREF="eoRepresentation.html" COORDS="270,350,460,370">
|
||||
<area SHAPE="rect" HREF="eoEngine.html" COORDS="15,377,400,397">
|
||||
<area SHAPE="rect" HREF="eoEval.html" COORDS="15,403,230,423">
|
||||
<area SHAPE="rect" HREF="eoCheckpoint.html" COORDS="15,430,221,450">
|
||||
<area SHAPE="rect" HREF="eoCheckpoint.html#stop" COORDS="221,430,345,450">
|
||||
<area SHAPE="rect" HREF="eoCheckpoint.html#stat" COORDS="375,430,445,450">
|
||||
</map>
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<br><a href="eoTutorial.html">Tutorial main page </a>- <a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> - <a href="doc/html/index.html">EO documentation</a>
|
||||
<br>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Mon Oct 30 07:27:13 CET 2000 --><!-- hhmts start -->Last
|
||||
modified: Mon Oct 30 07:28:36 CET 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
59
eo/tutorial/html/eoEngine.html
Normal file
59
eo/tutorial/html/eoEngine.html
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.72 [en] (X11; U; Linux 2.2.16 i686) [Netscape]">
|
||||
<title>Genetic Engine</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<body text="#000000" link="#0000EF" vlink="#51188E" alink="#FF0000" background="beige009.jpg">
|
||||
<center>
|
||||
<h1>
|
||||
<b><font color="#CC0000">Evolution Engine</font></b></h1></center>
|
||||
|
||||
<p><br><b><font color="#000099"><font size=+2>Contents</font></font></b>
|
||||
<ul>
|
||||
<li>
|
||||
Introduction</li>
|
||||
|
||||
<li>
|
||||
Selection</li>
|
||||
|
||||
<li>
|
||||
Replacement</li>
|
||||
|
||||
<li>
|
||||
Popular evolution engines</li>
|
||||
</ul>
|
||||
|
||||
<p><br>The term evolution engine denotes the different parts that simulate
|
||||
the Darwinism in Evolutionary Algorithms.<font color="#33CC00"></font>
|
||||
<center>
|
||||
<p><i><font color="#33CC00">The fittest individuals are more likely to
|
||||
reproduce and survive.</font></i></center>
|
||||
|
||||
<p>Darwinism takes place in two different phases of an EA, though in many
|
||||
<a href="#popular">popular variants</a>, only one phase is activated.
|
||||
<p><a href="#selection">Selection</a> is the Darwinistic choice of parents
|
||||
that will be allowed to <font color="#33CC00">reproduce</font><font color="#CC33CC">.</font>
|
||||
<br><a href="#replacement">Replacement</a> takes place after reproduction,
|
||||
and is the Darwinistic choice of those individuals that will <font color="#33CC00">survive</font>,
|
||||
i.e. become the parents of the next generation.
|
||||
<p><a NAME="selection"></a><b><font color="#000099"><font size=+2>Selection</font></font></b>
|
||||
<br>
|
||||
<p><a NAME="replacement"></a><b><font color="#000099"><font size=+2>Replacement</font></font></b>
|
||||
<br>
|
||||
<p><a NAME="popular"></a><b><font color="#000099"><font size=+2>Popular
|
||||
evolution engines</font></font></b>
|
||||
<br>
|
||||
<br>
|
||||
<p>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Mon Oct 30 18:15:16 CET 2000 --><!-- hhmts start -->Last
|
||||
modified: Mon Oct 30 18:15:17 CET 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
34
eo/tutorial/html/eoEval.html
Normal file
34
eo/tutorial/html/eoEval.html
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>Evaluation</title>
|
||||
</head>
|
||||
|
||||
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoTutorial.html">Tutorial main page </a>-
|
||||
<a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> - <a href="doc/html/index.html">EO documentation</a>
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
|
||||
<center>
|
||||
<h1><font color="#FF0000">Evaluation</font></h1>
|
||||
</center>
|
||||
|
||||
|
||||
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<a href="eoTutorial.html">Tutorial main page </a>-
|
||||
<a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> - <a href="doc/html/index.html">EO documentation</a>
|
||||
<hr>
|
||||
<address><a href="mailto:Marc.Schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<!-- Created: Mon Oct 30 17:51:43 CET 2000 -->
|
||||
<!-- hhmts start -->
|
||||
Last modified: Mon Nov 6 10:54:33 CET 2000
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
19
eo/tutorial/html/eoGeneration.html
Normal file
19
eo/tutorial/html/eoGeneration.html
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>Generation</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Generation</h1>
|
||||
|
||||
|
||||
|
||||
<hr>
|
||||
<address><a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<!-- Created: Mon Oct 30 19:29:28 CET 2000 -->
|
||||
<!-- hhmts start -->
|
||||
Last modified: Mon Oct 30 19:29:29 CET 2000
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
36
eo/tutorial/html/eoInit.html
Normal file
36
eo/tutorial/html/eoInit.html
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>Initialization</title>
|
||||
</head>
|
||||
<body text="#000000" background="beige009.jpg" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoTutorial.html">Tutorial main page </a>-
|
||||
<a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> - <a href="doc/html/index.html">EO documentation</a>
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<!-- -------------- End of header ------------------ -->
|
||||
<!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<b><font color="#CC0000">Initialization</font></b></h1></center>
|
||||
|
||||
|
||||
|
||||
<!-- Bottom of file: display some links and address -->
|
||||
<!-- ----------------------------------------------- -->
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<a href="eoTutorial.html">Tutorial main page </a>- <a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> - <a href="doc/html/index.html">EO documentation</a>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:Marc.Schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<!-- Created: Mon Oct 30 17:51:29 CET 2000 -->
|
||||
<!-- hhmts start -->
|
||||
Last modified: Mon Nov 6 11:25:18 CET 2000
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
25
eo/tutorial/html/eoIo.html
Normal file
25
eo/tutorial/html/eoIo.html
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>Input / Output</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Input / Output</h1>
|
||||
|
||||
<p>
|
||||
<A NAME=stop>
|
||||
<h3>Stopping criteria</h3>
|
||||
|
||||
<p>
|
||||
<A NAME=output>
|
||||
<h3>Displaying statistics</h3>
|
||||
|
||||
<hr>
|
||||
<address><a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<!-- Created: Mon Oct 30 19:29:46 CET 2000 -->
|
||||
<!-- hhmts start -->
|
||||
Last modified: Tue Oct 31 18:32:22 CET 2000
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
413
eo/tutorial/html/eoLesson1.html
Normal file
413
eo/tutorial/html/eoLesson1.html
Normal file
|
|
@ -0,0 +1,413 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.75 [en] (X11; U; Linux 2.2.17-21mdksmp i686) [Netscape]">
|
||||
<title>Tutorial: Lesson 1</title>
|
||||
</head>
|
||||
<body text="#000000" link="#0000EE" vlink="#551A8B" alink="#FF0000" background="beige009.jpg">
|
||||
<a href="eoLesson2.html">Lesson 2</a> -
|
||||
<a href="eoTutorial.html">Tutorial
|
||||
main page</a> -
|
||||
<a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> -<font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
|
||||
documentation</a></font></font>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">Tutorial: Lesson 1</font></h1></center>
|
||||
This lesson will let you
|
||||
<ul>
|
||||
<li>
|
||||
<a href="#run">run</a> your first Evolutionary Algorithm written within
|
||||
EO Library, choosing between evolving <font color="#999900">bitstrings</font>,
|
||||
or evolving <font color="#999900">vectors of real numbers</font>,</li>
|
||||
|
||||
<li>
|
||||
<a href="#browse">browse</a> through the code of these algorithms, or</li>
|
||||
|
||||
<li>
|
||||
follow the <a href="#tour">guided tour</a>.</li>
|
||||
</ul>
|
||||
Later you will be asked to
|
||||
<ul>
|
||||
<li>
|
||||
write your own <font color="#990000">fitness function</font>,</li>
|
||||
|
||||
<li>
|
||||
use different kinds of <font color="#009900">selection procedures</font>
|
||||
in the framework of a generational GA <font color="#009900">evolution engine</font>,</li>
|
||||
|
||||
<li>
|
||||
check that EO let you separate the <font color="#999900">representation</font>
|
||||
from the <font color="#009900">evolution engine</font>.</li>
|
||||
</ul>
|
||||
|
||||
<h3>
|
||||
<a NAME="run"></a><font color="#000099">I want to run an Evolutionary Algorithm
|
||||
<b>now</b></font></h3>
|
||||
You can choose to run a standard <a href="FirstBitGA.html">bitstring Genetic
|
||||
Algorithm</a> (as defined in Goldberg's book) or a standard <a href="FirstRealGA.html">real-valued
|
||||
genetic algorithm</a>, as proposed in Micahlewicz's book.
|
||||
<p>If you have not already done what was recommended in the <a href="eoTutorial.html#install">Tutorial
|
||||
main page</a> , do it <b><font color="#FF6600">NOW</font></b>. Then go
|
||||
to the Lesson1 sub-dir of the tutorial dir, and simply type at the system
|
||||
prompt
|
||||
<p><tt>(myname@myhost) EOdir/Tutorial/Lesson1 % <font color="#FF6666">FirstRealGA</font></tt>
|
||||
<br>or
|
||||
<br><tt>(myname@myhost) EOdir/Tutorial/Lesson1 % <font color="#FF6666">FirstBitGA</font></tt>
|
||||
<p>and something should happen.
|
||||
<h3>
|
||||
<font color="#000099">What is happening?</font></h3>
|
||||
At the moment, the <font color="#FF6600">FirstBitGA</font> maximizes the
|
||||
number of ones in the bitstring (also calls the <font color="#FF6600">OneMaxfunction</font>,
|
||||
whose solution is, as you can guess,
|
||||
<font color="#FF6600">11111111</font>),
|
||||
and the <font color="#FF6600">FirstRealGA</font> is maximizing (the default
|
||||
action for EO is to maximize) the inverse of the sum (i.e. minimizing the
|
||||
sum) of the square of its variables (also called the <font color="#FF6600">sphere
|
||||
function</font>, whose solution is ... <font color="#FF6600">all zeroes</font>).
|
||||
<p>And what you see on the screen when running one of these two programs
|
||||
is, in each case, the initial and final population of an Evolutionary run,
|
||||
one individual per line, its fitness first, then the number of items (bits
|
||||
or real numbers) of the genotype, and the genotype itself. The final population
|
||||
hopefully contains the solution in the discrete case, and is close to it
|
||||
in the continuous case.
|
||||
<p><a NAME="browse"></a>You need now to take a look at either programs
|
||||
by browsing alone through the sources for <a href="FirstBitGA.html">FirstBitGA</a>
|
||||
and <a href="FirstRealGA.html">FirstRealGA</a>, or follow the guided tour
|
||||
below, or go directly to the <a href="#exercise1">exercises</a>.
|
||||
<h3>
|
||||
<a NAME="tour"></a><font color="#000099">Browsing the code:</font></h3>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<b><font color="#993300">General includes:</font></b><font color="#000000">Like
|
||||
all C-like code, the file starts with include directives (<a href="FirstBitGA.html#start">Bit</a>
|
||||
- <a href="FirstRealGA.html#start">Real</a>). Apart from standard includes,
|
||||
the spedific file to include is </font><b><font color="#FF6600">eo</font></b><font color="#000000">:
|
||||
this is a file that contains the list of the most important EO files.</font></li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
<b><font color="#999900">Representation</font></b><font color="#000000">:
|
||||
you then have to delclare the type of individuals you will be handling.
|
||||
All evolution-related objects you will need are templatized w.r.t. the
|
||||
type of individuals.</font></li>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<a href="FirstBitGA.html#representation">Bit</a> You say here that you
|
||||
will be handling <b><font face="Arial,Helvetica"><a href="doc/html/class_eobin.html">Bitstring
|
||||
genotypes</a></font></b>, whose fitness is a double. This makes Indi derive
|
||||
from the <a href="eoProgramming.html#STL">STL class</a> <font color="#993300">vector<bool></font></li>
|
||||
|
||||
<li>
|
||||
<a href="FirstRealGA.html#representation">Real</a> You say here that you
|
||||
will be handling <b><font face="Arial,Helvetica"><a href="doc/html/class_eoreal.html">Real-valued
|
||||
genotypes</a></font></b>, whose fitness is a double. This makes Indi derive
|
||||
from the <a href="eoProgramming.html#STL">STL class</a> <font color="#993300">vector<double></font></li>
|
||||
|
||||
<br> </ul>
|
||||
|
||||
<li>
|
||||
<b><font color="#CC0000">Fitness function:</font></b><font color="#000000">
|
||||
the code for the fitness function is included in the file. It must take
|
||||
as argument a reference to an individual (at the moment).</font></li>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<a href="FirstBitGA.html#evalfunc">Bit</a> This function simply computes
|
||||
the number of ones of the bitstring (it's called the OneMax function).
|
||||
The optimum is of course the all-ones bitstring.</li>
|
||||
|
||||
<li>
|
||||
<a href="FirstRealGA.html#evalfunc">Real</a> This function simply computes
|
||||
the inverse of the sum of the squares of all variables (also called the
|
||||
sphere function). The optimum is of course the all-zeroes vector.</li>
|
||||
|
||||
<br> </ul>
|
||||
|
||||
<li>
|
||||
<a NAME="parametres"></a><b><font color="#3366FF">Parameters</font></b><font color="#000000">:
|
||||
all parameters of the algorithm are declared here (<a href="FirstBitGA.html#parametres">Bit</a>
|
||||
- <a href="FirstRealGA.html#parametres">Real</a>), and their values and
|
||||
assigned. Of course, this means that you will need to recompile to change
|
||||
these values - see Lesson 3 to get rid of that heavy requirement.</font></li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
<b><font color="#990000">Random seeding:</font></b><font color="#000000">
|
||||
Random numbers play an important role in Evolutionary Algorithms. See in
|
||||
<a href="eoProgramming.html#random">EO
|
||||
programming hints</a> more details about how this is simulated in EO -
|
||||
but as far as you are concerned now, remember that the </font><font color="#660000">global
|
||||
Random Number Generator</font><font color="#000000"> is called </font><b><tt><font color="#660000"><font size=+1>rng</font></font></tt></b><font color="#000000">
|
||||
and should be used everywhere you need a realization of a random variable
|
||||
of known law. Moreover, this RNG requires a </font><b><tt><font color="#660000"><font size=+1>seed</font></font></tt></b><font color="#000000">,
|
||||
which is set here (<a href="FirstBitGA.html#random">Bit</a> - <a href="FirstRealGA.html#random">Real</a>):
|
||||
everytime you run the algorithm with the </font><font color="#FF6600">same
|
||||
seed</font><font color="#000000">, you will get the </font><font color="#FF6600">same
|
||||
result</font><font color="#000000">. Hence, to test the robustness of your
|
||||
algorithm, you should run it with different seeds. This is rather time
|
||||
consuming in the present programs, so we suggest that you wait until Lesson
|
||||
3 to do so.</font></li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
<b><font color="#CC0000">Fitness function encapsulation: </font></b><font color="#000000">EO
|
||||
is based on the notion of <a href="eoProgramming.html#functors">functors</a>
|
||||
- hence you now need to encapsulate your fitness function into a functor
|
||||
object. This is what is done here (<a href="FirstBitGA.html#eval">Bit</a>
|
||||
- <a href="FirstRealGA.html#eval">Real</a>).</font></li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
<b><font color="#CC33CC">Initialization</font></b><font color="#000000">:
|
||||
to initialize the population, first declare an empty object of class </font><b><tt><font color="#990000">eoPop<Indi></font></tt></b><font color="#000000">,
|
||||
which is basically an <a href="eoProgramming.html#STL">STL</a> </font><b><tt><font color="#990000">vector<Indi></font></tt></b><font color="#000000">,
|
||||
then fill it with Indi's.</font> And remember that
|
||||
<b><tt><font color="#990000">v.push_back</font></tt></b>
|
||||
simply appends its argument at the end of <a href="eoProgramming.html#STL">STL</a>
|
||||
vector <b><tt><font color="#993300">v.</font></tt></b></li>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<a href="FirstBitGA.html#init">Bit</a> <b><tt><font color="#990000">rng.flip()</font></tt></b>
|
||||
return a <font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eorng.html">random
|
||||
boolean</a></font></font></li>
|
||||
|
||||
<li>
|
||||
<a href="FirstRealGA.html#init">Real</a> <b><tt><font color="#990000">rng.uniform()</font></tt></b>
|
||||
returns a <font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eorng.html">real
|
||||
value uniformly drawn in [0,1].</a></font></font></li>
|
||||
|
||||
<br> </ul>
|
||||
|
||||
<li>
|
||||
<b><font color="#3366FF">Output</font></b><font color="#000000">: take
|
||||
a snapshot at the initial population (<a href="FirstBitGA.html#output">Bit</a>
|
||||
- <a href="FirstRealGA.html#output">Real</a>). Sort it first, so the best
|
||||
individuals are first, and display it. Note that an eoPop has a </font><b><tt><font color="#990000"><<</font></tt></b><font color="#000000">
|
||||
method, which means that a simple </font><b><tt><font color="#990000">os
|
||||
<< pop </font></tt></b><font color="#000000">streams the </font><b><tt><font color="#990000">pop</font></tt></b><font color="#000000">
|
||||
onto the ostream </font><b><tt><font color="#990000">os</font></tt></b><font color="#000000">.
|
||||
This is true for </font><font color="#FF6600">all objects of of class </font><font color="#000000"><b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eoprintable.html">eoPrintable</a></font></font></b>
|
||||
(most EO objects) through the method </font><b><tt><font color="#990000">printOn</font></tt></b><font color="#000000">
|
||||
(which is then called by the </font><b><tt><font color="#990000"><<</font></tt></b><font color="#000000">
|
||||
operator).</font></li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
<b><font color="#009900">Evolution engine:</font></b><font color="#000000">
|
||||
The selection/replacement mechanism (<a href="FirstBitGA.html#engine">Bit</a>
|
||||
- <a href="FirstRealGA.html#engine">Real</a>) is a simple generational
|
||||
GA here: a simple selector, and a generational replacement. The <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eodettournament.html">eoDetTournament</a></font></font></b>
|
||||
has been chosen as a robust selection, and the generational replacement
|
||||
(all parents are replaced by the offspring) is hard-coded in the eoSGA
|
||||
<a href="#algorithm">algorithm</a>.</font></li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
<font color="#CC33CC"><b>Variation operators</b>:</font><font color="#000000">
|
||||
in the simple algorithm considered here, individuals undergo </font><font color="#CC33CC">crossover</font><font color="#000000">
|
||||
and </font><font color="#CC33CC">mutation</font><font color="#000000">.
|
||||
In EO, these operators are (<a href="eoProgramming.html#functors">functor</a>)
|
||||
objects of class</font> <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eoquadraticop.html">eoQuadOp</a></font></font></b>
|
||||
(binary operator that modifies both its arguments) and <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eomonop.html">eoMonOp</a></font></font></b>
|
||||
(unary operator). These operators are applied in turn to all selected
|
||||
parents, according to user-defined probabilities. These probabilities
|
||||
are defined with all other <a href="#parametres">parameters</a>, and will
|
||||
be passed to the <b><tt><font color="#FF6666"><font size=+1>eoSGA</font></font></tt></b><a href="#parametres">algorithm</a><font color="#000000">.</font></li>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
<a href="FirstBitGA.html#operators">Bit</a> The crossover <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eobincrossover.html">eoBinCrossover</a></font></font></b>
|
||||
is the standard <font color="#CC33CC">1-point crossover</font>, and <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eobinmutation.html">eoBinMutation</a></font></font></b>
|
||||
is the standard <font color="#FF6600">bit-flip mutation</font> that randomly
|
||||
flips all bits with a given probability <b><tt>P_MUT_PER_BIT.</tt></b></li>
|
||||
|
||||
<br><b><font color="#FF0000">Warning</font></b>: the <b><tt>P_MUT_PER_BIT</tt></b>
|
||||
probability is an <font color="#FF6600">internal parameter</font> of the
|
||||
<b><tt><font color="#CC33CC"><font size=+1>eoBinMutation</font></font></tt></b>,
|
||||
it is <b><font color="#FF6600">NOT</font></b> the probability of mutation
|
||||
at the individual level. EO corrects what can be viewed as an inconsistency
|
||||
in Holland's original work, further used in Goldberg's book by separating
|
||||
the probability of mutation for each individual (independent of the type
|
||||
of mutation that will be applied) from the probability of flipping each
|
||||
bit, which is specific of the bit-flip mutation. Hence, to run the
|
||||
same algorithm as Goldberg's SGA, the mutation probability (at individual
|
||||
level) is 1, and the probability of flipping each bit is <b><tt>P_MUT_PER_BIT.</tt></b>
|
||||
<li>
|
||||
<a href="FirstRealGA.html#operators">Real</a> The crossover <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eoarithmeticcrossover.html">eoArithmeticCrossover</a></font></font></b>
|
||||
is the standard <font color="#CC33CC">arithmetic crossover</font> for real-valued
|
||||
vectors, that chooses a point randomly on the segment between both parents
|
||||
(also termed <font color="#CC33CC">BLX-0</font>). <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eouniformmutation.html">eoUniformMutation</a></font></font></b>
|
||||
is the <font color="#CC33CC">uniform mutation</font> for real-valued vectors
|
||||
that choses a new value for each variable uniformly on an interval centered
|
||||
on the parent value. The width of the interval is an <font color="#FF6600">internal
|
||||
parameter</font> of the object, here called <b><tt>EPSILON</tt></b>.</li>
|
||||
|
||||
<br> </ul>
|
||||
|
||||
<li>
|
||||
<b><font color="#3366FF">Stopping criterion:</font></b><font color="#000000">
|
||||
Specify a </font><font color="#3366FF">maximum number of generations</font><font color="#000000">
|
||||
to run (<a href="FirstBitGA.html#stop">Bit</a> - <a href="FirstRealGA.html#stop">Real</a>):
|
||||
the simplest of all stopping criteria at the moment, using an object of
|
||||
a sub-class of class </font><b><font face="Arial,Helvetica"><font color="#3366FF"><font size=+1><a href="doc/html/class_eocontinue.html">eoContinue</a></font></font></font></b><font color="#000000">.</font></li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
<a NAME="algorithm"></a><b><font color="#FF6666">The algorithm: </font></b><font color="#000000">the
|
||||
simple algorithm that is used here, called </font><b><tt><font color="#FF6666"><font size=+1>eoSGA
|
||||
</font></font></tt></b><font color="#000000">requires
|
||||
as parameters a </font><font color="#FF6600">selector</font><font color="#000000">,
|
||||
a </font><font color="#FF6600">crossover</font><font color="#000000"> and
|
||||
the associated </font><font color="#FF6600">crossover rate</font><font color="#000000">,
|
||||
a </font><font color="#FF6600">mutation</font><font color="#000000"> and
|
||||
the associated </font><font color="#FF6600">mutation rate,</font><font color="#000000">
|
||||
and a </font><font color="#FF6600">stopping criterion</font><font color="#000000">.
|
||||
Take a look at the corresponding
|
||||
<a href="eoSGA.html#constructor">constructor</a>
|
||||
of the class </font><b><tt><font color="#FF6666"><font size=+1>eoSGA</font></font></tt></b><font color="#000000">:
|
||||
it only initializes its <a href="eoSGA.html#parametres">private data</a>
|
||||
with the parameters. Now look at the <a href="eoSGA.html#generation">operator()</a>
|
||||
method - the one that is called in the code for <a href="FirstBitGA.html#generation">FirstBitGA</a>
|
||||
or <a href="FirstRealGA.html#generation">FirstRealGA</a> - and you'll find
|
||||
out that is is as simple as it sounds.</font></li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
<b><font color="#3366FF">Output</font></b><font color="#000000">: After
|
||||
running the algorithm, output the sorted final population (<a href="FirstBitGA.html#final_output">Bit</a>
|
||||
- <a href="FirstRealGA.html#final_output">Real</a>) - and look at the best
|
||||
individual: this is the result of the algorithm.</font></li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
<b><font color="#990000">Main body:</font></b><font color="#000000"> for
|
||||
technical reasons (intercepting the exceptions), we need a main like this
|
||||
one (<a href="FirstBitGA.html#main">Bit</a> - <a href="FirstRealGA.html#main">Real</a>).,
|
||||
and you should not touch it unless you know what you are doing. Simply
|
||||
note that this main calls the function main_function, which we have been
|
||||
discussing up to now!</font></li>
|
||||
</ul>
|
||||
|
||||
<h3>
|
||||
<a NAME="exercise1"></a><font color="#000099">Exercise 1: maximize your
|
||||
own function</font></h3>
|
||||
This is very easy - if your search space is that of bitstring or of unbounded
|
||||
real numbers.
|
||||
<ul>
|
||||
<li>
|
||||
Go to the tutorial directory, and <font color="#FF6600">copy</font> the
|
||||
program you want to modify onto <b><tt><font color="#663300">mytest.cpp.</font></tt></b></li>
|
||||
|
||||
<li>
|
||||
<font color="#FF6600">Edit</font> <b><tt><font color="#990000">mytest.cpp</font></tt></b>
|
||||
with any text editor:</li>
|
||||
|
||||
<li>
|
||||
<font color="#FF6600">Modify</font> the fitness function itself (<a href="FirstBitGA.html#evalfunc">binary_value</a>
|
||||
in <font color="#FF6666"><b><tt>FirstBitGA</tt></b><font face="Courier New,Courier">,</font></font><a href="FirstRealGA.html#evalfunc">real_value</a>
|
||||
in <font color="#FF6666"><b><tt>FirstRealGA</tt></b><font face="Courier New,Courier">)</font></font></li>
|
||||
|
||||
<li>
|
||||
<font color="#FF6600">Compile</font> the program by typing <b><tt><font color="#990000">make
|
||||
mytest</font></tt></b> at system prompt</li>
|
||||
|
||||
<li>
|
||||
<font color="#FF6600">Run</font> the new program by entering the command
|
||||
<b><tt><font color="#990000">mytest</font></tt></b>
|
||||
at system prompt.</li>
|
||||
</ul>
|
||||
|
||||
<h3>
|
||||
<font color="#000099">Exercise 2: check the differences between both programs</font></h3>
|
||||
Go and take a look at the code for these programs <font color="#000000">(<a href="FirstBitGA.html">Bit</a>
|
||||
- <a href="FirstRealGA.html">Real</a>)</font>. Use the symbolic representation
|
||||
of an Evolutionary Algorithm (you should understand that figure now, otherwise
|
||||
go <a href="eoIntro.html">there</a> and come back) to understand how each
|
||||
part of the EA is coded. Try to spot the differences between both codes:
|
||||
there are not so many!
|
||||
<br>After you've tried that alone, take a look at the <a href="Firstmerge.html">solution</a>
|
||||
:-)
|
||||
<br>
|
||||
<h3>
|
||||
<font color="#000099">Exercise 3: change the selection procedure</font></h3>
|
||||
This is rather straightforward ... if you know what other types of selection
|
||||
are available!
|
||||
<br>At the moment, let's only consider only the following simple ones:
|
||||
<ul>
|
||||
<li>
|
||||
You already know the <font color="#FF6600">tournament selection</font></li>
|
||||
|
||||
<br><font color="#FF0000">Syntax: </font><tt><font color="#009900"><b>eoDetTournament<Indi>
|
||||
select(T_SIZE); </b>// T_SIZE in [2,POP_SIZE)</font></tt>
|
||||
<li>
|
||||
Try the well-known <font color="#FF6600">roulette wheel</font></li>
|
||||
|
||||
<br> <font color="#FF0000">Syntax: </font> <b><tt><font color="#009900">eoProportional<Indi>
|
||||
select;</font></tt></b>
|
||||
<li>
|
||||
Or the <font color="#FF6600">stochastic binary tournament</font></li>
|
||||
|
||||
<br><font color="#FF0000">Syntax: </font><tt><font color="#009900"><b>eoStochTournament<Indi>
|
||||
select(RATE); </b> // RATE in ]0.5,1]</font></tt>
|
||||
<li>
|
||||
and of course the <font color="#FF6600">random</font> selection should
|
||||
give bad results!</li>
|
||||
|
||||
<br><font color="#FF0000">Syntax: </font><b><tt><font color="#009900">eoSelectRandom<Indi>
|
||||
select;</font></tt></b></ul>
|
||||
Note that all these classes of eoObjects are derived from the abstract
|
||||
class
|
||||
<b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eoselectone.html">eoSelectOne.</a></font></font></b>
|
||||
<p><b><font color="#000099"><font size=+2>Lessons:</font></font></b>
|
||||
<ul>
|
||||
<li>
|
||||
in EO, all actions are performed by <a href="eoProgramming.html#functors">functor
|
||||
objects</a> (this section is the last time in this tutorial that there
|
||||
is a direct link to the <a href="eoProgramming.html">EO Programming hints</a>
|
||||
page - though the link at top and bottom of all pages will remain there).</li>
|
||||
|
||||
<li>
|
||||
in EO, all object you will usually need to manipulate are <a href="eoProgramming.html#templates">templatized</a><b><font color="#FF6600">
|
||||
w.r.t. the type of the individual</font></b> you are handling.</li>
|
||||
|
||||
<li>
|
||||
The type of the individual is itself <a href="eoProgramming.html#templates">templatized</a><b><font color="#FF6600">
|
||||
w.r.t. the type of fitness</font></b> (double by default).</li>
|
||||
|
||||
<li>
|
||||
In EO (actually, in EC!) <font color="#CC33CC">initialization and variation</font>
|
||||
operators are <font color="#999900">representation</font>-dependent, while
|
||||
the <font color="#009900">evolution engine</font> is <font color="#999900">representation</font>-independent
|
||||
(well, like any rule, this one does have some exceptions).</li>
|
||||
|
||||
<li>
|
||||
Changing the <font color="#990000">fitness function</font>, or the <font color="#009900">selection
|
||||
procedure</font> inside the generational GA evolution engine is straightforward.</li>
|
||||
|
||||
<li>
|
||||
remember, all <font color="#FF6600">solutions</font> to exercises are in
|
||||
the same sub-dir of dir Tutorial than the lesson itself (see <a href="NoWay.html">here</a>).</li>
|
||||
</ul>
|
||||
|
||||
<hr WIDTH="100%"><a href="eoLesson2.html">Lesson 2</a> -
|
||||
<a href="eoTutorial.html">Tutorial
|
||||
main page</a> -
|
||||
<a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
|
||||
documentation</a></font></font>
|
||||
<br>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:Marc.Schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Fri Nov 3 18:49:12 CET 2000 --><!-- hhmts start -->Last
|
||||
modified: Fri Nov 3 18:49:12 CET 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
301
eo/tutorial/html/eoLesson2.html
Normal file
301
eo/tutorial/html/eoLesson2.html
Normal file
|
|
@ -0,0 +1,301 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.75 [en] (X11; U; Linux 2.2.17-21mdksmp i686) [Netscape]">
|
||||
<title>Tutorial: Lesson 2</title>
|
||||
<!-- Changed by: Marc Schoenauer, 29-Nov-2000 -->
|
||||
</head>
|
||||
<body text="#000000" link="#0000EE" vlink="#551A8B" alink="#FF0000" background="beige009.jpg">
|
||||
<a href="eoLesson1.html">Lesson 1</a> -
|
||||
<a href="eoLesson3.html">Lesson
|
||||
3</a> -
|
||||
<a href="eoTutorial.html">Main page</a> -
|
||||
<a href="eoTopDown.html">Top-Down</a>
|
||||
- <a href="eoBottomUp.html">Bottom-up</a> - <a href="eoProgramming.html">Hints</a>
|
||||
-<font size=+1> <b><font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font></b></font>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">Tutorial Lesson 2: more encapsulations</font></h1></center>
|
||||
In this lesson, the same Evolutionary Algorithm will be rewritten in a
|
||||
much more general context.
|
||||
<br>First, look at the <a href="#changes">changes</a> that have been done
|
||||
to the algorithms. Then benefit from the new features by
|
||||
<ul>
|
||||
<li>
|
||||
<a href="#minimize">minimizing</a> (and not only maximize) the fitness</li>
|
||||
|
||||
<li>
|
||||
<a href="#combined_operators">combining</a> <font color="#CC33CC">several
|
||||
operators</font> of the same type</li>
|
||||
|
||||
<li>
|
||||
<a href="#combinedContinue">combining</a> <font color="#3366FF">several
|
||||
stopping criteria</font></li>
|
||||
|
||||
<li>
|
||||
use <font color="#009900"><a href="evolution">alternate</a> selection/replacement</font>
|
||||
engines, deviating from the pure generational GA</li>
|
||||
</ul>
|
||||
|
||||
<p><br>Again, two basic algorithms are provided, namely <a href="FirstBitEA.html">FirstBitEA</a>
|
||||
and <a href="FirstRealEA.html">FirstRealEA</a>.
|
||||
<br><font color="#FF6600">To compile and run</font> them, go to the <font color="#FF6600">Lesson2
|
||||
sub-directory</font> of the tutorial dir and simply type <b><tt><font color="#990000"><font size=+1>make</font></font></tt></b>.
|
||||
Both examples should get compiled, and you can then run them by calling
|
||||
their name from the system prompt.
|
||||
<p>Note the slim difference in names, from <font color="#FF6600">GA</font>
|
||||
to <font color="#FF6600">EA</font>: the behavior of these EAs is
|
||||
almost identical to that of their GA counterpart, at least with the default
|
||||
settings that are provided. But their potentialities for easy modifications
|
||||
are much larger, both in terms of <b><font color="#CC33CC">variation operators</font></b>
|
||||
and of <b><font color="#009900">evolution engine</font></b> (i.e. <font color="#009900">selection/replacement</font>
|
||||
mechanism).
|
||||
<hr WIDTH="100%"><a NAME="changes"></a><b><font color="#000099"><font size=+2>Changes</font></font></b>
|
||||
<p>Browse through the code, and discover them one after the other:
|
||||
<ul>
|
||||
<li>
|
||||
<font color="#000000">The</font><font color="#CC0000"> fitness function
|
||||
</font><font color="#000000">now
|
||||
lies in a </font><b><font color="#CC0000">separate file
|
||||
</font></b><font color="#000000">(<a href="FirstBitEA.html#evalfunc">Bit</a>
|
||||
- <a href="FirstRealEA.html#evalfunc">Real</a>). But, more important, its
|
||||
argument is a <a href="binary_value.html">vector<bool></a> or a <a href="real_value.html">vector<double></a>,
|
||||
and not an unknown type. This will allow to use the same file for any EO
|
||||
object that is a sub-class of the corresponding STL vector class.</font></li>
|
||||
|
||||
<p><b><font color="#FF0000">Note:</font></b> <font color="#000000">Also,
|
||||
a non-templatized fitness can be </font><b><font color="#FF6600">compiled
|
||||
separately</font></b><font color="#000000"> (not done here) into an object
|
||||
file once and forall (<a href="eoProgramming.html#templates">remember</a>
|
||||
that templates forbid that).</font>
|
||||
<br>
|
||||
<li>
|
||||
<a NAME="minimize"></a><font color="#000000">The </font><font color="#CC0000"><b>encapsulation
|
||||
</b>of
|
||||
the fitness </font><font color="#000000">(<a href="FirstBitEA.html#eval">Bit</a>
|
||||
- <a href="FirstRealEA.html#eval">Real</a>) looks more complicated: you
|
||||
have to declare 3 template arguments: the type of EO object it will be
|
||||
applied to, the return type and the type of argument the function actually
|
||||
requires.</font></li>
|
||||
|
||||
<p><b><font color="#FF0000">Note:</font></b> <font color="#000000">In the
|
||||
previous files (<a href="FirstBitGA.html#eval">Bit</a> - <a href="FirstRealGA.html#eval">Real</a>)
|
||||
, the last 2 types were deduced from the first (2nd argument = fitness
|
||||
type of EO object, third = first).</font>
|
||||
<br>
|
||||
<li>
|
||||
<font color="#000000">Both the above modifications makes it very easy to
|
||||
</font><b><font color="#CC0000">minimize</font></b><font color="#000000">
|
||||
rather than maximize a fitness function (see <a href="#Execrise1">Exercise
|
||||
1)</a>.</font></li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
<font color="#000000">The</font><font color="#CC33CC"> initialization</font><font color="#000000">
|
||||
of the population is now </font><b><font color="#CC33CC">encapsulated</font></b><font color="#000000">into
|
||||
a </font><font color="#CC33CC"><b>separate initializer</b> </font><font color="#000000">(based
|
||||
on a <a href="FirstBitEA.html#init">boolean generator</a> or a <a href="FirstRealEA.html#init">double-number
|
||||
generator</a> -see <a href="doc/html/class_random_generator.html">random_generators.h</a>)
|
||||
that is then used in the constructor of the population to build the individuals.
|
||||
You can also use different initializers and call them in turn through the
|
||||
call to <a href="doc/html/class_eopop.html#a2">pop.append()</a> function
|
||||
(see <a href="#exercise2">Exercise 2</a>).</font></li>
|
||||
|
||||
<p><b><font color="#FF0000">Note</font><font color="#CC33CC">: </font></b><font color="#000000">Don't
|
||||
forget to </font><b><font color="#CC0000">evaluate the population</font></b><font color="#000000">:
|
||||
the eoPop has no idea of the eval function, so it has to be done from outside!!!</font>
|
||||
<br>
|
||||
<li>
|
||||
<a NAME="combined_operators"></a><font color="#000000">You can now use
|
||||
</font><font color="#CC33CC"><b>different
|
||||
</b>crossover
|
||||
and mutation
|
||||
<b>operators</b></font><font color="#000000">in the same algorithm
|
||||
(<a href="FirstBitEA.html#operators">Bit</a> - <a href="FirstRealEA.html#operators">Real</a>),
|
||||
choosing among them according to
|
||||
</font><font color="#CC33CC"><b>relative
|
||||
rates.</b> </font><font color="#000000">The class </font><font color="#CC33CC"><b>eoPropCombinedxxxOp</b>,
|
||||
</font><font color="#000000">where
|
||||
xxx is either Mon (for mutation, of class </font><font color="#CC33CC"><b><tt>eoMonOp</tt></b>)</font><font color="#000000">
|
||||
or Quad (for crossovers, of class </font><b><tt><font color="#CC33CC">eoQuadOp</font></tt></b><font color="#000000">),
|
||||
is derived from the corresponding eoxxxOp class. When applying the eoPropCombinedxxxOp,
|
||||
one of the eoxxxOp it contains is chosen by a <a href="doc/html/class_eorng.html#a12">roulette
|
||||
wheel,</a> according to their respective rates, and is applied to the arguments.</font></li>
|
||||
|
||||
<p><b><font color="#FF0000">Note:</font></b> A third optional argument
|
||||
in method <b><tt><font color="#660000">add</font></tt></b> is a boolean
|
||||
(defaulted to false). When true, the actual rates for all operators are
|
||||
displayed on the screen as percentages: you don't have to input rates that
|
||||
sum up to 1, all rates are scaled anyway.
|
||||
<p><b><font color="#FF0000">Note:</font></b> The operators have to be encapsulated
|
||||
into an <b><tt><font color="#CC33CC">eoTransform</font></tt></b> object
|
||||
(<a href="FirstBitEA.html#transform">Bit</a> - <a href="FirstRealEA.html#transform">Real</a>)
|
||||
to be passed to the <b><tt><font color="#FF6666">eoEasyEA </font></tt></b>algorihtm.
|
||||
The <b><tt><font color="#CC33CC">eoSGATransform</font></tt></b> is a simple
|
||||
<b><tt><font color="#CC33CC">eoTransform</font></tt></b>
|
||||
that does exactly the same thing than <b><tt><font color="#FF6666">eoSGA</font></tt></b>:
|
||||
each pair from the selected parents undergoes the <font color="#CC33CC">crossover
|
||||
operator with given probablity</font>, and all individuals (after crossover
|
||||
eventually) undergo <font color="#CC33CC">mutation with given probability</font>.
|
||||
The arguments to the <b><tt><font color="#CC33CC">eoSGATransform</font></tt></b>
|
||||
are an <b><tt><font color="#CC33CC">eoQuadOp</font></tt></b> with its probability
|
||||
and an <b><tt><font color="#CC33CC">eoMonOp</font></tt></b> with the associated
|
||||
probability.
|
||||
<br>
|
||||
<li>
|
||||
<a NAME="combinedContinue"></a><font color="#000000">You can use </font><b><font color="#3366FF">combinations
|
||||
</font></b><font color="#000000">of
|
||||
several stopping criteria by using an object of the class </font><b><tt><font color="#3366FF">eoCombinedContinue
|
||||
</font></tt></b><font color="#000000">(<a href="FirstBitEA.html#stop">Bit</a>
|
||||
- <a href="FirstRealEA.html#stop">Real</a>). Initialize it with an object
|
||||
of class </font><font color="#3366FF"><b><tt>eoContinue</tt></b>, </font><font color="#000000">and
|
||||
</font><b><tt><font color="#3366FF">add</font></tt></b><font color="#000000">as
|
||||
many of other such objects as you wish. And as an </font><b><tt><font color="#3366FF">eoCombinedContinue
|
||||
</font></tt><font color="#FF6600">is
|
||||
an</font><tt><font color="#3366FF">eoContinue</font></tt></b><font color="#000000">,
|
||||
simply pass it to the algorithm (<a href="FirstBitEA.html#generation">Bit</a>
|
||||
- <a href="FirstRealEA.html#generation">Real</a>).</font></li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
<a NAME="evolution"></a><font color="#000000">The</font><b><font color="#009900">
|
||||
full selection/replacement mechanism </font></b><font color="#000000">is
|
||||
now in place through the </font><font color="#FF6666"><b><tt>eoEasyEA</tt></b>
|
||||
algorithm.</font></li>
|
||||
|
||||
<br>This means that you can use different <font color="#009900">selectors</font>.
|
||||
which was already true in Lesson 1, but also different <font color="#009900">replacement
|
||||
strategies</font> (see <a href="#Exercise3">Exercise 3</a>) whereas <font color="#009900">generational
|
||||
replacement</font> was hard-coded in the algorithm <b><tt><font color="#FF6666">eoSGA</font></tt></b>
|
||||
used in Lesson1.
|
||||
<p>Beware that we have to encapsulate <font color="#000000">(<a href="FirstBitEA.html#select_encapsulate">Bit</a>
|
||||
- <a href="FirstRealEA.html#select_encapsulate">Real</a>) </font>the <font color="#FF6600">eoDetTournament</font>,
|
||||
which is of class <font color="#FF6600">eoSelectOne</font> (i.e. allows
|
||||
to select one individual from a population, its <b><tt><font color="#660000">operator()</font></tt></b>
|
||||
returning a single individual) into an object of the <font color="#FF6600">eoSelectPerc</font>
|
||||
(perc stands for percentage) which allows to select a ... percentage of
|
||||
a population (his <b><tt><font color="#660000">operator()</font></tt></b>
|
||||
returns a population). This was done internally in the <a href="doc/html/class_eosga.html#a0">constructor
|
||||
of eoSGA</a> - see lesson1.</ul>
|
||||
|
||||
<hr WIDTH="100%"><a NAME="Exercise1"></a><b><font size=+2><font color="#000099">Exercice
|
||||
1: </font><font color="#FF0000">minimizing</font></font></b>
|
||||
<br><font color="#000000">Modify the algorithm so that it minimizes the
|
||||
fitness.</font>
|
||||
<ul>
|
||||
<li>
|
||||
<font color="#000000">For the bitstring case, you only have to modify the
|
||||
<a href="FirstBitEA.html#representation">declaration
|
||||
of the representation</a>, using </font><b><tt><font color="#999900">eoMinimizingFitness</font></tt></b><font color="#000000">
|
||||
instead of </font><b><tt><font color="#999900">double</font></tt></b><font color="#000000">.
|
||||
But is that really all? Give it a try, look at the output, and do it right
|
||||
the second time!!!</font></li>
|
||||
|
||||
<li>
|
||||
<font color="#000000">For the real-valued problem, you also need to modify
|
||||
the file <b><tt><a href="real_value.html">real_value.h</a></tt></b> so
|
||||
that it returns the sum of squares instead of its inverse. And again there
|
||||
is something else to modify...</font></li>
|
||||
</ul>
|
||||
<a NAME="exercise2"></a><b><font size=+2><font color="#000099">Exercice
|
||||
2: </font><font color="#FF0000">initialization</font></font></b>
|
||||
<br><font color="#000000">Use different initializers: for instance, on
|
||||
the real-valued sphere function minimization, try to initialize half of
|
||||
the population in [-2,-1] and the other half in [1,2], with and without
|
||||
the segment and arithmetic crossovers (and for large values of VEC_SIZE,
|
||||
the size of the vectors). Amazing, isn't it! Explain that result.</font>
|
||||
<p><a NAME="Exercise3"></a><b><font size=+2><font color="#000099">Exercice
|
||||
3: </font><font color="#FF0000">replacement</font></font></b>
|
||||
<br><font color="#000000">You can now twidle the number of offspring that
|
||||
will be generated from the parents. But of course you need to adjust the
|
||||
replacement to keep a constant population size.</font>
|
||||
<ul>
|
||||
<li>
|
||||
<font color="#000000">To </font><font color="#FF6600">modify the number
|
||||
of offspring</font><font color="#000000">, use the second argument of the
|
||||
</font>encapsulator<font color="#000000">
|
||||
(<a href="FirstBitEA.html#select_encapsulate">Bit</a> - <a href="FirstRealEA.html#select_encapsulate">Real</a>)
|
||||
of the </font><font color="#009900">selector</font><font color="#000000">
|
||||
of class </font><b><tt><font color="#009900">eoSelectOne</font></tt></b><font color="#000000">
|
||||
into an eoSelectPerc object. For instance, try</font></li>
|
||||
|
||||
<br><b><tt><font color="#009900">
|
||||
eoSelectPerc<Indi> select(selectOne,2.0)</font></tt></b>
|
||||
<br><font color="#000000">to generate twice as many offspring as there
|
||||
are parents.</font>
|
||||
<li>
|
||||
<font color="#000000">To </font><font color="#FF6600">keep a constant population
|
||||
size</font><font color="#000000">, you can use either the </font><b><tt><font color="#009900">eoCommaReplacement</font></tt></b><font color="#000000">
|
||||
class, or the </font><b><tt><font color="#009900">eoPlusReplacement</font></tt></b><font color="#000000">.
|
||||
The former selects the best offspring to replace the parents, the latter
|
||||
selects the best among parents+offspring. Of course you cannot use </font><b><tt><font color="#009900">eoCommaReplacement</font></tt></b><font color="#000000">
|
||||
if you have less offspring than parents!</font></li>
|
||||
|
||||
<br>Now if you use <b><tt><font color="#009900">eoSelectRandom</font></tt></b>
|
||||
as selector with a rate of
|
||||
<i>lambda</i>, you end up with exactly the <i>(mu+lambda)</i>
|
||||
or
|
||||
<i>(mu,lambda)</i> strategies from <a href="intro.html#ES">Evolution
|
||||
Strategies</a>.
|
||||
<li>
|
||||
<font color="#FF0000">Question</font><font color="#000000">: what do you
|
||||
get if you have use a rate of 1/POP_SIZE for the selection, and an </font><b><tt><font color="#009900">eoPlusReplacement</font></tt></b><font color="#000000">
|
||||
strategy? Yes, you get almost the replace_worst Steady-State GA, though
|
||||
rather inefficient, as you sort the population at every generation, which
|
||||
could be avoided - and will be in a later lesson).</font></li>
|
||||
|
||||
<li>
|
||||
<font color="#FF0000">Homework</font><font color="#000000">: Write the
|
||||
</font><b><tt><font color="#009900">eoCommaPlusReplacement</font></tt></b><font color="#000000">
|
||||
that would start by taking the best of the offspring, and if some are still
|
||||
missing to keep the population size constant, take the best of the parents.
|
||||
Write the </font><b><tt><font color="#009900">eoConservativeReplacement</font></tt></b><font color="#000000">
|
||||
that starts by taking a percentage of the parents (the best ones) and then
|
||||
adds the best from the offspring. In both cases, </font><font color="#FF6600">send
|
||||
use the code </font><font color="#000000">as we haven't done that yet (and
|
||||
hence there is no solution available at the moment - Nov. 29 :-)</font></li>
|
||||
</ul>
|
||||
<font color="#FF0000">Remember</font><font color="#000000">: all solutions
|
||||
are in the same sub-directory of the Tutorial dir than the examples (i.e.
|
||||
here Lesson2), and are described <a href="NoWay.html">here</a>.</font>
|
||||
<p>
|
||||
<hr WIDTH="100%">
|
||||
<br><b><font color="#000099"><font size=+2>Lessons learned:</font></font></b>
|
||||
<ul>
|
||||
<li>
|
||||
How to write a <font color="#CC0000">fitness function</font> that only
|
||||
needs a genotype, not a full individual. Moreover you can compile it separately.</li>
|
||||
|
||||
<br>How to <font color="#CC33CC">initialize</font> the population using
|
||||
random generators
|
||||
<li>
|
||||
How to use other <font color="#009900">evolution engine</font> than the
|
||||
simple generational GA.</li>
|
||||
|
||||
<li>
|
||||
How to combine different objects of the same kind into a single object
|
||||
that you can use like a simple basic object (<font color="#CC33CC">operators</font>
|
||||
and <font color="#3366FF">stopping criteria</font> here).</li>
|
||||
</ul>
|
||||
|
||||
<hr WIDTH="100%"><a href="eoLesson1.html">Lesson 1</a> -
|
||||
<a href="eoLesson3.html">Lesson
|
||||
3</a> -
|
||||
<a href="eoTutorial.html">Main page</a> -
|
||||
<a href="eoTopDown.html">Top-Down</a>
|
||||
- <a href="eoBottomUp.html">Bottom-up</a> - <a href="eoProgramming.html">Hints</a>
|
||||
- <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
|
||||
documentation</a></font></font></b>
|
||||
<br>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:Marc.Schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Fri Nov 3 18:49:12 CET 2000 --><!-- hhmts start -->Last
|
||||
modified: Fri Nov 3 18:49:12 CET 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
531
eo/tutorial/html/eoLesson3.html
Normal file
531
eo/tutorial/html/eoLesson3.html
Normal file
|
|
@ -0,0 +1,531 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.75 [en] (X11; U; Linux 2.2.17-21mdksmp i686) [Netscape]">
|
||||
<title>Tutorial: Lesson 3</title>
|
||||
</head>
|
||||
<body text="#000000" link="#0000EE" vlink="#551A8B" alink="#FF0000" background="beige009.jpg">
|
||||
<a href="eoLesson2.html">Lesson 2</a> -
|
||||
<a href="eoLesson4.html">Lesson
|
||||
4</a> -
|
||||
<a href="eoTutorial.html">Main page</a> -
|
||||
<a href="eoTopDown.html">Top-Down</a>
|
||||
- <a href="eoBottomUp.html">Bottom-up</a> - <a href="eoProgramming.html">Hints</a>
|
||||
- <b><font face="Arial,Helvetica"><a href="doc/html/index.html">EO documentation</a></font></b>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">Tutorial Lesson 3: input/output</font></h1></center>
|
||||
In this lesson, you will still use the same Evolutionary Algorithm, BUT
|
||||
in a much more <b><font color="#FF6600">user-friendly</font></b> way. You
|
||||
will discover how to
|
||||
<ul>
|
||||
<li>
|
||||
<a href="#parser">input parameters</a> on the command-line or from a text
|
||||
file</li>
|
||||
|
||||
<li>
|
||||
<a href="#state">save</a> the population to disk, together with every part
|
||||
of the algrithm you could think of - so you can decide to <font color="#FF6600">reload</font>
|
||||
everything later to continue the same run, eventually with different parameters.</li>
|
||||
|
||||
<li>
|
||||
<a href="#stats">generate statistics</a> on the populations, and <a href="#monitor">output
|
||||
them</a> to the screen, text or graphic, or to a file (or to any other
|
||||
device you might want to use).</li>
|
||||
</ul>
|
||||
First, but you should now have done it without being told, go into the
|
||||
<font color="#FF6600">Lesson3
|
||||
sub-dir</font> of the tutorial dir and type
|
||||
<b><tt><font color="#990000"><font size=+1>make</font></font></tt></b>.
|
||||
This will compile the <b><tt><font color="#660000"><font size=+1>SecondBitEA</font></font></tt></b>
|
||||
program (and, some day, SecondRealEA).
|
||||
<p>You can then either
|
||||
<ul>
|
||||
<li>
|
||||
browse the corresponding code (only <a href="SecondBitEA.html">SecondBitEA</a>
|
||||
available right now, but you can figure out how SecondRealEA will look
|
||||
like),</li>
|
||||
|
||||
<li>
|
||||
look at the <a href="#changes">summary of changes</a>,</li>
|
||||
|
||||
<li>
|
||||
or find out directly explanations about the new features: the <a href="#parser">eoParser</a>,
|
||||
<a href="#state">eoState</a>
|
||||
and <a href="#checkpoint">eoCheckpoint</a> classes.</li>
|
||||
</ul>
|
||||
|
||||
<p><br>
|
||||
<hr WIDTH="100%">
|
||||
<br><a NAME="changes"></a><b><font color="#000099"><font size=+2>Changes</font></font></b>
|
||||
<br><font color="#000000">As already said, the behavior of the algorithm
|
||||
will be exactly the same as the previous one as far as optimization is
|
||||
concerned. Only the input (of algorithm parameters) and output (of program
|
||||
results) will be very different.</font>
|
||||
<br><font color="#000000">Hence, the sections corresponding to the <a href="SecondBitEA.html#eval">fitness
|
||||
function</a>, the <a href="SecondBitEA.html#init">initialization</a>, the
|
||||
<a href="SecondBitEA.html#operators">variation
|
||||
operators</a>, the <a href="SecondBitEA.html#engine">evolution engine</a>
|
||||
and the <a href="SecondBitEA.html#generation">algorithm</a> itself are
|
||||
almost identical (apart from variable name changes).</font>
|
||||
<ul>
|
||||
<li>
|
||||
<font color="#000000"><a href="SecondBitEA.html#eval">Fitness function</a>:
|
||||
there is an </font><font color="#FF6600">additional line</font><font color="#000000">
|
||||
after the encapsulation of our </font><font color="#CC0000">binary_function</font><font color="#000000">
|
||||
into an </font><b><tt><font color="#CC0000"><font size=+1>eoEvalFunc</font></font></tt></b><font color="#000000">
|
||||
object, which again encapsulate the </font><b><tt><font color="#CC0000"><font size=+1>eoEvalFunc</font></font></tt></b><font color="#000000">
|
||||
into an </font><b><tt><font color="#CC0000"><font size=+1>eoEvalFuncCounter</font></font></tt></b><font color="#000000">.
|
||||
As its name says, thisobject will, in addition to computing the fitness,
|
||||
count the </font><b><font color="#FF6600">actual</font></b><font color="#000000">
|
||||
number of evaluations: the fitness of non-modified individuals is of course
|
||||
not recomputed - and this is taken care of by this object. Moreover, it
|
||||
can be later </font><font color="#FF6600">used for displays</font><font color="#000000">
|
||||
in <a href="#monitor">eoMonitor</a> objects, as done in the <a href="SecondBitEA.html#eval_monitor">checkpoint
|
||||
section</a>.</font></li>
|
||||
|
||||
<li>
|
||||
<font color="#000000">The <a href="SecondBitEA.html#init">initialization</a>
|
||||
section has been extended to account for the possibility to </font><font color="#CC33CC">re-load
|
||||
a previously saved population</font><font color="#000000">. This is achieved
|
||||
through an <a href="#state">eoState</a> object, if the corresponding program
|
||||
parameter is set.</font></li>
|
||||
|
||||
<li>
|
||||
<font color="#000000">The
|
||||
<a href="SecondBitEA.html#operators">variation
|
||||
operators</a> and the <a href="SecondBitEA.html#engine">evolution engine</a>
|
||||
sections are similar to the ones in Lesson2</font></li>
|
||||
|
||||
<li>
|
||||
<font color="#000000">The </font><b><font color="#3366FF">parameter section</font></b><font color="#000000">
|
||||
is completely different from the previous one. All variables corresponding
|
||||
to </font><font color="#3366FF">program parameters</font><font color="#000000">
|
||||
are now </font><a href="SecondBitEA.html#seed_declare">declared</a><font color="#000000">
|
||||
in the </font><b><tt><font color="#990000"><font size=+1>main_function</font></font></tt></b><font color="#000000">
|
||||
(as before), but their values are set in a <a href="SecondBitEA.html#parametres">new
|
||||
function</a>, called </font><b><tt><font color="#660000">read_param.</font></tt></b><font color="#000000">
|
||||
See the <a href="#parser">eoParser</a> description for more details.</font></li>
|
||||
|
||||
<li>
|
||||
<font color="#000000">The <a href="SecondBitEA.html#stop">stopping criterion
|
||||
section</a>, has in fact now become the checkpoint section, as it involves
|
||||
much more than just stopping criteria. See all details in the <a href="#checkpoint">eoCheckpoint
|
||||
paragraph below</a>.</font></li>
|
||||
</ul>
|
||||
|
||||
<hr WIDTH="100%"><a NAME="parser"></a><b><font color="#000099"><font size=+2>eoParser:
|
||||
parameter input</font></font></b>
|
||||
<br><font color="#000000">The first two examples of Lessons 1 and 2 had
|
||||
a very crude way to set parameter values: they were hard-coded, and you
|
||||
had to recompile the whole program to change a single value. We shall now
|
||||
see now to set parameter values in a flexible way (though we're still looking
|
||||
for volunteers to create a Graphical User Interface :-)</font>
|
||||
<br><font color="#000000">Two base classes are used for that purpose:</font>
|
||||
<ul>
|
||||
<li>
|
||||
<font color="#000000">The </font><b><tt><font color="#3366FF">eoValueParam</font></tt></b><font color="#000000">
|
||||
class, </font><font color="#FF6600">templatized by the type of the variable</font><font color="#000000">
|
||||
you want to handle (i.e. i</font><b><tt><font color="#660000">nteger, double,
|
||||
yourPrivateClass</font></tt></b><font color="#000000">, ...). In this lesson,
|
||||
we will not go into details: e.g. we will not tell you that the
|
||||
<b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/class_eovalueparam.html">eoValueParam</a></font></font></b>
|
||||
is actually a templatized sub-class of abstract class eoParam (oups, I
|
||||
said it!), nor will we deal with parameters outside their use from an eoParser.
|
||||
See the parameter section of the Bottom-up tutorial, or wait until <a href="eoLesson4.html">lesson
|
||||
4</a>).</font></li>
|
||||
|
||||
<li>
|
||||
<font color="#000000">The </font><b><tt><font color="#3366FF">eoParser</font></tt></b><font color="#000000">
|
||||
class, whose only purpose is the input of parameters.</font></li>
|
||||
</ul>
|
||||
<b><font color="#FF0000">Modifying parameter values at run-time:</font></b>
|
||||
<br><font color="#000000">Using an eoParser object, the parameter values
|
||||
are read, by order of priority</font>
|
||||
<ol>
|
||||
<li>
|
||||
<font color="#000000">from the command-line</font></li>
|
||||
|
||||
<li>
|
||||
<font color="#000000">from a text file</font></li>
|
||||
|
||||
<li>
|
||||
<font color="#000000">from the environement</font></li>
|
||||
|
||||
<li>
|
||||
<font color="#000000">from default values</font></li>
|
||||
</ol>
|
||||
The syntax of parameter reading is a <b><font color="#FF6600">keyword</font></b>-based
|
||||
syntax, now traditional in the Unix world:
|
||||
<ul>
|
||||
<li>
|
||||
in EO, each parameter is designated by <font color="#FF6600">a (long) keyword</font>,
|
||||
and optionally by a <font color="#FF6600">short (1 character) keyword</font>.</li>
|
||||
|
||||
<br>
|
||||
<li>
|
||||
the general syntax to modify parameter value at run-time is (either from
|
||||
the command-line or in a text file)</li>
|
||||
|
||||
<br>
|
||||
<b><tt><font color="#660000">--longKeyword=value</font></tt></b>
|
||||
or <b><tt><font color="#660000">-c=value</font></tt></b>
|
||||
if 'c' is the short keyword
|
||||
<br>
|
||||
<li>
|
||||
so, after compiling the executable for Lesson 3 (<b><tt><font color="#FF6666">make
|
||||
lesson3</font></tt></b> at system prompt in Unix), you can try to type
|
||||
in</li>
|
||||
|
||||
<br>
|
||||
<b><tt><font color="#FF6666">SecondBitEA</font></tt></b>
|
||||
<br>and see the algorithm run as before (OneMax optimized on 8-bits bitstrings).
|
||||
But you can now type in
|
||||
<br>
|
||||
<b><tt><font color="#FF6666">SecondBitEA --vecSize=100</font></tt></b>
|
||||
<br>and see the output of the optimization of OneMax on 100-bit bitstings.
|
||||
<br>
|
||||
<li>
|
||||
Take a look at all available parameters by typing in</li>
|
||||
|
||||
<br>
|
||||
<b><tt><font color="#FF6666">SecondBitEA --help</font></tt></b>
|
||||
<br>or by going into the code: all parameter inputs have been grouped in
|
||||
the
|
||||
<a href="SecondBitEA.html#parametres">read_param</a> function.
|
||||
<br>
|
||||
<li>
|
||||
After running the algorithm, a new file has been created, named <b><tt><font color="#990000">SecondBitEA.status</font></tt></b>:
|
||||
it contains the list of all actual parameters used, and can directly be
|
||||
used as parameter input file: change the file name (e.g. to <b><tt><font color="#660000">SecondBitEA.param</font></tt></b>),
|
||||
edit it, change whichever parameter you want, and type in</li>
|
||||
|
||||
<br>
|
||||
|
||||
<b><tt><font color="#FF6666">SecondBitEA @SecondBitEA.param</font></tt></b>
|
||||
<br>and you will see all values that you defined into the file taken into
|
||||
account.
|
||||
<br>
|
||||
<li>
|
||||
The <font color="#FF6600">priority</font> remains to the <font color="#FF6600">command-line</font>,
|
||||
so you can still override the values in the parameter file by giving a
|
||||
new value directly on the command-line.</li>
|
||||
</ul>
|
||||
<b><font color="#FF0000">Programming parameter input:</font></b>
|
||||
<br>the code of SeconBitEA provides examples of parameters reading. Lets
|
||||
take the example of the random number generator <b><tt><font color="#660000">seed</font></tt></b>.
|
||||
<ul>
|
||||
<li>
|
||||
You first need to <a href="SecondBitEA.html#seed_declare">declare</a> it
|
||||
in the <b><tt><font color="#660000">main_function</font></tt></b>. As parameter
|
||||
reading will be done in the <b><tt><font color="#660000">read_param</font></tt></b>
|
||||
function, you need to pass it the variable seed. Note that read_param receives
|
||||
it <a href="SecondBitEA.html#seed_passing">by reference</a>, as it is going
|
||||
to modify its value!</li>
|
||||
|
||||
<li>
|
||||
In read_param, you need first to <a href="SecondBitEA.html#parser_declare">declare
|
||||
an eoParser object</a> (it needs the standard argc and argv in its constructor).</li>
|
||||
|
||||
<br>Then, <a href="SecondBitEA.html#_seed_declare">declare a parameter</a>
|
||||
of type <b><tt><font color="#660000">uint32</font></tt></b> (32-bits integer),
|
||||
and read it directly from the parser, using method <b><tt><font color="#660000">create_param</font></tt></b>.
|
||||
The arguments are obvious: default value, long keyword, comment (that will
|
||||
appear in the help message and in the output "status" file if any).
|
||||
<li>
|
||||
Finally, you need to <a href="SecondBitEA.html#seed_assign">assign the
|
||||
value</a> to the variable _seed (hence modifying the original seed variable).</li>
|
||||
</ul>
|
||||
|
||||
<hr WIDTH="100%">
|
||||
<br><a NAME="state"></a><b><font color="#000099"><font size=+2>eoState:
|
||||
saving and loading</font></font></b>
|
||||
<br><font color="#000000">You might have noticed in the </font><b><tt><font color="#660000">read_param</font></tt></b><font color="#000000">
|
||||
described above a </font><font color="#FF6600">new parameter</font><font color="#000000">
|
||||
named </font><b><tt><font color="#660000"><a href="SecondBitEA.html#_load_name">load_name.</a></font></tt></b><font color="#000000">
|
||||
Now if you go to the <a href="SecondBitEA.html#init">init section</a> of
|
||||
the code, you will see an alternative way of </font><font color="#CC33CC">initializing
|
||||
the population</font><font color="#000000">: if load_name is an empty string,
|
||||
then we do as in the preceding example and use an eoInitFixedLength object.
|
||||
However, if a load_name name was entered, the population is read through
|
||||
the </font><b><tt><font color="#660000"><a href="SecondBitEA.html#instate.load">inState.load(load_name)</a></font></tt></b><font color="#000000">
|
||||
instruction. Moreover, the comment says "Loading pop </font><font color="#FF6600">and
|
||||
rng</font><font color="#000000">".</font>
|
||||
<p><font color="#000000">This is made possible using the </font><b><tt><font color="#3366FF">eoState</font></tt></b><font color="#000000">
|
||||
class. </font><b><tt><font color="#3366FF">eoState</font></tt></b><font color="#000000">
|
||||
objects maintain references to </font><b><tt><font color="#660000">eoObjects</font></tt></b><font color="#000000">
|
||||
that have both an input method (</font><b><tt><font color="#3366FF">readFrom</font></tt></b><font color="#000000">)
|
||||
and an output method (</font><b><tt><font color="#3366FF">printOn</font></tt></b><font color="#000000">),
|
||||
i.e. that derive from the base class </font><b><font face="Arial,Helvetica"><font color="#660000"><font size=+1><a href="doc/html/class_eopersistent.html">eoPersistent</a></font></font></font></b><font color="#000000">.
|
||||
You must first </font><b><tt><font color="#3366FF"><a href="SecondBitEA.html#register">register</a></font></tt></b><font color="#000000">
|
||||
object into a state, and can then save them to a (text) file, and later
|
||||
read them from that file using the </font><b><tt><font color="#3366FF">load</font></tt></b><font color="#000000">
|
||||
method, as done <a href="SecondBitEA.html#loadstate">here</a>.</font>
|
||||
<br><font color="#000000">Of course, you can call the </font><b><tt><font color="#3366FF">save</font></tt></b><font color="#000000">
|
||||
method for an </font><b><tt><font color="#3366FF">eoState</font></tt></b><font color="#000000">
|
||||
object anywhere in the code. But the <a href="#statesaver">checkpointing</a>
|
||||
mechanism offers you better ways to do that - and it's so easy ....</font>
|
||||
<p>
|
||||
<hr WIDTH="100%"><a NAME="checkpoint"></a><b><font color="#000099"><font size=+2>eoCheckpoint:
|
||||
every generation I'd like to ...</font></font></b>
|
||||
<br><font color="#000000">The checkpointing mechanism is a very powerfull
|
||||
construct to perform some </font><b><font color="#FF6600">systematic actions</font></b><font color="#000000">
|
||||
every generation - like </font><font color="#FF6600">saving things</font><font color="#000000">
|
||||
(using eoState objects described above), computing </font><font color="#FF6600">statistics</font><font color="#000000">
|
||||
on the population, </font><font color="#FF6600">updating</font><font color="#000000">
|
||||
dynamical parameters or </font><font color="#FF6600">displaying</font><font color="#000000">
|
||||
information.</font>
|
||||
<p><b><tt><font color="#3366FF">eoCheckpoint</font></tt></b><font color="#000000">
|
||||
objects are </font><b><tt><font color="#3366FF">eoContinue</font></tt></b><font color="#000000">
|
||||
objects that contain </font><font color="#FF6600">pointers to different
|
||||
types</font><font color="#000000"> of objects. When their </font><b><tt><font color="#660000">operator()</font></tt></b><font color="#000000">
|
||||
method is called (i.e. every generation in the examples up to now), they
|
||||
first call the </font><b><tt><font color="#660000">operator()</font></tt></b><font color="#000000">
|
||||
methods of all object they contain, and then return their result as an
|
||||
</font><b><tt><font color="#3366FF">eoContinue</font></tt></b><font color="#000000">
|
||||
object (i.e. should we continue or stop).</font>
|
||||
<br><b><font color="#FF0000">Programming: </font></b><font color="#000000">To
|
||||
do something every generation, you simply need to </font><b><tt><font color="#3366FF">add</font></tt></b><font color="#000000">
|
||||
an object whose </font><b><tt><font color="#660000">operator()</font></tt></b><font color="#000000">
|
||||
does what you want to the eoState that you will use as continuator in the
|
||||
algorithm.</font>
|
||||
<br>
|
||||
<hr WIDTH="100%"><a NAME="stop"></a><b><font color="#000099">eoCheckpoint:
|
||||
Stopping</font></b>
|
||||
<br><font color="#000000">The </font><b><tt><font color="#3366FF">eoContinue</font></tt></b><font color="#000000">
|
||||
part of an </font><b><tt><font color="#3366FF">eoCheckpoint</font></tt></b><font color="#000000">
|
||||
is a single object, <a href="SecondBitEA.html#checkpoint">passed to the
|
||||
constructor</a>. If you want more that one stopping criterion, use an <a href="SecondBitEA.html#stop">eoCombinedContinue</a>
|
||||
object as described in <a href="eoLesson2.html#combinedContinue">Lesson2</a>.</font>
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<br><a NAME="stats"></a><b><font color="#000099">eoCheckpoint: Computing
|
||||
statistics</font></b>
|
||||
<br><font color="#000000">Statistics are computed using </font><b><tt><font color="#3366FF">eoStat</font></tt></b><font color="#000000">
|
||||
objects, i.e. functor objects whose </font><b><tt><font color="#660000">operator()</font></tt></b><font color="#000000">
|
||||
receives as argument a reference to a population as argument, and can hence
|
||||
compute whatever is needed over that population. </font><b><tt><font color="#3366FF">eoStat</font></tt></b><font color="#000000">
|
||||
objects are </font><font color="#FF6600">templatized</font><font color="#000000">
|
||||
over the type of what they compute (e.g. </font><b><tt><font color="#660000">double</font></tt></b><font color="#000000">,
|
||||
or </font><b><tt><font color="#660000">pair<double></font></tt></b><font color="#000000">,
|
||||
or ...). But looking at the <a href="doc/html/class_eostat.html">inheritance
|
||||
diagram</a> of the </font><b><tt><font color="#3366FF">eoStat</font></tt></b><font color="#000000">
|
||||
class, you find that </font><b><tt><font color="#3366FF">eoStat</font></tt></b><font color="#000000">
|
||||
objects are also </font><b><tt><font color="#3366FF">eoValueParam</font></tt></b><font color="#000000">
|
||||
objects. And this allows </font><b><tt><font color="#3366FF">eoStat</font></tt></b><font color="#000000">
|
||||
to be used within </font><b><tt><font color="#3366FF">eoMonitor</font></tt></b><font color="#000000">
|
||||
object, and hence </font><b><font color="#FF6600">displayed</font></b><font color="#000000">
|
||||
to the user!</font>
|
||||
<p><b><font color="#FF0000">Available statistics</font></b>: Some widely
|
||||
used statistics are already available (and of course you can build you
|
||||
own!).
|
||||
<ul>
|
||||
<li>
|
||||
<b><tt><font color="#3366FF">eoBestFitnessStat</font></tt></b> returns
|
||||
the fitness value of the best individual in the population (of type <b><tt><font color="#660000">FitnessType</font></tt></b>,
|
||||
whatever this is).</li>
|
||||
|
||||
<li>
|
||||
<b><tt><font color="#3366FF">eoAverageStat</font></tt></b> and <b><tt><font color="#3366FF">eoSecondMomentStat</font></tt></b>
|
||||
respectiveley return the average (type double, assumes that <b><tt><font color="#660000">FitnessType</font></tt></b>
|
||||
is castable to a double) and a pair made of the average and the standard
|
||||
deviation (type <b><tt><font color="#660000">pair<double></font></tt></b>)
|
||||
of the fitnesses in the populations.</li>
|
||||
|
||||
<li>
|
||||
<b><tt><font color="#3366FF">eoDiversityStat</font></tt></b> returns the
|
||||
diversity in the population: asssuming that there is a distance function
|
||||
defined among individuals, it returns the average inter-individuals distance.
|
||||
See also Exercise 2.</li>
|
||||
</ul>
|
||||
<b><font color="#FF0000">Programming</font></b>: To compute some statistics
|
||||
within your algorithm, simply <a href="SecondBitEA.html#stat_declare">declare</a>
|
||||
the corresponding eoStat objects, and <a href="SecondBitEA.html#stat_pass">add</a>
|
||||
them to the <b><tt><font color="#3366FF">eoCheckpoint</font></tt></b> you
|
||||
use in the algorithm.
|
||||
<p><b><font color="#FF0000">Note</font></b>: actually, there are 2 disctinct
|
||||
classes that compute and gove access to statistics: <b><tt><font color="#3366FF">eoStat</font></tt></b>
|
||||
and <b><tt><font color="#3366FF">eoSortedStat</font></tt></b>. As its name
|
||||
indicate, the latter is used whenever computing the statistics require
|
||||
a sorted population: not only this avoids to sort the population many times,
|
||||
but also it avoids changing the order of the population at all as eoSortedStat
|
||||
work on a temporary vector of fitnesses . But <font color="#FF6600">as
|
||||
far as their usage is concerned, its makes no difference</font>.
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<br><a NAME="monitor"></a><b><font color="#000099">eoCheckpoint: Displaying
|
||||
eoParameters</font></b>
|
||||
<br><font color="#000000">The </font><b><tt><font color="#3366FF">eoMonitor</font></tt></b><font color="#000000">
|
||||
objects are used to display a set of </font><b><tt><font color="#3366FF">eoValueParam</font></tt></b><font color="#000000">
|
||||
objects.</font>
|
||||
<p><b><font color="#FF0000">Available monitors</font></b>: A few monitors
|
||||
are available in th eEO distribution:
|
||||
<ul>
|
||||
<li>
|
||||
<b><tt><font color="#3366FF">eoStdoutMonitor</font></tt></b> displays its
|
||||
parameters in <font color="#FF6600">text format on the screen</font>. The
|
||||
(optional) boolean value in the constructor modifies the output: when true
|
||||
(the default), vebose output is used, with one line per parameter. When
|
||||
false, parcimonious output displays one line for all parameters.</li>
|
||||
|
||||
<li>
|
||||
<b><tt><font color="#3366FF">eoStdoutMonitor</font></tt></b> writes its
|
||||
parameters in <font color="#FF6600">text format in a file</font>. A file
|
||||
name is required in the constructor, and an optional separator character
|
||||
can be added (default is ','). Note that the file is overwritten by next
|
||||
call to the same program.</li>
|
||||
|
||||
<li>
|
||||
<b><tt><font color="#3366FF">eoGnuplot1DMonitor</font></tt></b> displays
|
||||
its parameters in <font color="#FF6600">graphical format on the screen</font>
|
||||
by calling the <b><tt><font color="#660000">gnuplot</font></tt></b> program,
|
||||
and as of today, only works in the Unix version of EO (as always, volunteers
|
||||
are welcome to port that to MS Windows). It takes an optional filename
|
||||
as input, as communication of data with <b><tt><font color="#660000">gnuplot</font></tt></b>
|
||||
is done through a file. If no filename is provided, the file will be erased
|
||||
at the end of the run, while it is otherwise kept (though it will be overwritten
|
||||
by next call to the same program).</li>
|
||||
</ul>
|
||||
|
||||
<p><br><b><font color="#FF0000">Programming:</font></b> To display something
|
||||
while the algorithm is running, you need to <a href="SecondBitEA.html#monitor_declare">declare</a>
|
||||
an <b><tt><font color="#3366FF">eoMonitor</font></tt></b> object, <a href="SecondBitEA.html#monitor_add">add</a>
|
||||
some objects (that must be <b><tt><font color="#3366FF">eoValueParam</font></tt></b>
|
||||
objects) to that monitor, and of course <a href="SecondBitEA.html#monitor_pass">add</a>
|
||||
the monitor to the <b><tt><font color="#3366FF">eoCheckpoint</font></tt></b>
|
||||
you use in the algorithm.
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<br><a NAME="update"></a><b><font color="#000099">eoCheckpoint: Updating
|
||||
things</font></b>
|
||||
<br><font color="#000000">The last type of objects that </font><b><tt><font color="#3366FF">eoCheckpoint</font></tt></b><font color="#000000">
|
||||
can handle are </font><b><tt><font color="#3366FF">eoUpdater</font></tt></b><font color="#000000">
|
||||
objects. You should simply encapsulate in an </font><b><tt><font color="#3366FF">eoUpdater</font></tt></b><font color="#000000">
|
||||
anything you wish to do which does not fit into one of the above category.
|
||||
Note that their </font><b><tt><font color="#660000">operator() method</font></tt></b><font color="#000000">
|
||||
does not receive any argument.</font>
|
||||
<p><b><font color="#FF0000">Available monitors</font></b>: A few updaters
|
||||
are available in the EO distribution:
|
||||
<ul>
|
||||
<li>
|
||||
<b><tt><font color="#3366FF">eoIncrementor</font></tt></b> A simple updater
|
||||
which maintains a <font color="#FF6600">counter</font> (an <b><tt><font color="#3366FF">eoValueParam</font></tt></b>
|
||||
that needs to be created beforehand, and passed in the constructor). It
|
||||
is incremented everytime the <b><tt><font color="#660000">operator()</font></tt></b>
|
||||
method is called (every generation at the moment). You can of course also
|
||||
give an increment in the constructor (1 by default).</li>
|
||||
|
||||
<li>
|
||||
<a NAME="statesaver"></a><b><tt><font color="#3366FF">eoCountedStateSaver</font></tt></b>
|
||||
and <b><tt><font color="#3366FF">eoTimedStateSaver</font></tt></b> can
|
||||
be used to <font color="#FF6600">save</font> some existing <b><tt><font color="#3366FF">eoState</font></tt></b>
|
||||
(see <a href="#state">above</a>) to a file regularly, either based on the
|
||||
generation count (e.g. every 4 generations) or based on the clock (e.g.
|
||||
every 5 seconds).</li>
|
||||
</ul>
|
||||
<b><font color="#FF0000">Programming:</font></b>
|
||||
<br>A very simple example of using an <b><tt><font color="#3366FF">eoUpdater</font></tt></b>
|
||||
is given in the code for SecondBitEA: First <a href="SecondBitEA.html#param_declare">declare</a>
|
||||
an <b><tt><font color="#3366FF">eoValueParam</font></tt></b> object, then
|
||||
<a href="SecondBitEA.html#param_pass">use
|
||||
it to construct</a> an <b><tt><font color="#3366FF">eoIncrementor</font></tt></b>
|
||||
that you must <a href="SecondBitEA.html#updater_pass">add</a> to the <b><tt><font color="#3366FF">eoCheckpoint</font></tt></b>
|
||||
in order to activate its update. You can then use the parameter for your
|
||||
purpose, for instance as a first coordinate for a monitor.
|
||||
<br>Note also how to use the statesavers: first <a href="SecondBitEA.html#outstate_declare">declare</a>
|
||||
a state, then <a href="SecondBitEA.html#outstate_register">register</a>
|
||||
whatever you think necessary to that state, then <a href="SecondBitEA.html#statesaver_declare">pass</a>
|
||||
the state to some state-saver - and don't forget to <a href="SecondBitEA.html#statesaver_pass">add</a>
|
||||
the statesavers to the current <b><tt><font color="#3366FF">eoCheckpoint</font></tt></b>.
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<br><b><font color="#000099"><font size=+2>Exercice 1:</font></font></b>
|
||||
<ul>
|
||||
<li>
|
||||
<font color="#000000">The code of </font><b><tt><font color="#660000"><font size=+1>SecondBitEA</font></font></tt></b><font color="#000000">
|
||||
display things in the current window in text format. Replace the </font><b><tt><font color="#3366FF">eoFileMonitor</font></tt></b><font color="#000000">
|
||||
by an </font><b><tt><font color="#3366FF">eoGnuplot1DMonitor</font></tt></b><font color="#000000">
|
||||
and watch the </font><b><font color="#FF6600">graphical output </font></b><font color="#000000">(</font><font color="#FF6600">Unix</font><font color="#000000">
|
||||
systems with </font><font color="#FF6600">gnuplot</font><font color="#000000">
|
||||
installed only, sorry).</font></li>
|
||||
|
||||
<li>
|
||||
<font color="#000000">Note that you must also replace the </font><b><tt><font color="#3366FF">eoSecondMomentStat</font></tt></b><font color="#000000">
|
||||
by an </font><b><tt><font color="#3366FF">eoAverageStat</font></tt></b><font color="#000000">,
|
||||
otherwise the standard deviations won't make any sense here.</font></li>
|
||||
|
||||
<li>
|
||||
<font color="#000000">Please try to understand why the average is always
|
||||
0 before taking alook at the solution (file <a href="NoWay.html">exercise1.cpp</a>).</font></li>
|
||||
|
||||
<li>
|
||||
<font color="#000000">Then run</font></li>
|
||||
|
||||
<br><b><tt><font color="#660000"><font size=+1>
|
||||
exercise1 --vecSize=1000 --maxGen=1000</font></font></tt></b>
|
||||
<br><font color="#000000">to get a chance to see something hapenning before
|
||||
the program ends!</font></ul>
|
||||
|
||||
<hr WIDTH="100%"><b><font color="#000099"><font size=+2>Exercice 2:</font></font></b>
|
||||
<br><font color="#000000">Write the </font><b><tt><font color="#3366FF">eoDiversityStat</font></tt></b><font color="#000000">
|
||||
stat computation and test it. Thanks to send us the code!</font>
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<br><b><font color="#000099"><font size=+2>Exercice 3:</font></font></b>
|
||||
<br><font color="#000000">Write the code for an </font><b><tt><font color="#3366FF"><font size=+1>eoGnuplot1DwithErrorbarsMonitor</font></font></tt></b><font color="#000000">
|
||||
that would take into account the standard deviations and display them as
|
||||
errorbars.</font>
|
||||
<br><font color="#000000">Again, send us the code afterwards, thanks :-)</font>
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<br><b><font color="#000099"><font size=+2>Lessons learned:</font></font></b>
|
||||
<ul>
|
||||
<li>
|
||||
Value of program parameters can be set <b><font color="#FF6600">at run-time</font></b>
|
||||
using the <b><tt><font color="#3366FF"><font size=+1>eoParser</font></font></tt></b>
|
||||
class.</li>
|
||||
|
||||
<li>
|
||||
<b><font color="#FF6600">Snapshots</font></b> of the algorithms can easily
|
||||
be <font color="#FF6600">saved</font> (and <font color="#FF6600">restored</font>)
|
||||
thanks to the <b><tt><font color="#3366FF"><font size=+1>eoState</font></font></tt></b>
|
||||
class.</li>
|
||||
|
||||
<li>
|
||||
The <b><tt><font color="#3366FF"><font size=+1>eoCheckpoint</font></font></tt></b>
|
||||
mechanism let you <b><font color="#FF6600">do things every generation</font></b>
|
||||
without modifying existing algorithms, by simply writing the necessary
|
||||
code and encapsulating it into an object that <b><tt><font color="#3366FF"><font size=+1>eoCheckpoint</font></font></tt></b>
|
||||
is aware of, that are at the moment the following:</li>
|
||||
|
||||
<li>
|
||||
computing <b><font color="#FF6600">statistics</font></b>, <b><font color="#FF6600">displaying
|
||||
</font></b>parameters
|
||||
(e.g. statistics), <b><font color="#FF6600">saving</font></b> the
|
||||
<b><tt><font color="#3366FF"><font size=+1>(eo)State</font></font></tt></b>
|
||||
of the program.</li>
|
||||
</ul>
|
||||
In next lesson you will find out that many <a href="intro.html#adaptive">adaptive
|
||||
techniques</a> (the state-of-the-art in Evolutionary Computation) can easily
|
||||
be programmed through the <b><tt><font color="#3366FF"><font size=+1>eoUpdater</font></font></tt></b>
|
||||
construct.
|
||||
<br>
|
||||
<hr WIDTH="100%"><a href="eoLesson2.html">Lesson 2</a> -
|
||||
<a href="eoLesson4.html">Lesson
|
||||
4</a> -
|
||||
<a href="eoTutorial.html">Main page</a> -
|
||||
<a href="eoTopDown.html">Top-Down</a>
|
||||
- <a href="eoBottomUp.html">Bottom-up</a> - <a href="eoProgramming.html">Hints</a>
|
||||
-<b> <font face="Arial,Helvetica"><a href="doc/html/index.html">EO documentation</a></font></b>
|
||||
<br>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:Marc.Schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Fri Nov 3 18:49:12 CET 2000 --><!-- hhmts start -->Last
|
||||
modified: Mon Nov 27 8:49:12 CET 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
56
eo/tutorial/html/eoOperators.html
Normal file
56
eo/tutorial/html/eoOperators.html
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.72 [en] (X11; U; Linux 2.2.16 i686) [Netscape]">
|
||||
<title>Variation Operators</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>
|
||||
<b><font color="#000099">Variation Operators</font></b></h1>
|
||||
Variation operators modify individuals, or, equivalently, move them in
|
||||
the search space. They are almost always <font color="#FF0000">stochastic</font>,
|
||||
i.e. they generate random variations. Variation operators are classified
|
||||
depending on the number of arguments they use and/or modify.
|
||||
<p>Variation operators involving two individuals are called
|
||||
<a href="##crossover">crossover operators</a>.
|
||||
They can either modify one of the parents according
|
||||
to the material of the other parent, or modify both parents. In EO, the
|
||||
former are called Binary operators and the latter Quadratic operators.
|
||||
<br>
|
||||
<p>Variation operators involving one single individual are called <a href="##mutation">mutation
|
||||
operators.</a>
|
||||
<p>Note that in EO you can define and use variatio operators that generate
|
||||
any number of offspring fromany number of parents. These are called general
|
||||
operators, and require advanced knowledge of EO.
|
||||
<p><a NAME="#crossover"></a><b><font color="#000099"><font size=+2>Crossover</font></font></b>
|
||||
<br>Crossover operators involve two parents, and can modify one of them,
|
||||
or both of them.
|
||||
<p>Using crossover operators
|
||||
<p><a NAME="#mutation"></a><b><font color="#000099"><font size=+2>Mutation</font></font></b>
|
||||
<br>Mutation operators modify one single individual. The corresponding
|
||||
EO class is called eoMonOp.
|
||||
<h2>
|
||||
<font color="#009900"><font size=+1>Using mutation operators</font></font></h2>
|
||||
The standard EO genotypes (bistrings and real vectors) have pre-defined
|
||||
mutation operators.
|
||||
<br>
|
||||
<h2>
|
||||
<font color="#009900"><font size=+1>Writing a mutation operator</font></font></h2>
|
||||
|
||||
<p>
|
||||
<p><a NAME="general"></a><b><font color="#000099"><font size=+2>General
|
||||
Operators</font></font></b>
|
||||
<br><b><font color="#000099"><font size=+2></font></font></b>
|
||||
<br><b><font color="#000099"><font size=+2></font></font></b>
|
||||
<br>
|
||||
<p>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Mon Oct 30 18:16:54 CET 2000 --><!-- hhmts start -->Last
|
||||
modified: Mon Oct 30 18:24:39 CET 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
19
eo/tutorial/html/eoOutput.html
Normal file
19
eo/tutorial/html/eoOutput.html
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>Output</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Output</h1>
|
||||
|
||||
|
||||
|
||||
<hr>
|
||||
<address><a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<!-- Created: Mon Oct 30 19:29:17 CET 2000 -->
|
||||
<!-- hhmts start -->
|
||||
Last modified: Mon Oct 30 19:29:19 CET 2000
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
256
eo/tutorial/html/eoProgramming.html
Normal file
256
eo/tutorial/html/eoProgramming.html
Normal file
|
|
@ -0,0 +1,256 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.75 [en] (X11; U; Linux 2.2.17-21mdksmp i686) [Netscape]">
|
||||
<title>EO Programming guide</title>
|
||||
</head>
|
||||
<body text="#000000" link="#0000EE" vlink="#551A8B" alink="#FF0000" background="beige009.jpg">
|
||||
<a href="eoTutorial.html">Tutorial main page </a>-
|
||||
<a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">EO Programming guide</font></h1></center>
|
||||
<!-- ----------------------------------------------- --><a href="#templates">Templates</a>,
|
||||
<a href="#functors">Functors</a>,
|
||||
<a href="#STL">STL
|
||||
Library</a>, random numbers, <a href="#notations">EO programming style</a>!
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<br><a NAME="templates"></a><b><font color="#000099"><font size=+1>Templates</font></font></b>
|
||||
<br>Most EO code si written using templates. This allows to write generic
|
||||
code, i.e. involving a class which doesn't have to be known when writing
|
||||
the code -- but only when compiling it. In some sense this is similar to
|
||||
naming variables in algebra: you can write a lot of equations involving
|
||||
some variable $x$ without knowing even it if will be an integer or a float
|
||||
(or a matrix or ...). The main basic type that is templatized in EO is
|
||||
the fitness: an EO object is some object which has a fitness of some type
|
||||
F that can be anything. The definition for that is (see<font face="Arial,Helvetica"><a href="doc/EO.h-source.html">EO.h</a></font>)
|
||||
<p><b><tt><font color="#999900"><font size=+1>template<F> class EO</font></font></tt></b>
|
||||
<p>The idea is that, later in your code, you can declare for instance as
|
||||
in <a href="FirstBitGA.html#representation">FirstBitGA.cpp</a>
|
||||
<p><b><tt><font color="#999900"><font size=+1>typedef eoBin<double>
|
||||
Genotype;</font></font></tt></b>
|
||||
<p>meaning that in that file, you will manipulate as <b><tt><font color="#999900"><font size=+1>Genotype</font></font></tt></b>
|
||||
objects that are EO objects <font color="#000000">whose</font><b><font color="#FF6600">fitness</font></b><font color="#000000">
|
||||
is a </font><font color="#FF6600"><b>double</b>.</font>
|
||||
<p>Whereas the advantages are obvious (writing generic reusable code instead
|
||||
of having to rewrite the same pieces of code for different types), there
|
||||
are some drawbacks: namely, it makes some of the compiler error messages
|
||||
hard to understand; and it forbids the compilation of most parts of EO
|
||||
into an object library file, as the actual types are not known in advance.
|
||||
<p>
|
||||
<hr WIDTH="100%">
|
||||
<br><a NAME="functors"></a><b><font color="#000099"><font size=+1>Functors</font></font></b>
|
||||
<p>Though EO is a library, it contains almost no functions!
|
||||
<br>EO only contains functors, that are objects which have a method called
|
||||
<b><tt><font color="#FF6600"><font size=+1>operator()</font></font></tt></b>.
|
||||
Such objects are used as if they were a function, but the big differences
|
||||
are that
|
||||
<ul>
|
||||
<li>
|
||||
functors are functions with private data</li>
|
||||
|
||||
<li>
|
||||
you can have different funtors objects of the same class, i.e. you can
|
||||
use the same functionality with different parameters</li>
|
||||
|
||||
<li>
|
||||
you can heave a hierarchy of functors objects, which means that you have
|
||||
a hierarchy of functions with defaults behaviors and specialized sub-functions</li>
|
||||
|
||||
<li>
|
||||
...</li>
|
||||
</ul>
|
||||
Functors are so intimately linked to EO that a base class (<a href="doc/html/class_eofunctorbase.html">eoFunctorBase</a>)
|
||||
has been designed to hold all functors. This base class is itself divided
|
||||
into three derived class. These classes tell you immediately what kind
|
||||
of arguments the <font color="#993300">operator()</font> method requires
|
||||
and what kind of result it produces. See <a href="#notations">EO conventions</a>,
|
||||
and
|
||||
the <a href="doc/class_eofunctorbase.html">inheritance diagram of class
|
||||
eoFunctorBase</a>.
|
||||
<p><b><font color="#000099">Example</font></b>
|
||||
<p>A good example is given by the <tt><font color="#990000"><font size=+1>eoEvalFuncPtr</font></font></tt>
|
||||
class
|
||||
<p><tt><font color="#993300">class MyClass</font></tt>
|
||||
<br><tt><font color="#993300">{ ...</font></tt>
|
||||
<br><tt><font color="#993300"> void operator()(ArgType
|
||||
arg)</font></tt>
|
||||
<br><tt><font color="#993300"> {</font></tt>
|
||||
<br><tt><font color="#993300">
|
||||
// do what you have to do</font></tt>
|
||||
<br><tt><font color="#993300"> }</font></tt>
|
||||
<br><tt><font color="#993300">}; // end of class declaration</font></tt>
|
||||
<p>is used later in the code in something like
|
||||
<br>
|
||||
<p><tt><font color="#993300">ArgType myArgument;</font></tt>
|
||||
<br><tt><font color="#993300">MyClass myObject; // myObject
|
||||
is an object of class MyClass ...</font></tt>
|
||||
<br><tt><font color="#993300">myObject(myArgument); // calls method
|
||||
operator() of object myObject with argument myArgument ...</font></tt>
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
<h2>
|
||||
Why not plain C functions?</h2>
|
||||
Consider for instance variation operators. Of course, we could declare
|
||||
them as function acting on some objects.
|
||||
<p><tt>Blabla</tt>
|
||||
<p>
|
||||
<hr WIDTH="100%">
|
||||
<br><a NAME="STL"></a><b><font color="#000099"><font size=+1>A very brief
|
||||
introduction to STL</font></font></b>
|
||||
<p>All EO heavily relies on <b><font color="#FF6600">STL, the Standard
|
||||
Template Library</font></b>.
|
||||
<br>But <font color="#FF6600">you don't have to know more than a few words
|
||||
of STL</font> to use EO (like with "hello", "please" and "goodbye" you
|
||||
can survive in a foreign country :-) and even to contribute to new EO features.
|
||||
<p>You will only find here the basics of STL that you will need to understand
|
||||
most of EO code - and to guess what the parts you don't understand are
|
||||
actually doing. Don't worry, <b><font color="#FF6600">I </font></b>don't
|
||||
understand everything :-)
|
||||
<p>STL provides the user with <b><font color="#FF6600">container</font></b>
|
||||
and <b><font color="#FF6600">algorithms</font></b>. And you can apply (almost)
|
||||
all algorithms on (almost) all containers.
|
||||
<p><b><font color="#000099">Containers</font></b>
|
||||
<br>Containers are high level data types used to hold simpler data - the
|
||||
most widely used example of a container is the <b><font color="#FF6600">vector</font></b>
|
||||
construct.
|
||||
<br>The use of STL containers relieve the user from memory management.
|
||||
<ul>
|
||||
<li>
|
||||
<b><tt><font color="#000099"><font size=+1>vector </font></font></tt></b><font color="#000000">The
|
||||
most widely used container is a one-dimensional array of items.</font></li>
|
||||
|
||||
<br><font color="#000000">Data manipulation: suppose </font><font color="#FF6600">v
|
||||
is an STL </font><tt><font color="#993300"><font size=+1>vector<AtomType></font></font></tt><font color="#000000">.
|
||||
Then</font>
|
||||
<br><tt><font color="#993300"><font size=+1>v[i]</font></font></tt><font color="#000000">
|
||||
is the ith element of v, as in standard C arrays</font>
|
||||
<br><tt><font color="#993300"><font size=+1>v.size()</font></font></tt><font color="#000000">
|
||||
is the number of elements of v</font>
|
||||
<br><tt><font color="#993300"><font size=+1>v.push_back(atom)</font></font></tt><font color="#000000">
|
||||
appends the </font><tt><font color="#993300"><font size=+1>atom</font></font></tt><font color="#000000">
|
||||
at end of </font><tt><font color="#993300"><font size=+1>v</font></font></tt><font color="#000000">,
|
||||
provided of course that </font><tt><font color="#993300"><font size=+1>atom</font></font></tt><font color="#000000">
|
||||
is of type </font><tt><font color="#993300"><font size=+1>AtomType</font></font></tt>
|
||||
<br><font color="#000000">blabla</font>
|
||||
<li>
|
||||
<b><tt><font color="#000099"><font size=+1>pair</font></font></tt></b></li>
|
||||
|
||||
<br><font color="#000000">This simple container allows you to hold two
|
||||
data types together. It is very handy for temporary data handling. Assuming
|
||||
p is a </font><tt><font color="#993300"><font size=+1>pair<AtomType1,
|
||||
AtomType2></font></font></tt><font color="#000000">,</font>
|
||||
<br><tt><font color="#993300"><font size=+1>p.first()</font></font></tt><font color="#000000">
|
||||
and </font><tt><font color="#993300"><font size=+1>p.second()</font></font></tt><font color="#000000">
|
||||
refer to the encapsulated data, of respective types </font><tt><font color="#993300"><font size=+1>AtomType1</font></font></tt><font color="#000000">and
|
||||
</font><tt><font color="#993300"><font size=+1>AtomType2.</font></font></tt>
|
||||
<li>
|
||||
<b><tt><font color="#000099"><font size=+1>Blabla</font></font></tt></b></li>
|
||||
</ul>
|
||||
There are many other types of containers that are not used in EO and that
|
||||
we will not present here.
|
||||
<p><b><font color="#000099">STL Algorithms</font></b>
|
||||
<br>Algorithms are functions acting on containers - the most widely used
|
||||
example of a STL algorithm is the <b><font color="#FF6600">sort</font></b>
|
||||
function.
|
||||
<br>Blabla
|
||||
<p><b><font color="#000099">Drawbacks</font></b>
|
||||
<br>The main drawback I see in using STL is that it makes it almost
|
||||
<b><font color="#FF6600">impossible
|
||||
to use a debugger </font></b>normally: whereas acess to data is made simple
|
||||
to the progammer, data structures are actually so complex, and debuggers
|
||||
so willing to display everything that you get lines of template instanciation
|
||||
when asking your debugger what is inside some container! For instance I
|
||||
could never visualize some
|
||||
<tt><font color="#FF6600"><font size=+1>v[i]</font></font></tt>
|
||||
with <tt><font color="#FF6600"><font size=+1>gbd</font></font></tt>, v
|
||||
being an STL vector!
|
||||
<br>But there nonehteless are so many advantages !!!
|
||||
<p>
|
||||
<hr WIDTH="100%">
|
||||
<br><a NAME="random"></a><b><font color="#000099"><font size=+1>Random
|
||||
numbers</font></font></b>
|
||||
<br>Evolutionary Algorithms make intensive use of random numbers. Random
|
||||
numbers are simulated in computers by using <font color="#FF6600">pseudo-random</font>
|
||||
number generators (RNGs for short), i.e. functions that return series of
|
||||
numbers who look random (w.r.t. some statistical criteria).
|
||||
<p>To make sure the random number generator is as good as possible, and
|
||||
to ensure reproducibility of the results across diffrerent platforms, EO
|
||||
has its own RNG, the ``<font color="#FF6600">Mersenne Twister</font>''
|
||||
random number generator MT19937 (thanks to <font color="#FF0000">Takuji
|
||||
Nishimura</font>, see <font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/class_eorng.html">eoRNG.h</a></font></font>
|
||||
comments).
|
||||
<p>Though you can define and use as many RNGs as you wish in EO, the library
|
||||
also provides you with a global RNG termed rng: using only that single
|
||||
RNG in all calls to random numbers allows one to be able to <b><font color="#FF6600">reproduce
|
||||
a given run</font></b>:
|
||||
<ul>
|
||||
<li>
|
||||
as strange it seems for a random algorithm, it is mandatory for debugging
|
||||
purposes</li>
|
||||
|
||||
<li>
|
||||
random numbers are computed starting from a seed - starting from the same
|
||||
seed will lead to the same series of pseudo-random numbers, and hence to
|
||||
the same results of the algorithms. All examples in this tutorial will
|
||||
use the RNG seeding procedure, see e.g. in <a href="FirstBitGA.html#random">Lesson1</a>.</li>
|
||||
|
||||
<li>
|
||||
to simulate "true" random runs, you can just seed the RNG with a machine-clock
|
||||
realted number, e.g. calling time(0), as done for isntance in <a href="SecondBitEA.html#random">Lesson3</a>
|
||||
(and after).</li>
|
||||
</ul>
|
||||
As RNGs only produce (by definition) numbers that are uniformly distributed
|
||||
integers between 0 and some maximal number, EO provides you with random
|
||||
numbers follwing <b><font color="#FF6600">different probability distribution</font></b>
|
||||
(e.g. floating point following <font face="Arial,Helvetica"><font size=+1><a href="../../doc/html/class_eorng.html#a8">normal
|
||||
distribution</a></font></font>).
|
||||
<p>EO also provides <a href="../../doc/html/rnd_generators.h-source.html">random_generators</a>
|
||||
that can be used in STL call to generate series of random numbers, as in
|
||||
<a href="eoInit.html">eoPop
|
||||
initializers</a>.
|
||||
<p>
|
||||
<hr WIDTH="100%">
|
||||
<br><a NAME="notations"></a><b><font color="#000099"><font size=+1>EO conventions
|
||||
and naming style</font></font></b>
|
||||
<br>A few naming conventions should help you to navigate more easily through
|
||||
EO:
|
||||
<ul>
|
||||
<li>
|
||||
The name of local varoiables shoudl start with a lower case letter</li>
|
||||
|
||||
<li>
|
||||
The name of the parameters to a function shoudl start with an underscore
|
||||
(_)</li>
|
||||
|
||||
<br>The initialization parameters of constructors, for instance,
|
||||
shoudl be named from the names of the variables they are used to initialize.
|
||||
<li>
|
||||
The names of classes should start with eo + an Uppercase letter</li>
|
||||
|
||||
<li>
|
||||
Blabla</li>
|
||||
</ul>
|
||||
|
||||
<hr WIDTH="100%">
|
||||
<br><a href="eoTutorial.html">Tutorial main page </a>- <a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<br>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:Marc.Schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Mon Oct 30 19:04:58 CET 2000 --><!-- hhmts start -->Last
|
||||
modified: Mon Nov 6 07:01:57 CET 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
19
eo/tutorial/html/eoRepresentation.html
Normal file
19
eo/tutorial/html/eoRepresentation.html
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>Representation</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Representation</h1>
|
||||
|
||||
|
||||
|
||||
<hr>
|
||||
<address><a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<!-- Created: Mon Oct 30 19:27:59 CET 2000 -->
|
||||
<!-- hhmts start -->
|
||||
Last modified: Mon Oct 30 19:28:01 CET 2000
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
157
eo/tutorial/html/eoSGA.html
Normal file
157
eo/tutorial/html/eoSGA.html
Normal file
|
|
@ -0,0 +1,157 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.75 [en] (X11; U; Linux 2.2.17-21mdk i686) [Netscape]">
|
||||
<title>eoSGA.h</title>
|
||||
</head>
|
||||
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<br>
|
||||
<hr WIDTH="100%"><!-- -------------- End of header ------------------ --><!-- ----------------------------------------------- -->
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">eoSGA.h</font></h1></center>
|
||||
<a NAME="start"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr NOSAVE>
|
||||
<td NOSAVE><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
|
||||
<br><tt><font color="#993300">// eoSGA.h</font></tt>
|
||||
<br><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
|
||||
<br><b><tt><font color="#993300">#ifndef _eoSGA_h</font></tt></b>
|
||||
<br><b><tt><font color="#993300">#define _eoSGA_h</font></tt></b>
|
||||
<br><b><tt><font color="#993300">#include <eoOp.h></font></tt></b>
|
||||
<br><b><tt><font color="#993300">#include <eoContinue.h></font></tt></b>
|
||||
<br><b><tt><font color="#993300">#include <eoPop.h></font></tt></b>
|
||||
<br><b><tt><font color="#993300">#include <eoSelectOne.h></font></tt></b>
|
||||
<br><b><tt><font color="#993300">#include <eoSelectPerc.h></font></tt></b>
|
||||
<br><b><tt><font color="#993300">#include <eoEvalFunc.h></font></tt></b>
|
||||
<br><b><tt><font color="#993300">#include <eoAlgo.h></font></tt></b>
|
||||
<br><b><tt><font color="#993300">#include <apply.h></font></tt></b>
|
||||
<br><tt><font color="#993300">/** The Simple Genetic Algorithm, following
|
||||
Holland and Goldberg </font></tt>
|
||||
<br><tt><font color="#993300">* Needs a selector (class eoSelectOne)
|
||||
a crossover (eoQuadratic, </font></tt>
|
||||
<br><tt><font color="#993300">* i.e. a 2->2
|
||||
operator) and a mutation with their respective rates, </font></tt>
|
||||
<br><tt><font color="#993300">* of course
|
||||
an evaluation function (eoEvalFunc) and a continuator </font></tt>
|
||||
<br><tt><font color="#993300">* (eoContinue)
|
||||
which gives the stopping criterion. Performs full</font></tt>
|
||||
<br><tt><font color="#993300">* generational
|
||||
replacement.</font></tt>
|
||||
<br><tt><font color="#993300">*/ </font></tt>
|
||||
<br><b><tt><font color="#993300">template <class EOT></font></tt></b>
|
||||
<br><b><tt><font color="#993300">class eoSGA : public eoAlgo<EOT></font></tt></b>
|
||||
<br><b><tt><font color="#993300">{</font></tt></b>
|
||||
<br><b><tt><font color="#993300">public :</font></tt></b>
|
||||
<br><tt><font color="#993300"><b> </b>// added this second ctor as
|
||||
I didn't like the ordering of the parameters</font></tt>
|
||||
<br><tt><font color="#993300"><b> </b>// in the one above. Any objection
|
||||
:-) MS</font></tt>
|
||||
<br><a NAME="constructor"></a><b><tt><font color="#993300">eoSGA(</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> eoSelectOne<EOT>&
|
||||
_select,</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> eoQuadraticOp<EOT>&
|
||||
_cross, float _crate,</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> eoMonOp<EOT>&
|
||||
_mutate, float _mrate,</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> eoEvalFunc<EOT>&
|
||||
_eval,</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> eoContinue<EOT>&
|
||||
_cont)</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> : cont(_cont), </font></tt></b>
|
||||
<br><b><tt><font color="#993300"> mutate(_mutate), </font></tt></b>
|
||||
<br><b><tt><font color="#993300"> mutationRate(_mrate),</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> cross(_cross),</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> crossoverRate(_crate),</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> select(_select),</font></tt></b>
|
||||
<br><b><tt><font color="#993300"> eval(_eval)
|
||||
{}</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="generation"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td><b><tt><font color="#FF6666"> void operator()(eoPop<EOT>&
|
||||
_pop)</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666"> {</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666"> eoPop<EOT> offspring;</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666"> do {</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">
|
||||
select(_pop, offspring);</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">
|
||||
unsigned i;</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">
|
||||
for (i=0; i<_pop.size()/2; i++) </font></tt></b>
|
||||
<br><tt><font color="#FF6666"><b>
|
||||
{ </b>// generates 2 offspring from two parents</font></tt>
|
||||
<br><b><tt><font color="#FF6666">
|
||||
if ( rng.flip(crossoverRate) ) </font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">
|
||||
{ </font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">
|
||||
cross(offspring[2*i], offspring[2*i+1]);</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">
|
||||
}</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">
|
||||
}</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">
|
||||
for (i=0; i < _pop.size(); i++) </font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">
|
||||
{</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">
|
||||
if (rng.flip(mutationRate) ) </font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">
|
||||
{</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">
|
||||
mutate(offspring[i]);</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">
|
||||
}</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">
|
||||
}</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">
|
||||
_pop.swap(offspring);</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">
|
||||
apply<EOT>(eval, _pop);</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666"> } while (cont(_pop));</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666"> }</font></tt></b>
|
||||
<br> </td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="parametres"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#CCFFFF" NOSAVE >
|
||||
<tr>
|
||||
<td><b><tt><font color="#3366FF">private :</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> eoContinue<EOT>& cont;</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> eoMonOp<EOT>& mutate;</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> float mutationRate;</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> eoQuadraticOp<EOT>& cross;</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> float crossoverRate;</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> eoSelectPerc<EOT> select;</font></tt></b>
|
||||
<br><b><tt><font color="#3366FF"> eoEvalFunc<EOT>& eval;</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="general"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr>
|
||||
<td><b><tt><font color="#993300">};</font></tt></b>
|
||||
<br><b><tt><font color="#993300">#endif</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<hr WIDTH="100%"><a href="eoLesson1.html">Back to Lesson 1</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last modified: Sun Nov
|
||||
19 19:36:21 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
19
eo/tutorial/html/eoSelect.html
Normal file
19
eo/tutorial/html/eoSelect.html
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>Selection</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Selection</h1>
|
||||
|
||||
|
||||
|
||||
<hr>
|
||||
<address><a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<!-- Created: Mon Oct 30 17:51:54 CET 2000 -->
|
||||
<!-- hhmts start -->
|
||||
Last modified: Mon Oct 30 17:51:55 CET 2000
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
25
eo/tutorial/html/eoStop.html
Normal file
25
eo/tutorial/html/eoStop.html
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
|
||||
<html>
|
||||
<head>
|
||||
<title>Input / Output</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Input / Output</h1>
|
||||
|
||||
<p>
|
||||
<A NAME=stop>
|
||||
<h3>Stopping criteria</h3>
|
||||
|
||||
<p>
|
||||
<A NAME=output>
|
||||
<h3>Displaying statistics</h3>
|
||||
|
||||
<hr>
|
||||
<address><a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
|
||||
<!-- Created: Mon Oct 30 19:29:46 CET 2000 -->
|
||||
<!-- hhmts start -->
|
||||
Last modified: Tue Oct 31 18:32:22 CET 2000
|
||||
<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
88
eo/tutorial/html/eoTopDown.html
Normal file
88
eo/tutorial/html/eoTopDown.html
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.75 [en] (X11; U; Linux 2.2.17-21mdk i686) [Netscape]">
|
||||
<title>EO - The Top-Down approach</title>
|
||||
</head>
|
||||
<body text="#000000" link="#0000EE" vlink="#551A8B" alink="#FF0000" background="beige009.jpg">
|
||||
<a href="eoTutorial.html">Tutorial main page
|
||||
</a>-
|
||||
<a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> - <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
|
||||
documentation</a></font></font></b>
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">EO - The Top-Down approach</font></h1></center>
|
||||
|
||||
<p><br>Congratulations - You have chosen the top-down approach! This
|
||||
means that you want to start from something that already works, and gradually
|
||||
learn about the more complex constructs. We have prepared a series of "lessons"
|
||||
for you.
|
||||
<ul>
|
||||
<li>
|
||||
<a href="eoLesson1.html">Lesson 1 </a>- a gentle introduction to the <font color="#FF6600">EO
|
||||
way</font>: your first steps into <b><font color="#999900">EO representations</font></b>
|
||||
using a simple generational GA. Please, <b><font color="#FF0000">spend
|
||||
the necessary time</font></b> on that one, since all basic constructs presented
|
||||
there are used throughout EO.</li>
|
||||
|
||||
<li>
|
||||
<a href="eoLesson2.html">Lesson 2</a> - <font color="#FF6600">encapsulate</font>,
|
||||
encapsulate, and try more sophisticated <b><font color="#009900">selection/replacement</font></b>
|
||||
mechanisms, as well as <b><font color="#CC33CC">multiple operators</font></b></li>
|
||||
|
||||
<li>
|
||||
<a href="eoLesson3.html">Lesson 3 </a>- The same algorithms, but with <font color="#FF6600">improved
|
||||
input/outputs</font>: <font color="#000000">user-friendly input (i.e. without
|
||||
the need to recompile!</font>) of <b><font color="#3366FF">algorithm parameters</font></b><font color="#000000">,
|
||||
and </font><font color="#FF6600">checkpointing </font>(<b><font color="#3366FF">display</font></b>
|
||||
of on-line <b><font color="#3366FF">statistics</font></b>, <b><font color="#3366FF">save
|
||||
</font></b><font color="#000000">and
|
||||
</font><b><font color="#3366FF">restore</font></b>
|
||||
populations,
|
||||
<b><font color="#3366FF">restart</font></b> stopped runs,
|
||||
...).</li>
|
||||
|
||||
<p><br>Current version (Nov. 29, 2000) stops here, but here are the plans
|
||||
(sujected to many changes, of course!)
|
||||
<br>
|
||||
<li>
|
||||
Lesson 4 - More about checkpointing: write your first <font color="#FF6600">adaptive
|
||||
mechanism</font>, and find out how easy it is to <b><font color="#3366FF">update</font></b>
|
||||
and <b><font color="#3366FF">monitor </font></b><font color="#FF6600">dynamic
|
||||
parameters</font></li>
|
||||
|
||||
<li>
|
||||
Lesson 5 - more general operators: e.g. binary, n-ary, or even specific
|
||||
mate selection (your brain and my beauty)!</li>
|
||||
|
||||
<li>
|
||||
Lesson 6 - why not go parallel? From the simple asynchronous SSGA to the
|
||||
more sophisticated island model (no totally distributed population yet).</li>
|
||||
|
||||
<li>
|
||||
Lesson 7 - ...</li>
|
||||
</ul>
|
||||
Of course, in each lesson, you have links to the Bottom-Up page of the
|
||||
corresponding component of an EA you are modifying.
|
||||
<br>( ... Well, to tell you the truth, as of today, november 28, this is
|
||||
not true :-)
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<br><a href="eoTutorial.html">Tutorial main page </a>- <a href="eoTopDown.html">Top-Down
|
||||
page</a> - <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> - <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
|
||||
documentation</a></font></font></b>
|
||||
<br>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc@cmapx.polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: DATE --><!-- hhmts start -->Last modified: Fri Nov 28
|
||||
2000 <!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
178
eo/tutorial/html/eoTutorial.html
Normal file
178
eo/tutorial/html/eoTutorial.html
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.75 [en] (X11; U; Linux 2.2.17-21mdk i686) [Netscape]">
|
||||
<title>Tutorial EO</title>
|
||||
</head>
|
||||
<body text="#000000" link="#0000EF" vlink="#51188E" alink="#FF0000" background="beige009.jpg">
|
||||
<a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
|
||||
documentation</a></font></font></b>
|
||||
<hr WIDTH="100%">
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">EO Tutorial</font></h1></center>
|
||||
Welcome to EO - the Evolving Objects library. What is this tutorial good
|
||||
for?
|
||||
<br>Well, the short term idea here is to help you <font color="#FF6600">build
|
||||
your own Evolutionary Algorithms</font> using EO - while the long term
|
||||
idea is that you will be able to contribute to EO, and ultimately write
|
||||
<font color="#FF6600">our</font>
|
||||
EAs :-)
|
||||
<h3>
|
||||
<b><font color="#000099"><font size=+2>About this tutorial</font></font></b></h3>
|
||||
This tutorial can be used in 2 different ways: top-down and bottom-up.
|
||||
<ul>
|
||||
<li>
|
||||
<a href="eoTopdown.html">Top-down</a> means you start from a <font color="#FF6600">very
|
||||
simple, ready-to-run algorithm,</font> and gradually modify it, making
|
||||
it both more powerful and more complex.</li>
|
||||
|
||||
<li>
|
||||
<a href="eoBottomUp.html">Bottom-up</a> means you start by examining the
|
||||
<font color="#FF6600">components
|
||||
of an EA one by one</font>, down to the level of complexity you feel comfortable
|
||||
with, and then build the whole algorithm using those components you need
|
||||
(or the one you are mastering). Such approach might be viewed as going
|
||||
through a simplified user guide, too.</li>
|
||||
</ul>
|
||||
However, it is <b><font color="#FF6600">strongly recommended</font></b>
|
||||
that you take some time on the first lesson of the Top-down approach to
|
||||
get familiar with the basic concepts that are used throughout EO. Anyway,
|
||||
as of today, November 29, the Bottom-up page is not written yet :-)
|
||||
<p><b><font color="#000099"><font size=+2>Related documents</font></font></b>
|
||||
<ul>
|
||||
<li>
|
||||
There are of course a few (very few) <a href="eoProgramming.html">programming
|
||||
hints</a> that you should know.</li>
|
||||
|
||||
<li>
|
||||
THe <a href="doc/html/index.html">EO documentation </a>- automatically
|
||||
generated from the comments in the code - is very helpful, to get an idea
|
||||
of the inheritance diagrams of EO classes, and to quickly reach some specific
|
||||
part of the code.</li>
|
||||
|
||||
<br>The top page of each class documentation is for instance the inheritance
|
||||
diagram of the class, and you'll learn a lot by simply looking at it.
|
||||
<li>
|
||||
And, last but not least, we assume you know approximately that an Evolutionary
|
||||
Algorithm looks like this, but otherwise you can try this <a href="eoIntroEA.html">very
|
||||
brief introduction</a>).</li>
|
||||
</ul>
|
||||
|
||||
<p><br><b><font color="#000099"><font size=+2>Colors and navigation:</font></font></b>
|
||||
<p>You will see this diagram in quite many places, as for instance at the
|
||||
top of all examples - usually it will be clicable and will help you navigate
|
||||
among the different parts of an EO program. See the <a href="eoIntroEA.html">brief
|
||||
introduction to Evolutionary Computation</a> for a detailed explanation.
|
||||
<center>
|
||||
<p><img SRC="EA_tutorial.jpg" ></center>
|
||||
<a NAME="colors"></a>
|
||||
<p>But in the text itself, <b><font color="#FF6600">colors are important</font></b>,
|
||||
as they will be used throughout this tutorial to clearly mark which part
|
||||
of the algorithm we are discussing. So please keep in mind that, whereas
|
||||
<font color="#FF6600">orange
|
||||
is for emphasis</font>,
|
||||
<ul>
|
||||
<li>
|
||||
<b><font color="#999900">Yellowish</font></b> is for <b><font color="#999900">representation</font></b>,
|
||||
i.e. the choice of the <b><font color="#999900">genotype</font></b></li>
|
||||
|
||||
<li>
|
||||
<font color="#CC33CC">Magenta</font> is for the <font color="#CC33CC">stochastic
|
||||
operators</font> that are <b><font color="#999900">representation-dependent</font>,</b>
|
||||
i.e. <font color="#CC33CC">initialisation</font> and variation operators
|
||||
(<font color="#CC33CC">crossover</font>, <font color="#CC33CC">mutation</font>
|
||||
and the like).</li>
|
||||
|
||||
<li>
|
||||
<font color="#009900">Green</font> is for the implementation of <font color="#009900">Darwinism</font>,
|
||||
i.e. the way the individuals are <font color="#009900">selected</font>
|
||||
for reproduction and <font color="#009900">survive.</font></li>
|
||||
|
||||
<li>
|
||||
<font color="#CC0000">Red</font> is for evaluation, i.e. the computation
|
||||
of the <font color="#CC0000">fitness</font> of all individuals</li>
|
||||
|
||||
<li>
|
||||
<font color="#3366FF">Blue</font> is for interactions of the user and the
|
||||
program, as for instance choice of <font color="#3366FF">stopping criterion</font>,
|
||||
on-line display of nice <font color="#3366FF">statistics</font> or initial
|
||||
<font color="#3366FF">choice
|
||||
of all program parameters</font>.</li>
|
||||
|
||||
<li>
|
||||
<font color="#993300">Brown</font> is for everything that is NOT part of
|
||||
any of the above, i.e. random number generator, or basic C++/STL syntax
|
||||
.</li>
|
||||
|
||||
<li>
|
||||
Note that <font color="#FF6666">pink</font> will be used to desctibe the
|
||||
syntax of compile orders (i.e. at the oepratoring system level, see e.g.
|
||||
<a href="#install">below</a>).</li>
|
||||
|
||||
<li>
|
||||
<font face="Arial,Helvetica">Last, but not least, all links into EO documentation
|
||||
will use the Helvetica typeface, like this line you are now reading.</font></li>
|
||||
</ul>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
an interface that would allow you to build your Evolutionary Programs by
|
||||
a few clics; such a thing does exist, is called <a href="http://www-rocq.inria.fr/EASEA/">EASEA</a>,
|
||||
and is complementary to this tutorial as it helps the user to build some
|
||||
simple EO programs from simple description. But there are things that EASEA
|
||||
cannot do, and you will have to do it yourself and will need to imcrease
|
||||
your knowledge about EO for that.</li>
|
||||
</ul>
|
||||
<a NAME="install"></a>
|
||||
<h3>
|
||||
<font color="#000099">Before you start</font></h3>
|
||||
You should of course have downloaded and installed the whole <a href="http://www.sourceforge.net/projects/eodev">EO
|
||||
library</a> (how did you get this file if not???).
|
||||
<br>So we'll assume that you are now in the Tutorial directory, and that
|
||||
your prompt looks something like
|
||||
<p><b><tt><font color="#FF6666">(myname@myhost) EOdir/Tutorial %</font></tt></b>
|
||||
<p>so you should now type in
|
||||
<p><b><tt><font color="#FF6666">make lesson1</font></tt></b>
|
||||
<p>and see something like
|
||||
<p><font face="Courier New,Courier"><font color="#000000">(myname@myhost)
|
||||
EOdir/Tutorial % make lesson1</font></font>
|
||||
<br><b><tt><font color="#FF6666">c++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\"
|
||||
-I. -I../../src -Wall -g -c FirstBitGA.cpp</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">c++ -Wall -g -o FirstBitGA FirstBitGA.o
|
||||
../../src/libeo.a ../../src/utils/libeoutils.a</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">c++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\"
|
||||
-I. -I../../src -Wall -g -c FirstRealGA.cpp</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">c++ -Wall -g -o FirstRealGA FirstRealGA.o
|
||||
../../src/libeo.a ../../src/utils/libeoutils.a</font></tt></b>
|
||||
<p>and two now executable files should have appeared in the subdirectory
|
||||
Lesson1, namely <b><tt><font color="#990000">FirstBitGA</font></tt></b>
|
||||
and <b><tt><font color="#990000">FirstRealGA</font></tt></b> (see <a href="eoLesson1.html">First
|
||||
lesson</a> to know more about these two ready-to-run programs). If this
|
||||
doesn't work, please go back to the main EO directory and run the installation
|
||||
program.
|
||||
<p>You should also test that you can access the EO documentation in the
|
||||
menu line below: you might not need to go there immediately, but just in
|
||||
case you make rapid progress ... This menu bar should be on all pages of
|
||||
this tutorial, allowing you to navigate easily.
|
||||
<p>Last, but not least: EO is improving only from the good will of
|
||||
contributors. This is also true for this tutorial: If you find anything
|
||||
that you think could be improved, you are welcome to <a href="mailto:Marc.Schoenauer@polytechnique.fr">e-mail
|
||||
me</a>.
|
||||
<center>
|
||||
<p><font color="#000099"><font size=+2>Enjoy!
|
||||
<hr WIDTH="100%"></font></font><a href="eoTopDown.html">Top-Down page</a>
|
||||
- <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> -<b><font size=+1> <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font></font></b></center>
|
||||
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:Marc.Schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Mon Oct 30 07:27:13 CET 2000 --><!-- hhmts start -->Last
|
||||
modified: Fri Nov 28 2000 <!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
178
eo/tutorial/html/index.html
Normal file
178
eo/tutorial/html/index.html
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.61 [en] (X11; I; Linux 2.2.13-7mdk i686) [Netscape]">
|
||||
<title>Tutorial EO</title>
|
||||
</head>
|
||||
<body text="#000000" link="#0000EF" vlink="#51188E" alink="#FF0000" background="beige009.jpg">
|
||||
<a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
|
||||
documentation</a></font></font></b>
|
||||
<hr WIDTH="100%">
|
||||
<center>
|
||||
<h1>
|
||||
<font color="#FF0000">EO Tutorial</font></h1></center>
|
||||
Welcome to EO - the Evolving Objects library. What is this tutorial good
|
||||
for?
|
||||
<br>Well, the short term idea here is to help you <font color="#FF6600">build
|
||||
your own Evolutionary Algorithms</font> using EO - while the long term
|
||||
idea is that you will be able to contribute to EO, and ultimately write
|
||||
<font color="#FF6600">our</font>
|
||||
EAs :-)
|
||||
<h3>
|
||||
<b><font color="#000099"><font size=+2>About this tutorial</font></font></b></h3>
|
||||
This tutorial can be used in 2 different ways: top-down and bottom-up.
|
||||
<ul>
|
||||
<li>
|
||||
<a href="eoTopDown.html">Top-down</a> means you start from a <font color="#FF6600">very
|
||||
simple, ready-to-run algorithm,</font> and gradually modify it, making
|
||||
it both more powerful and more complex.</li>
|
||||
|
||||
<li>
|
||||
<a href="eoBottomUp.html">Bottom-up</a> means you start by examining the
|
||||
<font color="#FF6600">components
|
||||
of an EA one by one</font>, down to the level of complexity you feel comfortable
|
||||
with, and then build the whole algorithm using those components you need
|
||||
(or the one you are mastering). Such approach might be viewed as going
|
||||
through a simplified user guide, too.</li>
|
||||
</ul>
|
||||
However, it is <b><font color="#FF6600">strongly recommended</font></b>
|
||||
that you take some time on the first lesson of the Top-down approach to
|
||||
get familiar with the basic concepts that are used throughout EO. Anyway,
|
||||
as of today, November 29, the Bottom-up page is not written yet :-)
|
||||
<p><b><font color="#000099"><font size=+2>Related documents</font></font></b>
|
||||
<ul>
|
||||
<li>
|
||||
There are of course a few (very few) <a href="eoProgramming.html">programming
|
||||
hints</a> that you should know.</li>
|
||||
|
||||
<li>
|
||||
THe <a href="doc/html/index.html">EO documentation </a>- automatically
|
||||
generated from the comments in the code - is very helpful, to get an idea
|
||||
of the inheritance diagrams of EO classes, and to quickly reach some specific
|
||||
part of the code.</li>
|
||||
|
||||
<br>The top page of each class documentation is for instance the inheritance
|
||||
diagram of the class, and you'll learn a lot by simply looking at it.
|
||||
<li>
|
||||
And, last but not least, we assume you know approximately that an Evolutionary
|
||||
Algorithm looks like this, but otherwise you can try this <a href="eoIntroEA.html">very
|
||||
brief introduction</a>).</li>
|
||||
</ul>
|
||||
|
||||
<p><br><b><font color="#000099"><font size=+2>Colors and navigation:</font></font></b>
|
||||
<p>You will see this diagram in quite many places, as for instance at the
|
||||
top of all examples - usually it will be clicable and will help you navigate
|
||||
among the different parts of an EO program. See the <a href="eoIntroEA.html">brief
|
||||
introduction to Evolutionary Computation</a> for a detailed explanation.
|
||||
<center>
|
||||
<p><img SRC="EA_tutorial.jpg" ></center>
|
||||
<a NAME="colors"></a>
|
||||
<p>But in the text itself, <b><font color="#FF6600">colors are important</font></b>,
|
||||
as they will be used throughout this tutorial to clearly mark which part
|
||||
of the algorithm we are discussing. So please keep in mind that, whereas
|
||||
<font color="#FF6600">orange
|
||||
is for emphasis</font>,
|
||||
<ul>
|
||||
<li>
|
||||
<b><font color="#999900">Yellowish</font></b> is for <b><font color="#999900">representation</font></b>,
|
||||
i.e. the choice of the <b><font color="#999900">genotype</font></b></li>
|
||||
|
||||
<li>
|
||||
<font color="#CC33CC">Magenta</font> is for the <font color="#CC33CC">stochastic
|
||||
operators</font> that are <b><font color="#999900">representation-dependent</font>,</b>
|
||||
i.e. <font color="#CC33CC">initialisation</font> and variation operators
|
||||
(<font color="#CC33CC">crossover</font>, <font color="#CC33CC">mutation</font>
|
||||
and the like).</li>
|
||||
|
||||
<li>
|
||||
<font color="#009900">Green</font> is for the implementation of <font color="#009900">Darwinism</font>,
|
||||
i.e. the way the individuals are <font color="#009900">selected</font>
|
||||
for reproduction and <font color="#009900">survive.</font></li>
|
||||
|
||||
<li>
|
||||
<font color="#CC0000">Red</font> is for evaluation, i.e. the computation
|
||||
of the <font color="#CC0000">fitness</font> of all individuals</li>
|
||||
|
||||
<li>
|
||||
<font color="#3366FF">Blue</font> is for interactions of the user and the
|
||||
program, as for instance choice of <font color="#3366FF">stopping criterion</font>,
|
||||
on-line display of nice <font color="#3366FF">statistics</font> or initial
|
||||
<font color="#3366FF">choice
|
||||
of all program parameters</font>.</li>
|
||||
|
||||
<li>
|
||||
<font color="#993300">Brown</font> is for everything that is NOT part of
|
||||
any of the above, i.e. random number generator, or basic C++/STL syntax
|
||||
.</li>
|
||||
|
||||
<li>
|
||||
Note that <font color="#FF6666">pink</font> will be used to desctibe the
|
||||
syntax of compile orders (i.e. at the oepratoring system level, see e.g.
|
||||
<a href="#install">below</a>).</li>
|
||||
|
||||
<li>
|
||||
<font face="Arial,Helvetica">Last, but not least, all links into EO documentation
|
||||
will use the Helvetica typeface, like this line you are now reading.</font></li>
|
||||
</ul>
|
||||
|
||||
<ul>
|
||||
<li>
|
||||
an interface that would allow you to build your Evolutionary Programs by
|
||||
a few clics; such a thing does exist, is called <a href="http://www-rocq.inria.fr/EASEA/">EASEA</a>,
|
||||
and is complementary to this tutorial as it helps the user to build some
|
||||
simple EO programs from simple description. But there are things that EASEA
|
||||
cannot do, and you will have to do it yourself and will need to imcrease
|
||||
your knowledge about EO for that.</li>
|
||||
</ul>
|
||||
<a NAME="install"></a>
|
||||
<h3>
|
||||
<font color="#000099">Before you start</font></h3>
|
||||
You should of course have downloaded and installed the whole <a href="http://www.sourceforge.net/projects/eodev">EO
|
||||
library</a> (how did you get this file if not???).
|
||||
<br>So we'll assume that you are now in the Tutorial directory, and that
|
||||
your prompt looks something like
|
||||
<p><b><tt><font color="#FF6666">(myname@myhost) EOdir/Tutorial %</font></tt></b>
|
||||
<p>so you should now type in
|
||||
<p><b><tt><font color="#FF6666">make lesson1</font></tt></b>
|
||||
<p>and see something like
|
||||
<p><font face="Courier New,Courier"><font color="#000000">(myname@myhost)
|
||||
EOdir/Tutorial % make lesson1</font></font>
|
||||
<br><b><tt><font color="#FF6666">c++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\"
|
||||
-I. -I../../src -Wall -g -c FirstBitGA.cpp</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">c++ -Wall -g -o FirstBitGA FirstBitGA.o
|
||||
../../src/libeo.a ../../src/utils/libeoutils.a</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">c++ -DPACKAGE=\"eo\" -DVERSION=\"0.9.1\"
|
||||
-I. -I../../src -Wall -g -c FirstRealGA.cpp</font></tt></b>
|
||||
<br><b><tt><font color="#FF6666">c++ -Wall -g -o FirstRealGA FirstRealGA.o
|
||||
../../src/libeo.a ../../src/utils/libeoutils.a</font></tt></b>
|
||||
<p>and two now executable files should have appeared in the subdirectory
|
||||
Lesson1, namely <b><tt><font color="#990000">FirstBitGA</font></tt></b>
|
||||
and <b><tt><font color="#990000">FirstRealGA</font></tt></b> (see <a href="eoLesson1.html">First
|
||||
lesson</a> to know more about these two ready-to-run programs). If this
|
||||
doesn't work, please go back to the main EO directory and run the installation
|
||||
program.
|
||||
<p>You should also test that you can access the EO documentation in the
|
||||
menu line below: you might not need to go there immediately, but just in
|
||||
case you make rapid progress ... This menu bar should be on all pages of
|
||||
this tutorial, allowing you to navigate easily.
|
||||
<p>Last, but not least: EO is improving only from the good will of
|
||||
contributors. This is also true for this tutorial: If you find anything
|
||||
that you think could be improved, you are welcome to <a href="mailto:Marc.Schoenauer@polytechnique.fr">e-mail
|
||||
me</a>.
|
||||
<center>
|
||||
<p><font color="#000099"><font size=+2>Enjoy!
|
||||
<hr WIDTH="100%"></font></font><a href="eoTopDown.html">Top-Down page</a>
|
||||
- <a href="eoBottomUp.html">Bottom-up page</a> - <a href="eoProgramming.html">Programming
|
||||
hints</a> -<b><font size=+1> <font face="Arial,Helvetica"><a href="doc/html/index.html">EO
|
||||
documentation</a></font></font></b></center>
|
||||
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:Marc.Schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Mon Oct 30 07:27:13 CET 2000 --><!-- hhmts start -->Last
|
||||
modified: Fri Nov 28 2000 <!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
1952
eo/tutorial/html/lesson1.ps
Normal file
1952
eo/tutorial/html/lesson1.ps
Normal file
File diff suppressed because it is too large
Load diff
56
eo/tutorial/html/real_value.html
Normal file
56
eo/tutorial/html/real_value.html
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
||||
<meta name="GENERATOR" content="Mozilla/4.75 [en] (X11; U; Linux 2.2.17-21mdk i686) [Netscape]">
|
||||
<title>real_value.h</title>
|
||||
</head>
|
||||
<body text="#000000" bgcolor="#C3C2B4" link="#0000EE" vlink="#551A8B" alink="#FF0000">
|
||||
<a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> -<b><font size=+1>
|
||||
<font face="Arial,Helvetica"><a href="doc/html/index.html">EO documentation</a></font></font></b>
|
||||
<br>
|
||||
<hr WIDTH="100%">
|
||||
<br><a NAME="start"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCC99" NOSAVE >
|
||||
<tr NOSAVE>
|
||||
<td NOSAVE><b><tt><font color="#993300">#include <vector></font></tt></b>
|
||||
<br><tt><font color="#993300">//-----------------------------------------------------------------------------</font></tt>
|
||||
<br><tt><font color="#993300">/** Just a simple function that takes an
|
||||
vector<double> and sets the fitnes </font></tt>
|
||||
<br><tt><font color="#993300"> to the sphere function.
|
||||
Please use doubles not float!!!</font></tt>
|
||||
<br><tt><font color="#993300"> @param _ind A floatingpoint
|
||||
vector </font></tt>
|
||||
<br><tt><font color="#993300">*/</font></tt></td>
|
||||
</tr>
|
||||
</table>
|
||||
<a NAME="init"></a>
|
||||
<table BORDER=0 CELLSPACING=0 COLS=1 WIDTH="100%" BGCOLOR="#FFCCFF" NOSAVE >
|
||||
<tr>
|
||||
<td><b><tt><font color="#993399">double real_value(const std::vector<double>&
|
||||
_ind)</font></tt></b>
|
||||
<br><b><tt><font color="#993399">{</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> double sum = 0;</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> for (unsigned i = 0; i < _ind.size();
|
||||
i++)</font></tt></b>
|
||||
<br><b><tt><font color="#993399">
|
||||
sum += _ind[i] * _ind[i];</font></tt></b>
|
||||
<br><b><tt><font color="#993399"> return -sum;</font></tt></b>
|
||||
<br><b><tt><font color="#993399">}</font></tt></b></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<hr WIDTH="100%"><a href="eoLesson2.html">Back to Lesson 2</a> - <a href="eoTutorial.html">Tutorial
|
||||
main page </a>- <a href="eoTopDown.html">Top-Down page</a> - <a href="eoBottomUp.html">Bottom-up
|
||||
page</a> - <a href="eoProgramming.html">Programming hints</a> - <b><font face="Arial,Helvetica"><font size=+1><a href="doc/html/index.html">EO
|
||||
documentation</a></font></font></b>
|
||||
<hr>
|
||||
<address>
|
||||
<a href="mailto:marc.schoenauer@polytechnique.fr">Marc Schoenauer</a></address>
|
||||
|
||||
<br><!-- Created: Nov 2000 --><!-- hhmts start -->Last modified: Wed Nov
|
||||
29 08:58:50 2000<!-- hhmts end -->
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Add table
Add a link
Reference in a new issue