From c1b0a6c5034eb4cc194f114a8f8ef4c1269fcb9d Mon Sep 17 00:00:00 2001 From: mac Date: Sat, 10 Jun 2000 13:22:53 +0000 Subject: [PATCH] Removed dependence on eoUniform, changed it to use rng.random or rng.uniform directly --- eo/src/eoAtomMutation.h | 1 - eo/src/eoBreeder.h | 8 +- eo/src/eoDup.h | 8 +- eo/src/eoKill.h | 8 +- eo/src/eoMutation.h | 1 - eo/src/eoProportionalOpSel.h | 5 +- eo/src/eoRandomSelect.h | 104 +++++++++----- eo/src/eoRank.h | 128 +++++++++++------ eo/src/eoRnd.h | 59 +------- eo/src/eoTournament.h | 131 +++++++++++------ eo/src/eoTranspose.h | 5 +- eo/src/eoUniformXOver.h | 266 +++++++++++++++++++++++------------ eo/src/eoXOver2.h | 6 +- eo/src/ga/eoBitOp.h | 27 ++-- 14 files changed, 445 insertions(+), 312 deletions(-) diff --git a/eo/src/eoAtomMutation.h b/eo/src/eoAtomMutation.h index 3ecec82f..59266e3f 100644 --- a/eo/src/eoAtomMutation.h +++ b/eo/src/eoAtomMutation.h @@ -29,7 +29,6 @@ // EO includes #include -#include #include #include diff --git a/eo/src/eoBreeder.h b/eo/src/eoBreeder.h index 5da02fd5..bab14f52 100644 --- a/eo/src/eoBreeder.h +++ b/eo/src/eoBreeder.h @@ -29,7 +29,7 @@ //----------------------------------------------------------------------------- #include // vector -#include // eoUniform +#include #include // eoOp, eoMonOp, eoBinOp #include // eoPop #include // eoTransform @@ -74,16 +74,14 @@ template class eoBreeder: public eoMonPopOp case eoOp::binary: { eoBinOp* binop = static_cast* >(op); - eoUniform u(0, pop.size() ); - (*binop)(pop[i], pop[ u() ] ); + (*binop)(pop[i], pop[ rng.random(pop.size()) ] ); break; } case eoOp::quadratic: { eoQuadraticOp* Qop = static_cast* >(op); - eoUniform u(0, pop.size() ); - (*Qop)(pop[i], pop[ u() ] ); + (*Qop)(pop[i], pop[ rng.random(pop.size()) ] ); break; } case eoOp::general : diff --git a/eo/src/eoDup.h b/eo/src/eoDup.h index 727db3c3..a3ec9766 100644 --- a/eo/src/eoDup.h +++ b/eo/src/eoDup.h @@ -26,7 +26,7 @@ #ifndef _EODUP_h #define _EODUP_h -#include +#include #include @@ -42,9 +42,9 @@ public: virtual ~eoDup() {}; /// - virtual void operator()( EOT& _eo ) const { - eoUniform 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) ); } diff --git a/eo/src/eoKill.h b/eo/src/eoKill.h index d6ad11c7..e32eed28 100644 --- a/eo/src/eoKill.h +++ b/eo/src/eoKill.h @@ -25,7 +25,7 @@ #ifndef _EOKILL_h #define _EOKILL_h -#include +#include #include @@ -41,9 +41,9 @@ public: virtual ~eoKill() {}; /// - virtual void operator()( EOT& _eo ) const { - eoUniform uniform( 0, _eo.length() ); - unsigned pos = uniform( ); + virtual void operator()( EOT& _eo ) const + { + unsigned pos = rng.random(_eo.length()); _eo.deleteGene( pos ); } diff --git a/eo/src/eoMutation.h b/eo/src/eoMutation.h index 2c4672f6..ecca1826 100644 --- a/eo/src/eoMutation.h +++ b/eo/src/eoMutation.h @@ -27,7 +27,6 @@ #include // EO includes #include -#include /** Generic Mutation of an EO. This is a virtual class, just to establish the interface for the ctor. diff --git a/eo/src/eoProportionalOpSel.h b/eo/src/eoProportionalOpSel.h index 0a48a620..00ea5fb9 100644 --- a/eo/src/eoProportionalOpSel.h +++ b/eo/src/eoProportionalOpSel.h @@ -36,7 +36,7 @@ #include // Includes from EO -#include +#include #include #include @@ -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 u(0,1.0); - float aRnd = u(); + float aRnd = rng.uniform(); i=begin(); acc = 0; do { diff --git a/eo/src/eoRandomSelect.h b/eo/src/eoRandomSelect.h index 4265cc9d..53b7a03e 100644 --- a/eo/src/eoRandomSelect.h +++ b/eo/src/eoRandomSelect.h @@ -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 -#include // for accumulate +#include // for accumulate + #include #include -#include +#include //----------------------------------------------------------------------------- /** - * eoRandomSelect: an selection operator, which selects randomly a percentage + * eoRandomSelect: an selection operator, which selects randomly a percentage + of the initial population. */ template class eoRandomSelect: public eoBinPopOp @@ -53,41 +58,68 @@ template class eoRandomSelect: public eoBinPopOp /// Takes a percentage of the population randomly, and transfers it to siblings virtual void operator() ( eoPop& _parents, eoPop& _siblings ) { // generates random numbers - eoUniform 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; diff --git a/eo/src/eoRank.h b/eo/src/eoRank.h index adc3ff83..1412bc8a 100644 --- a/eo/src/eoRank.h +++ b/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 -#include + + +#include + +#include + /** * Takes those on the selection list and creates a list of new individuals @@ -41,13 +52,15 @@ class eoRank: public eoSelect{ public: /// Ctor - eoRank( unsigned _newPopSize, eoOpSelector& _opSelector) + eoRank( unsigned _newPopSize, eoOpSelector& _opSelector) + :eoSelect(), opSelector( _opSelector ), repNewPopSize( _newPopSize ) {}; /** Copy ctor * Needs a copy ctor for the EO operators */ eoRank( const eoRank& _rankBreeder) - :eoSelect( _rankBreeder), + :eoSelect( _rankBreeder), + opSelector( _rankBreeder.opSelector ), repNewPopSize( _rankBreeder.repNewPopSize ) {}; /// Dtor @@ -56,7 +69,8 @@ class eoRank: public eoSelect{ /** 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{ 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 u( 0, inLen ); - const eoOp& thisOp = opSelector.Op(); + // Choose operator + + const eoOp& thisOp = opSelector.Op(); if ( thisOp.readArity() == unary ) { const eoMonOp& mopPt = dynamic_cast< const eoMonOp& > ( thisOp ); mopPt( newEO ); } else { - const eoBinOp& bopPt = dynamic_cast< const eoBinOp& > ( thisOp ); - EOT mate = _ptVeo[ u() ]; + const eoBinOp& bopPt = dynamic_cast< const eoBinOp& > ( thisOp ); + + EOT mate = _ptVeo[ rng.random(inLen) ]; bopPt( newEO, mate ); } - + + _siblings.push_back( newEO ); } }; @@ -91,27 +110,48 @@ class eoRank: public eoSelect{ 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 & opSelector; diff --git a/eo/src/eoRnd.h b/eo/src/eoRnd.h index 93b61c2f..71a7095f 100644 --- a/eo/src/eoRnd.h +++ b/eo/src/eoRnd.h @@ -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 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 bool eoRnd::started = false; - - -//-------------------------------------------------------------------------- - -/** - * Read object. - * @param is A istream. - * @throw runtime_exception If a valid object can't be read. - */ -template -void eoRnd::readFrom(istream& _is) { - if (!_is) - throw runtime_error("Problems reading from stream in eoRnd"); - long seed; - _is >> seed; - srand(seed); -} - #endif diff --git a/eo/src/eoTournament.h b/eo/src/eoTournament.h index 45db15dc..6cc01390 100644 --- a/eo/src/eoTournament.h +++ b/eo/src/eoTournament.h @@ -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 // for ceil + +//----------------------------------------------------------------------------- + + +#include #include - -//----------------------------------------------------------------------------- -/** 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& _vEO, eoPop& _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 veoTournament; - eoUniform u( 0, thisSize); + eoPop 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; diff --git a/eo/src/eoTranspose.h b/eo/src/eoTranspose.h index 79146743..111da025 100644 --- a/eo/src/eoTranspose.h +++ b/eo/src/eoTranspose.h @@ -25,7 +25,7 @@ #ifndef _EOTRANSPOSE_h #define _EOTRANSPOSE_h -#include +#include #include @@ -45,9 +45,8 @@ public: /// virtual void operator()( EOT& _eo ) const { - eoUniform uniform(0, _eo.length() ); unsigned pos1 = uniform(), - pos2 = uniform(); + pos2 = rng.random(_eo.length()); applyAt( _eo, pos1, pos2 ); } diff --git a/eo/src/eoUniformXOver.h b/eo/src/eoUniformXOver.h index a2dfa5e2..961efa8b 100644 --- a/eo/src/eoUniformXOver.h +++ b/eo/src/eoUniformXOver.h @@ -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 -#else -#include -#endif - -// EO includes -#include -#include - -//----------------------------------------------------------------------------- -/** - * EOUniformCrossover: operator for binary chromosomes - * implementation of uniform crossover for EO - * swaps ranges of bits between the parents - */ -//----------------------------------------------------------------------------- - -template -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 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 + +#else + +#include + +#endif + + + +// EO includes + +#include + +#include + + + +//----------------------------------------------------------------------------- + +/** + + * EOUniformCrossover: operator for binary chromosomes + + * implementation of uniform crossover for EO + + * swaps ranges of bits between the parents + + */ + +//----------------------------------------------------------------------------- + + + +template + +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 + diff --git a/eo/src/eoXOver2.h b/eo/src/eoXOver2.h index 867de1c0..ddb1682e 100644 --- a/eo/src/eoXOver2.h +++ b/eo/src/eoXOver2.h @@ -35,7 +35,7 @@ // EO includes #include -#include +#include /** 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 uniform( 0, len ); - unsigned pos1 = uniform(), pos2 = uniform() ; + + unsigned pos1 = rng.random(len), pos2 = rng.random(len) ; applyAt( _eo1, _eo2, pos1, pos2 ); diff --git a/eo/src/ga/eoBitOp.h b/eo/src/ga/eoBitOp.h index ef3137f2..3e4c1ae7 100644 --- a/eo/src/ga/eoBitOp.h +++ b/eo/src/ga/eoBitOp.h @@ -8,7 +8,7 @@ //----------------------------------------------------------------------------- #include // swap_ranges -#include // eoUniform +#include #include // eoBin #include // eoMonOp @@ -43,9 +43,8 @@ template class eoBinRandom: public eoMonOp */ void operator()(Chrom& chrom) const { - eoUniform 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 eoBinBitFlip: public eoMonOp */ void operator()(Chrom& chrom) const { - eoUniform 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 eoBinMutation: public eoMonOp * (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 eoBinMutation: public eoMonOp 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 uniform; }; @@ -125,10 +122,9 @@ template class eoBinInversion: public eoMonOp */ void operator()(Chrom& chrom) const { - eoUniform 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 eoBinCrossover: public eoQuadraticOp */ void operator()(Chrom& chrom1, Chrom& chrom2) const { - eoUniform 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 eoBinNxOver: public eoQuadraticOp unsigned max_points = min(max_size - 1, num_points); vector points(max_size, false); - eoUniform 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 eoBinGxOver: public eoQuadraticOp unsigned cut_genes = min(max_genes, num_points); vector points(max_genes, false); - eoUniform uniform(0, max_genes); // selects genes to swap do { - unsigned bit = uniform(); + unsigned bit = rng.random(max_genes); if (points[bit]) continue; else