Simplify configuration.

Remove support for (outdated) <strstream>, require <sstream>.
Require uint32_t for now, defined in stdint.h according to C99.
Some general cleanup and more documentation.
This commit is contained in:
kuepper 2005-09-28 21:49:26 +00:00
commit cf2a57dd88
46 changed files with 482 additions and 886 deletions

View file

@ -1,8 +1,9 @@
## Makefile.am for eo/src/utils
lib_LIBRARIES = libeoutils.a
lib_LIBRARIES = libeoutils.a
libeoutils_a_SOURCES = eoParser.cpp \
libeoutils_a_SOURCES = eoData.cpp \
eoParser.cpp \
eoRNG.cpp \
eoState.cpp \
eoUpdater.cpp \
@ -12,8 +13,6 @@ libeoutils_a_SOURCES = eoParser.cpp \
eoIntBounds.cpp \
make_help.cpp
AM_CXXFLAGS = -I$(top_srcdir)/src
utilsincludedir = $(pkgincludedir)/utils
utilsinclude_HEADERS = checkpointing \
@ -50,3 +49,6 @@ utilsinclude_HEADERS = checkpointing \
pipecom.h \
rnd_generators.h \
selectors.h
AM_CXXFLAGS = -I$(top_srcdir)/src

30
eo/src/utils/eoData.cpp Normal file
View file

@ -0,0 +1,30 @@
// Copyright (C) 2005 Jochen Küpper <jochen@fhi-berlin.mpg.de>
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; see the file License. if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
#include "eoData.h"
#ifdef HAVE_NUMERIC_LIMITS
int MAXINT = numeric_limits<int>::max();
#else
#include <limits.h>
int MAXINT = INT_MAX;
#endif
// Local Variables:
// c-file-style: "Stroustrup"
// fill-column: 80
// End:

View file

@ -25,61 +25,31 @@
#ifndef EODATA_H
#define EODATA_H
//-----------------------------------------------------------------------------
#include <vector> // std::vector
#include <set> // set
#include <string> // std::string
#ifdef _MSC_VER
#include <limits> // MAXDOUBLE
#define MAXFLOAT numeric_limits<float>::max()
#define MINFLOAT numeric_limits<float>::min()
#define MAXDOUBLE numeric_limits<double>::max()
#define MAXINT numeric_limits<int>::max()
#else
#include <float.h>
#include <limits.h>
#ifdef MAXINT
#undef MAXINT
#endif
#if !defined(_WIN32) && !defined(__CYGWIN__) && !(defined(__APPLE__) || defined(MACOSX)) && !defined(__FreeBSD__)
#include <values.h>
#endif
// for cygwin and windows (and possibly MacOsX)
#ifndef MINFLOAT
#define MINFLOAT ( (float)1e-127 )
#endif
#ifndef MAXFLOAT
#define MAXFLOAT ( (float)1e127 )
#endif
#ifndef MAXINT
#define MAXINT 2147483647
#endif
#ifndef MAXDOUBLE
#define MAXDOUBLE (double)1.79769313486231570e+308
#endif
extern int MAXINT;
#ifndef _MSC_VER
#include <math.h>
#define _isnan isnan
#endif
//-----------------------------------------------------------------------------
// some defines to make things easier to get at first sight
// tuning the amount of output using a boolean argument:
// tuning the amount of output using a boolean argument:
// true should always mean more output
#define eo_verbose true
#define eo_no_verbose false
// to be used in selection / replacement procedures to indicate whether
// to be used in selection / replacement procedures to indicate whether
// the argument (rate, a double) shoudl be treated as a rate (number=rate*popSize)
// or as an absolute integer (number=rate regardless of popsize).
// the default value shoudl ALWAYS be true (eo_as_a_rate).
//
// this construct is mandatory because in some cases you might not know the
// population size that will enter the replacement for instance - so you
// this construct is mandatory because in some cases you might not know the
// population size that will enter the replacement for instance - so you
// cannot simply have a pre-computed (double) rate of 1/popSize
#define eo_is_a_rate true
#define eo_is_an_integer false

View file

@ -3,7 +3,7 @@
//-----------------------------------------------------------------------------
// eoFileSnapshot.h
// (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2001
/*
/*
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
@ -27,10 +27,6 @@
#ifndef _eoFileSnapshot_h
#define _eoFileSnapshot_h
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string>
#include <fstream>
#include <utils/eoParam.h>
@ -39,15 +35,15 @@
/**
Prints snapshots of fitnesses to a (new) file every N generations
Prints snapshots of fitnesses to a (new) file every N generations
Assumes that the parameters that are passed to the monitor
Assumes that the parameters that are passed to the monitor
(method add in eoMonitor.h) are eoValueParam<std::vector<double> > of same size.
A dir is created and one file per snapshot is created there -
A dir is created and one file per snapshot is created there -
so you can later generate a movie!
TODO: The counter is handled internally, but this should be changed
TODO: The counter is handled internally, but this should be changed
so that you can pass e.g. an evalcounter (minor)
I failed to templatize everything so that it can handle eoParam<std::vector<T> >
@ -103,17 +99,9 @@ public :
*/
void setCurrentFileName()
{
#ifdef HAVE_SSTREAM
std::ostringstream oscount;
oscount << counter;
#else
char buff[255];
std::ostrstream oscount(buff, 254);
oscount << counter;
oscount << std::ends;
#endif
currentFileName = dirname + "/" + filename + oscount.str();
std::ostringstream oscount;
oscount << counter;
currentFileName = dirname + "/" + filename + oscount.str();
}
/** The operator(void): opens the std::ostream and calls the write method

View file

@ -3,7 +3,7 @@
//-----------------------------------------------------------------------------
// eoGnuplot1DMonitor.h
// (c) Marc Schoenauer, 2001
/*
/*
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
@ -26,10 +26,6 @@
#ifndef _eoGnuplot_H
#define _eoGnuplot_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string>
/**
@ -52,13 +48,13 @@ class eoGnuplot
{
public:
// Ctor
eoGnuplot(std::string _title, std::string _extra = std::string("")) :
eoGnuplot(std::string _title, std::string _extra = std::string("")) :
firstTime(true)
{
// opens pipe with Gnuplot
initGnuPlot(_title, _extra);
}
// Dtor
virtual ~eoGnuplot() {
// close - the gnuplot windows if pipe was correctly opened
@ -109,24 +105,13 @@ private:
inline void eoGnuplot::initGnuPlot(std::string _title, std::string _extra)
/////////////////////////////////////////////////////////
{
#ifdef HAVE_SSTREAM
std::ostringstream os;
os << "250x150-0+" << numWindow*170;
#else
char snum[255];
std::ostrstream os(snum, 254);
os << "250x150-0+" << numWindow*170 << std::ends;
#endif
numWindow++;
char *args[6];
args[0] = strdup( "gnuplot" );
args[1] = strdup( "-geometry" );
#ifdef HAVE_SSTREAM
args[2] = strdup( os.str().c_str());
#else
args[2] = strdup( os.str() );
#endif
args[3] = strdup( "-title" );
args[4] = strdup( _title.c_str() );
args[5] = 0;
@ -154,7 +139,7 @@ inline void eoGnuplot::initGnuPlot(std::string _title, std::string _extra)
* Created......: Mon Mar 13 13:50:11 1995
* Description..: Communication par pipe bidirectionnel avec un autre process
*
* Ident........: $Id: eoGnuplot.h,v 1.12 2005-09-07 17:09:19 maartenkeijzer Exp $
* Ident........: $Id: eoGnuplot.h,v 1.13 2005-09-28 21:49:26 kuepper Exp $
* ----------------------------------------------------------------------
*/

View file

@ -3,7 +3,7 @@
//-----------------------------------------------------------------------------
// eoGnuplot1DMonitor.h
// (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000
/*
/*
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
@ -28,10 +28,6 @@
#ifndef _eoGnuplot1DMonitor_H
#define _eoGnuplot1DMonitor_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string>
#include <utils/eoMonitor.h>
@ -93,10 +89,10 @@ inline eoMonitor& eoGnuplot1DMonitor::operator() (void)
FirstPlot();
firstTime = false;
}
else
else
{
if( gpCom ) {
PipeComSend( gpCom, "replot\n" );
PipeComSend( gpCom, "replot\n" );
}
}
return *this;
@ -110,13 +106,7 @@ inline void eoGnuplot1DMonitor::FirstPlot()
{
throw std::runtime_error("Must have some stats to plot!\n");
}
#ifdef HAVE_SSTREAM
std::ostringstream os;
#else
char buff[1024];
std::ostrstream os(buff, 1024);
#endif
os << "plot";
for (unsigned i=1; i<vec.size(); i++) {
os << " '" << getFileName().c_str() <<
@ -125,12 +115,7 @@ inline void eoGnuplot1DMonitor::FirstPlot()
os << ", ";
}
os << '\n';
#ifdef HAVE_SSTREAM
PipeComSend( gpCom, os.str().c_str());
#else
os << std::ends;
PipeComSend( gpCom, buff );
#endif
}
#endif

View file

@ -3,7 +3,7 @@
//-----------------------------------------------------------------------------
// eoGnuplot1DSnapshot.h
// (c) Marc Schoenauer, Maarten Keijzer and GeNeura Team, 2000
/*
/*
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
@ -28,14 +28,8 @@
#ifndef _eoGnuplot1DSnapshot_H
#define _eoGnuplot1DSnapshot_H
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string>
#ifdef HAVE_SSTREAM
#include <sstream>
#endif
#include <string>
#include <utils/eoFileSnapshot.h>
#include <utils/eoGnuplot.h>
@ -57,7 +51,7 @@ This class plots through gnuplot the eoStat given as argument
/** eoGnuplot1DSnapshot plots stats through gnuplot
* assumes that the same file is re-written every so and so,
* assumes that the same file is re-written every so and so,
* and plots it from scratch everytime it's called
*/
class eoGnuplot1DSnapshot: public eoFileSnapshot, public eoGnuplot
@ -107,14 +101,8 @@ class eoGnuplot1DSnapshot: public eoFileSnapshot, public eoGnuplot
virtual void handleBounds(eoRealVectorBounds & _bounds)
{
#ifdef HAVE_SSTREAM
std::ostringstream os;
#else
// use strstream and not std::stringstream until strstream is in all distributions
char buf[1024];
std::ostrstream os(buf, 1023);
#endif
// std::ostrstream os;
// std::ostrstream os;
os << "set autoscale\nset yrange [" ;
if (_bounds.isMinBounded(0))
os << _bounds.minimum(0);
@ -140,25 +128,13 @@ inline eoMonitor& eoGnuplot1DSnapshot::operator() (void)
eoFileSnapshot::operator()();
// sends plot order to gnuplot
#ifdef HAVE_SSTREAM
//std::string buff; // need local memory
std::ostringstream os;
#else
char buff[1024];
std::ostrstream os(buff, 1024);
#endif
os << "set title 'Gen. " << getCounter() << "'; plot '"
// mk: had to use getFilename().c_str(), because it seems the string(stream) lib is screwed in gcc3.2
<< getFileName().c_str() << "' notitle with points ps " << pointSize;
os << std::endl;
#ifdef HAVE_SSTREAM
PipeComSend( gpCom, os.str().c_str());
#else
os << std::ends;
PipeComSend( gpCom, buff );
#endif
return (*this);
}

View file

@ -4,7 +4,7 @@
// eoHowMany_h.h
// Base class for choosing a number of guys to apply something from a popsize
// (c) Marc Schoenauer, 2000
/*
/*
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
@ -30,11 +30,11 @@
* Typically, is used in selection / replacement procedures, e.g.
* the number of offspring from the number of parents, or
* the number of survivors for an eoReduce functor, ...
*
* Such construct is very useful because in some cases you might not know the
* population size that will enter the replacement. For instance, you
* cannot simply have a pre-computed (double) rate of 1/popSize
* if you want to select or kill just 1 guy. Using an eoHowMany
*
* Such construct is very useful because in some cases you might not know the
* population size that will enter the replacement. For instance, you
* cannot simply have a pre-computed (double) rate of 1/popSize
* if you want to select or kill just 1 guy. Using an eoHowMany
* allows one to modify the population size without touching anything else.
*
* There are 4 possible way to compute the return value from the argument:
@ -49,11 +49,11 @@
* It has 2 private members, a double and an integer to cover all cases
*
* Example use: in <a href="class_eogeneralbreeder.html">eoGeneralBreeder.h</a>
* Example reading from parser: in
* Example reading from parser: in
* <a href="make_algo_scalar_h-source.html">do/make_algo_scalar.h line 141</a>
* MS 10/04/2002:
* Added the possibility to have a negative number -
* MS 10/04/2002:
* Added the possibility to have a negative number -
* when treated as a number: returns then (size - combien)
* Should not modify anything when a positive number is passed in the ctor
*
@ -64,20 +64,12 @@
* It is an eoPersistent because we need to be able to use eoParamValue<eoHowMany>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_SSTREAM
#include <sstream>
#else
#include <strstream>
#endif
class eoHowMany : public eoPersistent
{
public:
/** Original Ctor from direct rate + bool
/** Original Ctor from direct rate + bool
@param rate the rate, OR the integer to store, depending on 2nd arg.
@param _interpret_as_rate to tell whether the rate actually is a rate
*/
@ -102,18 +94,18 @@ public:
}
}
/** Ctor from an int - both from int and unsigned int are needed
/** Ctor from an int - both from int and unsigned int are needed
* to avoid ambiguity with the Ctor from a double */
eoHowMany(int _combien) : rate(0.0), combien(_combien) {}
/** Ctor from an unsigned int - both from int and unsigned int are needed
/** Ctor from an unsigned int - both from int and unsigned int are needed
* to avoid ambiguity with the Ctor from a double */
eoHowMany(unsigned int _combien) : rate(0.0), combien(_combien) {}
/// Virtual dtor. They are needed in virtual class hierarchies.
virtual ~eoHowMany() {}
/** Does what it was designed for
/** Does what it was designed for
* - combien==0 : return rate*_size
* - else
* - combien>0 : return combien (regardless of _size)
@ -135,7 +127,7 @@ public:
return unsigned(combien);
}
virtual void printOn(std::ostream& _os) const
virtual void printOn(std::ostream& _os) const
{
if (combien == 0)
_os << 100*rate << "% ";
@ -145,7 +137,7 @@ public:
}
virtual void readFrom(std::istream& _is)
virtual void readFrom(std::istream& _is)
{
std::string value;
_is >> value;
@ -163,12 +155,8 @@ public:
interpret_as_rate = true;
_value.resize(pos); // get rid of %
}
#ifdef HAVE_SSTREAM
std::istringstream is(_value);
#else
std::istrstream is(_value.c_str());
#endif
is >> rate;
// now store
if (interpret_as_rate)
@ -193,7 +181,7 @@ public:
combien = -combien;
return (*this);
}
private :
double rate;
int combien;

View file

@ -8,12 +8,7 @@
#endif
#include <ctime>
#ifdef HAVE_SSTREAM
#include <sstream>
#else
#include <strstream>
#endif
#include "eoIntBounds.h"

View file

@ -27,24 +27,13 @@
#ifndef eoParam_h
#define eoParam_h
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
//-----------------------------------------------------------------------------
#include <math.h> // for floor
#include <string>
#ifdef HAVE_SSTREAM
#include <sstream>
#else
#include <strstream>
#endif
#include <vector>
#include <iterator> // for GCC 3.2
#include <cmath>
#include <iterator>
#include <stdexcept>
#include <eoScalarFitness.h> // for specializations
#include <sstream>
#include <string>
#include <vector>
#include <eoScalarFitness.h>
/**
@ -189,26 +178,15 @@ public :
std::string getValue(void) const
{
#ifdef HAVE_SSTREAM
std::ostringstream os;
os << repValue;
#else
char buf[1024];
std::ostrstream os(buf, 1023);
os << repValue;
os << std::ends;
#endif
return os.str();
}
void setValue(const std::string& _value)
{
#ifdef HAVE_SSTREAM
std::istringstream is(_value);
#else
std::istrstream is(_value.c_str());
#endif
is >> repValue;
}
@ -235,11 +213,7 @@ inline void eoValueParam<bool>::setValue(const std::string& _value)
repValue = true;
return;
}
#ifdef HAVE_SSTREAM
std::istringstream is(_value);
#else
std::istrstream is(_value.c_str());
#endif
is >> repValue;
}
@ -249,14 +223,8 @@ template <>
inline std::string eoValueParam<std::pair<double, double> >::getValue(void) const
{
// use own buffer as MSVC's buffer leaks!
#ifdef HAVE_SSTREAM
std::ostringstream os;
os << repValue.first << ' ' << repValue.second;
#else
char buff[1024];
std::ostrstream os(buff, 1024);
os << repValue.first << ' ' << repValue.second << std::ends;
#endif
return os.str();
}
@ -264,11 +232,7 @@ inline std::string eoValueParam<std::pair<double, double> >::getValue(void) cons
template <>
inline void eoValueParam<std::pair<double, double> >::setValue(const std::string& _value)
{
#ifdef HAVE_SSTREAM
std::istringstream is(_value);
#else
std::istrstream is(_value.c_str());
#endif
is >> repValue.first;
is >> repValue.second;
}
@ -279,21 +243,13 @@ inline void eoValueParam<std::pair<double, double> >::setValue(const std::string
template <>
inline std::string eoValueParam<std::vector<std::vector<double> > >::getValue(void) const
{
#ifdef HAVE_SSTREAM
std::ostringstream os;
#else
std::ostrstream os;
#endif
os << repValue.size() << ' ';
for (unsigned i = 0; i < repValue.size(); ++i)
{
os << repValue[i].size() << ' ';
std::copy(repValue[i].begin(), repValue[i].end(), std::ostream_iterator<double>(os, " "));
}
#ifndef HAVE_SSTREAM
os << std::ends;
#endif
return os.str();
}
@ -301,11 +257,7 @@ inline std::string eoValueParam<std::vector<std::vector<double> > >::getValue(vo
template <>
inline void eoValueParam<std::vector<std::vector<double> > >::setValue(const std::string& _value)
{
#ifdef HAVE_SSTREAM
std::istringstream is(_value);
#else
std::istrstream is(_value.c_str());
#endif
unsigned i,j,sz;
is >> sz;
repValue.resize(sz);
@ -328,16 +280,9 @@ inline void eoValueParam<std::vector<std::vector<double> > >::setValue(const std
template <>
inline std::string eoValueParam<std::vector<double> >::getValue(void) const
{
#ifdef HAVE_SSTREAM
std::ostringstream os;
#else
std::ostrstream os;
#endif
os << repValue.size() << ' ';
std::copy(repValue.begin(), repValue.end(), std::ostream_iterator<double>(os, " "));
#ifndef HAVE_SSTREAM
os << std::ends;
#endif
return os.str();
}
@ -345,11 +290,7 @@ inline std::string eoValueParam<std::vector<double> >::getValue(void) const
template <>
inline void eoValueParam<std::vector<double> >::setValue(const std::string& _value)
{
#ifdef HAVE_SSTREAM
std::istringstream is(_value);
#else
std::istrstream is(_value.c_str());
#endif
unsigned sz;
is >> sz;
repValue.resize(sz);
@ -362,16 +303,9 @@ inline void eoValueParam<std::vector<double> >::setValue(const std::string& _val
template <>
inline std::string eoValueParam<std::vector<eoMinimizingFitness> >::getValue(void) const
{
#ifdef HAVE_SSTREAM
std::ostringstream os;
#else
std::ostrstream os;
#endif
os << repValue.size() << ' ';
std::copy(repValue.begin(), repValue.end(), std::ostream_iterator<eoMinimizingFitness>(os, " "));
#ifndef HAVE_SSTREAM
os<< std::ends;
#endif
return os.str();
}
@ -380,11 +314,7 @@ inline std::string eoValueParam<std::vector<eoMinimizingFitness> >::getValue(voi
template <>
inline void eoValueParam<std::vector<eoMinimizingFitness> >::setValue(const std::string& _value)
{
#ifdef HAVE_SSTREAM
std::istringstream is(_value);
#else
std::istrstream is(_value.c_str());
#endif
unsigned sz;
is >> sz;
repValue.resize(sz);

View file

@ -90,11 +90,7 @@ eoParser::eoParser ( unsigned _argc, char **_argv , string _programDescription,
}
}
// now read arguments on command-line
#ifdef HAVE_SSTREAM
stringstream stream;
#else
strstream stream;
#endif
for (i = 1; i < _argc; ++i)
{
stream << _argv[i] << '\n';

View file

@ -24,7 +24,7 @@
*/
//-----------------------------------------------------------------------------
/**
CVS Info: $Date: 2005-09-13 10:24:50 $ $Version$ $Author: kuepper $
CVS Info: $Date: 2005-09-28 21:49:26 $ $Version$ $Author: kuepper $
*/
#ifndef eoParser_h
#define eoParser_h
@ -222,11 +222,7 @@ public:
{
eoValueParam<ValueType>& param = createParam(_defaultValue, _longName, _description,
_shortHand, _section, _required);
#ifdef HAVE_SSTREAM
std::ostringstream os;
#else
std::ostrstream os;
#endif
os << _defaultValue;
if(isItThere(param)) {
param.setValue(os.str());

View file

@ -35,10 +35,6 @@ that can be used to dump to the screen
#ifndef _eoPopStat_h
#define _eoPopStat_h
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <utils/eoStat.h>
@ -69,7 +65,6 @@ public:
Adds a \n before so it does not get mixed up with the rest of the stats
that are written by the monitor it is probably used from.
*/
#ifdef HAVE_SSTREAM
void operator()(const eoPop<EOT>& _pop)
{
value() = "\n# ====== pop dump =====\n";
@ -83,24 +78,6 @@ void operator()(const eoPop<EOT>& _pop)
value() += os.str();
}
}
#else
void operator()(const eoPop<EOT>& _pop)
{
char buffer[1023]; // about one k of space per member
value() = "\n# ====== pop dump =====\n";
unsigned howmany=combien?combien:_pop.size();
value() += "\n"; // otherwise, possible mix-up with other stats
for (unsigned i = 0; i < howmany; ++i)
{
std::ostrstream os(buffer, 1022); // leave space for emergency terminate
os << _pop[i] << std::endl << std::ends;
// paranoid:
buffer[1022] = '\0';
value() += buffer;
}
}
#endif
private:
unsigned combien;
@ -136,7 +113,6 @@ public:
the rest of the stats that are written by the monitor it is
probably used from.
*/
#ifdef HAVE_SSTREAM
void operator()(const std::vector<const EOT*>& _pop)
{
value() = ""; // empty
@ -150,23 +126,6 @@ public:
value() += os.str();
}
}
#else
void operator()(const std::vector<const EOT*>& _pop)
{
char buffer[1023]; // about one K of space per member
value() = ""; // empty
unsigned howMany=combien?combien:_pop.size();
for (unsigned i = 0; i < howMany; ++i)
{
std::ostrstream os(buffer, 1022); // leave space for emergency terminate
os << *_pop[i] << std::endl << std::ends;
// paranoid:
buffer[1022] = '\0';
value() += buffer;
}
}
#endif
private:
unsigned combien;
};

View file

@ -1,10 +1,9 @@
/*
* Random number generator adapted from (see comments below)
*
* The random number generator is modified into a class
* by Maarten Keijzer (mak@dhi.dk). Also added the Box-Muller
* transformation to generate normal deviates.
*
/** Random number generator adapted from (see comments below)
The random number generator is modified into a class
by Maarten Keijzer (mak@dhi.dk). Also added the Box-Muller
transformation to generate normal deviates.
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
@ -22,142 +21,118 @@
Contact: todos@geneura.ugr.es, http://geneura.ugr.es
*/
/* ************ DOCUMENTATION IN ORIGINAL FILE *********************/
// This is the ``Mersenne Twister'' random number generator MT19937, which
// generates pseudorandom integers uniformly distributed in 0..(2^32 - 1)
// starting from any odd seed in 0..(2^32 - 1). This version is a recode
// by Shawn Cokus (Cokus@math.washington.edu) on March 8, 1998 of a version by
// Takuji Nishimura (who had suggestions from Topher Cooper and Marc Rieffel in
// July-August 1997).
//
// Effectiveness of the recoding (on Goedel2.math.washington.edu, a DEC Alpha
// running OSF/1) using GCC -O3 as a compiler: before recoding: 51.6 sec. to
// generate 300 million random numbers; after recoding: 24.0 sec. for the same
// (i.e., 46.5% of original time), so speed is now about 12.5 million random
// number generations per second on this machine.
//
// According to the URL <http://www.math.keio.ac.jp/~matumoto/emt.html>
// (and paraphrasing a bit in places), the Mersenne Twister is ``designed
// with consideration of the flaws of various existing generators,'' has
// a period of 2^19937 - 1, gives a sequence that is 623-dimensionally
// equidistributed, and ``has passed many std::stringent tests, including the
// die-hard test of G. Marsaglia and the load test of P. Hellekalek and
// S. Wegenkittl.'' It is efficient in memory usage (typically using 2506
// to 5012 bytes of static data, depending on data type sizes, and the code
// is quite short as well). It generates random numbers in batches of 624
// at a time, so the caching and pipelining of modern systems is exploited.
// It is also divide- and mod-free.
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Library 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 Library General Public License for more details. You should have
// received a copy of the GNU Library 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.
//
// The code as Shawn received it included the following notice:
//
// Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. When
// you use this, send an e-mail to <matumoto@math.keio.ac.jp> with
// an appropriate reference to your work.
//
// It would be nice to CC: <Cokus@math.washington.edu> when you write.
//
//
// uint32_t must be an unsigned integer type capable of holding at least 32
// bits; exactly 32 should be fastest, but 64 is better on an Alpha with
// GCC at -O3 optimization so try your options and see what's best for you
//
/* ************ END DOCUMENTATION IN ORIGINAL FILE *********************/
#ifndef EO_RANDOM_NUMBER_GENERATOR
#define EO_RANDOM_NUMBER_GENERATOR
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif
#include <stdint.h>
#include <vector>
#include "eoPersistent.h"
#include "eoObject.h"
#include "../eoPersistent.h"
#include "../eoObject.h"
/** Random Number Generator
// TODO: check for various compilers if this is exactly 32 bits
// Unfortunately MSVC's preprocessor does not comprehend sizeof()
// so neat preprocessing tricks will not work
@class eoRng eoRNG.h utils/eoRNG.h
#if(! (defined HAVE_UINT32_T))
#if(SIZEOF_UNSIGNED_LONG == 4)
typedef unsigned long uint32_t;
#else
#error Need to provide a type for uint32_t in eoRNG.h.
#endif
#endif
eoRng is a persistent class that uses the ``Mersenne Twister'' random
number generator MT19937 for generating random numbers. The various
member functions implement useful functions for evolutionary
algorithms. Included are: rand(), random(), flip() and normal().
<h1>DOCUMENTATION IN ORIGINAL FILE</h1>
This is the ``Mersenne Twister'' random number generator MT19937, which
generates pseudorandom integers uniformly distributed in 0..(2^32 - 1) starting
from any odd seed in 0..(2^32 - 1). This version is a recode by Shawn Cokus
(Cokus@math.washington.edu) on March 8, 1998 of a version by Takuji Nishimura
(who had suggestions from Topher Cooper and Marc Rieffel in July-August 1997).
Effectiveness of the recoding (on Goedel2.math.washington.edu, a DEC Alpha
running OSF/1) using GCC -O3 as a compiler: before recoding: 51.6 sec. to
generate 300 million random numbers; after recoding: 24.0 sec. for the same
(i.e., 46.5% of original time), so speed is now about 12.5 million random number
generations per second on this machine.
According to the URL <http://www.math.keio.ac.jp/~matumoto/emt.html> (and
paraphrasing a bit in places), the Mersenne Twister is ``designed with
consideration of the flaws of various existing generators,'' has a period of
2^19937 - 1, gives a sequence that is 623-dimensionally equidistributed, and
``has passed many std::stringent tests, including the die-hard test of G.
Marsaglia and the load test of P. Hellekalek and S. Wegenkittl.'' It is
efficient in memory usage (typically using 2506 to 5012 bytes of static data,
depending on data type sizes, and the code is quite short as well). It generates
random numbers in batches of 624 at a time, so the caching and pipelining of
modern systems is exploited. It is also divide- and mod-free.
The code as Shawn received it included the following notice:
- Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura. When you use this,
send an e-mail to <matumoto@math.keio.ac.jp> with an appropriate reference to
your work.
- It would be nice to CC: <Cokus@math.washington.edu> when you write.
//-----------------------------------------------------------------------------
// eoRng
//-----------------------------------------------------------------------------
/**
\class eoRng eoRNG.h utils/eoRNG.h
eoRng is a persistent class that uses the ``Mersenne Twister'' random number generator MT19937
for generating random numbers. The various member functions implement useful functions
for evolutionary algorithms. Included are: rand(), random(), flip() and normal().
<h1>Portability</h1>
Note for people porting EO to other platforms: please make sure that the typedef
Note for people porting EO to other platforms: please make sure that the type
uint32_t in the file eoRng.h is exactly 32 bits long. It may be longer, but not
shorter. If it is longer, file compatibility between EO on different platforms
may be broken.
*/
class eoRng : public eoObject, public eoPersistent
class eoRng : public eoObject, public eoPersistent
{
public :
/**
ctor takes a random seed; if you want another seed, use reseed
@see reseed to see why the parameter to initialize is doubled
*/
eoRng(uint32_t s) : state(0), next(0), left(-1), cached(false), N(624), M(397), K(0x9908B0DFU) {
state = new uint32_t[N+1];
initialize(2*s);
}
/** Constructor
~eoRng(void)
{
delete [] state;
}
@param s Random seed; if you want another seed, use reseed.
/**
* Re-initializes the Random Number Generator.
* WARNING: after Jeroen Eggermont <jeggermo@liacs.nl> noticed that
* initialize does not differentiate between odd and even numbers,
* the argument to reseed is now doubled before being passed on.
*
* Manually divide the seed by 2 if you want to re-run old runs
*
* MS. 5 Oct. 2001
*/
void reseed(uint32_t s)
{
initialize(2*s);
}
@see reseed for details on usage of the seeding value.
*/
eoRng(uint32_t s)
: state(0), next(0), left(-1), cached(false), N(624), M(397), K(0x9908B0DFU)
{
state = new uint32_t[N+1];
initialize(2*s);
}
/**
Re-initializes the Random Number Generator - old version
*/
void oldReseed(uint32_t s)
{
initialize(s);
}
~eoRng(void)
{
delete [] state;
}
/** Re-initializes the Random Number Generator.
WARNING: Jeroen Eggermont <jeggermo@liacs.nl> noticed that initialize does
not differentiate between odd and even numbers, therefore the argument to
reseed is now doubled before being passed on.
Manually divide the seed by 2 if you want to re-run old runs
@version MS. 5 Oct. 2001
*/
void reseed(uint32_t s)
{
initialize(2*s);
}
/** Re-initializes the Random Number Generator
This is the traditional seeding procedure.
@see reseed for details on usage of the seeding value.
@version old version
*/
void oldReseed(uint32_t s)
{
initialize(s);
}
/**
uniform(m = 1.0) returns a random double in the range [0, m)
@ -218,44 +193,52 @@ public :
*/
uint32_t rand();
/**
rand_max() the maximum returned by rand()
*/
uint32_t rand_max(void) const { return (uint32_t) 0xffffffff; }
/**
rand_max() the maximum returned by rand()
*/
uint32_t rand_max(void) const { return uint32_t(0xffffffff); }
/**
roulette_wheel(vec, total = 0) does a roulette wheel selection
on the input std::vector vec. If the total is not supplied, it is
calculated. It returns an integer denoting the selected argument.
*/
template <class T>
int roulette_wheel(const std::vector<T>& vec, T total = 0)
{
if (total == 0)
{ // count
for (unsigned i = 0; i < vec.size(); ++i)
total += vec[i];
}
/**
roulette_wheel(vec, total = 0) does a roulette wheel selection
on the input std::vector vec. If the total is not supplied, it is
calculated. It returns an integer denoting the selected argument.
*/
template <typename TYPE>
int roulette_wheel(const std::vector<TYPE>& vec, TYPE total = 0)
{
if (total == 0)
{ // count
for (unsigned i = 0; i < vec.size(); ++i)
total += vec[i];
}
double fortune = uniform() * total;
int i = 0;
while (fortune > 0)
{
fortune -= vec[i++];
}
return --i;
};
double fortune = uniform() * total;
int i = 0;
while (fortune > 0)
{
fortune -= vec[i++];
}
/** Randomly select element from vector.
return --i;
}
@return Uniformly chosen element from the vector.
*/
template <typename TYPE>
const TYPE& choice(const std::vector<TYPE>& vec) const
{ return vec[random(vec.size())]; }
/**
* choice(vec), returns a uniformly chosen element from the vector
*/
template <class T>
const T& choice(const std::vector<T>& vec) { return vec[random(vec.size())]; }
template <class T>
T& choice(std::vector<T>& vec) { return vec[random(vec.size())]; }
/** Randomly select element from vector.
@overload
@return Uniformly chosen element from the vector.
*/
template <typename TYPE>
TYPE& choice(std::vector<TYPE>& vec)
{ return vec[random(vec.size())]; }
///
void printOn(std::ostream& _os) const
@ -474,3 +457,10 @@ namespace eo {
#endif
// Local Variables:
// coding: iso-8859-1
// mode: C++
// c-file-style: "Stroustrup"
// fill-column: 80
// End:

View file

@ -8,11 +8,7 @@
#endif
#include <ctime>
#ifdef HAVE_SSTREAM
#include <sstream>
#else
#include <strstream>
#endif
#include "eoRealBounds.h"
#include "eoRealVectorBounds.h"
@ -37,11 +33,7 @@ bool remove_leading(std::string & _s, const std::string _delim)
double read_double(std::string _s)
{
#ifdef HAVE_SSTREAM
std::istringstream is(_s);
#else
std::istrstream is(_s.c_str());
#endif
double r;
is >> r;
return r;
@ -49,11 +41,7 @@ double read_double(std::string _s)
long int read_int(std::string _s)
{
#ifdef HAVE_SSTREAM
std::istringstream is(_s);
#else
std::istrstream is(_s.c_str());
#endif
long int i;
is >> i;
return i;

View file

@ -8,11 +8,7 @@
#include <algorithm>
#include <fstream>
#ifdef HAVE_SSTREAM
#include <sstream>
#else
#include <strstream>
#endif
#include "eoState.h"
#include "eoObject.h"
@ -32,7 +28,7 @@ void removeComment(string& str, string comment)
}
}
bool is_section(const string& str, string& name)
bool is_section(const string& str, string& name)
{
string::size_type pos = str.find("\\section{");
@ -58,12 +54,12 @@ eoState::~eoState(void)
delete ownedObjects[i];
}
}
void eoState::registerObject(eoPersistent& registrant)
{
string name = createObjectName(dynamic_cast<eoObject*>(&registrant));
pair<ObjectMap::iterator,bool> res = objectMap.insert(make_pair(name, &registrant));
pair<ObjectMap::iterator,bool> res = objectMap.insert(make_pair(name, &registrant));
if (res.second == true)
{
@ -84,7 +80,7 @@ void eoState::load(const string& _filename)
string str = "Could not open file " + _filename;
throw runtime_error(str);
}
load(is);
}
@ -94,7 +90,7 @@ void eoState::load(std::istream& is)
string name;
getline(is, str);
if (is.fail())
{
string str = "Error while reading stream";
@ -118,7 +114,7 @@ void eoState::load(std::istream& is)
}
else
{
eoPersistent* object = it->second;
// now we have the object, get lines, remove comments etc.
@ -135,11 +131,7 @@ void eoState::load(std::istream& is)
removeComment(str, getCommentString());
fullstring += str + "\n";
}
#ifdef HAVE_SSTREAM
istringstream the_stream(fullstring);
#else
istrstream the_stream(fullstring.c_str(), fullstring.size());
#endif
object->readFrom(the_stream);
}
}
@ -180,32 +172,21 @@ string eoState::createObjectName(eoObject* obj)
{
if (obj == 0)
{
#ifdef HAVE_SSTREAM
ostringstream os;
#else
ostrstream os;
#endif
os << objectMap.size();
return os.str();
}
// else
string name = obj->className();
ObjectMap::const_iterator it = objectMap.find(name);
unsigned count = 1;
while (it != objectMap.end())
{
#ifdef HAVE_SSTREAM
ostringstream os;
os << obj->className().c_str() << count++;
#else
ostrstream os;
os << obj->className().c_str() << count++ << ends;
#endif
name = os.str();
it = objectMap.find(name);
}

View file

@ -6,11 +6,7 @@
#include <config.h>
#endif
#ifdef HAVE_SSTREAM
#include <sstream>
#else
#include <strstream>
#endif
#include <utils/eoState.h>
#include <utils/eoUpdater.h>
@ -24,28 +20,17 @@ void eoTimedStateSaver::operator()(void)
if (now >= last_time + interval)
{
last_time = now;
#ifdef HAVE_SSTREAM
ostringstream os;
os << prefix << (now - first_time) << '.' << extension;
#else
ostrstream os;
os << prefix << (now - first_time) << '.' << extension << ends;
#endif
state.save(os.str());
}
}
void eoCountedStateSaver::doItNow(void)
{
#ifdef HAVE_SSTREAM
ostringstream os;
os << prefix << counter << '.' << extension;
#else
ostrstream os;
os << prefix << counter << '.' << extension << ends;
#endif
state.save(os.str());
os << prefix << counter << '.' << extension;
state.save(os.str());
}
void eoCountedStateSaver::operator()(void)