Modified eoInit so that it would use the eoRndGenerator base class.

To be able to use the primitive std::generate function, added a
set of wrappers in eoSTLFunctor.h that have the copy semantics most
STL functions expect (namely pass-by-value rather then pass-by-reference).

Updated test/Makefile.am to also test t-eoRandom
This commit is contained in:
maartenkeijzer 2001-02-19 12:23:13 +00:00
commit a79075f673
11 changed files with 240 additions and 83 deletions

View file

@ -62,6 +62,7 @@
#include <other/eoString.h>
#include <utils/eoRndGenerators.h>
#include <eoInit.h>
#include <eoOp.h>

View file

@ -3,7 +3,7 @@
//-----------------------------------------------------------------------------
// eoInit.h
// (c) Maarten Keijzer 2000, 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
@ -27,10 +27,14 @@
#ifndef _eoInit_H
#define _eoInit_H
#include <algorithm>
#include <eoOp.h>
#include <eoSTLFunctor.h>
#include <utils/eoRndGenerators.h>
/**
Base (name) class for Initialization of chromosomes, used in a population
Base (name) class for Initialization of chromosomes, used in a population
contructor. It is derived from eoMonOp, so it can be used
inside the algorithm as well.
@ -42,25 +46,29 @@ class eoInit : public eoUF<EOT&, void>
{};
/**
Initializor for fixed length representations with a single type
Initializer for fixed length representations with a single type
*/
template <class EOT, class Gen>
template <class EOT>
class eoInitFixedLength: public eoInit<EOT>
{
public:
eoInitFixedLength(unsigned _howmany, Gen _generator = Gen())
typedef typename EOT::AtomType AtomType;
eoInitFixedLength(unsigned _howmany, eoRndGenerator<AtomType>& _generator)
: howmany(_howmany), generator(_generator) {}
void operator()(EOT& chrom)
{
chrom.resize(howmany);
generate(chrom.begin(), chrom.end(), generator);
std::generate(chrom.begin(), chrom.end(), generator);
chrom.invalidate();
}
private :
unsigned howmany;
Gen generator;
/// generic wrapper for eoFunctor (s), to make them have the function-pointer style copy semantics
eoSTLF<AtomType> generator;
};
/**
@ -70,8 +78,8 @@ template <class EOT, class Gen>
class eoInitVariableLength: public eoInit<EOT>
{
public:
eoInitVariableLength(unsigned _minSize, unsigned _maxSize, Gen _generator = Gen())
: offset(_minSize), extent(_maxSize - _minSize), generator(_generator)
eoInitVariableLength(unsigned _minSize, unsigned _maxSize, Gen _generator = Gen())
: offset(_minSize), extent(_maxSize - _minSize), generator(_generator)
{
if (_minSize >= _maxSize)
throw logic_error("eoInitVariableLength: minSize larger or equal to maxSize");
@ -111,7 +119,7 @@ class eoInitAdaptor : public eoMonOp<EOT>
return true;
}
private :
eoInit<EOT>& init;
};

118
eo/src/eoSTLFunctor.h Normal file
View file

@ -0,0 +1,118 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
//-----------------------------------------------------------------------------
// eoSTLFunctor.h
// (c) Maarten Keijzer 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
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 _eoSTLFunctor_H
#define _eoSTLFunctor_H
#include "eoFunctor.h"
/**
Generic set of classes that wrap an eoF, eoUF or eoBF so that they have the
copy semantics the STL functions usually require (i.e. they can be passed
by value, rather than the EO standard pass by reference).
The family consists of eoSTLF, eoSTLUF, eoSTLBF that modify eoF, eoUF and eoBF
respectively
*/
template <class R>
class eoSTLF
{
public:
typedef R result_type;
eoSTLF(eoF<R>& _f) : f(_f) {}
R operator()(void)
{
return f();
}
private :
eoF<R>& f;
};
#ifdef _MSVC
/// specialization of void for MSVC users, unfortunately only works for eoF,
/// as MSVC does not support partial specialization either
template <>
void eoSTLF<void>::operator()(void)
{
f();
}
#endif
/**
Generic set of classes that wrap an eoF, eoUF or eoBF so that they have the
copy semantics the STL functions usually require (i.e. they can be passed
by value, rather than the EO standard pass by reference).
The family consists of eoSTLF, eoSTLUF, eoSTLBF that modify eoF, eoUF and eoBF
respectively
*/
template <class A1, class R>
class eoSTLUF : public std::unary_function<A1, R>
{
public:
eoSTLUF(eoUF<A1,R>& _f) : f(_f) {}
R operator()(A1 a)
{
return f(a);
}
private:
eoUF<A1, R>& f;
};
/**
Generic set of classes that wrap an eoF, eoUF or eoBF so that they have the
copy semantics the STL functions usually require (i.e. they can be passed
by value, rather than the EO standard pass by reference).
The family consists of eoSTLF, eoSTLUF, eoSTLBF that modify eoF, eoUF and eoBF
respectively
*/
template <class A1, class A2, class R>
class eoSTLBF : public std::binary_function<A1, A2, R>
{
public:
eoSTLBF(eoUF<A1,R>& _f) : f(_f) {}
R operator()(A1 a1, A2 a2)
{
return f(a1, a2);
}
private:
eoBF<A1, A2, R>& f;
};
// TODO: put automated wrappers here...
#endif

View file

@ -43,12 +43,18 @@ class eoVector : public EO<FitT>, public std::vector<GeneType>
typedef GeneType AtomType;
typedef std::vector<GeneType> ContainerType;
eoVector(unsigned size = 0, GeneType value = GeneType()) : EO<FitT>(), std::vector<GeneType>(size, value)
{}
/// copy ctor abstracting from the FitT
template <class OtherFitnessType>
eoVector(const eoVector<OtherFitnessType, GeneType>& _vec) : vector<GeneType>(_vec)
{}
// we can't have a Ctor from a vector, it would create ambiguity
// with the copy Ctor
void value(std::vector<GeneType> _v)
void value(const std::vector<GeneType>& _v)
{
if (_v.size() != size())
throw runtime_error("Wrong size in vector assignation in eoVector");

View file

@ -62,13 +62,13 @@ template <class FitT> class eoBit: public eoVector<FitT, bool>
*/
eoBit(unsigned size = 0, bool value = false):
eoVector<FitT, bool>(size, value) {}
/// My class name.
string className() const
{
return "eoBit";
string className() const
{
return "eoBit";
}
/**
* To print me on a stream.
* @param os The ostream.
@ -77,10 +77,10 @@ template <class FitT> class eoBit: public eoVector<FitT, bool>
{
EO<FitT>::printOn(os);
os << ' ';
os << size() << ' ';
os << size() << ' ';
copy(begin(), end(), ostream_iterator<bool>(os));
}
/**
* To read me from a stream.
* @param is The istream.
@ -95,7 +95,7 @@ template <class FitT> class eoBit: public eoVector<FitT, bool>
if (is)
{
resize(bits.size());
transform(bits.begin(), bits.end(), begin(),
transform(bits.begin(), bits.end(), begin(),
bind2nd(equal_to<char>(), '1'));
}
}

View file

@ -37,8 +37,8 @@ eoPop<eoBit<FitT> >& do_init_ga(eoParameterLoader& _parser, eoState& _state, Fi
_parser.processParam(chromSize, "initialization");
_parser.processParam(popSize, "initialization");
eoInitFixedLength<EOT, boolean_generator> init(chromSize.value(), boolean_generator());
eoBooleanGenerator gen;
eoInitFixedLength<EOT> init(chromSize.value(), gen);
// Let the state handle the memory
eoPop<EOT>& pop = _state.takeOwnership(eoPop<EOT>());

View file

@ -26,8 +26,8 @@
//-----------------------------------------------------------------------------
#ifndef eoRND_GENERATORS_H
#define eoRND_GENERATORS_H
#ifndef eoRndGenerators_h
#define eoRndGenerators_h
#include "eoRNG.h"
#include <eoFunctor.h>
@ -37,9 +37,7 @@
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
in an indepent way (thus without employing any knowledge about the problem domain).
See derived classes eoUniformGenerator, eoBooleanGenerator, eoNormalGenerator and eoNegExpGenerator
*/