From 861f11e1532e71ee0a9d75da9d61ff5902273867 Mon Sep 17 00:00:00 2001 From: evomarc Date: Tue, 4 Sep 2001 06:48:15 +0000 Subject: [PATCH] Added a class that turns an eoInit into a generator of EOT Also added a Ctor of variable length objects that take an eoInit. Some day we might want to clean all that stuff ... unless we leave the choice to the user (but then the documentation shoudl be as clear as glass, which it is not at the moment!) --- eo/src/eoInit.h | 86 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 64 insertions(+), 22 deletions(-) diff --git a/eo/src/eoInit.h b/eo/src/eoInit.h index 1e8b0112..f872c1dd 100644 --- a/eo/src/eoInit.h +++ b/eo/src/eoInit.h @@ -45,8 +45,32 @@ template class eoInit : public eoUF { public: - virtual void operator()(EOT& chrom) - { cout << "In the eoInit base class" << endl; } + virtual void operator()(EOT& chrom) + { + throw runtime_error("In the eoInit base class"); // just in case + } +}; + +/** turning an eoInit into a generator + * probably we should only use genrators - and suppress eoInit ??? + * MS - July 2001 + */ +template +class eoInitGenerator : public eoF +{ +public: + + /** Ctor from a plain eoInit */ + eoInitGenerator(eoInit & _init):init(_init) {} + + virtual EOT operator()() + { + EOT p; + init(p); + return (p); + } +private: + eoInit & init; }; /** @@ -76,33 +100,51 @@ class eoInitFixedLength: public eoInit }; /** - Initializor for variable length representations with a single type + Initializer for variable length representations with a single type */ -template +template class eoInitVariableLength: public eoInit { - public: - 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"); - } +public: +typedef typename EOT::AtomType AtomType; - virtual void operator()(EOT& chrom) - { - chrom.resize(offset + rng.random(extent)); - generate(chrom.begin(), chrom.end(), generator); - chrom.invalidate(); - } +// /** Ctor from a generator */ +// eoInitVariableLength(unsigned _minSize, unsigned _maxSize, eoF & _generator = Gen()) +// : offset(_minSize), extent(_maxSize - _minSize), +// repGenerator( eoInitGenerator(*(new eoInit)) ), +// generator(_generator) +// { +// if (_minSize >= _maxSize) +// throw logic_error("eoInitVariableLength: minSize larger or equal to maxSize"); +// } - private : - unsigned offset; - unsigned extent; - Gen generator; + /** Ctor from an eoInit */ + eoInitVariableLength(unsigned _minSize, unsigned _maxSize, eoInit & _init) + : offset(_minSize), extent(_maxSize - _minSize), init(_init) + { + if (_minSize >= _maxSize) + throw logic_error("eoInitVariableLength: minSize larger or equal to maxSize"); + } + + + virtual void operator()(EOT& _chrom) + { + _chrom.resize(offset + rng.random(extent)); + vector::iterator it; + for (it=_chrom.begin(); it<_chrom.end(); it++) + init(*it); + _chrom.invalidate(); + } + + // accessor to the atom initializer (needed by operator constructs sometimes) + const eoInit & atomInit() {return init;} + +private : + unsigned offset; + unsigned extent; + eoInit & init; }; - /** eoInitAdaptor changes the place in the hierarchy from eoInit to eoMonOp. This is mainly a type conversion,