Removed dependence on eoUniform, changed it to use rng.random or rng.uniform directly

This commit is contained in:
mac 2000-06-10 13:22:53 +00:00
commit c1b0a6c503
14 changed files with 446 additions and 313 deletions

View file

@ -29,7 +29,6 @@
// EO includes // EO includes
#include <eoOp.h> #include <eoOp.h>
#include <eoUniform.h>
#include <utils/eoRNG.h> #include <utils/eoRNG.h>
#include <eoAtomMutator.h> #include <eoAtomMutator.h>

View file

@ -29,7 +29,7 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <vector> // vector #include <vector> // vector
#include <eoUniform.h> // eoUniform #include <utils/eoRNG.h>
#include <eoOp.h> // eoOp, eoMonOp, eoBinOp #include <eoOp.h> // eoOp, eoMonOp, eoBinOp
#include <eoPop.h> // eoPop #include <eoPop.h> // eoPop
#include <eoPopOps.h> // eoTransform #include <eoPopOps.h> // eoTransform
@ -74,16 +74,14 @@ template<class Chrom> class eoBreeder: public eoMonPopOp<Chrom>
case eoOp<Chrom>::binary: case eoOp<Chrom>::binary:
{ {
eoBinOp<Chrom>* binop = static_cast<eoBinOp<Chrom>* >(op); eoBinOp<Chrom>* binop = static_cast<eoBinOp<Chrom>* >(op);
eoUniform<unsigned> u(0, pop.size() ); (*binop)(pop[i], pop[ rng.random(pop.size()) ] );
(*binop)(pop[i], pop[ u() ] );
break; break;
} }
case eoOp<Chrom>::quadratic: case eoOp<Chrom>::quadratic:
{ {
eoQuadraticOp<Chrom>* Qop = static_cast<eoQuadraticOp<Chrom>* >(op); eoQuadraticOp<Chrom>* Qop = static_cast<eoQuadraticOp<Chrom>* >(op);
eoUniform<unsigned> u(0, pop.size() ); (*Qop)(pop[i], pop[ rng.random(pop.size()) ] );
(*Qop)(pop[i], pop[ u() ] );
break; break;
} }
case eoOp<Chrom>::general : case eoOp<Chrom>::general :

View file

@ -26,7 +26,7 @@
#ifndef _EODUP_h #ifndef _EODUP_h
#define _EODUP_h #define _EODUP_h
#include <eoUniform.h> #include <utils/eoRNG.h>
#include <eoOp.h> #include <eoOp.h>
@ -42,9 +42,9 @@ public:
virtual ~eoDup() {}; virtual ~eoDup() {};
/// ///
virtual void operator()( EOT& _eo ) const { virtual void operator()( EOT& _eo ) const
eoUniform<unsigned> uniform( 0, _eo.length() ); {
unsigned pos = uniform(); unsigned pos = rng.random(_eo.length());
_eo.insertGene( pos, _eo.gene(pos) ); _eo.insertGene( pos, _eo.gene(pos) );
} }

View file

@ -25,7 +25,7 @@
#ifndef _EOKILL_h #ifndef _EOKILL_h
#define _EOKILL_h #define _EOKILL_h
#include <eoUniform.h> #include <utils/eoRNG.h>
#include <eoOp.h> #include <eoOp.h>
@ -41,9 +41,9 @@ public:
virtual ~eoKill() {}; virtual ~eoKill() {};
/// ///
virtual void operator()( EOT& _eo ) const { virtual void operator()( EOT& _eo ) const
eoUniform<unsigned> uniform( 0, _eo.length() ); {
unsigned pos = uniform( ); unsigned pos = rng.random(_eo.length());
_eo.deleteGene( pos ); _eo.deleteGene( pos );
} }

View file

@ -27,7 +27,6 @@
#include <math.h> #include <math.h>
// EO includes // EO includes
#include <eoOp.h> #include <eoOp.h>
#include <eoUniform.h>
/** Generic Mutation of an EO. /** Generic Mutation of an EO.
This is a virtual class, just to establish the interface for the ctor. This is a virtual class, just to establish the interface for the ctor.

View file

@ -36,7 +36,7 @@
#include <map> #include <map>
// Includes from EO // Includes from EO
#include <eoUniform.h> #include <utils/eoRNG.h>
#include <eoOpSelector.h> #include <eoOpSelector.h>
#include <eoOp.h> #include <eoOp.h>
@ -114,8 +114,7 @@ public:
throw runtime_error( "Operator rates added up different from 1.0" ); throw runtime_error( "Operator rates added up different from 1.0" );
// If here, operators ordered by rate and no problem // If here, operators ordered by rate and no problem
eoUniform<float> u(0,1.0); float aRnd = rng.uniform();
float aRnd = u();
i=begin(); i=begin();
acc = 0; acc = 0;
do { do {

View file

@ -1,7 +1,10 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// eoRandomSelect.h // eoRandomSelect.h
// (c) GeNeura Team, 1998 // (c) GeNeura Team, 1998
/* /*
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
@ -28,16 +31,18 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <algorithm> #include <algorithm>
#include <numeric> // for accumulate #include <numeric> // for accumulate
#include <functional> #include <functional>
#include <eoPopOps.h> #include <eoPopOps.h>
#include <eoUniform.h> #include <utils/eoRNG.h>
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** /**
* eoRandomSelect: an selection operator, which selects randomly a percentage * eoRandomSelect: an selection operator, which selects randomly a percentage
of the initial population. of the initial population.
*/ */
template<class EOT> class eoRandomSelect: public eoBinPopOp<EOT> template<class EOT> class eoRandomSelect: public eoBinPopOp<EOT>
@ -53,41 +58,68 @@ template<class EOT> class eoRandomSelect: public eoBinPopOp<EOT>
/// Takes a percentage of the population randomly, and transfers it to siblings /// Takes a percentage of the population randomly, and transfers it to siblings
virtual void operator() ( eoPop<EOT>& _parents, eoPop<EOT>& _siblings ) { virtual void operator() ( eoPop<EOT>& _parents, eoPop<EOT>& _siblings ) {
// generates random numbers // generates random numbers
eoUniform<unsigned> uniform(0, _parents.size()-1);
unsigned num_chroms = (unsigned)(repRate * _parents.size()); unsigned num_chroms = (unsigned)(repRate * _parents.size());
// selection of chromosomes // selection of chromosomes
do { do {
_siblings.push_back(_parents[uniform()]); _siblings.push_back(_parents[rng.random(_parents.size())]);
} while (_siblings.size() < num_chroms); } while (_siblings.size() < num_chroms);
} }
/// @name Methods from eoObject
//@{
/** /// @name Methods from eoObject
* Read object. Reads the percentage
* Should call base class, just in case. //@{
* @param _s A istream.
*/ /**
virtual void readFrom(istream& _s) {
_s >> repRate; * Read object. Reads the percentage
}
* Should call base class, just in case.
/** Print itself: inherited from eoObject implementation. Declared virtual so that
it can be reimplemented anywhere. Instance from base classes are processed in * @param _s A istream.
base classes, so you don´t have to worry about, for instance, fitness.
@param _s the ostream in which things are written*/ */
virtual void printOn( ostream& _s ) const{
_s << repRate; virtual void readFrom(istream& _s) {
}
_s >> repRate;
/** Inherited from eoObject
@see eoObject }
*/
string className() const {return "eoRandomSelect";};
//@} /** Print itself: inherited from eoObject implementation. Declared virtual so that
it can be reimplemented anywhere. Instance from base classes are processed in
base classes, so you don´t have to worry about, for instance, fitness.
@param _s the ostream in which things are written*/
virtual void printOn( ostream& _s ) const{
_s << repRate;
}
/** Inherited from eoObject
@see eoObject
*/
string className() const {return "eoRandomSelect";};
//@}
private: private:
float repRate; float repRate;

View file

@ -1,8 +1,13 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoRank.h
// (c) GeNeura Team 1999 //-----------------------------------------------------------------------------
// eoRank.h
// (c) GeNeura Team 1999
/* /*
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
@ -20,13 +25,19 @@
Contact: todos@geneura.ugr.es, http://geneura.ugr.es Contact: todos@geneura.ugr.es, http://geneura.ugr.es
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef _eoRank_H
#ifndef _eoRank_H
#define _eoRank_H #define _eoRank_H
#include <eoOpSelector.h>
#include <eoPopOps.h> #include <eoOpSelector.h>
#include <eoPopOps.h>
/** /**
* Takes those on the selection list and creates a list of new individuals * Takes those on the selection list and creates a list of new individuals
@ -41,13 +52,15 @@ class eoRank: public eoSelect<EOT>{
public: public:
/// Ctor /// Ctor
eoRank( unsigned _newPopSize, eoOpSelector<EOT>& _opSelector) eoRank( unsigned _newPopSize, eoOpSelector<EOT>& _opSelector)
:eoSelect<EOT>(), opSelector( _opSelector ), repNewPopSize( _newPopSize ) {}; :eoSelect<EOT>(), opSelector( _opSelector ), repNewPopSize( _newPopSize ) {};
/** Copy ctor /** Copy ctor
* Needs a copy ctor for the EO operators */ * Needs a copy ctor for the EO operators */
eoRank( const eoRank& _rankBreeder) eoRank( const eoRank& _rankBreeder)
:eoSelect<EOT>( _rankBreeder), :eoSelect<EOT>( _rankBreeder),
opSelector( _rankBreeder.opSelector ), repNewPopSize( _rankBreeder.repNewPopSize ) {}; opSelector( _rankBreeder.opSelector ), repNewPopSize( _rankBreeder.repNewPopSize ) {};
/// Dtor /// Dtor
@ -56,7 +69,8 @@ class eoRank: public eoSelect<EOT>{
/** Takes the genetic pool, and returns next generation, destroying the /** Takes the genetic pool, and returns next generation, destroying the
* genetic pool container * genetic pool container
* Non-const because it might order the operator vector*/ * Non-const because it might order the operator vector*/
virtual void operator() ( const eoPop< EOT >& _ptVeo, virtual void operator() ( const eoPop< EOT >& _ptVeo,
eoPop< EOT >& _siblings ) const { eoPop< EOT >& _siblings ) const {
unsigned inLen = _ptVeo.size(); // size of subPop unsigned inLen = _ptVeo.size(); // size of subPop
@ -64,24 +78,29 @@ class eoRank: public eoSelect<EOT>{
throw runtime_error( "zero population in eoRank"); throw runtime_error( "zero population in eoRank");
for ( unsigned i = 0; i < repNewPopSize; i ++ ) { for ( unsigned i = 0; i < repNewPopSize; i ++ ) {
// Create a copy of a random input EO with copy ctor. The members of the // Create a copy of a random input EO with copy ctor. The members of the
// population will be selected by rank, with a certain probability of
// being selected several times if the new population is bigger than the // population will be selected by rank, with a certain probability of
// being selected several times if the new population is bigger than the
// old // old
EOT newEO = _ptVeo[ i%inLen ]; EOT newEO = _ptVeo[ i%inLen ];
// Choose operator // Choose operator
eoUniform<unsigned> u( 0, inLen );
const eoOp<EOT >& thisOp = opSelector.Op(); const eoOp<EOT >& thisOp = opSelector.Op();
if ( thisOp.readArity() == unary ) { if ( thisOp.readArity() == unary ) {
const eoMonOp<EOT>& mopPt = dynamic_cast< const eoMonOp<EOT>& > ( thisOp ); const eoMonOp<EOT>& mopPt = dynamic_cast< const eoMonOp<EOT>& > ( thisOp );
mopPt( newEO ); mopPt( newEO );
} else { } else {
const eoBinOp<EOT>& bopPt = dynamic_cast< const eoBinOp<EOT>& > ( thisOp ); const eoBinOp<EOT>& bopPt = dynamic_cast< const eoBinOp<EOT>& > ( thisOp );
EOT mate = _ptVeo[ u() ];
EOT mate = _ptVeo[ rng.random(inLen) ];
bopPt( newEO, mate ); bopPt( newEO, mate );
} }
_siblings.push_back( newEO ); _siblings.push_back( newEO );
} }
}; };
@ -91,27 +110,48 @@ class eoRank: public eoSelect<EOT>{
void select( unsigned _select ) { void select( unsigned _select ) {
repNewPopSize = _select; repNewPopSize = _select;
} }
/// Methods inherited from eoObject
//@{
/// Methods inherited from eoObject
/** Return the class id.
@return the class name as a string //@{
*/
virtual string className() const { return "eoRank"; };
/** Print itself: inherited from eoObject implementation. Declared virtual so that /** Return the class id.
it can be reimplemented anywhere. Instance from base classes are processed in
base classes, so you don´t have to worry about, for instance, fitness. @return the class name as a string
@param _s the ostream in which things are written*/
virtual void printOn( ostream& _s ) const{ */
_s << opSelector;
_s << repNewPopSize; virtual string className() const { return "eoRank"; };
};
//@} /** Print itself: inherited from eoObject implementation. Declared virtual so that
it can be reimplemented anywhere. Instance from base classes are processed in
base classes, so you don´t have to worry about, for instance, fitness.
@param _s the ostream in which things are written*/
virtual void printOn( ostream& _s ) const{
_s << opSelector;
_s << repNewPopSize;
};
//@}
private: private:
eoOpSelector<EOT> & opSelector; eoOpSelector<EOT> & opSelector;

View file

@ -43,72 +43,19 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** /**
* Base class for a family of random number generators. Generates numbers * Base class for a family of random 'number' generators. These 'numbers'
* according to the parameters given in the constructor. Uses the machine's * can be anything, including full-fledged chromosomes.
* own random number generators. Which is not too good, after all.
*/ */
template<class T> template<class T>
class eoRnd: public eoObject, public eoPersistent class eoRnd
{ {
public: public:
/// default constructor
eoRnd(unsigned _seed = 0) {
if ( !started ) {
srand(_seed?_seed:time(0));
started = true;
}
}
/// Copy cotor
eoRnd(const eoRnd& _r ) {srand(time(0));};
/** Main function: random generators act as functors, that return random numbers. /** Main function: random generators act as functors, that return random numbers.
It´s non-const because it might modify a seed
@return return a random number @return return a random number
*/ */
virtual T operator()() = 0; virtual T operator()() = 0;
/** Return the class id.
@return the class name as a string
*/
virtual string className() const { return "eoRandom"; };
/**
* Read object.
* @param is A istream.
* @throw runtime_exception If a valid object can't be read.
*/
virtual void readFrom(istream& _is);
/**
* print object. Prints just the ID, since the seed is not accesible.
* @param is A ostream.
*/
void printOn(ostream& _os) const { _os << endl; };
private:
/// true if the RNG has been started already. If it starts every time, means trouble.
static bool started;
}; };
template<class T> bool eoRnd<T>::started = false;
//--------------------------------------------------------------------------
/**
* Read object.
* @param is A istream.
* @throw runtime_exception If a valid object can't be read.
*/
template<class T>
void eoRnd<T>::readFrom(istream& _is) {
if (!_is)
throw runtime_error("Problems reading from stream in eoRnd");
long seed;
_is >> seed;
srand(seed);
}
#endif #endif

View file

@ -1,8 +1,13 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoTournament.h
// (c) GeNeura Team, 1998 //-----------------------------------------------------------------------------
// eoTournament.h
// (c) GeNeura Team, 1998
/* /*
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
@ -20,20 +25,28 @@
Contact: todos@geneura.ugr.es, http://geneura.ugr.es Contact: todos@geneura.ugr.es, http://geneura.ugr.es
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef _EOGSTOURN_H #ifndef _EOGSTOURN_H
#define _EOGSTOURN_H #define _EOGSTOURN_H
//-----------------------------------------------------------------------------
#include <eoUniform.h> // for ceil
//-----------------------------------------------------------------------------
#include <utils/eoRNG.h>
#include <eoPopOps.h> #include <eoPopOps.h>
//-----------------------------------------------------------------------------
/** Selects those who are going to reproduce using Tournament selection:
a subset of the population of size tournamentSize is chosen, //-----------------------------------------------------------------------------
/** Selects those who are going to reproduce using Tournament selection:
a subset of the population of size tournamentSize is chosen,
and the best is selected for the new population . and the best is selected for the new population .
@author JJ Merelo, 1998 @author JJ Merelo, 1998
*/ */
@ -52,7 +65,8 @@ public:
void tournamentSize( unsigned _size ) { repTournamentSize = _size; }; void tournamentSize( unsigned _size ) { repTournamentSize = _size; };
/** /**
* Selects from the initial pop using tournament selection, and copies it * Selects from the initial pop using tournament selection, and copies it
* to the other population. * to the other population.
*/ */
virtual void operator() ( eoPop<EOT>& _vEO, eoPop<EOT>& _aVEO) { virtual void operator() ( eoPop<EOT>& _vEO, eoPop<EOT>& _aVEO) {
@ -62,10 +76,10 @@ public:
// Build vector // Build vector
for ( unsigned j = 0; j < thisSize*perc; j ++ ) { for ( unsigned j = 0; j < thisSize*perc; j ++ ) {
// Randomly select a tournamentSize set, and choose the best // Randomly select a tournamentSize set, and choose the best
eoPop<EOT> veoTournament; eoPop<EOT> veoTournament;
eoUniform<unsigned> u( 0, thisSize);
for ( unsigned k = 0; k < repTournamentSize; k++ ) { for ( unsigned k = 0; k < repTournamentSize; k++ ) {
unsigned chosen = u(); unsigned chosen = rng.random(thisSize);
EOT newEO = _vEO[chosen]; EOT newEO = _vEO[chosen];
veoTournament.push_back( newEO ); veoTournament.push_back( newEO );
} }
@ -80,34 +94,61 @@ public:
_aVEO.push_back( *best ); _aVEO.push_back( *best );
} }
}; };
/// @name Methods from eoObject
//@{ /// @name Methods from eoObject
/**
* Read object. Reads the percentage //@{
* Should call base class, just in case.
* @param _s A istream. /**
*/
virtual void readFrom(istream& _s) { * Read object. Reads the percentage
_s >> perc >> repTournamentSize;
} * Should call base class, just in case.
/** Print itself: inherited from eoObject implementation. Declared virtual so that * @param _s A istream.
it can be reimplemented anywhere. Instance from base classes are processed in
base classes, so you don´t have to worry about, for instance, fitness. */
@param _s the ostream in which things are written*/
virtual void printOn( ostream& _s ) const{ virtual void readFrom(istream& _s) {
_s << perc << endl << repTournamentSize << endl;
} _s >> perc >> repTournamentSize;
/** Inherited from eoObject }
@see eoObject
*/
string className() const {return "eoTournament";};
/** Print itself: inherited from eoObject implementation. Declared virtual so that
//@}
it can be reimplemented anywhere. Instance from base classes are processed in
base classes, so you don´t have to worry about, for instance, fitness.
@param _s the ostream in which things are written*/
virtual void printOn( ostream& _s ) const{
_s << perc << endl << repTournamentSize << endl;
}
/** Inherited from eoObject
@see eoObject
*/
string className() const {return "eoTournament";};
//@}
private:
private:
float perc; float perc;
unsigned repTournamentSize; unsigned repTournamentSize;

View file

@ -25,7 +25,7 @@
#ifndef _EOTRANSPOSE_h #ifndef _EOTRANSPOSE_h
#define _EOTRANSPOSE_h #define _EOTRANSPOSE_h
#include <eoUniform.h> #include <utils/eoRNG.h>
#include <eoOp.h> #include <eoOp.h>
@ -45,9 +45,8 @@ public:
/// ///
virtual void operator()( EOT& _eo ) const { virtual void operator()( EOT& _eo ) const {
eoUniform<unsigned> uniform(0, _eo.length() );
unsigned pos1 = uniform(), unsigned pos1 = uniform(),
pos2 = uniform(); pos2 = rng.random(_eo.length());
applyAt( _eo, pos1, pos2 ); applyAt( _eo, pos1, pos2 );
} }

View file

@ -1,90 +1,176 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoUniformXOver.h
// (c) GeNeura Team, 1998 //-----------------------------------------------------------------------------
/*
This library is free software; you can redistribute it and/or // eoUniformXOver.h
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either // (c) GeNeura Team, 1998
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 This library is free software; you can redistribute it and/or
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. modify it under the terms of the GNU Lesser General Public
You should have received a copy of the GNU Lesser General Public License as published by the Free Software Foundation; either
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA version 2 of the License, or (at your option) any later version.
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
*/
//----------------------------------------------------------------------------- This library is distributed in the hope that it will be useful,
#ifndef _EOUNIFORMXOVER_h but WITHOUT ANY WARRANTY; without even the implied warranty of
#define _EOUNIFORMXOVER_h
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// for swap Lesser General Public License for more details.
#if defined( __BORLANDC__ )
#include <algorith>
#else
#include <algorithm> You should have received a copy of the GNU Lesser General Public
#endif
License along with this library; if not, write to the Free Software
// EO includes
#include <eoOp.h> Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#include <eoUniform.h>
//-----------------------------------------------------------------------------
/** Contact: todos@geneura.ugr.es, http://geneura.ugr.es
* EOUniformCrossover: operator for binary chromosomes
* implementation of uniform crossover for EO */
* swaps ranges of bits between the parents
*/ //-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
template<class EOT>
class eoUniformXOver: public eoQuadraticOp< EOT > #ifndef _EOUNIFORMXOVER_h
{
public: #define _EOUNIFORMXOVER_h
///
eoUniformXOver( float _rate = 0.5 ):
eoQuadraticOp< EOT > ( ), rate( _rate ) {
if (rate < 0 || rate > 1)
runtime_error("UxOver --> invalid rate"); // for swap
}
#if defined( __BORLANDC__ )
/// #include <algorith>
void operator() ( EOT& chrom1, EOT& chrom2 ) const {
unsigned end = min(chrom1.length(),chrom2.length()) - 1; #else
// select bits to change
eoUniform<float> rnd(0, 1); #include <algorithm>
// aply changes #endif
for (unsigned bit = 0; bit < end; bit++)
if (rnd() < rate)
swap(chrom1[ bit], chrom2[ bit]);
} // EO includes
/** @name Methods from eoObject #include <eoOp.h>
readFrom and printOn are directly inherited from eoOp
*/ #include <utils/eoRNG.h>
//@{
/** Inherited from eoObject
@see eoObject
*/ //-----------------------------------------------------------------------------
string className() const {return "eoUniformXOver";};
//@} /**
private: * EOUniformCrossover: operator for binary chromosomes
float rate; /// rate of uniform crossover
}; * implementation of uniform crossover for EO
//----------------------------------------------------------------------------- * swaps ranges of bits between the parents
*/
#endif
//-----------------------------------------------------------------------------
template<class EOT>
class eoUniformXOver: public eoQuadraticOp< EOT >
{
public:
///
eoUniformXOver( float _rate = 0.5 ):
eoQuadraticOp< EOT > ( ), rate( _rate ) {
if (rate < 0 || rate > 1)
runtime_error("UxOver --> invalid rate");
}
///
void operator() ( EOT& chrom1, EOT& chrom2 ) const {
unsigned end = min(chrom1.length(),chrom2.length()) - 1;
// select bits to change
// aply changes
for (unsigned bit = 0; bit < end; bit++)
if (rng.flip(rate))
swap(chrom1[ bit], chrom2[ bit]);
}
/** @name Methods from eoObject
readFrom and printOn are directly inherited from eoOp
*/
//@{
/** Inherited from eoObject
@see eoObject
*/
string className() const {return "eoUniformXOver";};
//@}
private:
float rate; /// rate of uniform crossover
};
//-----------------------------------------------------------------------------
#endif

View file

@ -35,7 +35,7 @@
// EO includes // EO includes
#include <eoOp.h> #include <eoOp.h>
#include <eoUniform.h> #include <utils/eoRNG.h>
/** 2-point crossover: takes the genes in the central section of two EOs /** 2-point crossover: takes the genes in the central section of two EOs
and interchanges it and interchanges it
@ -55,8 +55,8 @@ public:
EOT& _eo2 ) const { EOT& _eo2 ) const {
unsigned len1 = _eo1.length(), len2 = _eo2.length(), unsigned len1 = _eo1.length(), len2 = _eo2.length(),
len= (len1 > len2)?len2:len1; len= (len1 > len2)?len2:len1;
eoUniform<unsigned> uniform( 0, len );
unsigned pos1 = uniform(), pos2 = uniform() ; unsigned pos1 = rng.random(len), pos2 = rng.random(len) ;
applyAt( _eo1, _eo2, pos1, pos2 ); applyAt( _eo1, _eo2, pos1, pos2 );

View file

@ -8,7 +8,7 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <algorithm> // swap_ranges #include <algorithm> // swap_ranges
#include <eoUniform.h> // eoUniform #include <utils/eoRNG.h>
#include <ga/eoBin.h> // eoBin #include <ga/eoBin.h> // eoBin
#include <eoOp.h> // eoMonOp #include <eoOp.h> // eoMonOp
@ -43,9 +43,8 @@ template<class Chrom> class eoBinRandom: public eoMonOp<Chrom>
*/ */
void operator()(Chrom& chrom) const void operator()(Chrom& chrom) const
{ {
eoUniform<float> uniform(0.0, 1.0);
for (unsigned i = 0; i < chrom.size(); i++) for (unsigned i = 0; i < chrom.size(); i++)
chrom[i] = (uniform() < 0.5) ? false : true; chrom[i] = rng.flip(0.5) ? false : true;
} }
}; };
@ -67,8 +66,7 @@ template<class Chrom> class eoBinBitFlip: public eoMonOp<Chrom>
*/ */
void operator()(Chrom& chrom) const void operator()(Chrom& chrom) const
{ {
eoUniform<int> uniform(0, chrom.size()); unsigned i = rng.random(chrom.size());
unsigned i = uniform();
chrom[i] = (chrom[i]) ? false : true; chrom[i] = (chrom[i]) ? false : true;
} }
}; };
@ -86,7 +84,7 @@ template<class Chrom> class eoBinMutation: public eoMonOp<Chrom>
* (Default) Constructor. * (Default) Constructor.
* @param _rate Rate of mutation. * @param _rate Rate of mutation.
*/ */
eoBinMutation(const double& _rate = 0.01): rate(_rate), uniform(0.0, 1.0) {} eoBinMutation(const double& _rate = 0.01): rate(_rate) {}
/// The class name. /// The class name.
string className() const { return "eoBinMutation"; } string className() const { return "eoBinMutation"; }
@ -98,13 +96,12 @@ template<class Chrom> class eoBinMutation: public eoMonOp<Chrom>
void operator()(Chrom& chrom) const void operator()(Chrom& chrom) const
{ {
for (unsigned i = 0; i < chrom.size(); i++) for (unsigned i = 0; i < chrom.size(); i++)
if (uniform() < rate) if (rng.flip(rate))
chrom[i] = !chrom[i]; chrom[i] = !chrom[i];
} }
private: private:
double rate; double rate;
mutable eoUniform<float> uniform;
}; };
@ -125,10 +122,9 @@ template<class Chrom> class eoBinInversion: public eoMonOp<Chrom>
*/ */
void operator()(Chrom& chrom) const void operator()(Chrom& chrom) const
{ {
eoUniform<unsigned> uniform(0, chrom.size() + 1);
unsigned u1 = uniform(), u2; unsigned u1 = rng.random(chrom.size() + 1) , u2;
do u2 = uniform(); while (u1 == u2); do u2 = rng.random(chrom.size() + 1); while (u1 == u2);
unsigned r1 = min(u1, u2), r2 = max(u1, u2); unsigned r1 = min(u1, u2), r2 = max(u1, u2);
reverse(chrom.begin() + r1, chrom.begin() + r2); reverse(chrom.begin() + r1, chrom.begin() + r2);
@ -218,8 +214,7 @@ template<class Chrom> class eoBinCrossover: public eoQuadraticOp<Chrom>
*/ */
void operator()(Chrom& chrom1, Chrom& chrom2) const void operator()(Chrom& chrom1, Chrom& chrom2) const
{ {
eoUniform<unsigned> uniform(1, min(chrom1.size(), chrom2.size())); swap_ranges(chrom1.begin(), chrom1.begin() + rng.random(min(chrom1.size(), chrom2.size())), chrom2.begin());
swap_ranges(chrom1.begin(), chrom1.begin() + uniform(), chrom2.begin());
} }
}; };
@ -253,11 +248,10 @@ template<class Chrom> class eoBinNxOver: public eoQuadraticOp<Chrom>
unsigned max_points = min(max_size - 1, num_points); unsigned max_points = min(max_size - 1, num_points);
vector<bool> points(max_size, false); vector<bool> points(max_size, false);
eoUniform<unsigned> uniform(1, max_size);
// select ranges of bits to swap // select ranges of bits to swap
do { do {
unsigned bit = uniform(); unsigned bit = rng.random(max_size) + 1;
if (points[bit]) if (points[bit])
continue; continue;
else else
@ -317,11 +311,10 @@ template<class Chrom> class eoBinGxOver: public eoQuadraticOp<Chrom>
unsigned cut_genes = min(max_genes, num_points); unsigned cut_genes = min(max_genes, num_points);
vector<bool> points(max_genes, false); vector<bool> points(max_genes, false);
eoUniform<unsigned> uniform(0, max_genes);
// selects genes to swap // selects genes to swap
do { do {
unsigned bit = uniform(); unsigned bit = rng.random(max_genes);
if (points[bit]) if (points[bit])
continue; continue;
else else