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 <other/eoString.h>
#include <utils/eoRndGenerators.h>
#include <eoInit.h> #include <eoInit.h>
#include <eoOp.h> #include <eoOp.h>

View file

@ -27,7 +27,11 @@
#ifndef _eoInit_H #ifndef _eoInit_H
#define _eoInit_H #define _eoInit_H
#include <algorithm>
#include <eoOp.h> #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
@ -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> class eoInitFixedLength: public eoInit<EOT>
{ {
public: public:
eoInitFixedLength(unsigned _howmany, Gen _generator = Gen())
typedef typename EOT::AtomType AtomType;
eoInitFixedLength(unsigned _howmany, eoRndGenerator<AtomType>& _generator)
: howmany(_howmany), generator(_generator) {} : howmany(_howmany), generator(_generator) {}
void operator()(EOT& chrom) void operator()(EOT& chrom)
{ {
chrom.resize(howmany); chrom.resize(howmany);
generate(chrom.begin(), chrom.end(), generator); std::generate(chrom.begin(), chrom.end(), generator);
chrom.invalidate(); chrom.invalidate();
} }
private : private :
unsigned howmany; unsigned howmany;
Gen generator; /// generic wrapper for eoFunctor (s), to make them have the function-pointer style copy semantics
eoSTLF<AtomType> generator;
}; };
/** /**

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 GeneType AtomType;
typedef std::vector<GeneType> ContainerType; typedef std::vector<GeneType> ContainerType;
eoVector(unsigned size = 0, GeneType value = GeneType()) : EO<FitT>(), std::vector<GeneType>(size, value) 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 // we can't have a Ctor from a vector, it would create ambiguity
// with the copy Ctor // with the copy Ctor
void value(std::vector<GeneType> _v) void value(const std::vector<GeneType>& _v)
{ {
if (_v.size() != size()) if (_v.size() != size())
throw runtime_error("Wrong size in vector assignation in eoVector"); throw runtime_error("Wrong size in vector assignation in eoVector");

View file

@ -37,8 +37,8 @@ eoPop<eoBit<FitT> >& do_init_ga(eoParameterLoader& _parser, eoState& _state, Fi
_parser.processParam(chromSize, "initialization"); _parser.processParam(chromSize, "initialization");
_parser.processParam(popSize, "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 // Let the state handle the memory
eoPop<EOT>& pop = _state.takeOwnership(eoPop<EOT>()); eoPop<EOT>& pop = _state.takeOwnership(eoPop<EOT>());

View file

@ -26,8 +26,8 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#ifndef eoRND_GENERATORS_H #ifndef eoRndGenerators_h
#define eoRND_GENERATORS_H #define eoRndGenerators_h
#include "eoRNG.h" #include "eoRNG.h"
#include <eoFunctor.h> #include <eoFunctor.h>
@ -37,9 +37,7 @@
By popular demand re-introducing a base class for a family of By popular demand re-introducing a base class for a family of
random number generators. Derived members of this class are useful random number generators. Derived members of this class are useful
to initialize fixed/variable length genotypes that have an 'atomic' type to initialize fixed/variable length genotypes that have an 'atomic' type
in an indepent way (thus without employing any knowledge abou the problem domain). in an indepent way (thus without employing any knowledge about 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 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 \ 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 t-eoExternalEO t-eoSymreg t-eo t-eoReplacement t-eoSelect t-eoGenOp t-eoGA t-eoVector
TESTS=run_tests TESTS=run_tests t-eoVector t-eoRandom
# removing temporarily t-eoESFull # 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 #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_SOURCES = t-eoGA.cpp binary_value.h
t_eoGA_DEPENDENCIES = $(DEPS) $(top_builddir)/src/ga/libga.a t_eoGA_DEPENDENCIES = $(DEPS) $(top_builddir)/src/ga/libga.a
t_eoGA_LDFLAGS = -lm t_eoGA_LDFLAGS = -lm

View file

@ -24,13 +24,13 @@
*/ */
/** /**
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 <iostream> // cout
#include <strstream> // ostrstream, istrstream #include <fstream> // ostrstream, istrstream
#include <utils/eoRndGenerators.h> // eoBin #include <utils/eoRndGenerators.h> // eoBin
//#include <eoNormal.h> //#include <eoNormal.h>
//#include <eoNegExp.h> //#include <eoNegExp.h>
@ -38,27 +38,28 @@ CVS Info: $Date: 2001-02-18 04:34:57 $ $Author: evomarc $ $Revision: 1.9 $
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
main() { main() {
try{
eoUniformGenerator<float> u1(-2.5,3.5); eoUniformGenerator<float> u1(-2.5,3.5);
eoUniformGenerator<double> u2(0.0003, 0.0005 ); eoUniformGenerator<double> u2(0.003, 0.05 );
eoUniformGenerator<unsigned long> u3( 100000U, 10000000U); eoUniformGenerator<unsigned long> u3( 10000U, 10000000U);
eoNegExpGenerator<float> e1(3.5); try
eoNegExpGenerator<double> e2(0.003 ); { // throws an error
eoNegExpGenerator<long> e3( 10000U); eoUniformGenerator<unsigned long> utest( 10000000U, 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;
} }
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;
} }
catch (exception& e)
{
cout << "exception: " << e.what() << endl;;
exit(EXIT_FAILURE);
}
return 0; // to avoid VC++ complaints 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; -*- /* -*- 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 This program tests vector-like chromosomes
(c) GeNeura Team, 1999, 2000 (c) GeNeura Team, 1999, 2000
Modified by Maarten Keijzer 2001
This program is free software; you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or the Free Software Foundation; either version 2 of the License, or
@ -26,13 +28,17 @@
#include <iostream> // cout #include <iostream> // cout
#include <strstream> // ostrstream, istrstream #include <strstream> // ostrstream, istrstream
#include <eoUniform.h> #include <assert.h>
#include <utils/eoRndGenerators.h>
#include <eoVector.h> // eoVector #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,16 +46,26 @@ main()
{ {
const unsigned SIZE = 4; const unsigned SIZE = 4;
eoUniform<Chrom::Type> uniform(-1,1); // check if the appropriate ctor gets called
Chrom1 chrom(SIZE, 5);
Chrom chrom1(SIZE,uniform), chrom2( SIZE, uniform); for (int i = 0; i < chrom.size(); ++i)
{
assert(chrom[i] == 5);
}
cout << "chrom1: " << chrom1 << endl << eoUniformGenerator<Chrom1::AtomType> uniform(-1,1);
"chrom2: " << chrom2 << endl; eoInitFixedLength<Chrom1> init(SIZE, uniform);
eo1dWDistance< float, float > chromDist( chrom1 ); init(chrom);
cout << "Distance from chrom1 to chrom2 " << chromDist.distance( chrom2 ) << endl;
cout << chrom << endl;
Chrom2 chrom2(chrom);
cout << chrom2 << endl;
// eoInitVariableLength<Chrom1> initvar(
return 0; return 0;
} }

View file

@ -27,6 +27,7 @@
#include <strstream> // ostrstream, istrstream #include <strstream> // ostrstream, istrstream
#include <eo> // general EO #include <eo> // general EO
#include <ga.h> // bitstring representation & operators #include <ga.h> // bitstring representation & operators
#include <utils/eoRndGenerators.h>
#include "binary_value.h" #include "binary_value.h"
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -39,6 +40,7 @@ void main_function()
{ {
const unsigned SIZE = 8; const unsigned SIZE = 8;
unsigned i, j; unsigned i, j;
eoBooleanGenerator gen;
Chrom chrom(SIZE), chrom2; Chrom chrom(SIZE), chrom2;
chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2)); chrom.fitness(binary_value(chrom)); chrom2.fitness(binary_value(chrom2));
@ -68,8 +70,8 @@ void main_function()
cout << "--------------------------------------------------" cout << "--------------------------------------------------"
<< endl << "eoMonOp's aplied to .......... " << chrom << endl; << endl << "eoMonOp's aplied to .......... " << chrom << endl;
eoInitFixedLength<Chrom, boolean_generator> eoInitFixedLength<Chrom>
random(chrom.size(), boolean_generator()); random(chrom.size(), gen);
random(chrom); chrom.fitness(binary_value(chrom)); random(chrom); chrom.fitness(binary_value(chrom));
cout << "after eoBinRandom ............ " << chrom << endl; cout << "after eoBinRandom ............ " << chrom << endl;
@ -152,7 +154,7 @@ void main_function()
eoSGA<Chrom> sga(select, xover, 0.8f, bitflip, 0.1f, eval, checkpoint); 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); eoPop<Chrom> pop(100, init);
apply<Chrom>(eval, pop); apply<Chrom>(eval, pop);