From 2443677f13edf1c59f5bafc82ded943a2dd0353e Mon Sep 17 00:00:00 2001 From: mac Date: Sat, 19 Feb 2000 16:30:42 +0000 Subject: [PATCH] Moved the static eoRNG rng to an extern eoRNG This external object is now defined in eoPersistent.cpp This should change... --- eo/src/compatibility.h | 8 + eo/src/eoDetTournament.h | 2 +- eo/src/eoGOpSelector.h | 14 +- eo/src/eoInserter.h | 3 +- eo/src/eoOp.h | 2 +- eo/src/eoPersistent.cpp | 4 + eo/src/eoPop.h | 304 ++++++++++++++++++----------------- eo/src/eoProportionalOpSel.h | 5 +- eo/src/eoRNG.h | 4 +- eo/src/eoSteadyStateEA.h | 7 +- eo/src/eoUniformSelect.h | 128 +++++++-------- eo/src/eoWrappedOps.h | 3 +- 12 files changed, 257 insertions(+), 227 deletions(-) diff --git a/eo/src/compatibility.h b/eo/src/compatibility.h index a93aa2e5..7a4f5364 100644 --- a/eo/src/compatibility.h +++ b/eo/src/compatibility.h @@ -28,6 +28,14 @@ #ifndef COMPAT_H #define COMPAT_H +#include +#include + +#ifdef _1__GNUC__ +// Specifics for GNUC +#define NO_GOOD_ISTREAM_ITERATORS +#endif + #ifdef _MSC_VER /* Maarten: added this code here because Mirkosoft has the diff --git a/eo/src/eoDetTournament.h b/eo/src/eoDetTournament.h index e5b1fce1..b4a5bec9 100644 --- a/eo/src/eoDetTournament.h +++ b/eo/src/eoDetTournament.h @@ -43,7 +43,7 @@ template class eoDetTournament: public eoSelectOne { public: /// (Default) Constructor. - eoDetTournament(unsigned _Tsize = 2 ):eoSelectOne(), Tsize(_Tsize) { + eoDetTournament(unsigned _Tsize = 2 ):eoSelectOne(), Tsize(_Tsize) { // consistency check if (Tsize < 2) { cout << "Warning, Tournament size should be >= 2\nAdjusted\n"; diff --git a/eo/src/eoGOpSelector.h b/eo/src/eoGOpSelector.h index 75e5e0ca..2370d72f 100644 --- a/eo/src/eoGOpSelector.h +++ b/eo/src/eoGOpSelector.h @@ -45,6 +45,8 @@ class eoGOpSelector: public eoOpSelector, public vector*> { public: + typedef eoOpSelector::ID ID; + /// Dtor virtual ~eoGOpSelector() { for ( list< eoGeneralOp* >::iterator i= ownOpList.begin(); @@ -84,12 +86,12 @@ public: virtual string className() const { return "eoGOpSelector"; }; /// - void printOn(ostream& _os) const { - _os << className() << endl; - for ( unsigned i=0; i!= rates.size(); i++ ) { - _os << *(operator[](i)) << "\t" << rates[i] << endl; - } - } + void printOn(ostream& _os) const {} + // _os << className().c_str() << endl; + // for ( unsigned i=0; i!= rates.size(); i++ ) { + // _os << *(operator[](i)) << "\t" << rates[i] << endl; + // } + //} const vector& getRates(void) const { return rates; } diff --git a/eo/src/eoInserter.h b/eo/src/eoInserter.h index a799beb8..eb9eb3c6 100644 --- a/eo/src/eoInserter.h +++ b/eo/src/eoInserter.h @@ -30,8 +30,7 @@ #ifndef eoInserter_h #define eoInserter_h -#include "eoObject.h" - +#include "eoPop.h" /** * eoInserter: Interface class that enables an operator to insert new individuals into the (intermediate) population. diff --git a/eo/src/eoOp.h b/eo/src/eoOp.h index 178ae4e1..dc81878c 100644 --- a/eo/src/eoOp.h +++ b/eo/src/eoOp.h @@ -79,7 +79,7 @@ public: * @param _os A ostream. */ virtual void printOn(ostream& _os) const { - _os << className(); + _os << className().c_str(); // _os << arity; }; diff --git a/eo/src/eoPersistent.cpp b/eo/src/eoPersistent.cpp index 756353f5..2790534e 100644 --- a/eo/src/eoPersistent.cpp +++ b/eo/src/eoPersistent.cpp @@ -8,3 +8,7 @@ istream & operator >> ( istream& _is, eoPersistent& _o ) { _o.readFrom(_is); return _is; }; + +#include "eoRNG.h" + +eoRng rng; diff --git a/eo/src/eoPop.h b/eo/src/eoPop.h index e6cf0aef..ebd86490 100644 --- a/eo/src/eoPop.h +++ b/eo/src/eoPop.h @@ -1,144 +1,160 @@ -// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - -//----------------------------------------------------------------------------- -// eoPop.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 _EOPOP_H -#define _EOPOP_H - -#include -#include - -// EO includes -#include -#include - -/** Subpopulation: it is used to move parts of population - from one algorithm to another and one population to another. It is safer -to declare it as a separate object. I have no idea if a population can be -some other thing that a vector, but if somebody thinks of it, this concrete -implementation will be moved to "generic" and an abstract Population -interface will be provided. -It can be instantiated with anything, provided that it accepts a "size" and a -random generator in the ctor. This happens to all the eo1d chromosomes declared -so far. EOT must also have a copy ctor, since temporaries are created and copied -to the population. -@author Geneura Team -@version 0.0 -*/ - -template -class eoPop: public vector, public eoObject, public eoPersistent { - -/// Type is the type of each gene in the chromosome -#ifdef _MSC_VER - typedef EOT::Type Type; -#else - typedef typename EOT::Type Type; -#endif - - public: - /** Protected ctor. This is intended to avoid creation of void populations, except - from sibling classes - */ - eoPop() :vector() {}; - - - /** Ctor for fixed-size chromosomes, with variable content - @param _popSize total population size - @param _eoSize chromosome size. EOT should accept a fixed-size ctor - @param _geneRdn random number generator for each of the genes - */ - eoPop( unsigned _popSize, unsigned _eoSize, eoRnd & _geneRnd ) - :vector() { - for ( unsigned i = 0; i < _popSize; i ++ ){ - EOT tmpEOT( _eoSize, _geneRnd); - push_back( tmpEOT ); - } - }; - - /** Ctor for variable-size chromosomes, with variable content - @param _popSize total population size - @param _sizeRnd RNG for the chromosome size. This will be added 1, just in case. - @param _geneRdn random number generator for each of the genes - */ - eoPop( unsigned _popSize, eoRnd & _sizeRnd, eoRnd & _geneRnd ) - :vector() { - for ( unsigned i = 0; i < _popSize; i ++ ){ - unsigned size = 1 + _sizeRnd(); - EOT tmpEOT( size, _geneRnd); - push_back( tmpEOT ); - } - }; - - /** Ctor from an istream; reads the population from a stream, - each element should be in different lines - @param _is the stream - */ - eoPop( istream& _is ):vector() { - readFrom( _is ); - } - - /// - ~eoPop() {}; - - /** @name Methods from eoObject */ - //@{ - /** - * Read object. The EOT class must have a ctor from a stream; - in this case, a strstream is used. - * @param _is A istream. - - */ - virtual void readFrom(istream& _is) { - while( _is ) { // reads line by line, and creates an object per - // line - char line[MAXLINELENGTH]; - _is.getline( line, MAXLINELENGTH-1 ); - if (strlen( line ) ) { - istrstream s( line ); - EOT thisEOT( s ); - push_back( thisEOT ); - } - } - } - - /** - * Write object. It's called printOn since it prints the object _on_ a stream. - * @param _os A ostream. In this case, prints the population to - standard output. The EOT class must hav standard output with cout, - but since it should be an eoObject anyways, it's no big deal. - */ - virtual void printOn(ostream& _os) const { - copy( begin(), end(), ostream_iterator( _os, "\n") ); - }; - - /** Inherited from eoObject. Returns the class name. - @see eoObject - */ - virtual string className() const {return "eoPop";}; - //@} - - protected: - -}; -#endif +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoPop.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 _EOPOP_H +#define _EOPOP_H + +#include +#include + +// EO includes +#include +#include +#include + +/** Subpopulation: it is used to move parts of population + from one algorithm to another and one population to another. It is safer +to declare it as a separate object. I have no idea if a population can be +some other thing that a vector, but if somebody thinks of it, this concrete +implementation will be moved to "generic" and an abstract Population +interface will be provided. +It can be instantiated with anything, provided that it accepts a "size" and a +random generator in the ctor. This happens to all the eo1d chromosomes declared +so far. EOT must also have a copy ctor, since temporaries are created and copied +to the population. +@author Geneura Team +@version 0.0 +*/ + +template +class eoPop: public vector, public eoObject, public eoPersistent { + +/// Type is the type of each gene in the chromosome +#ifdef _MSC_VER + typedef EOT::Type Type; +#else + typedef typename EOT::Type Type; +#endif + + public: + /** Protected ctor. This is intended to avoid creation of void populations, except + from sibling classes + */ + eoPop() :vector() {}; + + + /** Ctor for fixed-size chromosomes, with variable content + @param _popSize total population size + @param _eoSize chromosome size. EOT should accept a fixed-size ctor + @param _geneRdn random number generator for each of the genes + */ + eoPop( unsigned _popSize, unsigned _eoSize, eoRnd & _geneRnd ) + :vector() { + for ( unsigned i = 0; i < _popSize; i ++ ){ + EOT tmpEOT( _eoSize, _geneRnd); + push_back( tmpEOT ); + } + }; + + /** Ctor for variable-size chromosomes, with variable content + @param _popSize total population size + @param _sizeRnd RNG for the chromosome size. This will be added 1, just in case. + @param _geneRdn random number generator for each of the genes + */ + eoPop( unsigned _popSize, eoRnd & _sizeRnd, eoRnd & _geneRnd ) + :vector() { + for ( unsigned i = 0; i < _popSize; i ++ ){ + unsigned size = 1 + _sizeRnd(); + EOT tmpEOT( size, _geneRnd); + push_back( tmpEOT ); + } + }; + + /** Ctor for fixed-size chromosomes, with variable content + @param _popSize total population size + @param _eoSize chromosome size. EOT should accept a fixed-size ctor + @param _geneRdn random number generator for each of the genes + */ + eoPop( unsigned _popSize, unsigned _eoSize, eoRnd & _geneRnd, eoEvalFunc& _eval) + :vector() { + for ( unsigned i = 0; i < _popSize; i ++ ){ + EOT tmpEOT( _eoSize, _geneRnd); + push_back( tmpEOT ); + _eval(back()); + } + }; + + + /** Ctor from an istream; reads the population from a stream, + each element should be in different lines + @param _is the stream + */ + eoPop( istream& _is ):vector() { + readFrom( _is ); + } + + /// + ~eoPop() {}; + + /** @name Methods from eoObject */ + //@{ + /** + * Read object. The EOT class must have a ctor from a stream; + in this case, a strstream is used. + * @param _is A istream. + + */ + virtual void readFrom(istream& _is) { + while( _is ) { // reads line by line, and creates an object per + // line + char line[MAXLINELENGTH]; + _is.getline( line, MAXLINELENGTH-1 ); + if (strlen( line ) ) { + istrstream s( line ); + EOT thisEOT( s ); + push_back( thisEOT ); + } + } + } + + /** + * Write object. It's called printOn since it prints the object _on_ a stream. + * @param _os A ostream. In this case, prints the population to + standard output. The EOT class must hav standard output with cout, + but since it should be an eoObject anyways, it's no big deal. + */ + virtual void printOn(ostream& _os) const { + copy( begin(), end(), ostream_iterator( _os, "\n") ); + }; + + /** Inherited from eoObject. Returns the class name. + @see eoObject + */ + virtual string className() const {return "eoPop";}; + //@} + + protected: + +}; +#endif diff --git a/eo/src/eoProportionalOpSel.h b/eo/src/eoProportionalOpSel.h index 3f8cbc99..1ea9fbc6 100644 --- a/eo/src/eoProportionalOpSel.h +++ b/eo/src/eoProportionalOpSel.h @@ -59,7 +59,8 @@ public: /** Gets a non-const reference to an operator, so that it can be changed, modified or whatever @param _id a previously assigned ID - @throw runtime_error if the ID does not exist*/ + @throw runtime_error if the ID does not exist*/ + virtual eoOp& getOp( ID _id ) { MMF::iterator i=begin(); ID j = 1; @@ -135,7 +136,7 @@ public: 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 << className() << endl; + _s << className().c_str() << endl; for ( MMF::const_iterator i=begin(); i!=end(); i++ ) { _s << i->first << "\t" << *(i->second )<< endl; } diff --git a/eo/src/eoRNG.h b/eo/src/eoRNG.h index d82fdfa1..b560e7c1 100644 --- a/eo/src/eoRNG.h +++ b/eo/src/eoRNG.h @@ -197,7 +197,7 @@ public : { if (total == 0) { // count - for (unsigned i = 0; i < vec.size(); ++i) + for (int i = 0; i < vec.size(); ++i) total += vec[i]; } @@ -270,7 +270,7 @@ private : /** The one and only global eoRng object */ -static eoRng rng; +extern eoRng rng; /** The class uniform_generator can be used in the STL generate function diff --git a/eo/src/eoSteadyStateEA.h b/eo/src/eoSteadyStateEA.h index fffe659c..6a0b5abf 100644 --- a/eo/src/eoSteadyStateEA.h +++ b/eo/src/eoSteadyStateEA.h @@ -44,7 +44,7 @@ template class eoSteadyStateEA: public eoAlgo eoGOpSelector& _opSelector, eoPopIndiSelector& _selector, eoSteadyStateInserter& _inserter, - eoTerm& _terminator, + eoTerm& _terminator, unsigned _steps = 0 ) : step(_opSelector, _selector, _inserter), terminator( _terminator) @@ -57,19 +57,20 @@ template class eoSteadyStateEA: public eoAlgo terminator( _terminator){}; /// Apply one generation of evolution to the population. - virtual void operator()(eoPop& pop) { + virtual void operator()(eoPop& pop) { do { try { step(pop); } - catch (exception& e) + catch (exception& e) { string s = e.what(); s.append( " in eoSteadyStateEA "); throw runtime_error( s ); } } while ( terminator( pop ) ); + } /// Class name. diff --git a/eo/src/eoUniformSelect.h b/eo/src/eoUniformSelect.h index 8e71e0da..74775aab 100644 --- a/eo/src/eoUniformSelect.h +++ b/eo/src/eoUniformSelect.h @@ -1,64 +1,64 @@ -// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - -//----------------------------------------------------------------------------- -// eoUniformSelect.h -// (c) GeNeura Team, 1998 - EEAAX 1999 -/* - 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 - Marc.Schoenauer@polytechnique.fr - */ -//----------------------------------------------------------------------------- - -#ifndef eoUniformSelect_h -#define eoUniformSelect_h -// WARNING: 2 classes in this one - eoUniformSelect and eoCopySelect - -//----------------------------------------------------------------------------- - -#include // -#include // accumulate -#include // eoPop eoSelect MINFLOAT -#include - -//----------------------------------------------------------------------------- -/** eoUniformSelect: a selection method that selects ONE individual randomly - -MS- 22/10/99 */ -//----------------------------------------------------------------------------- - -template class eoUniformSelect: public eoSelectOne -{ - public: - /// (Default) Constructor. - eoUniformSelect():eoSelectOne() {} - - /// not a big deal!!! - virtual const EOT& operator()(const eoPop& pop) { - return pop[rng.random(pop.size())] ; - } - - /// Methods inherited from eoObject - //@{ - - /** Return the class id. - * @return the class name as a string - */ - virtual string className() const { return "eoUniformSelect"; }; - - private: -}; - -#endif eoUniformSelect_h +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoUniformSelect.h +// (c) GeNeura Team, 1998 - EEAAX 1999 +/* + 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 + Marc.Schoenauer@polytechnique.fr + */ +//----------------------------------------------------------------------------- + +#ifndef eoUniformSelect_h +#define eoUniformSelect_h +// WARNING: 2 classes in this one - eoUniformSelect and eoCopySelect + +//----------------------------------------------------------------------------- + +#include // +#include // accumulate +#include // eoPop eoSelect MINFLOAT +#include + +//----------------------------------------------------------------------------- +/** eoUniformSelect: a selection method that selects ONE individual randomly + -MS- 22/10/99 */ +//----------------------------------------------------------------------------- + +template class eoUniformSelect: public eoSelectOne +{ + public: + /// (Default) Constructor. + eoUniformSelect():eoSelectOne() {} + + /// not a big deal!!! + virtual const EOT& operator()(const eoPop& pop) { + return pop[rng.random(pop.size())] ; + } + + /// Methods inherited from eoObject + //@{ + + /** Return the class id. + * @return the class name as a string + */ + virtual string className() const { return "eoUniformSelect"; }; + + private: +}; + +#endif eoUniformSelect_h diff --git a/eo/src/eoWrappedOps.h b/eo/src/eoWrappedOps.h index cc332a0b..2e226182 100644 --- a/eo/src/eoWrappedOps.h +++ b/eo/src/eoWrappedOps.h @@ -144,7 +144,6 @@ public : } /// Helper class to make sure that stuff that is inserted will be used again with the next operator - template class eoIndiSelectorInserter : public eoIndiSelector, public eoInserter { public : @@ -204,7 +203,7 @@ public : void operator()( eoIndiSelector& _in, eoInserter& _out ) const { - eoIndiSelectorInserter in_out(_in); + eoIndiSelectorInserter in_out(_in); for (size_t i = 0; i < ops.size(); ++i) {