Added the normal mutation - and the 1/5 mutation
This commit is contained in:
parent
a8bf667774
commit
20b70de2a1
2 changed files with 209 additions and 83 deletions
165
eo/src/es/eoNormalMutation.h
Normal file
165
eo/src/es/eoNormalMutation.h
Normal file
|
|
@ -0,0 +1,165 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoNormalMutation.h
|
||||
// (c) EEAAX 2001 - Maarten Keijzer 2000
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2 of the License, or (at your option) any later version.
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
|
||||
Contact: Marc.Schoenauer@polytechnique.fr
|
||||
mak@dhi.dk
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef eoNormalMutation_h
|
||||
#define eoNormalMutation_h
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <algorithm> // swap_ranges
|
||||
#include <utils/eoRNG.h>
|
||||
#include <utils/eoUpdatable.h>
|
||||
#include <eoEvalFunc.h>
|
||||
#include <es/eoReal.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/** Simple normal mutation of a vector of real values.
|
||||
* The stDev is fixed - but it is passed ans stored as a reference,
|
||||
* to enable dynamic mutations (see eoOenFithMutation below).
|
||||
*/
|
||||
|
||||
template<class EOT> class eoNormalMutation: public eoMonOp<EOT>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* (Default) Constructor.
|
||||
* @param _sigma the range for uniform nutation
|
||||
* @param _p_change the probability to change a given coordinate
|
||||
*/
|
||||
eoNormalMutation(double & _sigma, const double& _p_change = 1.0):
|
||||
sigma(_sigma), p_change(_p_change) {}
|
||||
|
||||
/// The class name.
|
||||
string className() const { return "eoNormalMutation"; }
|
||||
|
||||
/**
|
||||
* Do it!
|
||||
* @param _eo The cromosome undergoing the mutation
|
||||
*/
|
||||
void operator()(EOT& _eo)
|
||||
{
|
||||
bool hasChanged=false;
|
||||
for (unsigned lieu=0; lieu<_eo.size(); lieu++)
|
||||
{
|
||||
if (rng.flip(p_change))
|
||||
{
|
||||
_eo[lieu] += sigma*rng.normal();
|
||||
hasChanged = true;
|
||||
}
|
||||
}
|
||||
if (hasChanged)
|
||||
_eo.invalidate();
|
||||
}
|
||||
|
||||
protected:
|
||||
double & sigma;
|
||||
private:
|
||||
double p_change;
|
||||
};
|
||||
|
||||
/** the dynamic version: just say it is updatable -
|
||||
* and write the update() method!
|
||||
* here the 1 fifth rule: count the proportion of successful mutations, and
|
||||
* increase sigma if more than threshold (1/5 !)
|
||||
*/
|
||||
|
||||
template<class EOT> class eoOneFifthMutation :
|
||||
public eoNormalMutation<EOT>, public eoUpdatable
|
||||
{
|
||||
public:
|
||||
typedef typename EOT::Fitness Fitness;
|
||||
|
||||
/**
|
||||
* (Default) Constructor.
|
||||
*
|
||||
* @param eval the evaluation fuinction, needed to recompute the fitmess
|
||||
* @param _sigmaInit the initial value for uniform nutation
|
||||
* @param _windowSize the size of the window for statistics
|
||||
* @param _threshold the threshold (the 1/5 - 0.2)
|
||||
* @param _updateFactor multiplicative update factor for sigma
|
||||
*/
|
||||
eoOneFifthMutation(eoEvalFunc<EOT> & _eval, double & _sigmaInit,
|
||||
unsigned _windowSize = 10,
|
||||
double _threshold=0.2, double _updateFactor=0.83):
|
||||
eoNormalMutation<EOT>(_sigmaInit), eval(_eval),
|
||||
threshold(_threshold), updateFactor(_updateFactor),
|
||||
nbMut(_windowSize, 0), nbSuccess(_windowSize, 0), genIndex(0) {}
|
||||
|
||||
/**
|
||||
* Do it!
|
||||
* @param _eo The cromosome undergoing the mutation
|
||||
* calls the standard mutation, then checks for success
|
||||
*/
|
||||
void operator()(EOT & _eo)
|
||||
{
|
||||
Fitness oldFitness = _eo.fitness(); // save old fitness
|
||||
|
||||
eoNormalMutation<EOT>::operator()(_eo); // normal mutation
|
||||
nbMut++; // assumes normal mutation always modifies _eo
|
||||
|
||||
eval(_eo); // compute fitness of offspring
|
||||
|
||||
if (_eo.fitness() > oldFitness)
|
||||
nbSuccess++; // update counter
|
||||
}
|
||||
|
||||
// this will be called every generation
|
||||
void update()
|
||||
{
|
||||
unsigned totalMut = 0;
|
||||
unsigned totalSuccess = 0;
|
||||
// compute the average stats over the time window
|
||||
for ( unsigned i=0; i<nbMut.size(); i++)
|
||||
{
|
||||
totalMut += nbMut[i];
|
||||
totalSuccess += nbSuccess[i];
|
||||
}
|
||||
|
||||
// update sigma accordingly
|
||||
double prop = (double) totalSuccess / totalMut;
|
||||
if (prop > threshold)
|
||||
sigma /= updateFactor; // increase sigma
|
||||
else
|
||||
sigma *= updateFactor; // decrease sigma
|
||||
|
||||
// go to next generation
|
||||
genIndex = (genIndex+1) % nbMut.size() ;
|
||||
nbMut[genIndex] = nbSuccess[genIndex] = 0;
|
||||
}
|
||||
private:
|
||||
eoEvalFunc<EOT> & eval;
|
||||
double threshold; // 1/5 !
|
||||
double updateFactor ; // the multiplicative factor
|
||||
vector<unsigned> nbMut; // total number of mutations per gen
|
||||
vector<unsigned> nbSuccess; // number of successful mutations per gen
|
||||
unsigned genIndex ; // current gen
|
||||
};
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
//@}
|
||||
#endif eoRealOp_h
|
||||
|
||||
|
|
@ -41,7 +41,7 @@
|
|||
\ingroup parameteric
|
||||
*/
|
||||
|
||||
template<class Chrom> class eoUniformMutation: public eoMonOp<Chrom>
|
||||
template<class EOT> class eoUniformMutation: public eoMonOp<EOT>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
|
|
@ -57,21 +57,21 @@ template<class Chrom> class eoUniformMutation: public eoMonOp<Chrom>
|
|||
|
||||
/**
|
||||
* Do it!
|
||||
* @param chrom The cromosome undergoing the mutation
|
||||
* @param _eo The cromosome undergoing the mutation
|
||||
*/
|
||||
void operator()(Chrom& chrom)
|
||||
void operator()(EOT& _eo)
|
||||
{
|
||||
bool hasChanged=false;
|
||||
for (unsigned lieu=0; lieu<chrom.size(); lieu++)
|
||||
for (unsigned lieu=0; lieu<_eo.size(); lieu++)
|
||||
{
|
||||
if (rng.flip(p_change))
|
||||
{
|
||||
chrom[lieu] += 2*epsilon*rng.uniform()-epsilon;
|
||||
_eo[lieu] += 2*epsilon*rng.uniform()-epsilon;
|
||||
hasChanged = true;
|
||||
}
|
||||
}
|
||||
if (hasChanged)
|
||||
chrom.invalidate();
|
||||
_eo.invalidate();
|
||||
}
|
||||
|
||||
private:
|
||||
|
|
@ -85,7 +85,7 @@ private:
|
|||
\ingroup parameteric
|
||||
*/
|
||||
|
||||
template<class Chrom> class eoDetUniformMutation: public eoMonOp<Chrom>
|
||||
template<class EOT> class eoDetUniformMutation: public eoMonOp<EOT>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
|
|
@ -101,16 +101,16 @@ template<class Chrom> class eoDetUniformMutation: public eoMonOp<Chrom>
|
|||
|
||||
/**
|
||||
* Do it!
|
||||
* @param chrom The cromosome undergoing the mutation
|
||||
* @param _eo The cromosome undergoing the mutation
|
||||
*/
|
||||
void operator()(Chrom& chrom)
|
||||
void operator()(EOT& _eo)
|
||||
{
|
||||
chrom.invalidate();
|
||||
_eo.invalidate();
|
||||
for (unsigned i=0; i<no; i++)
|
||||
{
|
||||
unsigned lieu = rng.random(chrom.size());
|
||||
unsigned lieu = rng.random(_eo.size());
|
||||
// actually, we should test that we don't re-modify same variable!
|
||||
chrom[lieu] += 2*epsilon*rng.uniform()-epsilon;
|
||||
_eo[lieu] += 2*epsilon*rng.uniform()-epsilon;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -120,45 +120,6 @@ 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
|
||||
|
|
@ -167,7 +128,7 @@ private:
|
|||
\ingroup parameteric
|
||||
*/
|
||||
|
||||
template<class Chrom> class eoSegmentCrossover: public eoQuadraticOp<Chrom>
|
||||
template<class EOT> class eoSegmentCrossover: public eoQuadraticOp<EOT>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
|
|
@ -184,23 +145,23 @@ template<class Chrom> class eoSegmentCrossover: public eoQuadraticOp<Chrom>
|
|||
|
||||
/**
|
||||
* segment crossover - modifies both parents
|
||||
* @param chrom1 The first parent
|
||||
* @param chrom2 The first parent
|
||||
* @param _eo1 The first parent
|
||||
* @param _eo2 The first parent
|
||||
*/
|
||||
void operator()(Chrom& chrom1, Chrom& chrom2)
|
||||
void operator()(EOT& _eo1, EOT& _eo2)
|
||||
{
|
||||
unsigned i;
|
||||
double r1, r2, fact;
|
||||
fact = rng.uniform(range); // in [0,range)
|
||||
for (i=0; i<chrom1.size(); i++)
|
||||
for (i=0; i<_eo1.size(); i++)
|
||||
{
|
||||
r1=chrom1[i];
|
||||
r2=chrom2[i];
|
||||
chrom1[i] = fact * r1 + (1-fact) * r2;
|
||||
chrom2[i] = (1-fact) * r1 + fact * r2;
|
||||
r1=_eo1[i];
|
||||
r2=_eo2[i];
|
||||
_eo1[i] = fact * r1 + (1-fact) * r2;
|
||||
_eo2[i] = (1-fact) * r1 + fact * r2;
|
||||
}
|
||||
chrom1.invalidate(); // shoudl test if fact was 0 or 1 :-)))
|
||||
chrom2.invalidate();
|
||||
_eo1.invalidate(); // shoudl test if fact was 0 or 1 :-)))
|
||||
_eo2.invalidate();
|
||||
}
|
||||
|
||||
protected:
|
||||
|
|
@ -214,7 +175,7 @@ protected:
|
|||
\ingroup parameteric
|
||||
*/
|
||||
|
||||
template<class Chrom> class eoArithmeticCrossover: public eoQuadraticOp<Chrom>
|
||||
template<class EOT> class eoArithmeticCrossover: public eoQuadraticOp<EOT>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
|
|
@ -231,20 +192,20 @@ template<class Chrom> class eoArithmeticCrossover: public eoQuadraticOp<Chrom>
|
|||
|
||||
/**
|
||||
* arithmetical crossover - modifies both parents
|
||||
* @param chrom1 The first parent
|
||||
* @param chrom2 The first parent
|
||||
* @param _eo1 The first parent
|
||||
* @param _eo2 The first parent
|
||||
*/
|
||||
void operator()(Chrom& chrom1, Chrom& chrom2)
|
||||
void operator()(EOT& _eo1, EOT& _eo2)
|
||||
{
|
||||
unsigned i;
|
||||
double r1, r2, fact;
|
||||
for (i=0; i<chrom1.size(); i++)
|
||||
for (i=0; i<_eo1.size(); i++)
|
||||
{
|
||||
r1=chrom1[i];
|
||||
r2=chrom2[i];
|
||||
r1=_eo1[i];
|
||||
r2=_eo2[i];
|
||||
fact = rng.uniform(range); // in [0,range)
|
||||
chrom1[i] = fact * r1 + (1-fact) * r2;
|
||||
chrom2[i] = (1-fact) * r1 + fact * r2;
|
||||
_eo1[i] = fact * r1 + (1-fact) * r2;
|
||||
_eo2[i] = (1-fact) * r1 + fact * r2;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -259,7 +220,7 @@ protected:
|
|||
\ingroup parameteric
|
||||
*/
|
||||
|
||||
template<class Chrom> class eoRealUxOver: public eoQuadraticOp<Chrom>
|
||||
template<class EOT> class eoRealUxOver: public eoQuadraticOp<EOT>
|
||||
{
|
||||
public:
|
||||
/**
|
||||
|
|
@ -277,30 +238,30 @@ template<class Chrom> class eoRealUxOver: public eoQuadraticOp<Chrom>
|
|||
|
||||
/**
|
||||
* Uniform crossover for real vectors
|
||||
* @param chrom1 The first parent
|
||||
* @param chrom2 The second parent
|
||||
* @param _eo1 The first parent
|
||||
* @param _eo2 The second parent
|
||||
* @runtime_error if sizes don't match
|
||||
*/
|
||||
void operator()(Chrom& chrom1, Chrom& chrom2)
|
||||
void operator()(EOT& _eo1, EOT& _eo2)
|
||||
{
|
||||
if ( chrom1.size() != chrom2.size())
|
||||
if ( _eo1.size() != _eo2.size())
|
||||
runtime_error("UxOver --> chromosomes sizes don't match" );
|
||||
bool changed = false;
|
||||
for (unsigned int i=0; i<chrom1.size(); i++)
|
||||
for (unsigned int i=0; i<_eo1.size(); i++)
|
||||
{
|
||||
if (rng.flip(preference))
|
||||
if (chrom1[i] == chrom2[i])
|
||||
if (_eo1[i] == _eo2[i])
|
||||
{
|
||||
double tmp = chrom1[i];
|
||||
chrom1[i]=chrom2[i];
|
||||
chrom2[i] = tmp;
|
||||
double tmp = _eo1[i];
|
||||
_eo1[i]=_eo2[i];
|
||||
_eo2[i] = tmp;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if (changed)
|
||||
{
|
||||
chrom1.invalidate();
|
||||
chrom2.invalidate();
|
||||
_eo1.invalidate();
|
||||
_eo2.invalidate();
|
||||
}
|
||||
}
|
||||
private:
|
||||
|
|
|
|||
Reference in a new issue