diff --git a/eo/src/other/eoExternalEO.h b/eo/src/other/eoExternalEO.h index ab9b4816..51ad7795 100644 --- a/eo/src/other/eoExternalEO.h +++ b/eo/src/other/eoExternalEO.h @@ -2,8 +2,8 @@ ----------------------------------------------------------------------------- eoExternalEO.h - * Definition of an object that allows an external struct - * to be inserted in EO + Definition of an object that allows an external struct to be inserted in EO + (c) Maarten Keijzer (mkeijzer@mad.scientist.com) and GeNeura Team, 1999, 2000 This library is free software; you can redistribute it and/or @@ -30,27 +30,33 @@ /** * Definition of an object that allows an external struct - * to be inserted in EO + * to be inserted in EO. This struct or class can be of any + * form, the only thing this class does is attach a fitness + * value to it and makes it the appropriate type (derives it from EO). */ + template -class eoExternalEO : public EO, virtual public External +class eoExternalEO : public EO, virtual public External { public : typedef External Type; - eoExternalEO(void) : EO(), Base() {} - eoExternalEO(istream& is) : EO(), Base() { readFrom(is); } + eoExternalEO(void) : EO(), External() {} /** - * Read object.\\ - * @param _is a istream. - * @throw runtime_exception If a valid object can't be read. + Init externalEo with the struct itself and set fitness to zero + */ + eoExternalEO(const External& ext) : EO(), External(ext) {} + eoExternalEO(istream& is) : EO(), Base() { readFrom(is); } + + /** + * Read object, the external struct needs to have an operator>> defined */ virtual void readFrom(istream& _is) { - EO::readFrom(is); - throw runtime_exception("Reading not defined yet"); + EO::readFrom(_is); + _is >> static_cast(*this); } /** @@ -59,10 +65,25 @@ public : */ virtual void printOn(ostream& _os) const { - EO::printOn(is); - throw runtime_excpetion("Writing not defined yet"); + EO::printOn(_os); + _os << static_cast(*this); } }; +/// To remove ambiguities between EO and External, streaming operators are defined yet again +template +std::ostream& operator<<(std::ostream& os, const eoExternalEO& eo) +{ + eo.printOn(os); + return os; +} + +template +std::istream& operator>>(std::istream& is, eoExternalEO& eo) +{ + eo.readFrom(is); + return is; +} + #endif diff --git a/eo/src/other/eoExternalOpFunctions.h b/eo/src/other/eoExternalOpFunctions.h new file mode 100644 index 00000000..b27dd93b --- /dev/null +++ b/eo/src/other/eoExternalOpFunctions.h @@ -0,0 +1,177 @@ +/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + + ----------------------------------------------------------------------------- + eoExternalOpFunc.h + Defines eoExternalInitOpFunc, eoExternalMonOpFunc, eoExternalBinOpFunc, eoExternalQuadOpFunc + that are used to wrap a function pointer to externally defined initialization + and 'genetic' operators + + (c) Maarten Keijzer (mkeijzer@mad.scientist.com) and GeNeura Team, 1999, 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 + 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 + */ + +#ifndef eoExternalOpFunc_h +#define eoExternalOpFunc_h + +#include +#include +#include +#include + +/** + Initialization of external struct, ctor expects a function of the following + signature: + + External func(); + + Where External is the user defined struct or class +*/ +template +class eoExternalInit : public eoRnd > +{ + +public : + + typedef eoExternalEO ExternalEO; + + eoExternalInit(External (*_init)(void)) : init(_init) {} + + + ExternalEO operator()(void) { return (*init)(); } + +private : + + External (*init)(void); +}; + +/** + Evaluation of external struct, ctor expects a function of the following + signature: + + Fit func(External&); + + Where External is the user defined struct or class and Fit the fitness type +*/ +template +class eoExternalEvalFunc : public eoEvalFunc > +{ + public : + + typedef eoExternalEO ExternalEO; + + eoExternalEvalFunc(F (*_eval)(const External&)) : eval(_eval) {} + + void operator()(ExternalEO& eo) const + { + eo.fitness( (*eval)(eo) ); + } + + private : + + F (*eval)(const External&); +}; + +/** + Mutation of external struct, ctor expects a function of the following + signature: + + void func(External&); + + Where External is the user defined struct or class +*/ + +template +class eoExternalMonOp : public eoMonOp > +{ + public : + + typedef eoExternalEO ExternalEO; + + eoExternalMonOp(void (*_mutate)(External&)) : mutate(_mutate) {} + + void operator()(ExternalEO& eo) const + { + (*mutate)(eo); + eo.invalidate(); + } + + private : + + void (*mutate)(External&); +}; + +/** + Crossover of external struct, ctor expects a function of the following + signature: + + void func(External&, const External&); + + Where External is the user defined struct or class +*/ +template +class eoExternalBinOp : public eoBinOp > +{ + public : + + typedef eoExternalEO ExternalEO; + + eoExternalBinOp(void (*_binop)(External&, const External&)) : binop(_binop) {} + + void operator()(ExternalEO& eo1, const ExternalEO& eo2) const + { + (*binop)(eo1, eo2); + eo1.invalidate(); + } + + private : + + void (*binop)(External&, const External&); +}; + +/** + Crossover of external struct, ctor expects a function of the following + signature: + + void func(External&, External&); + + Where External is the user defined struct or class +*/ +template +class eoExternalQuadraticOp : public eoQuadraticOp > +{ + public : + + typedef eoExternalEO ExternalEO; + + eoExternalQuadraticOp(void (*_quadop)(External&, External&)) : quadop(_quadop) {} + + void operator()(ExternalEO& eo1, ExternalEO& eo2) const + { + (*quadop)(eo1, eo2); + eo1.invalidate(); + eo2.invalidate(); + } + + private : + + void (*quadop)(External&, External&); +}; + + + +#endif diff --git a/eo/src/other/external_eo b/eo/src/other/external_eo new file mode 100644 index 00000000..aeaaaab6 --- /dev/null +++ b/eo/src/other/external_eo @@ -0,0 +1,2 @@ +#include +#include