* New tree configuration of the project:

.../
   ...           + -- EO
   |             |
   |             |
   +-- src ----- + -- EDO
   |             |
   |             |
   +-- test      + -- MO
   |             |
   |             |
   +-- tutorial  + -- MOEO
   |             |
   |             |
   +-- doc       + -- SMP
   |             |
   |             |
   ...           + -- EOMPI
                 |
                 |
                 + -- EOSERIAL

Question for current maintainers: ./README: new release?

Also:

* Moving out eompi & eoserial modules (issue #2).

* Correction of the errors when executing "make doc" command.

* Adding a solution for the conflicting headers problem (see the two CMake Cache
 Values: PROJECT_TAG & PROJECT_HRS_INSTALL_SUBPATH) (issue #1)

* Header inclusions:
        ** src: changing absolute paths into relative paths ('#include <...>' -> '#include "..."')
        ** test, tutorial: changing relative paths into absolute paths ('#include "..."' -> '#include <...>')

* Moving out some scripts from EDO -> to the root

* Add a new script for compilation and installation (see build_gcc_linux_install)

* Compilation with uBLAS library or EDO module: now ok

* Minor modifications on README & INSTALL files

* Comment eompi failed tests with no end

*** TODO: CPack (debian (DEB) & RedHat (RPM) packages) (issues #6 & #7) ***
This commit is contained in:
Adèle Harrissart 2014-08-04 13:40:28 +02:00
commit 490e837f7a
2359 changed files with 7688 additions and 16329 deletions

View file

@ -0,0 +1,64 @@
######################################################################################
### 1) Include the sources
######################################################################################
#include_directories(${EO_SRC_DIR}/src)
#include_directories(${EO_SRC_DIR}/src/ga)
#include_directories(${EO_SRC_DIR}/src/utils)
######################################################################################
### 2) Specify where CMake can find the libraries
######################################################################################
if(NOT WIN32 OR CYGWIN)
link_directories(${EO_BIN_DIR}/lib)
endif(NOT WIN32 OR CYGWIN)
# especially for Visual Studio
if(WIN32 AND NOT CYGWIN)
link_directories(${EO_BIN_DIR}\\lib\\${CMAKE_BUILD_TYPE})
endif(WIN32 AND NOT CYGWIN)
######################################################################################
### 3) Define your targets
######################################################################################
# no matter what is the OS, hopefully
add_executable(SecondBitEA SecondBitEA.cpp)
add_executable(SecondRealEA SecondRealEA.cpp)
add_executable(exercise3.1 exercise3.1.cpp)
add_dependencies(SecondBitEA ga eoutils eo)
add_dependencies(SecondRealEA ga eoutils eo)
add_dependencies(exercise3.1 ga eoutils eo)
######################################################################################
### 4) Optionnal
######################################################################################
set(SECONDBITEA_VERSION ${GLOBAL_VERSION})
set_target_properties(SecondBitEA PROPERTIES VERSION "${SECONDBITEA_VERSION}")
set(SECONDREALEA_VERSION ${GLOBAL_VERSION})
set_target_properties(SecondRealEA PROPERTIES VERSION "${SECONDREALEA_VERSION}")
set(EXERCICE31_VERSION ${GLOBAL_VERSION})
set_target_properties(exercise3.1 PROPERTIES VERSION "${EXERCICE31_VERSION}")
######################################################################################
### 5) Link the librairies for the targets
######################################################################################
target_link_libraries(SecondBitEA ga eoutils eo)
target_link_libraries(SecondRealEA ga eoutils eo)
target_link_libraries(exercise3.1 ga eoutils eo)
######################################################################################
### 6) Configure project installation paths
######################################################################################
install(TARGETS SecondBitEA RUNTIME DESTINATION share/${PROJECT_TAG}/eo/examples/Lesson3 COMPONENT examples)
install(TARGETS SecondRealEA RUNTIME DESTINATION share/${PROJECT_TAG}/eo/examples/Lesson3 COMPONENT examples)
install(TARGETS exercise3.1 RUNTIME DESTINATION share/${PROJECT_TAG}/eo/examples/Lesson3 COMPONENT examples)
######################################################################################

View file

@ -0,0 +1,33 @@
### This Makefile is part of the tutorial of the EO library
# Unlike other Makefiles in EO, it is not using the automake/autoconf
# so that it stays easy to understant (you are in the tutorial, remember!)
# MS, Oct. 2002
# if you use this Makefile as a starting point for another application
# you might need to modify the following
DIR_EO = ../../src
.SUFFIXES: .cpp
# Warning: $(CXX) in Linux (RedHat and Mandrake at least) is g++
# However, if you are using this Makefile within xemacs,
# and have problems with the interpretation of the output (and its colors)
# then you should use c++ instead (make CXX=c++ will do)
.cpp: ; $(CXX) -DPACKAGE=\"eo\" -DVERSION=\"0.9.3\" -I. -I$(DIR_EO) -Wall -g -pg -o $@ $*.cpp $(DIR_EO)/utils/libeoutils.a $(DIR_EO)/libeo.a
.cpp.o: ; $(CXX) -DPACKAGE=\"eo\" -DVERSION=\"0.9.3\" -I. -I$(DIR_EO) -Wall -g -c -pg $*.cpp
secondEA = SecondBitEA SecondRealEA
ALL = $(secondEA) exercise3.1
lesson3 : $(secondEA)
all : $(ALL)
SecondBitEA : binary_value.h
SecondRealEA : real_value.h
clean :
@/bin/rm $(ALL) *.o *.sav *.xg *.status *~

View file

@ -0,0 +1,348 @@
//-----------------------------------------------------------------------------
// 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!
//-----------------------------------------------------------------------------
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
// standard includes
#include <fstream>
#include <iostream> // cout
#include <stdexcept> // runtime_error
// the general include for eo
#include <paradiseo/eo.h>
#include <paradiseo/eo/ga.h>
// Use functions from namespace std
using namespace std;
// EVAL
#include "binary_value.h"
// REPRESENTATION
//-----------------------------------------------------------------------------
// define your genotype and fitness types
typedef eoBit<double> Indi;
// the main_function: nothing changed(!), except variable initialization
void main_function(int argc, char **argv)
{
// 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)
// First define a parser from the command-line arguments
eoParser parser(argc, argv);
// For each parameter, define Parameter, read it through the parser,
// and assign the value to the variable
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// description of genotype
eoValueParam<unsigned int> vecSizeParam(8, "vecSize", "Genotype size",'V');
parser.processParam( vecSizeParam, "Representation" );
unsigned vecSize = vecSizeParam.value();
// parameters for evolution engine
eoValueParam<unsigned int> popSizeParam(10, "popSize", "Population size",'P');
parser.processParam( popSizeParam, "Evolution engine" );
unsigned popSize = popSizeParam.value();
eoValueParam<unsigned int> tSizeParam(10, "tSize", "Tournament size",'T');
parser.processParam( tSizeParam, "Evolution Engine" );
unsigned tSize = tSizeParam.value();
// init and stop
eoValueParam<string> loadNameParam("", "Load","A save file to restart from",'L');
parser.processParam( loadNameParam, "Persistence" );
string loadName = loadNameParam.value();
eoValueParam<unsigned int> maxGenParam(100, "maxGen", "Maximum number of generations",'G');
parser.processParam( maxGenParam, "Stopping criterion" );
unsigned maxGen = maxGenParam.value();
eoValueParam<unsigned int> minGenParam(100, "minGen", "Minimum number of generations",'g');
parser.processParam( minGenParam, "Stopping criterion" );
unsigned minGen = minGenParam.value();
eoValueParam<unsigned int> steadyGenParam(100, "steadyGen", "Number of generations with no improvement",'s');
parser.processParam( steadyGenParam, "Stopping criterion" );
unsigned steadyGen = steadyGenParam.value();
// operators probabilities at the algorithm level
eoValueParam<double> pCrossParam(0.6, "pCross", "Probability of Crossover", 'C');
parser.processParam( pCrossParam, "Genetic Operators" );
double pCross = pCrossParam.value();
eoValueParam<double> pMutParam(0.1, "pMut", "Probability of Mutation", 'M');
parser.processParam( pMutParam, "Genetic Operators" );
double pMut = pMutParam.value();
// relative rates for crossovers
eoValueParam<double> onePointRateParam(1, "onePointRate", "Relative rate for one point crossover", '1');
parser.processParam( onePointRateParam, "Genetic Operators" );
double onePointRate = onePointRateParam.value();
eoValueParam<double> twoPointsRateParam(1, "twoPointRate", "Relative rate for two point crossover", '2');
parser.processParam( twoPointsRateParam, "Genetic Operators" );
double twoPointsRate = twoPointsRateParam.value();
eoValueParam<double> uRateParam(2, "uRate", "Relative rate for uniform crossover", 'U');
parser.processParam( uRateParam, "Genetic Operators" );
double URate = uRateParam.value();
// relative rates and private parameters for mutations;
eoValueParam<double> pMutPerBitParam(0.01, "pMutPerBit", "Probability of flipping 1 bit in bit-flip mutation", 'b');
parser.processParam( pMutPerBitParam, "Genetic Operators" );
double pMutPerBit = pMutPerBitParam.value();
eoValueParam<double> bitFlipRateParam(0.01, "bitFlipRate", "Relative rate for bit-flip mutation", 'B');
parser.processParam( bitFlipRateParam, "Genetic Operators" );
double bitFlipRate = bitFlipRateParam.value();
eoValueParam<double> oneBitRateParam(0.01, "oneBitRate", "Relative rate for deterministic bit-flip mutation", 'D');
parser.processParam( oneBitRateParam, "Genetic Operators" );
double oneBitRate = oneBitRateParam.value();
// the name of the "status" file where all actual parameter values will be saved
string str_status = parser.ProgramName() + ".status"; // default value
eoValueParam<string> statusParam(str_status.c_str(), "status","Status file",'S');
parser.processParam( statusParam, "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 (statusParam.value() != "")
{
ofstream os(statusParam.value().c_str());
os << parser; // and you can use that file as parameter file
}
// 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 (loadName != "")
{
inState.load(loadName); // 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)
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
// 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
eoDetTournamentSelect<Indi> selectOne(tSize); // tSize in [2,POPSIZE]
// is now encapsulated in a eoSelectPerc (stands for Percentage)
eoSelectPerc<Indi> select(selectOne);
// REPLACE
// And we now have the full slection/replacement - though with
// the same generational replacement at the moment :-)
eoGenerationalReplacement<Indi> replace;
// OPERATORS
//////////////////////////////////////
// The variation operators
//////////////////////////////////////
// CROSSOVER
// 1-point crossover for bitstring
eo1PtBitXover<Indi> xover1;
// uniform crossover for bitstring
eoUBitXover<Indi> xoverU;
// 2-pots xover
eoNPtsBitXover<Indi> xover2(2);
// Combine them with relative rates
eoPropCombinedQuadOp<Indi> xover(xover1, onePointRate);
xover.add(xoverU, URate);
xover.add(xover2, twoPointsRate);
// MUTATION
// standard bit-flip mutation for bitstring
eoBitMutation<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(parser);
outState.registerObject(pop);
outState.registerObject(rng);
// and feed the state to state savers
// save state every 100th generation
eoCountedStateSaver stateSaver1(20, 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
// stopping criterion, eval, selection, transformation, replacement
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)
{
try
{
main_function(argc, argv);
}
catch(exception& e)
{
cout << "Exception: " << e.what() << '\n';
}
return 1;
}

View file

@ -0,0 +1,329 @@
//-----------------------------------------------------------------------------
// SecondRealEA.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
// (also slightly different than in SecondBitEA.cpp)
// and to twidle the output to your preferences (as in SecondBitEA.cpp)
//
//-----------------------------------------------------------------------------
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
// standard includes
#include <fstream>
#include <iostream> // cout
#include <stdexcept> // runtime_error
// the general include for eo
#include <paradiseo/eo.h>
#include <paradiseo/eo/es.h>
// REPRESENTATION
//-----------------------------------------------------------------------------
// define your individuals
typedef eoReal<eoMinimizingFitness> Indi;
// Use functions from namespace std
using namespace std;
// 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
//-----------------------------------------------------------------------------
// 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)
// First define a parser from the command-line arguments
eoParser parser(argc, argv);
// For each parameter, you can in on single line
// define the parameter, read it through the parser, and assign it
unsigned seed = parser.createParam(unsigned(time(0)), "seed", "Random number seed", 'S').value(); // will be in default section General
// description of genotype
unsigned vecSize = parser.createParam(unsigned(8), "vecSize", "Genotype size",'V', "Representation" ).value();
// parameters for evolution engine
unsigned popSize = parser.createParam(unsigned(10), "popSize", "Population size",'P', "Evolution engine" ).value();
unsigned tSize = parser.createParam(unsigned(2), "tSize", "Tournament size",'T', "Evolution Engine" ).value();
// init and stop
string loadName = parser.createParam(string(""), "Load","A save file to restart from",'L', "Persistence" ).value();
unsigned maxGen = parser.createParam(unsigned(100), "maxGen", "Maximum number of generations",'G', "Stopping criterion" ).value();
unsigned minGen = parser.createParam(unsigned(100), "minGen", "Minimum number of generations",'g', "Stopping criterion" ).value();
unsigned steadyGen = parser.createParam(unsigned(100), "steadyGen", "Number of generations with no improvement",'s', "Stopping criterion" ).value();
// operators probabilities at the algorithm level
double pCross = parser.createParam(double(0.6), "pCross", "Probability of Crossover", 'C', "Genetic Operators" ).value();
double pMut = parser.createParam(double(0.1), "pMut", "Probability of Mutation", 'M', "Genetic Operators" ).value();
// relative rates for crossovers
double hypercubeRate = parser.createParam(double(1), "hypercubeRate", "Relative rate for hypercube crossover", '\0', "Genetic Operators" ).value();
double segmentRate = parser.createParam(double(1), "segmentRate", "Relative rate for segment crossover", '\0', "Genetic Operators" ).value();
// internal parameters for the mutations
double EPSILON = parser.createParam(double(0.01), "EPSILON", "Width for uniform mutation", '\0', "Genetic Operators" ).value();
double SIGMA = parser.createParam(double(0.3), "SIGMA", "Sigma for normal mutation", '\0', "Genetic Operators" ).value();
// relative rates for mutations
double uniformMutRate = parser.createParam(double(1), "uniformMutRate", "Relative rate for uniform mutation", '\0', "Genetic Operators" ).value();
double detMutRate = parser.createParam(double(1), "detMutRate", "Relative rate for det-uniform mutation", '\0', "Genetic Operators" ).value();
double normalMutRate = parser.createParam(double(1), "normalMutRate", "Relative rate for normal mutation", '\0', "Genetic Operators" ).value();
// the name of the "status" file where all actual parameter values will be saved
string str_status = parser.ProgramName() + ".status"; // default value
string statusName = parser.createParam(str_status, "status","Status file",'S', "Persistence" ).value();
// 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 (statusName != "")
{
ofstream os(statusName.c_str());
os << parser; // and you can use that file as parameter file
}
// 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>& > plainEval( real_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 (loadName != "")
{
inState.load(loadName); // 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)
eoUniformGenerator<double> uGen(-1.0, 1.0);
eoInitFixedLength<Indi> random(vecSize, uGen);
// 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 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
eoDetTournamentSelect<Indi> selectOne(tSize);
// 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 :-)
eoGenerationalReplacement<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
eoHypercubeCrossover<Indi> xoverA;
// Combine them with relative weights
eoPropCombinedQuadOp<Indi> xover(xoverS, segmentRate);
xover.add(xoverA, hypercubeRate);
// 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);
// all coordinates of parents are normally modified (stDev SIGMA)
eoNormalMutation<Indi> mutationN(SIGMA);
// Combine them with relative weights
eoPropCombinedMonOp<Indi> mutation(mutationU, uniformMutRate);
mutation.add(mutationD, detMutRate);
mutation.add(mutationN, normalMutRate, 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(0);
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(parser);
outState.registerObject(pop);
outState.registerObject(rng);
// and feed the state to state savers
// save state every 100th generation
eoCountedStateSaver stateSaver1(20, 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
// stopping criterion, eval, selection, transformation, replacement
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)
{
try
{
main_function(argc, argv);
}
catch(exception& e)
{
cout << "Exception: " << e.what() << '\n';
}
return 1;
}

View file

@ -0,0 +1,16 @@
#include <paradiseo/eo.h>
//-----------------------------------------------------------------------------
/** 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 std::vector<bool>& _chrom)
{
double sum = 0;
for (unsigned i = 0; i < _chrom.size(); i++)
sum += _chrom[i];
return sum;
}

View file

@ -0,0 +1,404 @@
//-----------------------------------------------------------------------------
// 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!
//-----------------------------------------------------------------------------
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
// standard includes
#include <fstream>
#include <iostream> // cout
#include <stdexcept> // runtime_error
// the general include for eo
#include <paradiseo/eo.h>
// EVAL
#include "binary_value.h"
// REPRESENTATION
//-----------------------------------------------------------------------------
// Include the corresponding file
#include <paradiseo/eo/ga.h> // bitstring representation & operators
// define your genotype and fitness types
typedef eoBit<eoMinimizingFitness> Indi;
using namespace std;
// the main_function: nothing changed(!), except variable initialization
void main_function(int argc, char **argv)
{
// 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)
// First define a parser from the command-line arguments
eoParser parser(argc, argv);
// For each parameter, define Parameter, read it through the parser,
// and assign the value to the variable
eoValueParam<uint32_t> seedParam(time(0), "seed", "Random number seed", 'S');
parser.processParam( seedParam );
unsigned seed = seedParam.value();
// description of genotype
eoValueParam<unsigned int> vecSizeParam(100, "vecSize", "Genotype size",'V');
parser.processParam( vecSizeParam, "Representation" );
unsigned vecSize = vecSizeParam.value();
// parameters for evolution engine
eoValueParam<unsigned int> popSizeParam(100, "popSize", "Population size",'P');
parser.processParam( popSizeParam, "Evolution engine" );
unsigned popSize = popSizeParam.value();
eoValueParam<unsigned int> tSizeParam(10, "tSize", "Tournament size",'T');
parser.processParam( tSizeParam, "Evolution Engine" );
unsigned tSize = tSizeParam.value();
// init and stop
eoValueParam<string> loadNameParam("", "Load","A save file to restart from",'L');
parser.processParam( loadNameParam, "Persistence" );
string loadName = loadNameParam.value();
eoValueParam<unsigned int> maxGenParam(500, "maxGen", "Maximum number of generations",'G');
parser.processParam( maxGenParam, "Stopping criterion" );
unsigned maxGen = maxGenParam.value();
eoValueParam<unsigned int> minGenParam(500, "minGen", "Minimum number of generations",'g');
parser.processParam( minGenParam, "Stopping criterion" );
unsigned minGen = minGenParam.value();
eoValueParam<unsigned int> steadyGenParam(100, "steadyGen", "Number of generations with no improvement",'s');
parser.processParam( steadyGenParam, "Stopping criterion" );
unsigned steadyGen = steadyGenParam.value();
// operators probabilities at the algorithm level
eoValueParam<double> pCrossParam(0.6, "pCross", "Probability of Crossover", 'C');
parser.processParam( pCrossParam, "Genetic Operators" );
double pCross = pCrossParam.value();
eoValueParam<double> pMutParam(0.1, "pMut", "Probability of Mutation", 'M');
parser.processParam( pMutParam, "Genetic Operators" );
double pMut = pMutParam.value();
// relative rates for crossovers
eoValueParam<double> onePointRateParam(1, "onePointRate", "Relative rate for one point crossover", '1');
parser.processParam( onePointRateParam, "Genetic Operators" );
double onePointRate = onePointRateParam.value();
eoValueParam<double> twoPointsRateParam(1, "twoPointRate", "Relative rate for two point crossover", '2');
parser.processParam( twoPointsRateParam, "Genetic Operators" );
double twoPointsRate = twoPointsRateParam.value();
eoValueParam<double> uRateParam(2, "uRate", "Relative rate for uniform crossover", 'U');
parser.processParam( uRateParam, "Genetic Operators" );
double URate = uRateParam.value();
// relative rates and private parameters for mutations;
eoValueParam<double> pMutPerBitParam(0.01, "pMutPerBit", "Probability of flipping 1 bit in bit-flip mutation", 'b');
parser.processParam( pMutPerBitParam, "Genetic Operators" );
double pMutPerBit = pMutPerBitParam.value();
eoValueParam<double> bitFlipRateParam(0.01, "bitFlipRate", "Relative rate for bit-flip mutation", 'B');
parser.processParam( bitFlipRateParam, "Genetic Operators" );
double bitFlipRate = bitFlipRateParam.value();
eoValueParam<double> oneBitRateParam(0.01, "oneBitRate", "Relative rate for deterministic bit-flip mutation", 'D');
parser.processParam( oneBitRateParam, "Genetic Operators" );
double oneBitRate = oneBitRateParam.value();
// the name of the "status" file where all actual parameter values will be saved
string str_status = parser.ProgramName() + ".status"; // default value
eoValueParam<string> statusParam(str_status.c_str(), "status","Status file",'S');
parser.processParam( statusParam, "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 (statusParam.value() != "")
{
ofstream os(statusParam.value().c_str());
os << parser; // and you can use that file as parameter file
}
// 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 (loadName != "")
{
inState.load(loadName); // 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)
eoUniformGenerator<bool> uGen;
eoInitFixedLength<Indi> random(vecSize, uGen);
// 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 ;
cout << "and best is " << pop.best_element() << "\n\n";
cout << "and worse is " << pop.worse_element() << "\n\n";
// ENGINE
/////////////////////////////////////
// selection and replacement
////////////////////////////////////
// SELECT
// The robust tournament selection
eoDetTournamentSelect<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
// generational replacement at the moment :-)
eoGenerationalReplacement<Indi> replace;
// want to add (weak) elitism? easy!
// rename the eoGenerationalReplacement replace_main,
// then encapsulate it in the elitist replacement
// eoWeakElitistReplacement<Indi> replace(replace_main);
// OPERATORS
//////////////////////////////////////
// The variation operators
//////////////////////////////////////
// CROSSOVER
// 1-point crossover for bitstring
eo1PtBitXover<Indi> xover1;
// uniform crossover for bitstring
eoUBitXover<Indi> xoverU;
// 2-pots xover
eoNPtsBitXover<Indi> xover2(2);
// Combine them with relative rates
eoPropCombinedQuadOp<Indi> xover(xover1, onePointRate);
xover.add(xoverU, URate);
xover.add(xover2, twoPointsRate);
// MUTATION
// standard bit-flip mutation for bitstring
eoBitMutation<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); // remove if minimizing :-)
eoCombinedContinue<Indi> continuator(genCont);
continuator.add(steadyCont);
// continuator.add(fitCont);
// Ctrl C signal handling: don't know if that works in MSC ...
#ifndef _MSC_VER
eoCtrlCContinue<Indi> ctrlC;
continuator.add(ctrlC);
#endif
// 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;
// the Fitness Distance Correlation
// need first an object to compute the distances
eoQuadDistance<Indi> dist; // Hamming distance
eoFDCStat<Indi> fdcStat(dist);
// Add them to the checkpoint to get them called at the appropriate time
checkpoint.add(bestStat);
checkpoint.add(averageStat);
checkpoint.add(SecondStat);
checkpoint.add(fdcStat);
// 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);
monitor.add(fdcStat);
// test de eoPopStat and/or eoSortedPopStat.
// Dumps the whole pop every 10 gen.
// eoSortedPopStat<Indi> popStat(10, "Dump of whole population");
// eoPopStat<Indi> popStat(10, "Dump of whole population");
// checkpoint.add(popStat);
// monitor.add(popStat);
// A file monitor: will print parameters to ... a File, yes, you got it!
eoFileMonitor fileMonitor("stats.xg", " ");
// the checkpoint mechanism can handle monitors
checkpoint.add(fileMonitor);
// the fileMonitor can monitor parameters, too, but you must tell it!
fileMonitor.add(generationCounter);
fileMonitor.add(bestStat);
fileMonitor.add(SecondStat);
#ifndef _MSC_VER
// and an eoGnuplot1DMonitor will 1-print to a file, and 2- plot on screen
eoGnuplot1DMonitor gnuMonitor("best_average.xg",minimizing_fitness<Indi>());
// the checkpoint mechanism can handle multiple monitors
checkpoint.add(gnuMonitor);
// the gnuMonitor can monitor parameters, too, but you must tell it!
gnuMonitor.add(eval);
gnuMonitor.add(bestStat);
gnuMonitor.add(averageStat);
// send a scaling command to gnuplot
gnuMonitor.gnuplotCommand("set yrange [0:500]");
// a specific plot monitor for FDC
// first into a file (it adds everything ti itself
eoFDCFileSnapshot<Indi> fdcFileSnapshot(fdcStat);
// then to a Gnuplot monitor
eoGnuplot1DSnapshot fdcGnuplot(fdcFileSnapshot);
// and of coruse add them to the checkPoint
checkpoint.add(fdcFileSnapshot);
checkpoint.add(fdcGnuplot);
// want to see how the fitness is spread?
eoScalarFitnessStat<Indi> fitStat;
checkpoint.add(fitStat);
// a gnuplot-based monitor for snapshots: needs a dir name
// where to store the files
eoGnuplot1DSnapshot fitSnapshot("Fitnesses");
// add any stat that is a vector<double> to it
fitSnapshot.add(fitStat);
// and of course add it to the checkpoint
checkpoint.add(fitSnapshot);
#endif
// 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)
{
try
{
main_function(argc, argv);
}
catch(exception& e)
{
cout << "Exception: " << e.what() << '\n';
}
return 1;
}

View file

@ -0,0 +1,17 @@
#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;
}