diff --git a/eo/src/eo b/eo/src/eo index a80cf4ce..98bc61ab 100644 --- a/eo/src/eo +++ b/eo/src/eo @@ -62,6 +62,7 @@ #include +#include #include #include diff --git a/eo/src/eoInit.h b/eo/src/eoInit.h index 6af2ff2d..ac875b24 100644 --- a/eo/src/eoInit.h +++ b/eo/src/eoInit.h @@ -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 + #include +#include +#include /** - 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 {}; /** - Initializor for fixed length representations with a single type + Initializer for fixed length representations with a single type */ -template +template class eoInitFixedLength: public eoInit { public: - eoInitFixedLength(unsigned _howmany, Gen _generator = Gen()) + + typedef typename EOT::AtomType AtomType; + + eoInitFixedLength(unsigned _howmany, eoRndGenerator& _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 generator; }; /** @@ -70,8 +78,8 @@ template class eoInitVariableLength: public eoInit { 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 return true; } private : - + eoInit& init; }; diff --git a/eo/src/eoSTLFunctor.h b/eo/src/eoSTLFunctor.h new file mode 100644 index 00000000..682b4294 --- /dev/null +++ b/eo/src/eoSTLFunctor.h @@ -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 eoSTLF +{ + public: + + typedef R result_type; + + eoSTLF(eoF& _f) : f(_f) {} + + R operator()(void) + { + return f(); + } + + private : + + eoF& 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::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 eoSTLUF : public std::unary_function +{ + public: + eoSTLUF(eoUF& _f) : f(_f) {} + + R operator()(A1 a) + { + return f(a); + } + + private: + eoUF& 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 eoSTLBF : public std::binary_function +{ + public: + eoSTLBF(eoUF& _f) : f(_f) {} + + R operator()(A1 a1, A2 a2) + { + return f(a1, a2); + } + + private: + + eoBF& f; +}; + +// TODO: put automated wrappers here... + +#endif \ No newline at end of file diff --git a/eo/src/eoVector.h b/eo/src/eoVector.h index aacf8618..d4e89e5b 100644 --- a/eo/src/eoVector.h +++ b/eo/src/eoVector.h @@ -43,12 +43,18 @@ class eoVector : public EO, public std::vector typedef GeneType AtomType; typedef std::vector ContainerType; + eoVector(unsigned size = 0, GeneType value = GeneType()) : EO(), std::vector(size, value) {} + /// copy ctor abstracting from the FitT + template + eoVector(const eoVector& _vec) : vector(_vec) + {} + // we can't have a Ctor from a vector, it would create ambiguity // with the copy Ctor - void value(std::vector _v) + void value(const std::vector& _v) { if (_v.size() != size()) throw runtime_error("Wrong size in vector assignation in eoVector"); diff --git a/eo/src/ga/eoBit.h b/eo/src/ga/eoBit.h index ed411b7d..668c9112 100644 --- a/eo/src/ga/eoBit.h +++ b/eo/src/ga/eoBit.h @@ -62,13 +62,13 @@ template class eoBit: public eoVector */ eoBit(unsigned size = 0, bool value = false): eoVector(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 eoBit: public eoVector { EO::printOn(os); os << ' '; - os << size() << ' '; + os << size() << ' '; copy(begin(), end(), ostream_iterator(os)); } - + /** * To read me from a stream. * @param is The istream. @@ -95,7 +95,7 @@ template class eoBit: public eoVector if (is) { resize(bits.size()); - transform(bits.begin(), bits.end(), begin(), + transform(bits.begin(), bits.end(), begin(), bind2nd(equal_to(), '1')); } } diff --git a/eo/src/ga/ga.cpp b/eo/src/ga/ga.cpp index 802834b2..be17928b 100644 --- a/eo/src/ga/ga.cpp +++ b/eo/src/ga/ga.cpp @@ -37,8 +37,8 @@ eoPop >& do_init_ga(eoParameterLoader& _parser, eoState& _state, Fi _parser.processParam(chromSize, "initialization"); _parser.processParam(popSize, "initialization"); - eoInitFixedLength init(chromSize.value(), boolean_generator()); - + eoBooleanGenerator gen; + eoInitFixedLength init(chromSize.value(), gen); // Let the state handle the memory eoPop& pop = _state.takeOwnership(eoPop()); diff --git a/eo/src/utils/eoRndGenerators.h b/eo/src/utils/eoRndGenerators.h index 57a58c69..089887ef 100644 --- a/eo/src/utils/eoRndGenerators.h +++ b/eo/src/utils/eoRndGenerators.h @@ -26,8 +26,8 @@ //----------------------------------------------------------------------------- -#ifndef eoRND_GENERATORS_H -#define eoRND_GENERATORS_H +#ifndef eoRndGenerators_h +#define eoRndGenerators_h #include "eoRNG.h" #include @@ -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 */ diff --git a/eo/test/Makefile.am b/eo/test/Makefile.am index 7e3c608f..3128dc8e 100644 --- a/eo/test/Makefile.am +++ b/eo/test/Makefile.am @@ -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 diff --git a/eo/test/t-eoRandom.cpp b/eo/test/t-eoRandom.cpp index 09f9b7a6..3197dccf 100644 --- a/eo/test/t-eoRandom.cpp +++ b/eo/test/t-eoRandom.cpp @@ -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 // cout -#include // ostrstream, istrstream +#include // ostrstream, istrstream #include // eoBin //#include //#include @@ -38,27 +38,28 @@ CVS Info: $Date: 2001-02-18 04:34:57 $ $Author: evomarc $ $Revision: 1.9 $ //----------------------------------------------------------------------------- main() { - try{ eoUniformGenerator u1(-2.5,3.5); - eoUniformGenerator u2(0.0003, 0.0005 ); - eoUniformGenerator u3( 100000U, 10000000U); + eoUniformGenerator u2(0.003, 0.05 ); + eoUniformGenerator u3( 10000U, 10000000U); - eoNegExpGenerator e1(3.5); - eoNegExpGenerator e2(0.003 ); - eoNegExpGenerator 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 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 + } //----------------------------------------------------------------------------- diff --git a/eo/test/t-eoVector.cpp b/eo/test/t-eoVector.cpp index d807a977..7d241ae6 100644 --- a/eo/test/t-eoVector.cpp +++ b/eo/test/t-eoVector.cpp @@ -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 // cout #include // ostrstream, istrstream -#include +#include + +#include #include // eoVector -#include +#include +#include //----------------------------------------------------------------------------- -typedef eoVector Chrom; +typedef eoVector Chrom1; +typedef eoVector Chrom2; //----------------------------------------------------------------------------- @@ -40,17 +46,27 @@ main() { const unsigned SIZE = 4; - eoUniform 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 uniform(-1,1); + eoInitFixedLength init(SIZE, uniform); + + init(chrom); + + cout << chrom << endl; + + Chrom2 chrom2(chrom); + + cout << chrom2 << endl; + +// eoInitVariableLength 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; } diff --git a/eo/test/t-eobin.cpp b/eo/test/t-eobin.cpp index 9e91c88c..cdc29c09 100644 --- a/eo/test/t-eobin.cpp +++ b/eo/test/t-eobin.cpp @@ -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 // ostrstream, istrstream #include // general EO #include // bitstring representation & operators +#include #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 - random(chrom.size(), boolean_generator()); - + + eoInitFixedLength + random(chrom.size(), gen); + random(chrom); chrom.fitness(binary_value(chrom)); cout << "after eoBinRandom ............ " << chrom << endl; @@ -149,10 +151,10 @@ void main_function() eoProportionalSelect select; eoEvalFuncPtr eval(binary_value); - + eoSGA sga(select, xover, 0.8f, bitflip, 0.1f, eval, checkpoint); - - eoInitFixedLength init(16, boolean_generator()); + + eoInitFixedLength init(16, gen); eoPop pop(100, init); apply(eval, pop);