Moved the static eoRNG rng to an extern eoRNG

This external object is now defined in eoPersistent.cpp

This should change...
This commit is contained in:
mac 2000-02-19 16:30:42 +00:00
commit 2443677f13
12 changed files with 257 additions and 227 deletions

View file

@ -28,6 +28,14 @@
#ifndef COMPAT_H #ifndef COMPAT_H
#define COMPAT_H #define COMPAT_H
#include <string>
#include <iostream>
#ifdef _1__GNUC__
// Specifics for GNUC
#define NO_GOOD_ISTREAM_ITERATORS
#endif
#ifdef _MSC_VER #ifdef _MSC_VER
/* /*
Maarten: added this code here because Mirkosoft has the Maarten: added this code here because Mirkosoft has the

View file

@ -43,7 +43,7 @@ template <class EOT> class eoDetTournament: public eoSelectOne<EOT>
{ {
public: public:
/// (Default) Constructor. /// (Default) Constructor.
eoDetTournament(unsigned _Tsize = 2 ):eoSelectOne(), Tsize(_Tsize) { eoDetTournament(unsigned _Tsize = 2 ):eoSelectOne<EOT>(), Tsize(_Tsize) {
// consistency check // consistency check
if (Tsize < 2) { if (Tsize < 2) {
cout << "Warning, Tournament size should be >= 2\nAdjusted\n"; cout << "Warning, Tournament size should be >= 2\nAdjusted\n";

View file

@ -45,6 +45,8 @@ class eoGOpSelector: public eoOpSelector<EOT>, public vector<eoGeneralOp<EOT>*>
{ {
public: public:
typedef eoOpSelector<EOT>::ID ID;
/// Dtor /// Dtor
virtual ~eoGOpSelector() { virtual ~eoGOpSelector() {
for ( list< eoGeneralOp<EOT>* >::iterator i= ownOpList.begin(); for ( list< eoGeneralOp<EOT>* >::iterator i= ownOpList.begin();
@ -84,12 +86,12 @@ public:
virtual string className() const { return "eoGOpSelector"; }; virtual string className() const { return "eoGOpSelector"; };
/// ///
void printOn(ostream& _os) const { void printOn(ostream& _os) const {}
_os << className() << endl; // _os << className().c_str() << endl;
for ( unsigned i=0; i!= rates.size(); i++ ) { // for ( unsigned i=0; i!= rates.size(); i++ ) {
_os << *(operator[](i)) << "\t" << rates[i] << endl; // _os << *(operator[](i)) << "\t" << rates[i] << endl;
} // }
} //}
const vector<float>& getRates(void) const { return rates; } const vector<float>& getRates(void) const { return rates; }

View file

@ -30,8 +30,7 @@
#ifndef eoInserter_h #ifndef eoInserter_h
#define eoInserter_h #define eoInserter_h
#include "eoObject.h" #include "eoPop.h"
/** /**
* eoInserter: Interface class that enables an operator to insert * eoInserter: Interface class that enables an operator to insert
new individuals into the (intermediate) population. new individuals into the (intermediate) population.

View file

@ -79,7 +79,7 @@ public:
* @param _os A ostream. * @param _os A ostream.
*/ */
virtual void printOn(ostream& _os) const { virtual void printOn(ostream& _os) const {
_os << className(); _os << className().c_str();
// _os << arity; // _os << arity;
}; };

View file

@ -8,3 +8,7 @@ istream & operator >> ( istream& _is, eoPersistent& _o ) {
_o.readFrom(_is); _o.readFrom(_is);
return _is; return _is;
}; };
#include "eoRNG.h"
eoRng rng;

View file

@ -1,144 +1,160 @@
// -*- 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; -*-
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// eoPop.h // eoPop.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
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version. version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es Contact: todos@geneura.ugr.es, http://geneura.ugr.es
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef _EOPOP_H #ifndef _EOPOP_H
#define _EOPOP_H #define _EOPOP_H
#include <vector> #include <vector>
#include <strstream> #include <strstream>
// EO includes // EO includes
#include <eoRnd.h> #include <eoRnd.h>
#include <eoPersistent.h> #include <eoPersistent.h>
#include <eoEvalFunc.h>
/** Subpopulation: it is used to move parts of population
from one algorithm to another and one population to another. It is safer /** Subpopulation: it is used to move parts of population
to declare it as a separate object. I have no idea if a population can be from one algorithm to another and one population to another. It is safer
some other thing that a vector, but if somebody thinks of it, this concrete to declare it as a separate object. I have no idea if a population can be
implementation will be moved to "generic" and an abstract Population some other thing that a vector, but if somebody thinks of it, this concrete
interface will be provided. implementation will be moved to "generic" and an abstract Population
It can be instantiated with anything, provided that it accepts a "size" and a interface will be provided.
random generator in the ctor. This happens to all the eo1d chromosomes declared It can be instantiated with anything, provided that it accepts a "size" and a
so far. EOT must also have a copy ctor, since temporaries are created and copied random generator in the ctor. This happens to all the eo1d chromosomes declared
to the population. so far. EOT must also have a copy ctor, since temporaries are created and copied
@author Geneura Team to the population.
@version 0.0 @author Geneura Team
*/ @version 0.0
*/
template<class EOT>
class eoPop: public vector<EOT>, public eoObject, public eoPersistent { template<class EOT>
class eoPop: public vector<EOT>, public eoObject, public eoPersistent {
/// Type is the type of each gene in the chromosome
#ifdef _MSC_VER /// Type is the type of each gene in the chromosome
typedef EOT::Type Type; #ifdef _MSC_VER
#else typedef EOT::Type Type;
typedef typename EOT::Type Type; #else
#endif typedef typename EOT::Type Type;
#endif
public:
/** Protected ctor. This is intended to avoid creation of void populations, except public:
from sibling classes /** Protected ctor. This is intended to avoid creation of void populations, except
*/ from sibling classes
eoPop() :vector<EOT>() {}; */
eoPop() :vector<EOT>() {};
/** Ctor for fixed-size chromosomes, with variable content
@param _popSize total population size /** Ctor for fixed-size chromosomes, with variable content
@param _eoSize chromosome size. EOT should accept a fixed-size ctor @param _popSize total population size
@param _geneRdn random number generator for each of the genes @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<Type> & _geneRnd ) */
:vector<EOT>() { eoPop( unsigned _popSize, unsigned _eoSize, eoRnd<Type> & _geneRnd )
for ( unsigned i = 0; i < _popSize; i ++ ){ :vector<EOT>() {
EOT tmpEOT( _eoSize, _geneRnd); for ( unsigned i = 0; i < _popSize; i ++ ){
push_back( tmpEOT ); EOT tmpEOT( _eoSize, _geneRnd);
} push_back( tmpEOT );
}; }
};
/** Ctor for variable-size chromosomes, with variable content
@param _popSize total population size /** Ctor for variable-size chromosomes, with variable content
@param _sizeRnd RNG for the chromosome size. This will be added 1, just in case. @param _popSize total population size
@param _geneRdn random number generator for each of the genes @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<unsigned> & _sizeRnd, eoRnd<Type> & _geneRnd ) */
:vector<EOT>() { eoPop( unsigned _popSize, eoRnd<unsigned> & _sizeRnd, eoRnd<Type> & _geneRnd )
for ( unsigned i = 0; i < _popSize; i ++ ){ :vector<EOT>() {
unsigned size = 1 + _sizeRnd(); for ( unsigned i = 0; i < _popSize; i ++ ){
EOT tmpEOT( size, _geneRnd); unsigned size = 1 + _sizeRnd();
push_back( tmpEOT ); EOT tmpEOT( size, _geneRnd);
} push_back( tmpEOT );
}; }
};
/** Ctor from an istream; reads the population from a stream,
each element should be in different lines /** Ctor for fixed-size chromosomes, with variable content
@param _is the stream @param _popSize total population size
*/ @param _eoSize chromosome size. EOT should accept a fixed-size ctor
eoPop( istream& _is ):vector<EOT>() { @param _geneRdn random number generator for each of the genes
readFrom( _is ); */
} eoPop( unsigned _popSize, unsigned _eoSize, eoRnd<Type> & _geneRnd, eoEvalFunc<EOT>& _eval)
:vector<EOT>() {
/// for ( unsigned i = 0; i < _popSize; i ++ ){
~eoPop() {}; EOT tmpEOT( _eoSize, _geneRnd);
push_back( tmpEOT );
/** @name Methods from eoObject */ _eval(back());
//@{ }
/** };
* Read object. The EOT class must have a ctor from a stream;
in this case, a strstream is used.
* @param _is A istream. /** Ctor from an istream; reads the population from a stream,
each element should be in different lines
*/ @param _is the stream
virtual void readFrom(istream& _is) { */
while( _is ) { // reads line by line, and creates an object per eoPop( istream& _is ):vector<EOT>() {
// line readFrom( _is );
char line[MAXLINELENGTH]; }
_is.getline( line, MAXLINELENGTH-1 );
if (strlen( line ) ) { ///
istrstream s( line ); ~eoPop() {};
EOT thisEOT( s );
push_back( thisEOT ); /** @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.
* 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, virtual void readFrom(istream& _is) {
but since it should be an eoObject anyways, it's no big deal. while( _is ) { // reads line by line, and creates an object per
*/ // line
virtual void printOn(ostream& _os) const { char line[MAXLINELENGTH];
copy( begin(), end(), ostream_iterator<EOT>( _os, "\n") ); _is.getline( line, MAXLINELENGTH-1 );
}; if (strlen( line ) ) {
istrstream s( line );
/** Inherited from eoObject. Returns the class name. EOT thisEOT( s );
@see eoObject push_back( thisEOT );
*/ }
virtual string className() const {return "eoPop";}; }
//@} }
protected: /**
* 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
#endif 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<EOT>( _os, "\n") );
};
/** Inherited from eoObject. Returns the class name.
@see eoObject
*/
virtual string className() const {return "eoPop";};
//@}
protected:
};
#endif

View file

@ -59,7 +59,8 @@ public:
/** Gets a non-const reference to an operator, so that it can be changed, /** Gets a non-const reference to an operator, so that it can be changed,
modified or whatever modified or whatever
@param _id a previously assigned ID @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<EOT>& getOp( ID _id ) { virtual eoOp<EOT>& getOp( ID _id ) {
MMF::iterator i=begin(); MMF::iterator i=begin();
ID j = 1; ID j = 1;
@ -135,7 +136,7 @@ public:
base classes, so you don´t have to worry about, for instance, fitness. base classes, so you don´t have to worry about, for instance, fitness.
@param _s the ostream in which things are written*/ @param _s the ostream in which things are written*/
virtual void printOn( ostream& _s ) const{ virtual void printOn( ostream& _s ) const{
_s << className() << endl; _s << className().c_str() << endl;
for ( MMF::const_iterator i=begin(); i!=end(); i++ ) { for ( MMF::const_iterator i=begin(); i!=end(); i++ ) {
_s << i->first << "\t" << *(i->second )<< endl; _s << i->first << "\t" << *(i->second )<< endl;
} }

View file

@ -197,7 +197,7 @@ public :
{ {
if (total == 0) if (total == 0)
{ // count { // count
for (unsigned i = 0; i < vec.size(); ++i) for (int i = 0; i < vec.size(); ++i)
total += vec[i]; total += vec[i];
} }
@ -270,7 +270,7 @@ private :
/** /**
The one and only global eoRng object 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 The class uniform_generator can be used in the STL generate function

View file

@ -44,7 +44,7 @@ template<class EOT> class eoSteadyStateEA: public eoAlgo<EOT>
eoGOpSelector<EOT>& _opSelector, eoGOpSelector<EOT>& _opSelector,
eoPopIndiSelector<EOT>& _selector, eoPopIndiSelector<EOT>& _selector,
eoSteadyStateInserter<EOT>& _inserter, eoSteadyStateInserter<EOT>& _inserter,
eoTerm<Chrom>& _terminator, eoTerm<EOT>& _terminator,
unsigned _steps = 0 ) unsigned _steps = 0 )
: step(_opSelector, _selector, _inserter), : step(_opSelector, _selector, _inserter),
terminator( _terminator) terminator( _terminator)
@ -57,19 +57,20 @@ template<class EOT> class eoSteadyStateEA: public eoAlgo<EOT>
terminator( _terminator){}; terminator( _terminator){};
/// Apply one generation of evolution to the population. /// Apply one generation of evolution to the population.
virtual void operator()(eoPop<Chrom>& pop) { virtual void operator()(eoPop<EOT>& pop) {
do { do {
try try
{ {
step(pop); step(pop);
} }
catch (exception& e) catch (exception& e)
{ {
string s = e.what(); string s = e.what();
s.append( " in eoSteadyStateEA "); s.append( " in eoSteadyStateEA ");
throw runtime_error( s ); throw runtime_error( s );
} }
} while ( terminator( pop ) ); } while ( terminator( pop ) );
} }
/// Class name. /// Class name.

View file

@ -1,64 +1,64 @@
// -*- 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; -*-
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// eoUniformSelect.h // eoUniformSelect.h
// (c) GeNeura Team, 1998 - EEAAX 1999 // (c) GeNeura Team, 1998 - EEAAX 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
License as published by the Free Software Foundation; either License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version. version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful, This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details. Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Contact: todos@geneura.ugr.es, http://geneura.ugr.es Contact: todos@geneura.ugr.es, http://geneura.ugr.es
Marc.Schoenauer@polytechnique.fr Marc.Schoenauer@polytechnique.fr
*/ */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef eoUniformSelect_h #ifndef eoUniformSelect_h
#define eoUniformSelect_h #define eoUniformSelect_h
// WARNING: 2 classes in this one - eoUniformSelect and eoCopySelect // WARNING: 2 classes in this one - eoUniformSelect and eoCopySelect
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#include <functional> // #include <functional> //
#include <numeric> // accumulate #include <numeric> // accumulate
#include <eoPopOps.h> // eoPop eoSelect MINFLOAT #include <eoPopOps.h> // eoPop eoSelect MINFLOAT
#include <eoRNG.h> #include <eoRNG.h>
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** eoUniformSelect: a selection method that selects ONE individual randomly /** eoUniformSelect: a selection method that selects ONE individual randomly
-MS- 22/10/99 */ -MS- 22/10/99 */
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <class EOT> class eoUniformSelect: public eoSelectOne<EOT> template <class EOT> class eoUniformSelect: public eoSelectOne<EOT>
{ {
public: public:
/// (Default) Constructor. /// (Default) Constructor.
eoUniformSelect():eoSelectOne() {} eoUniformSelect():eoSelectOne<EOT>() {}
/// not a big deal!!! /// not a big deal!!!
virtual const EOT& operator()(const eoPop<EOT>& pop) { virtual const EOT& operator()(const eoPop<EOT>& pop) {
return pop[rng.random(pop.size())] ; return pop[rng.random(pop.size())] ;
} }
/// Methods inherited from eoObject /// Methods inherited from eoObject
//@{ //@{
/** Return the class id. /** Return the class id.
* @return the class name as a string * @return the class name as a string
*/ */
virtual string className() const { return "eoUniformSelect"; }; virtual string className() const { return "eoUniformSelect"; };
private: private:
}; };
#endif eoUniformSelect_h #endif eoUniformSelect_h

View file

@ -144,7 +144,6 @@ public :
} }
/// Helper class to make sure that stuff that is inserted will be used again with the next operator /// Helper class to make sure that stuff that is inserted will be used again with the next operator
template <class EOT>
class eoIndiSelectorInserter : public eoIndiSelector<EOT>, public eoInserter<EOT> class eoIndiSelectorInserter : public eoIndiSelector<EOT>, public eoInserter<EOT>
{ {
public : public :
@ -204,7 +203,7 @@ public :
void operator()( eoIndiSelector<EOT>& _in, void operator()( eoIndiSelector<EOT>& _in,
eoInserter<EOT>& _out ) const { eoInserter<EOT>& _out ) const {
eoIndiSelectorInserter<EOT> in_out(_in); eoIndiSelectorInserter in_out(_in);
for (size_t i = 0; i < ops.size(); ++i) for (size_t i = 0; i < ops.size(); ++i)
{ {