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
#define COMPAT_H
#include <string>
#include <iostream>
#ifdef _1__GNUC__
// Specifics for GNUC
#define NO_GOOD_ISTREAM_ITERATORS
#endif
#ifdef _MSC_VER
/*
Maarten: added this code here because Mirkosoft has the

View file

@ -43,7 +43,7 @@ template <class EOT> class eoDetTournament: public eoSelectOne<EOT>
{
public:
/// (Default) Constructor.
eoDetTournament(unsigned _Tsize = 2 ):eoSelectOne(), Tsize(_Tsize) {
eoDetTournament(unsigned _Tsize = 2 ):eoSelectOne<EOT>(), Tsize(_Tsize) {
// consistency check
if (Tsize < 2) {
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:
typedef eoOpSelector<EOT>::ID ID;
/// Dtor
virtual ~eoGOpSelector() {
for ( list< eoGeneralOp<EOT>* >::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<float>& getRates(void) const { return rates; }

View file

@ -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.

View file

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

View file

@ -8,3 +8,7 @@ istream & operator >> ( istream& _is, eoPersistent& _o ) {
_o.readFrom(_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; -*-
//-----------------------------------------------------------------------------
// 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 <vector>
#include <strstream>
// EO includes
#include <eoRnd.h>
#include <eoPersistent.h>
/** 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 EOT>
class eoPop: public vector<EOT>, 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<EOT>() {};
/** 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<Type> & _geneRnd )
:vector<EOT>() {
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<unsigned> & _sizeRnd, eoRnd<Type> & _geneRnd )
:vector<EOT>() {
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<EOT>() {
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<EOT>( _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 <vector>
#include <strstream>
// EO includes
#include <eoRnd.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
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 EOT>
class eoPop: public vector<EOT>, 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<EOT>() {};
/** 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<Type> & _geneRnd )
:vector<EOT>() {
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<unsigned> & _sizeRnd, eoRnd<Type> & _geneRnd )
:vector<EOT>() {
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<Type> & _geneRnd, eoEvalFunc<EOT>& _eval)
:vector<EOT>() {
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<EOT>() {
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<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,
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<EOT>& 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;
}

View file

@ -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

View file

@ -44,7 +44,7 @@ template<class EOT> class eoSteadyStateEA: public eoAlgo<EOT>
eoGOpSelector<EOT>& _opSelector,
eoPopIndiSelector<EOT>& _selector,
eoSteadyStateInserter<EOT>& _inserter,
eoTerm<Chrom>& _terminator,
eoTerm<EOT>& _terminator,
unsigned _steps = 0 )
: step(_opSelector, _selector, _inserter),
terminator( _terminator)
@ -57,19 +57,20 @@ template<class EOT> class eoSteadyStateEA: public eoAlgo<EOT>
terminator( _terminator){};
/// Apply one generation of evolution to the population.
virtual void operator()(eoPop<Chrom>& pop) {
virtual void operator()(eoPop<EOT>& 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.

View file

@ -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 <functional> //
#include <numeric> // accumulate
#include <eoPopOps.h> // eoPop eoSelect MINFLOAT
#include <eoRNG.h>
//-----------------------------------------------------------------------------
/** eoUniformSelect: a selection method that selects ONE individual randomly
-MS- 22/10/99 */
//-----------------------------------------------------------------------------
template <class EOT> class eoUniformSelect: public eoSelectOne<EOT>
{
public:
/// (Default) Constructor.
eoUniformSelect():eoSelectOne() {}
/// not a big deal!!!
virtual const EOT& operator()(const eoPop<EOT>& 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 <functional> //
#include <numeric> // accumulate
#include <eoPopOps.h> // eoPop eoSelect MINFLOAT
#include <eoRNG.h>
//-----------------------------------------------------------------------------
/** eoUniformSelect: a selection method that selects ONE individual randomly
-MS- 22/10/99 */
//-----------------------------------------------------------------------------
template <class EOT> class eoUniformSelect: public eoSelectOne<EOT>
{
public:
/// (Default) Constructor.
eoUniformSelect():eoSelectOne<EOT>() {}
/// not a big deal!!!
virtual const EOT& operator()(const eoPop<EOT>& 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

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
template <class EOT>
class eoIndiSelectorInserter : public eoIndiSelector<EOT>, public eoInserter<EOT>
{
public :
@ -204,7 +203,7 @@ public :
void operator()( eoIndiSelector<EOT>& _in,
eoInserter<EOT>& _out ) const {
eoIndiSelectorInserter<EOT> in_out(_in);
eoIndiSelectorInserter in_out(_in);
for (size_t i = 0; i < ops.size(); ++i)
{