- added the #define for eo_verbose (true) and eo_no_verbose (false)
- added the eoNormMutation, simple normal mutation for simple real variables Modified Files: src/eo src/es/eoRealOp.h tutorial/Lesson2/FirstRealEA.cpp tutorial/Lesson3/SecondBitEA.cpp
This commit is contained in:
parent
0e62de2d14
commit
4944881d7c
4 changed files with 57 additions and 6 deletions
|
|
@ -28,6 +28,10 @@
|
|||
#ifndef _eo_
|
||||
#define _eo_
|
||||
|
||||
// some defines to make things easier to get at first sight
|
||||
|
||||
#define eo_verbose true
|
||||
#define eo_no_verbose false
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <utils/eoData.h>
|
||||
|
|
|
|||
|
|
@ -61,14 +61,17 @@ template<class Chrom> class eoUniformMutation: public eoMonOp<Chrom>
|
|||
*/
|
||||
void operator()(Chrom& chrom)
|
||||
{
|
||||
chrom.invalidate();
|
||||
bool hasChanged=false;
|
||||
for (unsigned lieu=0; lieu<chrom.size(); lieu++)
|
||||
{
|
||||
if (rng.flip(p_change))
|
||||
{
|
||||
chrom[lieu] += 2*epsilon*rng.uniform()-epsilon;
|
||||
hasChanged = true;
|
||||
}
|
||||
}
|
||||
if (hasChanged)
|
||||
chrom.invalidate();
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
@ -117,6 +120,45 @@ private:
|
|||
};
|
||||
|
||||
|
||||
template<class Chrom> class eoNormalMutation: public eoMonOp<Chrom>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* (Default) Constructor.
|
||||
* @param _epsilon the range for uniform nutation
|
||||
* @param _p_change the probability to change a given coordinate
|
||||
*/
|
||||
eoNormalMutation(const double& _epsilon, const double& _p_change = 1.0):
|
||||
epsilon(_epsilon), p_change(_p_change) {}
|
||||
|
||||
/// The class name.
|
||||
string className() const { return "eoNormalMutation"; }
|
||||
|
||||
/**
|
||||
* Do it!
|
||||
* @param chrom The cromosome undergoing the mutation
|
||||
*/
|
||||
void operator()(Chrom& chrom)
|
||||
{
|
||||
bool hasChanged=false;
|
||||
for (unsigned lieu=0; lieu<chrom.size(); lieu++)
|
||||
{
|
||||
if (rng.flip(p_change))
|
||||
{
|
||||
chrom[lieu] += epsilon*rng.normal();
|
||||
hasChanged = true;
|
||||
}
|
||||
}
|
||||
if (hasChanged)
|
||||
chrom.invalidate();
|
||||
}
|
||||
|
||||
private:
|
||||
double epsilon;
|
||||
double p_change;
|
||||
};
|
||||
|
||||
|
||||
// two arithmetical crossovers
|
||||
|
||||
/** eoSegmentCrossover --> uniform choice in segment
|
||||
|
|
|
|||
|
|
@ -45,11 +45,13 @@ void main_function(int argc, char **argv)
|
|||
const float P_MUT = 0.5; // mutation probability
|
||||
|
||||
const double EPSILON = 0.01; // range for real uniform mutation
|
||||
const double SIGMA = 0.01; // std. dev. of normal 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
|
||||
const double normMutRate = 0.5; // rate for normal mutation
|
||||
|
||||
// GENERAL
|
||||
//////////////////////////
|
||||
|
|
@ -113,16 +115,19 @@ void main_function(int argc, char **argv)
|
|||
eoArithmeticCrossover<Indi> xoverA;
|
||||
// Combine them with relative rates
|
||||
eoPropCombinedQuadOp<Indi> xover(xoverS, segmentRate);
|
||||
xover.add(xoverA, arithmeticRate, true);
|
||||
xover.add(xoverA, arithmeticRate, eo_verbose);
|
||||
|
||||
// MUTATION
|
||||
// Gaussian mutation - std dev as argument
|
||||
eoNormalMutation<Indi> mutationN(SIGMA);
|
||||
// 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);
|
||||
mutation.add(mutationD, detMutRate);
|
||||
mutation.add(mutationN, normMutRate, eo_verbose);
|
||||
|
||||
// STOP
|
||||
// CHECKPOINT
|
||||
|
|
|
|||
|
|
@ -235,7 +235,7 @@ void main_function(int argc, char **argv)
|
|||
// Combine them with relative rates
|
||||
eoPropCombinedQuadOp<Indi> xover(xover1, onePointRate);
|
||||
xover.add(xoverU, URate);
|
||||
xover.add(xover2, twoPointsRate, true);
|
||||
xover.add(xover2, twoPointsRate, eo_verbose);
|
||||
|
||||
// MUTATION
|
||||
// standard bit-flip mutation for bitstring
|
||||
|
|
@ -244,7 +244,7 @@ void main_function(int argc, char **argv)
|
|||
eoDetBitFlip<Indi> mutationOneBit;
|
||||
// Combine them with relative rates
|
||||
eoPropCombinedMonOp<Indi> mutation(mutationBitFlip, bitFlipRate);
|
||||
mutation.add(mutationOneBit, oneBitRate, true);
|
||||
mutation.add(mutationOneBit, oneBitRate, eo_verbose);
|
||||
|
||||
// The operators are encapsulated into an eoTRansform object
|
||||
eoSGATransform<Indi> transform(xover, pCross, mutation, pMut);
|
||||
|
|
@ -294,7 +294,7 @@ void main_function(int argc, char **argv)
|
|||
checkpoint.add(SecondStat);
|
||||
|
||||
// The Stdout monitor will print parameters to the screen ...
|
||||
eoStdoutMonitor monitor(false);
|
||||
eoStdoutMonitor monitor(eo_no_verbose);
|
||||
|
||||
// when called by the checkpoint (i.e. at every generation)
|
||||
checkpoint.add(monitor);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue