From 025eb736ee523bb51c27fb2c923c2aa24f7b0cf9 Mon Sep 17 00:00:00 2001 From: maartenkeijzer Date: Sat, 17 Feb 2001 10:51:31 +0000 Subject: [PATCH] eoVector is back (eoFixedLength and eoVariableLength are gone) Also: introducing eoRndGenerators.h for eoRndGenerator derived classes: eoNormalGenerator eoUniformGenerator eoBooleanGenerator eoNegExpGenerator Note the suffix that are added to aid in determining what these classes do --- eo/app/mastermind/mastermind.h | 4 +- eo/src/Makefile.am | 2 +- eo/src/eo | 3 +- eo/src/eoPerf2Worth.h | 4 +- eo/src/eoScalarFitness.h | 21 ++-- eo/src/eoVariableLength.h | 104 ----------------- eo/src/eoVariableLengthCrossover.h | 1 - eo/src/{eoFixedLength.h => eoVector.h} | 20 ++-- eo/src/es/eoEsFull.h | 18 +-- eo/src/es/eoEsSimple.h | 12 +- eo/src/es/eoEsStdev.h | 20 ++-- eo/src/es/eoReal.h | 20 ++-- eo/src/ga/eoBit.h | 12 +- eo/src/utils/Makefile.am | 2 +- eo/src/utils/eoRNG.cpp | 3 +- eo/src/utils/eoRnd.h | 63 ----------- eo/src/utils/eoRndGenerators.h | 149 +++++++++++++++++++++++++ eo/src/utils/eoUniform.h | 104 ----------------- eo/test/t-eoRandom.cpp | 10 +- 19 files changed, 222 insertions(+), 350 deletions(-) delete mode 100644 eo/src/eoVariableLength.h rename eo/src/{eoFixedLength.h => eoVector.h} (85%) delete mode 100644 eo/src/utils/eoRnd.h create mode 100644 eo/src/utils/eoRndGenerators.h delete mode 100644 eo/src/utils/eoUniform.h diff --git a/eo/app/mastermind/mastermind.h b/eo/app/mastermind/mastermind.h index 44b64b85a..82495a3d4 100644 --- a/eo/app/mastermind/mastermind.h +++ b/eo/app/mastermind/mastermind.h @@ -8,7 +8,7 @@ //----------------------------------------------------------------------------- #include // exit EXIT_FAILURE -#include // eoFixedLength +#include // eoVectorLength #include // eoMonOp eoQuadraticOp #include // eoInit #include // uniform_generator @@ -29,7 +29,7 @@ typedef vector genotype; // Chrom //----------------------------------------------------------------------------- -typedef eoFixedLength Chrom; +typedef eoVector Chrom; //----------------------------------------------------------------------------- // eoChromEvaluator diff --git a/eo/src/Makefile.am b/eo/src/Makefile.am index 5aad893d8..3137bf258 100644 --- a/eo/src/Makefile.am +++ b/eo/src/Makefile.am @@ -11,5 +11,5 @@ lib_LIBRARIES = libeo.a libeo_a_SOURCES = eoPrintable.cpp eoPersistent.cpp eoFunctorStore.cpp libeoincdir = $(includedir)/eo -libeoinc_HEADERS = eo EO.h apply.h eoAlgo.h eoBreed.h eoCombinedContinue.h eoContinue.h eoCounter.h eoDetSelect.h eoDetTournamentSelect.h eoEasyEA.h eoEvalFunc.h eoEvalFuncPtr.h eoFactory.h eoFitContinue.h eoFitnessScalingSelect.h eoFixedLength.h eoFunctor.h eoFunctorStore.h eoGenContinue.h eoInvalidateOps.h eoInit.h eoMerge.h eoMergeReduce.h eoObject.h eoOp.h eoOpSelMason.h eoPersistent.h eoPop.h eoPrintable.h eoProportionalCombinedOp.h eoProportionalSelect.h eoRandomSelect.h eoRankingSelect.h eoReduce.h eoReduceMerge.h eoReplacement.h eoSGA.h eoSGATransform.h eoScalarFitness.h eoSelect.h eoSelectFactory.h eoSelectMany.h eoSelectNumber.h eoSelectOne.h eoSelectPerc.h eoSteadyFitContinue.h eoStochTournamentSelect.h eoSurviveAndDie.h eoTransform.h eoVariableLength.h eoVariableLengthCrossover.h eoVariableLengthMutation.h eoPerf2Worth.h eoFitnessScalingSelect.h eoLinearFitScaling.h eoRanking.h es.h ga.h +libeoinc_HEADERS = eo EO.h apply.h eoAlgo.h eoBreed.h eoCombinedContinue.h eoContinue.h eoCounter.h eoDetSelect.h eoDetTournamentSelect.h eoEasyEA.h eoEvalFunc.h eoEvalFuncPtr.h eoFactory.h eoFitContinue.h eoFitnessScalingSelect.h eoVector.h eoFunctor.h eoFunctorStore.h eoGenContinue.h eoInvalidateOps.h eoInit.h eoMerge.h eoMergeReduce.h eoObject.h eoOp.h eoOpSelMason.h eoPersistent.h eoPop.h eoPrintable.h eoProportionalCombinedOp.h eoProportionalSelect.h eoRandomSelect.h eoRankingSelect.h eoReduce.h eoReduceMerge.h eoReplacement.h eoSGA.h eoSGATransform.h eoScalarFitness.h eoSelect.h eoSelectFactory.h eoSelectMany.h eoSelectNumber.h eoSelectOne.h eoSelectPerc.h eoSteadyFitContinue.h eoStochTournamentSelect.h eoSurviveAndDie.h eoTransform.h eoVariableLengthCrossover.h eoVariableLengthMutation.h eoPerf2Worth.h eoFitnessScalingSelect.h eoLinearFitScaling.h eoRanking.h es.h ga.h diff --git a/eo/src/eo b/eo/src/eo index 4d5fe9b98..a80cf4ce6 100644 --- a/eo/src/eo +++ b/eo/src/eo @@ -58,8 +58,7 @@ #include // eo's -#include -#include +#include #include diff --git a/eo/src/eoPerf2Worth.h b/eo/src/eoPerf2Worth.h index ea9d8d179..d27bcd1c9 100644 --- a/eo/src/eoPerf2Worth.h +++ b/eo/src/eoPerf2Worth.h @@ -37,12 +37,12 @@ It is an eoStat so @see eoSelectFromWorth */ -template +template class eoPerf2Worth : public eoStat > { public: eoPerf2Worth():eoStat >(vector(0), - "Worthes") {} + "Worths") {} }; #endif diff --git a/eo/src/eoScalarFitness.h b/eo/src/eoScalarFitness.h index ef35722bb..8402e1e99 100644 --- a/eo/src/eoScalarFitness.h +++ b/eo/src/eoScalarFitness.h @@ -4,7 +4,7 @@ eoScalarFitness.h (c) Maarten Keijzer (mkeijzer@mad.scientist.com) and GeNeura Team, 1999, 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 @@ -31,19 +31,16 @@ /** * eoScalarFitness >: - * Wraps a scalar fitness values such as a double or int, with the option of + * Wraps a scalar fitness values such as a double or int, with the option of * maximizing (using less) or minimizing (using greater) - * - * - * - * + * It overrides operator<() to use the Compare template argument * - * Suitable constructors and assignments and casts are defined to work + * Suitable constructors and assignments and casts are defined to work * with this quantity as if it were a ScalarType. */ -template -class eoScalarFitness +template +class eoScalarFitness { public : @@ -59,7 +56,7 @@ class eoScalarFitness operator ScalarType(void) const { return value; } /// Comparison, using less by default - bool operator<(const eoScalarFitness& other) const + bool operator<(const eoScalarFitness& other) const { return Compare()(value, other.value); } // implementation of the other operators @@ -77,9 +74,9 @@ class eoScalarFitness /** Typedefs for fitness comparison, Maximizing Fitness compares with less, and minimizing fitness compares with greater. This because we want ordinary -fitness values (doubles) to be equivalent with Maximizing Fitness, and +fitness values (doubles) to be equivalent with Maximizing Fitness, and comparing with less is the default behaviour. -*/ +*/ typedef eoScalarFitness > eoMaximizingFitness; typedef eoScalarFitness > eoMinimizingFitness; diff --git a/eo/src/eoVariableLength.h b/eo/src/eoVariableLength.h deleted file mode 100644 index 838892088..000000000 --- a/eo/src/eoVariableLength.h +++ /dev/null @@ -1,104 +0,0 @@ -// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - -//----------------------------------------------------------------------------- -// eoVariableLength.h -// (c) GeNeura Team, 2000 - EEAAX 1999 - Maarten Keijzer 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 - 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 - mak@dhi.dk - */ -//----------------------------------------------------------------------------- - -#ifndef _eoVariableLength_h -#define _eoVariableLength_h - -#include - -/** - Base class for variable length chromosomes. Derives from EO and vector, - redirects the smaller than operator to EO (fitness based comparison), - and implements the virtual functions printOn() and readFrom() -*/ - -template -class eoVariableLength : public EO, public std::vector -{ - public : - - typedef GeneType AtomType; - typedef std::vector ContainerType; - - // default ctor - eoVariableLength(unsigned size = 0, GeneType value = GeneType()) : EO(), std::vector(size, value) - {} - - // we can't have a Ctor from a vector, it would create ambiguity - // with the copy Ctor - void value(std::vector _v) - { - resize(_v.size()); - copy(_v.begin(), _v.end(), begin()); - invalidate(); - } - - /// printing... - void printOn(ostream& os) const - { - EO::printOn(os); - os << ' '; - - os << size() << ' '; - - std::copy(begin(), end(), ostream_iterator(os, " ")); - } - - /// reading... - void readFrom(istream& is) - { - EO::readFrom(is); - - unsigned sz; - is >> sz; - - resize(sz); - unsigned i; - - for (i = 0; i < sz; ++i) - { - AtomType atom; - is >> atom; - operator[](i) = atom; - } - } - - /// to avoid conflicts between EO::operator< and vector::operator< - bool operator<(const eoVariableLength& _eo) const - { - return EO::operator<(_eo); - } - -}; - -/// to avoid conflicts between EO::operator< and vector::operator< -template -bool operator<(const eoVariableLength& _eo1, const eoVariableLength& _eo2) -{ - return _eo1.operator<(_eo2); -} - -#endif diff --git a/eo/src/eoVariableLengthCrossover.h b/eo/src/eoVariableLengthCrossover.h index 72f7cba5a..7d8da085f 100644 --- a/eo/src/eoVariableLengthCrossover.h +++ b/eo/src/eoVariableLengthCrossover.h @@ -28,7 +28,6 @@ #define _eoVariableLengthCrossover_h #include -#include #include /** diff --git a/eo/src/eoFixedLength.h b/eo/src/eoVector.h similarity index 85% rename from eo/src/eoFixedLength.h rename to eo/src/eoVector.h index 9b65dc6e0..aacf8618d 100644 --- a/eo/src/eoFixedLength.h +++ b/eo/src/eoVector.h @@ -1,9 +1,9 @@ // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- //----------------------------------------------------------------------------- -// eoFixedLength.h +// eoVector.h // (c) GeNeura Team, 2000 - EEAAX 1999 - Maarten Keijzer 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 @@ -24,8 +24,8 @@ */ //----------------------------------------------------------------------------- -#ifndef _eoFixedLength_h -#define _eoFixedLength_h +#ifndef _eoVector_h +#define _eoVector_h #include #include @@ -36,14 +36,14 @@ */ template -class eoFixedLength : public EO, public std::vector +class eoVector : public EO, public std::vector { public : - + typedef GeneType AtomType; typedef std::vector ContainerType; - eoFixedLength(unsigned size = 0, GeneType value = GeneType()) : EO(), std::vector(size, value) + eoVector(unsigned size = 0, GeneType value = GeneType()) : EO(), std::vector(size, value) {} // we can't have a Ctor from a vector, it would create ambiguity @@ -51,13 +51,13 @@ class eoFixedLength : public EO, public std::vector void value(std::vector _v) { if (_v.size() != size()) - throw runtime_error("Wrong size in vector assignation in eoFixedLength"); + throw runtime_error("Wrong size in vector assignation in eoVector"); copy(_v.begin(), _v.end(), begin()); invalidate(); } /// to avoid conflicts between EO::operator< and vector::operator< - bool operator<(const eoFixedLength& _eo) const + bool operator<(const eoVector& _eo) const { return EO::operator<(_eo); } @@ -95,7 +95,7 @@ class eoFixedLength : public EO, public std::vector /// to avoid conflicts between EO::operator< and vector::operator< template -bool operator<(const eoFixedLength& _eo1, const eoFixedLength& _eo2) +bool operator<(const eoVector& _eo1, const eoVector& _eo2) { return _eo1.operator<(_eo2); } diff --git a/eo/src/es/eoEsFull.h b/eo/src/es/eoEsFull.h index 3036cc8b7..f4f2710af 100644 --- a/eo/src/es/eoEsFull.h +++ b/eo/src/es/eoEsFull.h @@ -27,7 +27,7 @@ #ifndef _eoEsFull_h #define _eoEsFull_h -#include +#include /** \ingroup EvolutionStrategies @@ -37,32 +37,32 @@ rates and correlated mutations. */ template -class eoEsFull : public eoFixedLength +class eoEsFull : public eoVector { public : typedef double Type; - - eoEsFull(void) : eoFixedLength() {} - + + eoEsFull(void) : eoVector() {} + std::string className(void) const { return "eoEsFull"; } void printOn(std::ostream& os) const { - eoFixedLength::printOn(os); + eoVector::printOn(os); os << ' '; std::copy(stdevs.begin(), stdevs.end(), std::ostream_iterator(os, " ")); - + os << ' '; std::copy(correlations.begin(), correlations.end(), std::ostream_iterator(os, " ")); - + os << ' '; } void readFrom(istream& is) { - eoFixedLength::readFrom(is); + eoVector::readFrom(is); stdevs.resize(size()); diff --git a/eo/src/es/eoEsSimple.h b/eo/src/es/eoEsSimple.h index 2f337fb00..c7ba1e6cd 100644 --- a/eo/src/es/eoEsSimple.h +++ b/eo/src/es/eoEsSimple.h @@ -30,13 +30,13 @@ #include #include -#include +#include /** \ingroup EvolutionStrategies One of the more simple evolution strategies, sporting just a single -stdeviation for the entire chromosome. For more advanced versions +stdeviation for the entire chromosome. For more advanced versions see also eoEsStdev eoEsFull @see eoEsStdev eoEsFull @@ -44,26 +44,26 @@ see also eoEsStdev eoEsFull */ template -class eoEsSimple : public eoFixedLength +class eoEsSimple : public eoVector { public : typedef double Type; - eoEsSimple(void) : eoFixedLength() {} + eoEsSimple(void) : eoVector() {} std::string className(void) const { return "eoEsSimple"; } void printOn(std::ostream& os) const { - eoFixedLength::printOn(os); + eoVector::printOn(os); os << ' ' << stdev << ' '; } void readFrom(istream& is) { - eoFixedLength::readFrom(is); + eoVector::readFrom(is); is >> stdev; } diff --git a/eo/src/es/eoEsStdev.h b/eo/src/es/eoEsStdev.h index f46b939e3..9c7b38315 100644 --- a/eo/src/es/eoEsStdev.h +++ b/eo/src/es/eoEsStdev.h @@ -27,39 +27,39 @@ #ifndef _eoEsStdev_h #define _eoEsStdev_h -#include +#include /** \ingroup EvolutionStrategies Evolutionary strategie style representation, supporting co-evolving standard -deviations. +deviations. */ template -class eoEsStdev : public eoFixedLength +class eoEsStdev : public eoVector { public : - + typedef double Type; - eoEsStdev(void) : eoFixedLength() {} + eoEsStdev(void) : eoVector() {} std::string className(void) const { return "eoEsStdev"; } - + void printOn(std::ostream& os) const { - eoFixedLength::printOn(os); - + eoVector::printOn(os); + os << ' '; std::copy(stdevs.begin(), stdevs.end(), std::ostream_iterator(os, " ")); - + os << ' '; } void readFrom(istream& is) { - eoFixedLength::readFrom(is); + eoVector::readFrom(is); stdevs.resize(size()); unsigned i; diff --git a/eo/src/es/eoReal.h b/eo/src/es/eoReal.h index abf85fccf..9143f2784 100644 --- a/eo/src/es/eoReal.h +++ b/eo/src/es/eoReal.h @@ -29,12 +29,12 @@ #include // ostream, istream #include // string -#include +#include /** eoReal: implementation of simple real-valued chromosome. - * based on eoFixedLength class + * based on eoVector class */ -template class eoReal: public eoFixedLength +template class eoReal: public eoVector { public: @@ -42,15 +42,15 @@ template class eoReal: public eoFixedLength * (Default) Constructor. * @param size Size of the vector */ - eoReal(unsigned size = 0, double value = 0.0): - eoFixedLength(size, value) {} - + eoReal(unsigned size = 0, double value = 0.0): + eoVector(size, value) {} + /// My class name. - string className() const - { - return "eoReal"; + string className() const + { + return "eoReal"; } - + }; //----------------------------------------------------------------------------- diff --git a/eo/src/ga/eoBit.h b/eo/src/ga/eoBit.h index 61be6e2f1..ed411b7da 100644 --- a/eo/src/ga/eoBit.h +++ b/eo/src/ga/eoBit.h @@ -39,7 +39,7 @@ #include // bind2nd #include // string -#include +#include /** \defgroup bitstring @@ -47,12 +47,12 @@ Various functions for a bitstring representation */ -/** eoBit: implementation of bitstring chromosome. +/** eoBit: implementation of bitstring chromosome. \class eoBit eoBit.h ga/eoBit.h \ingroup bitstring - * based on STL's vector specialization. + * based on STL's vector specialization. */ -template class eoBit: public eoFixedLength +template class eoBit: public eoVector { public: @@ -60,8 +60,8 @@ template class eoBit: public eoFixedLength * (Default) Constructor. * @param size Size of the binary string. */ - eoBit(unsigned size = 0, bool value = false): - eoFixedLength(size, value) {} + eoBit(unsigned size = 0, bool value = false): + eoVector(size, value) {} /// My class name. string className() const diff --git a/eo/src/utils/Makefile.am b/eo/src/utils/Makefile.am index 109533aff..a75438f53 100644 --- a/eo/src/utils/Makefile.am +++ b/eo/src/utils/Makefile.am @@ -10,7 +10,7 @@ lib_LIBRARIES = libeoutils.a libeoutils_a_SOURCES = eoParser.cpp eoRNG.cpp eoState.cpp eoUpdater.cpp eoFileMonitor.cpp eoStdoutMonitor.cpp libeoincdir = $(includedir)/eo/utils -libeoinc_HEADERS = compatibility.h eoCheckPoint.h eoData.h eoDistance.h eoFDCStat.h eoFileMonitor.h eoGnuplot1DMonitor.h eoGnuplot1DSnapshot.h eoHowMany.h eoMonitor.h eoParam.h eoParser.h eoRNG.h eoStat.h eoScalarFitnessStat.h eoState.h eoStdoutMonitor.h eoUpdatable.h eoUpdater.h rnd_generators.h selectors.h +libeoinc_HEADERS = compatibility.h eoCheckPoint.h eoData.h eoDistance.h eoFDCStat.h eoFileMonitor.h eoGnuplot1DMonitor.h eoGnuplot1DSnapshot.h eoHowMany.h eoMonitor.h eoParam.h eoParser.h eoRNG.h eoStat.h eoScalarFitnessStat.h eoState.h eoStdoutMonitor.h eoUpdatable.h eoUpdater.h rnd_generators.h eoRndGenerators.h selectors.h diff --git a/eo/src/utils/eoRNG.cpp b/eo/src/utils/eoRNG.cpp index 36f6a4893..b1e592baf 100644 --- a/eo/src/utils/eoRNG.cpp +++ b/eo/src/utils/eoRNG.cpp @@ -1,10 +1,9 @@ #include #include "eoRNG.h" -/// The global object, should probably be initialized with an xor -/// between time and process_id. namespace eo { +/// The Global random number generator. eoRng rng((uint32) time(0)); } diff --git a/eo/src/utils/eoRnd.h b/eo/src/utils/eoRnd.h deleted file mode 100644 index eca67ec7a..000000000 --- a/eo/src/utils/eoRnd.h +++ /dev/null @@ -1,63 +0,0 @@ -/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - ------------------------------------------------------------------------------ - eoRnd.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 - */ -//----------------------------------------------------------------------------- -/** -CVS Info: $Date: 2001-02-13 22:35:07 $ $Header: /home/nojhan/dev/eodev/eodev_cvs/eo/src/utils/Attic/eoRnd.h,v 1.1 2001-02-13 22:35:07 jmerelo Exp $ $Author: jmerelo $ $Log$ -*/ -#ifndef _EORND_H -#define _EORND_H - -//----------------------------------------------------------------------------- - -#include - -//----------------------------------------------------------------------------- -// Class eoRnd -//----------------------------------------------------------------------------- - -#include // srand -#include // time -#include // runtime_error - -//----------------------------------------------------------------------------- - -#include - -//----------------------------------------------------------------------------- -/** - * Base class for a family of random 'number' generators. These 'numbers' - * can be anything, including full-fledged chromosomes. - */ -template -class eoRnd -{ -public: - - /** Main function: random generators act as functors, that return random numbers. - @return return a random number - */ - virtual T operator()() = 0; - -}; - -#endif diff --git a/eo/src/utils/eoRndGenerators.h b/eo/src/utils/eoRndGenerators.h new file mode 100644 index 000000000..26d22111f --- /dev/null +++ b/eo/src/utils/eoRndGenerators.h @@ -0,0 +1,149 @@ +/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + + ----------------------------------------------------------------------------- + eoRndGenerators.h + + mimics the rnd_generator.h by putting the generators in the EO-hierarchy + + (c) Maarten Keijzer (mak@dhi.dk) and GeNeura Team, 1999, 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 + 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 eoRND_GENERATORS_H +#define eoRND_GENERATORS_H + +#include "eoRNG.h" +#include +#include + +/** + By popular demand re-introducing a base class for a family of + random number generators. Derived members of this class are useful + to initialize fixed/variable length genotypes that have an 'atomic' type + in an indepent way (thus without employing any knowledge abou the problem domain). + + The only change from previous EO's us the use of the suffix EO. This to + + See derived classes eoUniformGenerator, eoBooleanGenerator, eoNormalGenerator and eoNegExpGenerator +*/ +template +class eoRndGenerator : public eoF +{ + typedef T AtomType; +}; + +/** + The class eoUniform can be used in the STL generate function + to easily generate random floats and doubles. It can also + be used for ints and unsigneds by virtue of the static_cast + in the generator function. + + Also present is a specialization for boolean, that will + ignore the minima and maxima that are possibly set and will + return an unbiased flip of a coin. For a biased flip, use the eoBoolean + + either in [0, _max) if only 1 value (_max) is given + (or none, as _max defaults to 1.0) + or in [_min,_max) if 2 values are given (_min, _max) +*/ +template class eoUniformGenerator : public eoRndGenerator +{ + // added new ctor with 2 params, and modified the data to minim and range + // (was maxim only). MS 3/11/2000 + public : + eoUniformGenerator(T _max = T(1.0), eoRng& _rng = rng) : + minim(T(0.0)), range(_max), uniform(_rng) {} + eoUniformGenerator(T _min, T _max, eoRng& _rng = rng) : + minim(_min), range(_max-_min), uniform(_rng) + { + if (_min>_max) + throw logic_error("Min is greater than Max in uniform_generator"); + } + + /** + Generates the number, uses a static_cast to get the right behaviour + for ints and unsigneds + */ + T operator()(void) { return minim+static_cast(uniform.uniform(range)); } + +private : + T minim; + T range; + eoRng& uniform; +}; + + +/// Specialization for bool, does an unbiased coin flip +template <> +bool eoUniformGenerator::operator()(void) +{ + return uniform.flip(0.5); +} + +/** + The class eoBooleanGenerator can be used in the STL generate function + to easily generate random booleans with a specified bias +*/ +class eoBooleanGenerator : public eoRndGenerator +{ + public : + eoBooleanGenerator(float _bias = 0.5, eoRng& _rng = rng) : bias(_bias), gen(_rng) {} + + bool operator()(void) { return gen.flip(bias); } + private : + float bias; + eoRng& gen; +}; + +/** + The class normal_generator can be used in the STL generate function + to easily generate gaussian distributed floats and doubles. The user + can supply a standard deviation which defaults to 1. +*/ +template class eoNormalGenerator : public eoRndGenerator +{ + public : + eoNormalGenerator(T _stdev = T(1.0), eoRng& _rng = rng) : stdev(_stdev), normal(_rng) {} + + T operator()(void) { return (T) normal.normal(stdev); } + + private : + T stdev; + eoRng& normal; +}; + +/** + The class negexp_generator can be used in the STL generate function + to easily generate negative exponential distributed floats and doubles. The user + can supply a mean. +*/ +template class eoNegExpGenerator : public eoRndGenerator +{ + public : + eoNegExpGenerator(T _mean = 1.0, eoRng& _rng = rng) : mean(_mean), negexp(_rng) {} + + T operator()(void) { return (T) negexp.negexp(mean); } + + private : + T mean; + eoRng& negexp; +}; + +#endif diff --git a/eo/src/utils/eoUniform.h b/eo/src/utils/eoUniform.h deleted file mode 100644 index dbeb40468..000000000 --- a/eo/src/utils/eoUniform.h +++ /dev/null @@ -1,104 +0,0 @@ -/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - - ----------------------------------------------------------------------------- - eoUniform.h - Uniform random number generator; - (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 -*/ - -/** -CVS Info: $Date: 2001-02-13 22:35:07 $ $Header: /home/nojhan/dev/eodev/eodev_cvs/eo/src/utils/Attic/eoUniform.h,v 1.1 2001-02-13 22:35:07 jmerelo Exp $ $Author: jmerelo $ $Log$ -*/ - -//----------------------------------------------------------------------------- - -#ifndef _EOUNIFORM_H -#define _EOUNIFORM_H - -//----------------------------------------------------------------------------- - -#include -#include - -//----------------------------------------------------------------------------- -// Class eoUniform -//----------------------------------------------------------------------------- - -/** Generates uniform random number over the interval [min, max) -Uses the global variable rng - */ - -using eo::rng; - -template -class eoUniform: public eoRnd -{ - public: - /** - * Default constructor. - * @param _min The minimum value in the interval. - * @param _max The maximum value in the interval. - */ - eoUniform(T _min = 0, T _max = 1) - : eoRnd(), min(_min), diff(_max - _min) {} - - /** - * copy constructor. - * @param _rnd the other rnd - */ - eoUniform( const eoUniform& _rnd) - : eoRnd( _rnd), min(_rnd.min), diff(_rnd.diff) {} - - /** Returns an uniform random number over the interval [min, max) - Uses global rng object */ - virtual T operator()() { - return min + T( rng.uniform( diff ) ); - } - - private: - T min; - double diff; -}; - -template<> -class eoUniform: public eoRnd -{ - public: - /** - * Default constructor. - * @param _min The minimum value in the interval. - * @param _max The maximum value in the interval. - */ - eoUniform(bool _min = false, bool _max = true) - : eoRnd() {} - - /** Returns an uniform random number over the interval [min, max) - Uses global rng object */ - virtual bool operator()() { - return rng.flip(0.5); - } - - private: - bool min; - double diff; -}; - -//----------------------------------------------------------------------------- - -#endif diff --git a/eo/test/t-eoRandom.cpp b/eo/test/t-eoRandom.cpp index 57116af83..7250a0f61 100644 --- a/eo/test/t-eoRandom.cpp +++ b/eo/test/t-eoRandom.cpp @@ -24,23 +24,23 @@ */ /** -CVS Info: $Date: 2001-02-13 22:39:56 $ $Author: jmerelo $ $Revision: 1.7 $ +CVS Info: $Date: 2001-02-17 10:51:31 $ $Author: maartenkeijzer $ $Revision: 1.8 $ */ //----------------------------------------------------------------------------- #include // cout #include // ostrstream, istrstream -#include // eoBin +#include // eoBin //#include //#include //----------------------------------------------------------------------------- main() { - eoUniform u1(-2.5,3.5); - eoUniform u2(0.003, 0.0005 ); - eoUniform u3( 10000000U, 10000U); + eoUniformGenerator u1(-2.5,3.5); + eoUniformGenerator u2(0.003, 0.0005 ); + eoUniformGenerator u3( 10000000U, 10000U); /* eoNegExp e1(3.5); eoNegExp e2(0.003 ); eoNegExp e3( 10000U);