Removed dependence on eoUniform, changed it to use rng.random or rng.uniform directly
This commit is contained in:
parent
f39f159924
commit
c1b0a6c503
14 changed files with 446 additions and 313 deletions
|
|
@ -29,7 +29,6 @@
|
|||
|
||||
// EO includes
|
||||
#include <eoOp.h>
|
||||
#include <eoUniform.h>
|
||||
#include <utils/eoRNG.h>
|
||||
#include <eoAtomMutator.h>
|
||||
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <vector> // vector
|
||||
#include <eoUniform.h> // eoUniform
|
||||
#include <utils/eoRNG.h>
|
||||
#include <eoOp.h> // eoOp, eoMonOp, eoBinOp
|
||||
#include <eoPop.h> // eoPop
|
||||
#include <eoPopOps.h> // eoTransform
|
||||
|
|
@ -74,16 +74,14 @@ template<class Chrom> class eoBreeder: public eoMonPopOp<Chrom>
|
|||
case eoOp<Chrom>::binary:
|
||||
{
|
||||
eoBinOp<Chrom>* binop = static_cast<eoBinOp<Chrom>* >(op);
|
||||
eoUniform<unsigned> u(0, pop.size() );
|
||||
(*binop)(pop[i], pop[ u() ] );
|
||||
(*binop)(pop[i], pop[ rng.random(pop.size()) ] );
|
||||
break;
|
||||
}
|
||||
case eoOp<Chrom>::quadratic:
|
||||
{
|
||||
eoQuadraticOp<Chrom>* Qop = static_cast<eoQuadraticOp<Chrom>* >(op);
|
||||
|
||||
eoUniform<unsigned> u(0, pop.size() );
|
||||
(*Qop)(pop[i], pop[ u() ] );
|
||||
(*Qop)(pop[i], pop[ rng.random(pop.size()) ] );
|
||||
break;
|
||||
}
|
||||
case eoOp<Chrom>::general :
|
||||
|
|
|
|||
|
|
@ -26,7 +26,7 @@
|
|||
#ifndef _EODUP_h
|
||||
#define _EODUP_h
|
||||
|
||||
#include <eoUniform.h>
|
||||
#include <utils/eoRNG.h>
|
||||
|
||||
#include <eoOp.h>
|
||||
|
||||
|
|
@ -42,9 +42,9 @@ public:
|
|||
virtual ~eoDup() {};
|
||||
|
||||
///
|
||||
virtual void operator()( EOT& _eo ) const {
|
||||
eoUniform<unsigned> uniform( 0, _eo.length() );
|
||||
unsigned pos = uniform();
|
||||
virtual void operator()( EOT& _eo ) const
|
||||
{
|
||||
unsigned pos = rng.random(_eo.length());
|
||||
_eo.insertGene( pos, _eo.gene(pos) );
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@
|
|||
#ifndef _EOKILL_h
|
||||
#define _EOKILL_h
|
||||
|
||||
#include <eoUniform.h>
|
||||
#include <utils/eoRNG.h>
|
||||
|
||||
#include <eoOp.h>
|
||||
|
||||
|
|
@ -41,9 +41,9 @@ public:
|
|||
virtual ~eoKill() {};
|
||||
|
||||
///
|
||||
virtual void operator()( EOT& _eo ) const {
|
||||
eoUniform<unsigned> uniform( 0, _eo.length() );
|
||||
unsigned pos = uniform( );
|
||||
virtual void operator()( EOT& _eo ) const
|
||||
{
|
||||
unsigned pos = rng.random(_eo.length());
|
||||
_eo.deleteGene( pos );
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,7 +27,6 @@
|
|||
#include <math.h>
|
||||
// EO includes
|
||||
#include <eoOp.h>
|
||||
#include <eoUniform.h>
|
||||
|
||||
/** Generic Mutation of an EO.
|
||||
This is a virtual class, just to establish the interface for the ctor.
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@
|
|||
#include <map>
|
||||
|
||||
// Includes from EO
|
||||
#include <eoUniform.h>
|
||||
#include <utils/eoRNG.h>
|
||||
#include <eoOpSelector.h>
|
||||
#include <eoOp.h>
|
||||
|
||||
|
|
@ -114,8 +114,7 @@ public:
|
|||
throw runtime_error( "Operator rates added up different from 1.0" );
|
||||
|
||||
// If here, operators ordered by rate and no problem
|
||||
eoUniform<float> u(0,1.0);
|
||||
float aRnd = u();
|
||||
float aRnd = rng.uniform();
|
||||
i=begin();
|
||||
acc = 0;
|
||||
do {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
|
|
@ -28,16 +31,18 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <algorithm>
|
||||
#include <numeric> // for accumulate
|
||||
#include <numeric> // for accumulate
|
||||
|
||||
#include <functional>
|
||||
|
||||
#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.
|
||||
*/
|
||||
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
|
||||
virtual void operator() ( eoPop<EOT>& _parents, eoPop<EOT>& _siblings ) {
|
||||
// generates random numbers
|
||||
eoUniform<unsigned> uniform(0, _parents.size()-1);
|
||||
unsigned num_chroms = (unsigned)(repRate * _parents.size());
|
||||
|
||||
// selection of chromosomes
|
||||
do {
|
||||
_siblings.push_back(_parents[uniform()]);
|
||||
_siblings.push_back(_parents[rng.random(_parents.size())]);
|
||||
} while (_siblings.size() < num_chroms);
|
||||
}
|
||||
|
||||
/// @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;
|
||||
}
|
||||
|
||||
/** 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";};
|
||||
|
||||
//@}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// @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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** 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:
|
||||
float repRate;
|
||||
|
|
|
|||
128
eo/src/eoRank.h
128
eo/src/eoRank.h
|
|
@ -1,8 +1,13 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoRank.h
|
||||
// (c) GeNeura Team 1999
|
||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// eoRank.h
|
||||
|
||||
// (c) GeNeura Team 1999
|
||||
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
|
|
@ -20,13 +25,19 @@
|
|||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _eoRank_H
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
#ifndef _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
|
||||
|
|
@ -41,13 +52,15 @@ class eoRank: public eoSelect<EOT>{
|
|||
public:
|
||||
|
||||
/// Ctor
|
||||
eoRank( unsigned _newPopSize, eoOpSelector<EOT>& _opSelector)
|
||||
eoRank( unsigned _newPopSize, eoOpSelector<EOT>& _opSelector)
|
||||
|
||||
:eoSelect<EOT>(), opSelector( _opSelector ), repNewPopSize( _newPopSize ) {};
|
||||
|
||||
/** Copy ctor
|
||||
* Needs a copy ctor for the EO operators */
|
||||
eoRank( const eoRank& _rankBreeder)
|
||||
:eoSelect<EOT>( _rankBreeder),
|
||||
:eoSelect<EOT>( _rankBreeder),
|
||||
|
||||
opSelector( _rankBreeder.opSelector ), repNewPopSize( _rankBreeder.repNewPopSize ) {};
|
||||
|
||||
/// Dtor
|
||||
|
|
@ -56,7 +69,8 @@ class eoRank: public eoSelect<EOT>{
|
|||
/** Takes the genetic pool, and returns next generation, destroying the
|
||||
* genetic pool container
|
||||
* 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 {
|
||||
|
||||
unsigned inLen = _ptVeo.size(); // size of subPop
|
||||
|
|
@ -64,24 +78,29 @@ class eoRank: public eoSelect<EOT>{
|
|||
throw runtime_error( "zero population in eoRank");
|
||||
|
||||
for ( unsigned i = 0; i < repNewPopSize; i ++ ) {
|
||||
// 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
|
||||
// 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
|
||||
|
||||
// old
|
||||
EOT newEO = _ptVeo[ i%inLen ];
|
||||
|
||||
// Choose operator
|
||||
eoUniform<unsigned> u( 0, inLen );
|
||||
const eoOp<EOT >& thisOp = opSelector.Op();
|
||||
// Choose operator
|
||||
|
||||
const eoOp<EOT >& thisOp = opSelector.Op();
|
||||
if ( thisOp.readArity() == unary ) {
|
||||
const eoMonOp<EOT>& mopPt = dynamic_cast< const eoMonOp<EOT>& > ( thisOp );
|
||||
mopPt( newEO );
|
||||
} else {
|
||||
const eoBinOp<EOT>& bopPt = dynamic_cast< const eoBinOp<EOT>& > ( thisOp );
|
||||
EOT mate = _ptVeo[ u() ];
|
||||
const eoBinOp<EOT>& bopPt = dynamic_cast< const eoBinOp<EOT>& > ( thisOp );
|
||||
|
||||
EOT mate = _ptVeo[ rng.random(inLen) ];
|
||||
bopPt( newEO, mate );
|
||||
}
|
||||
|
||||
|
||||
|
||||
_siblings.push_back( newEO );
|
||||
}
|
||||
};
|
||||
|
|
@ -91,27 +110,48 @@ class eoRank: public eoSelect<EOT>{
|
|||
void select( unsigned _select ) {
|
||||
repNewPopSize = _select;
|
||||
}
|
||||
|
||||
|
||||
/// 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
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
//@}
|
||||
|
||||
|
||||
|
||||
|
||||
/// 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
|
||||
|
||||
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:
|
||||
eoOpSelector<EOT> & opSelector;
|
||||
|
|
|
|||
|
|
@ -43,72 +43,19 @@
|
|||
|
||||
//-----------------------------------------------------------------------------
|
||||
/**
|
||||
* Base class for a family of random number generators. Generates numbers
|
||||
* according to the parameters given in the constructor. Uses the machine's
|
||||
* own random number generators. Which is not too good, after all.
|
||||
* Base class for a family of random 'number' generators. These 'numbers'
|
||||
* can be anything, including full-fledged chromosomes.
|
||||
*/
|
||||
template<class T>
|
||||
class eoRnd: public eoObject, public eoPersistent
|
||||
class eoRnd
|
||||
{
|
||||
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.
|
||||
It´s non-const because it might modify a seed
|
||||
@return return a random number
|
||||
*/
|
||||
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
|
||||
|
|
|
|||
|
|
@ -1,8 +1,13 @@
|
|||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// eoTournament.h
|
||||
// (c) GeNeura Team, 1998
|
||||
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
|
||||
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
// eoTournament.h
|
||||
|
||||
// (c) GeNeura Team, 1998
|
||||
|
||||
/*
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
|
|
@ -20,20 +25,28 @@
|
|||
|
||||
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
#ifndef _EOGSTOURN_H
|
||||
#define _EOGSTOURN_H
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <eoUniform.h> // for ceil
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
#include <utils/eoRNG.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 .
|
||||
@author JJ Merelo, 1998
|
||||
*/
|
||||
|
|
@ -52,7 +65,8 @@ public:
|
|||
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.
|
||||
*/
|
||||
virtual void operator() ( eoPop<EOT>& _vEO, eoPop<EOT>& _aVEO) {
|
||||
|
|
@ -62,10 +76,10 @@ public:
|
|||
// Build vector
|
||||
for ( unsigned j = 0; j < thisSize*perc; j ++ ) {
|
||||
// Randomly select a tournamentSize set, and choose the best
|
||||
eoPop<EOT> veoTournament;
|
||||
eoUniform<unsigned> u( 0, thisSize);
|
||||
eoPop<EOT> veoTournament;
|
||||
|
||||
for ( unsigned k = 0; k < repTournamentSize; k++ ) {
|
||||
unsigned chosen = u();
|
||||
unsigned chosen = rng.random(thisSize);
|
||||
EOT newEO = _vEO[chosen];
|
||||
veoTournament.push_back( newEO );
|
||||
}
|
||||
|
|
@ -80,34 +94,61 @@ public:
|
|||
_aVEO.push_back( *best );
|
||||
}
|
||||
};
|
||||
|
||||
/// @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 >> perc >> repTournamentSize;
|
||||
}
|
||||
|
||||
/** 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";};
|
||||
|
||||
//@}
|
||||
|
||||
|
||||
/// @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 >> perc >> repTournamentSize;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/** 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;
|
||||
unsigned repTournamentSize;
|
||||
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@
|
|||
#ifndef _EOTRANSPOSE_h
|
||||
#define _EOTRANSPOSE_h
|
||||
|
||||
#include <eoUniform.h>
|
||||
#include <utils/eoRNG.h>
|
||||
|
||||
#include <eoOp.h>
|
||||
|
||||
|
|
@ -45,9 +45,8 @@ public:
|
|||
|
||||
///
|
||||
virtual void operator()( EOT& _eo ) const {
|
||||
eoUniform<unsigned> uniform(0, _eo.length() );
|
||||
unsigned pos1 = uniform(),
|
||||
pos2 = uniform();
|
||||
pos2 = rng.random(_eo.length());
|
||||
applyAt( _eo, pos1, pos2 );
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,90 +1,176 @@
|
|||
// -*- 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
|
||||
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: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
*/
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _EOUNIFORMXOVER_h
|
||||
#define _EOUNIFORMXOVER_h
|
||||
|
||||
|
||||
// for swap
|
||||
#if defined( __BORLANDC__ )
|
||||
#include <algorith>
|
||||
#else
|
||||
#include <algorithm>
|
||||
#endif
|
||||
|
||||
// EO includes
|
||||
#include <eoOp.h>
|
||||
#include <eoUniform.h>
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/**
|
||||
* 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 >
|
||||
{
|
||||
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
|
||||
eoUniform<float> rnd(0, 1);
|
||||
|
||||
// aply changes
|
||||
for (unsigned bit = 0; bit < end; bit++)
|
||||
if (rnd() < 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
|
||||
// -*- 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
|
||||
|
||||
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: todos@geneura.ugr.es, http://geneura.ugr.es
|
||||
|
||||
*/
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
|
||||
|
||||
#ifndef _EOUNIFORMXOVER_h
|
||||
|
||||
#define _EOUNIFORMXOVER_h
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// for swap
|
||||
|
||||
#if defined( __BORLANDC__ )
|
||||
|
||||
#include <algorith>
|
||||
|
||||
#else
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
// EO includes
|
||||
|
||||
#include <eoOp.h>
|
||||
|
||||
#include <utils/eoRNG.h>
|
||||
|
||||
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
|
||||
* 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 >
|
||||
|
||||
{
|
||||
|
||||
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
|
||||
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@
|
|||
|
||||
// EO includes
|
||||
#include <eoOp.h>
|
||||
#include <eoUniform.h>
|
||||
#include <utils/eoRNG.h>
|
||||
|
||||
/** 2-point crossover: takes the genes in the central section of two EOs
|
||||
and interchanges it
|
||||
|
|
@ -55,8 +55,8 @@ public:
|
|||
EOT& _eo2 ) const {
|
||||
unsigned len1 = _eo1.length(), len2 = _eo2.length(),
|
||||
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 );
|
||||
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include <algorithm> // swap_ranges
|
||||
#include <eoUniform.h> // eoUniform
|
||||
#include <utils/eoRNG.h>
|
||||
#include <ga/eoBin.h> // eoBin
|
||||
#include <eoOp.h> // eoMonOp
|
||||
|
||||
|
|
@ -43,9 +43,8 @@ template<class Chrom> class eoBinRandom: public eoMonOp<Chrom>
|
|||
*/
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
eoUniform<float> uniform(0.0, 1.0);
|
||||
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
|
||||
{
|
||||
eoUniform<int> uniform(0, chrom.size());
|
||||
unsigned i = uniform();
|
||||
unsigned i = rng.random(chrom.size());
|
||||
chrom[i] = (chrom[i]) ? false : true;
|
||||
}
|
||||
};
|
||||
|
|
@ -86,7 +84,7 @@ template<class Chrom> class eoBinMutation: public eoMonOp<Chrom>
|
|||
* (Default) Constructor.
|
||||
* @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.
|
||||
string className() const { return "eoBinMutation"; }
|
||||
|
|
@ -98,13 +96,12 @@ template<class Chrom> class eoBinMutation: public eoMonOp<Chrom>
|
|||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
for (unsigned i = 0; i < chrom.size(); i++)
|
||||
if (uniform() < rate)
|
||||
if (rng.flip(rate))
|
||||
chrom[i] = !chrom[i];
|
||||
}
|
||||
|
||||
private:
|
||||
double rate;
|
||||
mutable eoUniform<float> uniform;
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -125,10 +122,9 @@ template<class Chrom> class eoBinInversion: public eoMonOp<Chrom>
|
|||
*/
|
||||
void operator()(Chrom& chrom) const
|
||||
{
|
||||
eoUniform<unsigned> uniform(0, chrom.size() + 1);
|
||||
|
||||
unsigned u1 = uniform(), u2;
|
||||
do u2 = uniform(); while (u1 == u2);
|
||||
unsigned u1 = rng.random(chrom.size() + 1) , u2;
|
||||
do u2 = rng.random(chrom.size() + 1); while (u1 == u2);
|
||||
unsigned r1 = min(u1, u2), r2 = max(u1, u2);
|
||||
|
||||
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
|
||||
{
|
||||
eoUniform<unsigned> uniform(1, min(chrom1.size(), chrom2.size()));
|
||||
swap_ranges(chrom1.begin(), chrom1.begin() + uniform(), chrom2.begin());
|
||||
swap_ranges(chrom1.begin(), chrom1.begin() + rng.random(min(chrom1.size(), chrom2.size())), chrom2.begin());
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -253,11 +248,10 @@ template<class Chrom> class eoBinNxOver: public eoQuadraticOp<Chrom>
|
|||
unsigned max_points = min(max_size - 1, num_points);
|
||||
|
||||
vector<bool> points(max_size, false);
|
||||
eoUniform<unsigned> uniform(1, max_size);
|
||||
|
||||
// select ranges of bits to swap
|
||||
do {
|
||||
unsigned bit = uniform();
|
||||
unsigned bit = rng.random(max_size) + 1;
|
||||
if (points[bit])
|
||||
continue;
|
||||
else
|
||||
|
|
@ -317,11 +311,10 @@ template<class Chrom> class eoBinGxOver: public eoQuadraticOp<Chrom>
|
|||
unsigned cut_genes = min(max_genes, num_points);
|
||||
|
||||
vector<bool> points(max_genes, false);
|
||||
eoUniform<unsigned> uniform(0, max_genes);
|
||||
|
||||
// selects genes to swap
|
||||
do {
|
||||
unsigned bit = uniform();
|
||||
unsigned bit = rng.random(max_genes);
|
||||
if (points[bit])
|
||||
continue;
|
||||
else
|
||||
|
|
|
|||
Reference in a new issue