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
*/

View file

@ -14,8 +14,8 @@ CXXFLAGS = -g
###############################################################################
check_PROGRAMS = t-eofitness t-eoRandom t-eobin t-eoStateAndParser t-eoCheckpointing \
t-eoExternalEO t-eoSymreg t-eo t-eoReplacement t-eoSelect t-eoGenOp t-eoGA
TESTS=run_tests
t-eoExternalEO t-eoSymreg t-eo t-eoReplacement t-eoSelect t-eoGenOp t-eoGA t-eoVector
TESTS=run_tests t-eoVector t-eoRandom
# removing temporarily t-eoESFull
#noinst_PROGRAMS = t-eofitness t-eobin t-eoStateAndParser t-eoCheckpointing t-eoExternalEO t-eoESFull t-eoSymreg t-eo t-eoReplacement t-eoSelect t-eoGenOp t-eoGA
@ -101,6 +101,13 @@ t_eoGenOp_LDADD = $(LDADDS)
###############################################################################
t_eoVector_SOURCES = t-eoVector.cpp
t_eoVector_DEPENDENCIES = $(DEPS)
t_eoVector_LDFLAGS = -lm
t_eoVector_LDADD = $(LDADDS)
###############################################################################
t_eoGA_SOURCES = t-eoGA.cpp binary_value.h
t_eoGA_DEPENDENCIES = $(DEPS) $(top_builddir)/src/ga/libga.a
t_eoGA_LDFLAGS = -lm

View file

@ -3,8 +3,8 @@
t-eoRandom.cpp
Test program for random generator
(c) GeNeura Team, 1999
(c) GeNeura Team, 1999
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
@ -18,19 +18,19 @@
You should have received a copy of the GNU General Public License
along with this program; 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-18 04:34:57 $ $Author: evomarc $ $Revision: 1.9 $
CVS Info: $Date: 2001-02-19 12:23:13 $ $Author: maartenkeijzer $ $Revision: 1.10 $
*/
//-----------------------------------------------------------------------------
#include <iostream> // cout
#include <strstream> // ostrstream, istrstream
#include <fstream> // ostrstream, istrstream
#include <utils/eoRndGenerators.h> // eoBin
//#include <eoNormal.h>
//#include <eoNegExp.h>
@ -38,27 +38,28 @@ CVS Info: $Date: 2001-02-18 04:34:57 $ $Author: evomarc $ $Revision: 1.9 $
//-----------------------------------------------------------------------------
main() {
try{
eoUniformGenerator<float> u1(-2.5,3.5);
eoUniformGenerator<double> u2(0.0003, 0.0005 );
eoUniformGenerator<unsigned long> u3( 100000U, 10000000U);
eoUniformGenerator<double> u2(0.003, 0.05 );
eoUniformGenerator<unsigned long> u3( 10000U, 10000000U);
eoNegExpGenerator<float> e1(3.5);
eoNegExpGenerator<double> e2(0.003 );
eoNegExpGenerator<long> e3( 10000U);
/* cout << "n1\t\tn2\t\tn3\t\te1\t\te2\t\te3" << endl; */
for ( unsigned i = 0; i < 100; i ++) {
cout << "Uniform: " << u1() << "\t" << u2() << "\t" << u3() << endl;
cout << "NegExp: " << e1() << "\t" << e2() << "\t" << e3() << endl;
try
{ // throws an error
eoUniformGenerator<unsigned long> utest( 10000000U, 10000U);
}
}
catch (exception& e)
{
cout << "exception: " << e.what() << endl;;
exit(EXIT_FAILURE);
}
catch (logic_error& e)
{
cout << e.what() << endl;
}
ofstream os("t-eoRandom.out");
for ( unsigned i = 0; i < 100; i ++)
{
os << u1() << "\t" << u2() << "\t" << u3() << endl;
}
return 0; // to avoid VC++ complaints
}
//-----------------------------------------------------------------------------

View file

@ -1,9 +1,11 @@
/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
t-eoVectpr.cpp
t-eoVector.cpp
This program tests vector-like chromosomes
(c) GeNeura Team, 1999, 2000
Modified by Maarten Keijzer 2001
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
@ -17,22 +19,26 @@
You should have received a copy of the GNU General Public License
along with this program; 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
*/
//-----------------------------------------------------------------------------
#include <iostream> // cout
#include <strstream> // ostrstream, istrstream
#include <eoUniform.h>
#include <assert.h>
#include <utils/eoRndGenerators.h>
#include <eoVector.h> // eoVector
#include <eo1dWDistance.h>
#include <eoInit.h>
#include <eoScalarFitness.h>
//-----------------------------------------------------------------------------
typedef eoVector<float> Chrom;
typedef eoVector<eoMaximizingFitness, int> Chrom1;
typedef eoVector<eoMinimizingFitness, int> Chrom2;
//-----------------------------------------------------------------------------
@ -40,17 +46,27 @@ main()
{
const unsigned SIZE = 4;
eoUniform<Chrom::Type> uniform(-1,1);
// check if the appropriate ctor gets called
Chrom1 chrom(SIZE, 5);
for (int i = 0; i < chrom.size(); ++i)
{
assert(chrom[i] == 5);
}
eoUniformGenerator<Chrom1::AtomType> uniform(-1,1);
eoInitFixedLength<Chrom1> init(SIZE, uniform);
init(chrom);
cout << chrom << endl;
Chrom2 chrom2(chrom);
cout << chrom2 << endl;
// eoInitVariableLength<Chrom1> initvar(
Chrom chrom1(SIZE,uniform), chrom2( SIZE, uniform);
cout << "chrom1: " << chrom1 << endl <<
"chrom2: " << chrom2 << endl;
eo1dWDistance< float, float > chromDist( chrom1 );
cout << "Distance from chrom1 to chrom2 " << chromDist.distance( chrom2 ) << endl;
return 0;
}

View file

@ -2,8 +2,8 @@
t-eobin.cpp
This program tests the the binary cromosomes and several genetic operators
(c) GeNeura Team, 1999
(c) GeNeura Team, 1999
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
@ -17,9 +17,9 @@
You should have received a copy of the GNU General Public License
along with this program; 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
*/
//-----------------------------------------------------------------------------
@ -27,6 +27,7 @@
#include <strstream> // ostrstream, istrstream
#include <eo> // general EO
#include <ga.h> // bitstring representation & operators
#include <utils/eoRndGenerators.h>
#include "binary_value.h"
//-----------------------------------------------------------------------------
@ -39,10 +40,11 @@ void main_function()
{
const unsigned SIZE = 8;
unsigned i, j;
eoBooleanGenerator gen;
Chrom chrom(SIZE), chrom2;
chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2));
cout << "chrom: " << chrom << endl;
chrom[0] = chrom[SIZE - 1] = true; chrom.fitness(binary_value(chrom));
cout << "chrom: " << chrom << endl;
@ -51,7 +53,7 @@ void main_function()
chrom[0] = chrom[SIZE - 1] = true; chrom.fitness(binary_value(chrom));
cout << "chrom.className() = " << chrom.className() << endl;
cout << "chrom: " << chrom << endl
<< "chrom2: " << chrom2 << endl;
@ -67,10 +69,10 @@ void main_function()
fill(chrom.begin(), chrom.end(), false);
cout << "--------------------------------------------------"
<< endl << "eoMonOp's aplied to .......... " << chrom << endl;
eoInitFixedLength<Chrom, boolean_generator>
random(chrom.size(), boolean_generator());
eoInitFixedLength<Chrom>
random(chrom.size(), gen);
random(chrom); chrom.fitness(binary_value(chrom));
cout << "after eoBinRandom ............ " << chrom << endl;
@ -149,10 +151,10 @@ void main_function()
eoProportionalSelect<Chrom> select;
eoEvalFuncPtr<Chrom> eval(binary_value);
eoSGA<Chrom> sga(select, xover, 0.8f, bitflip, 0.1f, eval, checkpoint);
eoInitFixedLength<Chrom, boolean_generator> init(16, boolean_generator());
eoInitFixedLength<Chrom> init(16, gen);
eoPop<Chrom> pop(100, init);
apply<Chrom>(eval, pop);