From 9fba2bfbb9a15dbe86a89b3637ea95d13b9e2ee7 Mon Sep 17 00:00:00 2001 From: mac Date: Wed, 16 Feb 2000 15:05:19 +0000 Subject: [PATCH] Mak: Added the eoQuadratic Op and more ... (and I hate VI) --- eo/src/compatibility.h | 68 ++++++ eo/src/eoBitOp.h | 8 +- eo/src/eoBreeder.h | 216 +++++++++--------- eo/src/eoDetTournament.h | 148 ++++++------- eo/src/eoEvalFuncPtr.h | 118 +++++----- eo/src/eoGOpSelector.h | 310 ++++++++++++++------------ eo/src/eoLottery.h | 194 ++++++++-------- eo/src/eoMultiBinOp.h | 198 ++++++++--------- eo/src/eoObject.h | 137 ++++++------ eo/src/eoOp.h | 438 ++++++++++++++++++++----------------- eo/src/eoStochTournament.h | 146 ++++++------- eo/src/eoUniformXOver.h | 178 +++++++-------- eo/src/eoXOver2.h | 212 +++++++++--------- 13 files changed, 1247 insertions(+), 1124 deletions(-) create mode 100644 eo/src/compatibility.h diff --git a/eo/src/compatibility.h b/eo/src/compatibility.h new file mode 100644 index 00000000..a93aa2e5 --- /dev/null +++ b/eo/src/compatibility.h @@ -0,0 +1,68 @@ +/* -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + + ----------------------------------------------------------------------------- + compatibility.h + File to store some compiler specific stuff in. Currently handles, or + least tries to handle the min() max() problems when using MSVC + + + (c) Maarten Keijzer (mak@dhi.dk) 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 COMPAT_H +#define COMPAT_H + +#ifdef _MSC_VER +/* +Maarten: added this code here because Mirkosoft has the +nasty habit of #define min and max in stdlib.h (and windows.h) +I'm trying to undo this horrible macro magic (microsoft yet macrohard) +here. Sure hope it works +*/ + +#include + +#ifdef min +#undef min +#undef max // as they come in pairs +#endif + +// add min and max to std... +namespace std +{ + template T min(const T& a, const T& b) + { + if(a < b) + return a; + // else + return b; + } + + template T max(const T& a, const T& b) + { + if(a > b) + return a; + // else + return b; + } +} + +#endif // _MSC_VER + +#endif \ No newline at end of file diff --git a/eo/src/eoBitOp.h b/eo/src/eoBitOp.h index cfe3d68d..ca1ee734 100644 --- a/eo/src/eoBitOp.h +++ b/eo/src/eoBitOp.h @@ -184,7 +184,7 @@ template class eoBinPrev: public eoMonOp /** eoBinCrossover --> classic crossover */ -template class eoBinCrossover: public eoBinOp +template class eoBinCrossover: public eoQuadraticOp { public: /// The class name. @@ -205,7 +205,7 @@ template class eoBinCrossover: public eoBinOp /** eoBinNxOver --> n-point crossover */ -template class eoBinNxOver: public eoBinOp +template class eoBinNxOver: public eoQuadraticOp { public: /// (Defualt) Constructor. @@ -263,7 +263,7 @@ template class eoBinNxOver: public eoBinOp /** eoBinGxOver --> gene crossover */ -template class eoBinGxOver: public eoBinOp +template class eoBinGxOver: public eoQuadraticOp { public: /// Constructor. @@ -318,7 +318,7 @@ template class eoBinGxOver: public eoBinOp }; - + //----------------------------------------------------------------------------- //@} #endif eoBitOp_h diff --git a/eo/src/eoBreeder.h b/eo/src/eoBreeder.h index 66eb0aed..e94e815e 100644 --- a/eo/src/eoBreeder.h +++ b/eo/src/eoBreeder.h @@ -1,103 +1,113 @@ -// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - -//----------------------------------------------------------------------------- -// eoBreeder.h -// Takes two populations and mixes them -// (c) GeNeura Team, 1998 -/* - 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 eoBreeder_h -#define eoBreeder_h - -//----------------------------------------------------------------------------- - -#include // vector -#include // eoUniform -#include // eoOp, eoMonOp, eoBinOp -#include // eoPop -#include // eoTransform -#include // eoOpSelector - -using namespace std; - -/***************************************************************************** - * eoBreeder: transforms a population using genetic operators. * - * For every operator there is a rated to be applyed. * - *****************************************************************************/ - -template class eoBreeder: public eoMonPopOp -{ - public: - /// Default constructor. - eoBreeder( eoOpSelector& _opSel): opSel( _opSel ) {} - - /// Destructor. - virtual ~eoBreeder() {} - - /** - * Transforms a population. - * @param pop The population to be transformed. - */ - void operator()(eoPop& pop) - { - for (unsigned i = 0; i < pop.size(); i++) { - eoOp* op = opSel.Op(); - switch (op->readArity()) { - case unary: - { - eoMonOp* monop = static_cast* >(op); - (*monop)( pop[i] ); - break; - } - case binary: - { - eoBinOp* binop = static_cast* >(op); - eoUniform u(0, pop.size() ); - (*binop)(pop[i], pop[ u() ] ); - break; - } - case Nary: - { - eoNaryOp* Nop = static_cast* >(op); - eoUniform u(0, pop.size() ); - eoPop inVec, outVec; - inVec.push_back( pop[i] ); - unsigned numberOfOperands = u(); - for ( unsigned i = 0; i < numberOfOperands; i ++ ) { - inVec.push_back( pop[ u() ] ); - } - (*Nop)( inVec, outVec ); - break; - } - } - } - }; - - /// The class name. - string classname() const { return "eoBreeder"; } - - private: - eoOpSelector& opSel; - -}; - -//----------------------------------------------------------------------------- - -#endif eoBreeder_h +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoBreeder.h +// Takes two populations and mixes them +// (c) GeNeura Team, 1998 +/* + 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 eoBreeder_h +#define eoBreeder_h + +//----------------------------------------------------------------------------- + +#include // vector +#include // eoUniform +#include // eoOp, eoMonOp, eoBinOp +#include // eoPop +#include // eoTransform +#include // eoOpSelector + +#include "eoRandomIndiSelector.h" +#include "eoBackInserter.h" + +using namespace std; + +/***************************************************************************** + * eoBreeder: transforms a population using genetic operators. * + * For every operator there is a rated to be applyed. * + *****************************************************************************/ + +template class eoBreeder: public eoMonPopOp +{ + public: + /// Default constructor. + eoBreeder( eoOpSelector& _opSel): opSel( _opSel ) {} + + /// Destructor. + virtual ~eoBreeder() {} + + /** + * Transforms a population. + * @param pop The population to be transformed. + */ + void operator()(eoPop& pop) + { + size_t orgsize = pop.size(); + + for (unsigned i = 0; i < pop.size(); i++) { + eoOp* op = opSel.Op(); + switch (op->getType()) { + case eoOp::unary: + { + eoMonOp* monop = static_cast* >(op); + (*monop)( pop[i] ); + break; + } + case eoOp::binary: + { + eoBinOp* binop = static_cast* >(op); + eoUniform u(0, pop.size() ); + (*binop)(pop[i], pop[ u() ] ); + break; + } + case eoOp::quadratic: + { + eoQuadraticOp* Qop = static_cast* >(op); + + eoUniform u(0, pop.size() ); + (*Qop)(pop[i], pop[ u() ] ); + break; + } + case eoOp::general : + { + eoGeneralOp* Gop = static_cast* >(op); + + eoRandomIndiSelector selector; + eoBackInserter inserter; + + (*Gop)(selector(pop, orgsize, i), inserter(pop)); + break; + } + } + } + }; + + /// The class name. + string classname() const { return "eoBreeder"; } + + private: + eoOpSelector& opSel; + +}; + +//----------------------------------------------------------------------------- + +#endif eoBreeder_h diff --git a/eo/src/eoDetTournament.h b/eo/src/eoDetTournament.h index a01fc080..e5b1fce1 100644 --- a/eo/src/eoDetTournament.h +++ b/eo/src/eoDetTournament.h @@ -1,78 +1,70 @@ -// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - -//----------------------------------------------------------------------------- -// eoDetTournament.h -// (c) GeNeura Team, 1998 - EEAAX 1999 -/* - 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 - */ -//----------------------------------------------------------------------------- - -#ifndef eoDetTournament_h -#define eoDetTournament_h - -//----------------------------------------------------------------------------- - -#include // -#include // accumulate -#include // eoPop eoSelect MINFLOAT -#include - -//----------------------------------------------------------------------------- -/** eoDetTournament: a selection method that selects ONE individual by - deterministic tournament - -MS- 24/10/99 */ -//----------------------------------------------------------------------------- - -template class eoDetTournament: public eoSelectOne -{ - public: - /// (Default) Constructor. - eoDetTournament(unsigned _Tsize = 2 ):eoSelectOne(), Tsize(_Tsize) { - // consistency check - if (Tsize < 2) { - cout << "Warning, Tournament size shoudl be >= 2\nAdjusted\n"; - Tsize = 2; - } - } - - /** DANGER: if you want to be able to minimize as well as maximizem - DON'T cast the fitness to a float, use the EOT comparator! */ - virtual const EOT& operator()(const eoPop& pop) { - unsigned best = rng.random(pop.size()); // random individual - - for (unsigned i = 0; i // +#include // accumulate +#include // eoPop eoSelect MINFLOAT +#include "selectors.h" + +//----------------------------------------------------------------------------- +/** eoDetTournament: a selection method that selects ONE individual by + deterministic tournament + -MS- 24/10/99 */ +//----------------------------------------------------------------------------- + +template class eoDetTournament: public eoSelectOne +{ + public: + /// (Default) Constructor. + eoDetTournament(unsigned _Tsize = 2 ):eoSelectOne(), Tsize(_Tsize) { + // consistency check + if (Tsize < 2) { + cout << "Warning, Tournament size should be >= 2\nAdjusted\n"; + Tsize = 2; + } + } + + virtual const EOT& operator()(const eoPop& pop) + { + return deterministic_tournament(pop, Tsize)(); + } + + /** Inherited from eoObject + @see eoObject + */ + string className() const {return "eoDetTournament";}; + + private: + unsigned Tsize; +}; + +//----------------------------------------------------------------------------- + +#endif eoDetTournament_h diff --git a/eo/src/eoEvalFuncPtr.h b/eo/src/eoEvalFuncPtr.h index 1c40eecf..d6d387d6 100644 --- a/eo/src/eoEvalFuncPtr.h +++ b/eo/src/eoEvalFuncPtr.h @@ -1,59 +1,59 @@ -/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - ------------------------------------------------------------------------------ - eoEvalFuncPtr.h - Converts a classical C fitness evaluation function into a fitness - evaluation object - - (c) 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 - 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 EOEVALFUNCPTR_H -#define EOEVALFUNCPTR_H - -#include - -/** EOEvalFuncPtr: This class - * takes an existing function pointer and converts it into a evaluation - * function class. That way, old style C or C++ functions can be adapted to EO - * function classes. - */ -template< class EOT > -struct eoEvalFuncPtr: public eoEvalFunc { - - /** Applies the function to the chromosome and sets the fitness of the - Chrom. Thus, the evaluation function need not be worried about that. - @param _eval pointer to the evaluation function, takes a EOT as an - argument and returns the fitness - @return the evaluated fitness for that object. - */ - eoEvalFuncPtr( EOFitT (* _eval)( const EOT& ) ) - : eoEvalFunc(), evalFunc( _eval ) {}; - - /// Effectively applies the evaluation function to an EO - virtual void operator() ( EOT & _eo ) const { - _eo.fitness((*evalFunc)( _eo )); - }; - - private: - EOFitT (* evalFunc )( EOT& ); -}; - -#endif +/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +----------------------------------------------------------------------------- + eoEvalFuncPtr.h + Converts a classical C fitness evaluation function into a fitness + evaluation object + + (c) 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 + 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 EOEVALFUNCPTR_H +#define EOEVALFUNCPTR_H + +#include + +/** EOEvalFuncPtr: This class + * takes an existing function pointer and converts it into a evaluation + * function class. That way, old style C or C++ functions can be adapted to EO + * function classes. + */ +template< class EOT > +struct eoEvalFuncPtr: public eoEvalFunc { + + /** Applies the function to the chromosome and sets the fitness of the + Chrom. Thus, the evaluation function need not be worried about that. + @param _eval pointer to the evaluation function, takes a EOT as an + argument and returns the fitness + @return the evaluated fitness for that object. + */ + eoEvalFuncPtr( EOFitT (* _eval)( const EOT& ) ) + : eoEvalFunc(), evalFunc( _eval ) {}; + + /// Effectively applies the evaluation function to an EO + virtual void operator() ( EOT & _eo ) const { + _eo.fitness((*evalFunc)( _eo )); + }; + + private: + EOFitT (* evalFunc )( const EOT& ); +}; + +#endif diff --git a/eo/src/eoGOpSelector.h b/eo/src/eoGOpSelector.h index f33b6bb1..75e5e0ca 100644 --- a/eo/src/eoGOpSelector.h +++ b/eo/src/eoGOpSelector.h @@ -1,139 +1,171 @@ -/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - - ----------------------------------------------------------------------------- - eoGOpSelector.h - Base class for generalized (n-inputs, n-outputs) operator selectors. - Includes code and variables that contain operators and rates - - (c) Maarten Keijzer, GeNeura Team 1998, 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 eoGOpSelector_h -#define eoGOpSelector_h - -//----------------------------------------------------------------------------- - -#include // vector -#include -#include // eoUniform -#include // eoOp, eoMonOp, eoBinOp -#include // eoPop -#include // eoTransform -#include // eoOpSelector -#include -#include "eoRNG.h" - -using namespace std; - -/** Base class for alternative selectors, which use the generalized operator - interface */ - -template -class eoGOpSelector: public eoOpSelector, public vector*> -{ -public: - - /// Dtor - virtual ~eoGOpSelector() { - for ( list< eoGeneralOp* >::iterator i= ownOpList.begin(); - i != ownOpList.begin(); i ++ ) { - delete *i; - } - } - - /// Add any kind of operator to the operator mix, with an argument - virtual ID addOp( eoOp& _op, float _arg ) { - eoGeneralOp* op = dynamic_cast*>(&_op); - - // if it's not a general op, it's a "old" op; create a wrapped op from it - // and keep it on a list to delete them afterwards - // will use auto_ptr when they're readily available - if (op == 0) { - switch(_op.readArity()) - { - case unary : - op= new eoWrappedMonOp(static_cast&>(_op)); - break; - case binary : - op = new eoWrappedBinOp(static_cast&>(_op)); - break; - } - ownOpList.push_back( op ); - } - - iterator result = find(begin(), end(), (eoGeneralOp*) 0); // search for nullpointer - - if (result == end()) - { - push_back(op); - rates.push_back(_arg); - return size(); - } - // else - - *result = op; - ID id = result - begin(); - rates[id] = _arg; - return id; - } - - /** Retrieve the operator using its integer handle - @param _id The id number. Should be a valid id, or an exception - will be thrown - @return a reference of the operator corresponding to that id. - */ - virtual const eoOp& getOp( ID _id ) - { - return *operator[](_id); - } - - /// - virtual void deleteOp( ID _id ) - { - operator[](_id) = 0; // TODO, check oplist and clear it there too. - rates[_id] = 0.0; - } - - /// - virtual eoOp* Op() - { - return &selectOp(); - } - - /// - virtual eoGeneralOp& selectOp() = 0; - - /// - virtual string className() const { return "eoGOpSelector"; }; - - /// - void printOn(ostream& _os) const { - _os << className() << endl; - for ( unsigned i=0; i!= rates.size(); i++ ) { - _os << *(operator[](i)) << "\t" << rates[i] << endl; - } - } - - -protected : - vector rates; - list< eoGeneralOp* > ownOpList; -}; - -#endif eoGOpSelector_h +/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + + ----------------------------------------------------------------------------- + eoGOpSelector.h + Base class for generalized (n-inputs, n-outputs) operator selectors. + Includes code and variables that contain operators and rates. + Also included eoProportionalGOpSelector and eoSequentialGOpSelector, that offer + a few concrete implementations. + + (c) Maarten Keijzer, GeNeura Team 1998, 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 eoGOpSelector_h +#define eoGOpSelector_h + +//----------------------------------------------------------------------------- + +#include +#include "eoOpSelector.h" +#include "eoWrappedOps.h" // for eoCombinedOp +#include "eoRNG.h" + +using namespace std; + +/** Base class for alternative selectors, which use the generalized operator + interface. eoGOpBreeders expects this class */ +template +class eoGOpSelector: public eoOpSelector, public vector*> +{ +public: + + /// Dtor + virtual ~eoGOpSelector() { + for ( list< eoGeneralOp* >::iterator i= ownOpList.begin(); + i != ownOpList.end(); i++ ) { + delete *i; + } + } + + /// Add any kind of operator to the operator mix, with an argument + virtual ID addOp( eoOp& _op, float _arg ); + // implementation can be found below + + /** Retrieve the operator using its integer handle + @param _id The id number. Should be a valid id, or an exception + will be thrown + @return a reference of the operator corresponding to that id. + */ + virtual const eoOp& getOp( ID _id ) + { + return *operator[](_id); + } + + /// + virtual void deleteOp( ID _id ); + // implemented below + + /// + virtual eoOp* Op() + { + return &selectOp(); + } + + /// + virtual eoGeneralOp& selectOp() = 0; + + /// + virtual string className() const { return "eoGOpSelector"; }; + + /// + void printOn(ostream& _os) const { + _os << className() << endl; + for ( unsigned i=0; i!= rates.size(); i++ ) { + _os << *(operator[](i)) << "\t" << rates[i] << endl; + } + } + + + const vector& getRates(void) const { return rates; } + +private : + vector rates; + list< eoGeneralOp* > ownOpList; +}; + +/* Implementation of longish functions defined above */ + +template +inline eoOpSelector::ID eoGOpSelector::addOp( eoOp& _op, float _arg ) +{ + + eoGeneralOp* op; + + if (_op.getType() == eoOp::general) + { + op = static_cast*>(&_op); + } + else + { + // if it's not a general op, it's a "old" op; create a wrapped op from it + // and keep it on a list to delete them afterwards + // will use auto_ptr when they're readily available + + switch(_op.getType()) + { + case eoOp::unary : + op= new eoWrappedMonOp(static_cast&>(_op)); + break; + case eoOp::binary : + op = new eoWrappedBinOp(static_cast&>(_op)); + case eoOp::quadratic : + op = new eoWrappedQuadraticOp(static_cast&>(_op)); + break; + } + ownOpList.push_back( op ); + } + + // Now 'op' is a general operator, either because '_op' was one or + // because we wrapped it in an appropriate wrapper in the code above. + + iterator result = find(begin(), end(), (eoGeneralOp*) 0); // search for nullpointer + + if (result == end()) + { + push_back(op); + rates.push_back(_arg); + return size(); + } + // else + + *result = op; + ID id = result - begin(); + rates[id] = _arg; + return id; +} + +template +inline void eoGOpSelector::deleteOp( ID _id ) +{ + eoGeneralOp* op = operator[](_id); + + operator[](_id) = 0; + rates[_id] = 0.0; + + // check oplist and clear it there too. + + list< eoGeneralOp* >::iterator it = find(ownOpList.begin(), ownOpList.end(), op); + + if(it != ownOpList.end()) + { + ownOpList.erase(it); + } +} + +#endif eoGOpSelector_h diff --git a/eo/src/eoLottery.h b/eo/src/eoLottery.h index 40c56962..333ee792 100644 --- a/eo/src/eoLottery.h +++ b/eo/src/eoLottery.h @@ -1,97 +1,97 @@ -// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - -//----------------------------------------------------------------------------- -// eoLottery.h -// Implements the lottery procedure for selection -// (c) GeNeura Team, 1998 - Marc Schoenauer, 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 eoLottery_h -#define eoLottery_h - -//----------------------------------------------------------------------------- - -#include // -#include // accumulate -#include "eoPopOps.h" -#include "eoRNG.h" - -//----------------------------------------------------------------------------- -/// eoLottery: a selection method. -/// requires EOT::Fitness to be float castable -//----------------------------------------------------------------------------- - -template class eoLottery: public eoBinPopOp -{ - public: - /// (Default) Constructor. - eoLottery(const double & _rate = 1.0): rate(_rate) {} - - /** actually selects individuals from pop and put them into breeders - * until breeders has the right size: rate*pop.size() - * BUT what happens if breeders is already too big? - */ - void operator()( eoPop& pop, eoPop& breeders) - { - // scores of chromosomes - vector score(pop.size()); - - // calculates total scores for chromosomes - double total = 0; - for (unsigned i = 0; i < pop.size(); i++) { - score[i] = static_cast(pop[i].fitness()); - total += score[i]; - } - - // number of offspring needed - int target = (int)rint(rate * pop.size()); - - // test of consistency - if (breeders.size() >= target) { - throw("Problem in eoLottery: already too many offspring"); - } - - // selection of chromosomes - while (breeders.size() < target) { - unsigned indloc = rng.roulette_wheel(score, total); - breeders.push_back(pop[indloc]); - } - } - - /// accessor to private rate - double Rate() {return rate;} - - /** @name Methods from eoObject */ - //@{ - /** readFrom and printOn inherited from eoMerge */ - - /** Inherited from eoObject. Returns the class name. - @see eoObject - */ - virtual string className() const {return "eoLottery";}; - //@} - - private: - double rate; // selection rate -}; - -//----------------------------------------------------------------------------- - -#endif eoLottery_h +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoLottery.h +// Implements the lottery procedure for selection +// (c) GeNeura Team, 1998 - Marc Schoenauer, 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 eoLottery_h +#define eoLottery_h + +//----------------------------------------------------------------------------- + +#include // +#include // accumulate +#include "selectors.h" +#include // eoPop eoSelect MINFLOAT + +//----------------------------------------------------------------------------- +/** eoLottery: a selection method. Puts into the output a group of individuals + selected using lottery; individuals with higher probability will have more + chances of being selected. + Requires EOT::Fitness to be float castable +*/ +//----------------------------------------------------------------------------- + +template class eoLottery: public eoBinPopOp +{ + public: + /// (Default) Constructor. + eoLottery(const float& _rate = 1.0): eoBinPopOp(), rate(_rate) + { + if (minimizing_fitness()) + { + eoMinimizingFitnessException up(*this); + throw up; // :-) + } + } + + /** actually selects individuals from pop and pushes them back them into breeders + * until breeders has the right size: rate*pop.size() + * BUT what happens if breeders is already too big? + * Too big for what? + */ + void operator()( eoPop& pop, eoPop& breeders) + { + int target = (int)(rate * pop.size()); + + // test of consistency + if (breeders.size() >= target) { + throw("Problem in eoLottery: already too many offspring"); + } + + double total; + + try + { + total = sum_fitness(pop); + } + catch (eoNegativeFitnessException&) + { // say where it occured... + throw eoNegativeFitnessException(*this); + } + + // selection of chromosomes + while (breeders.size() < target) + { + breeders.push_back(roulette_wheel(pop, total)); + } + } + + double Rate(void) const { return rate; } + + private: + double rate; // selection rate +}; + +//----------------------------------------------------------------------------- + +#endif eoLottery_h diff --git a/eo/src/eoMultiBinOp.h b/eo/src/eoMultiBinOp.h index ddb30c5d..231655b3 100644 --- a/eo/src/eoMultiBinOp.h +++ b/eo/src/eoMultiBinOp.h @@ -1,99 +1,99 @@ -// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - -//----------------------------------------------------------------------------- -// eoMultiBinOp.h -// Class that combines several binary or unary operators -// (c) GeNeura Team, 1998 -/* - 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 _EOMULTIBINOP_h -#define _EOMULTIBINOP_h - -#include - -#include - -/** MultiMonOp combines several monary operators. By itself, it does nothing to the -EO itīs handled*/ -template -class eoMultiBinOp: public eoBinOp { -public: - /// Ctor from an already existing op - eoMultiBinOp( const eoBinOp* _op ) - : eoBinOp< EOT >( ), vOp(){ - vOp.push_back( _op ); - }; - - /// - eoMultiBinOp( ) - : eoBinOp< EOT >( ), vOp(){}; - - /// Ads a new operator - void adOp( const eoOp* _op ){ - vOp.push_back( _op ); - }; - - /// needed virtual dtor - virtual ~eoMultiBinOp() {}; - - /// - /// Applies all operators to the EO - virtual void operator()( EOT& _eo1, EOT& _eo2 ) const { - if ( vOp.begin() != vOp.end() ) { // which would mean it's empty - for ( vector< const eoOp* >::const_iterator i = vOp.begin(); - i != vOp.end(); i++ ) { - // Admits only unary or binary operator - switch ((*i)->readArity()) { - case unary: - { - const eoMonOp* monop = static_cast* >(*i); - (*monop)( _eo1 ); - (*monop)( _eo2 ); - break; - } - case binary: - { - const eoBinOp* binop = static_cast* >(*i); - (*binop)( _eo1, _eo2 ); - break; - } - } - } - } - } - - - /** @name Methods from eoObject - readFrom and printOn are directly inherited from eoOp - */ - //@{ - /** Inherited from eoObject - @see eoObject - */ - string className() const {return "eoMultiBinOp";}; - //@} - -private: - - /// uses pointers to base class since operators can be unary or binary - vector< const eoOp* > vOp; -}; - -#endif +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoMultiBinOp.h +// Class that combines several binary or unary operators +// (c) GeNeura Team, 1998 +/* + 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 _EOMULTIBINOP_h +#define _EOMULTIBINOP_h + +#include + +#include + +/** MultiMonOp combines several monary operators. By itself, it does nothing to the +EO itīs handled*/ +template +class eoMultiBinOp: public eoBinOp { +public: + /// Ctor from an already existing op + eoMultiBinOp( const eoBinOp* _op ) + : eoBinOp< EOT >( ), vOp(){ + vOp.push_back( _op ); + }; + + /// + eoMultiBinOp( ) + : eoBinOp< EOT >( ), vOp(){}; + + /// Ads a new operator + void adOp( const eoOp* _op ){ + vOp.push_back( _op ); + }; + + /// needed virtual dtor + virtual ~eoMultiBinOp() {}; + + /// + /// Applies all operators to the EO + virtual void operator()( EOT& _eo1, EOT& _eo2 ) const { + if ( vOp.begin() != vOp.end() ) { // which would mean it's empty + for ( vector< const eoOp* >::const_iterator i = vOp.begin(); + i != vOp.end(); i++ ) { + // Admits only unary or binary operator + switch ((*i)->readArity()) { + case unary: + { + const eoMonOp* monop = static_cast* >(*i); + (*monop)( _eo1 ); + (*monop)( _eo2 ); + break; + } + case binary: + { + const eoBinOp* binop = static_cast* >(*i); + (*binop)( _eo1, _eo2 ); + break; + } + } + } + } + } + + + /** @name Methods from eoObject + readFrom and printOn are directly inherited from eoOp + */ + //@{ + /** Inherited from eoObject + @see eoObject + */ + string className() const {return "eoMultiBinOp";}; + //@} + +private: + + /// uses pointers to base class since operators can be unary or binary + vector< const eoOp* > vOp; +}; + +#endif diff --git a/eo/src/eoObject.h b/eo/src/eoObject.h index e1b2d617..0e2e9e68 100644 --- a/eo/src/eoObject.h +++ b/eo/src/eoObject.h @@ -1,69 +1,68 @@ -/** -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - - ----------------------------------------------------------------------------- - eoObject.h - This is the base class for most objects in EO. It basically defines an interf - face for giving names to classes. - - (c) GeNeura Team, 1998, 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 EOOBJECT_H -#define EOOBJECT_H - -//----------------------------------------------------------------------------- - -#include // For limits definition -#include // istream, ostream -#include // string - -using namespace std; - -//----------------------------------------------------------------------------- -// eoObject -//----------------------------------------------------------------------------- -/** -This is the base class for the whole hierarchy; an eoObject defines -basically an interface for the whole hierarchy: each object should -know its name (#className#). Previously, this object defined a print and read -interface, but itīs been moved to eoPrintable and eoPersistent. - */ -class eoObject -{ - public: - - /// Default Constructor. - eoObject() {} - - /// Copy constructor. - eoObject( const eoObject& ) {} - - /// Virtual dtor. They are needed in virtual class hierarchies. - virtual ~eoObject() {} - - /** Return the class id. This should be redefined in each class; but - it's got code as an example of implementation. Only "leaf" classes - can be non-virtual. - */ - virtual string className() const { return "eoObject"; } - -}; - -#endif EOOBJECT_H +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoObject.h +// (c) GeNeura Team, 1998 +/* + 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 EOOBJECT_H +#define EOOBJECT_H + +//----------------------------------------------------------------------------- + +#include // For limits definition +#include // istream, ostream +#include // string + +#include "compatibility.h" + +using namespace std; + +//----------------------------------------------------------------------------- +// eoObject +//----------------------------------------------------------------------------- +/** +This is the base class for the whole hierarchy; an eoObject defines +basically an interface for the whole hierarchy: each object should +know its name (#className#). Previously, this object defined a print and read +interface, but itīs been moved to eoPrintable and eoPersistent. + */ +class eoObject +{ + public: + + /// Default Constructor. + eoObject() {} + + /// Copy constructor. + eoObject( const eoObject& ) {} + + /// Virtual dtor. They are needed in virtual class hierarchies. + virtual ~eoObject() {} + + /** Return the class id. This should be redefined in each class; but + it's got code as an example of implementation. Only "leaf" classes + can be non-virtual. + */ + virtual string className() const { return "eoObject"; } + +}; + +#endif EOOBJECT_H diff --git a/eo/src/eoOp.h b/eo/src/eoOp.h index 609cf8c3..178ae4e1 100644 --- a/eo/src/eoOp.h +++ b/eo/src/eoOp.h @@ -1,203 +1,235 @@ -// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - -//----------------------------------------------------------------------------- -// eoOp.h -// (c) GeNeura Team, 1998 -/* - 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 _eoOp_H -#define _eoOp_H - -#include -#include -#include - -/** @name Genetic operators - -What is a genetic algorithm without genetic operators? There is a genetic operator hierarchy, with -eoOp as father and eoMonOp (monary or unary operator) and eoBinOp (binary operator) as siblings. Nobody -should subclass eoOp, you should subclass eoBinOp or eoMonOp, those are the ones actually used here.\\ -#eoOp#s are only printable objects, so if you want to build them from a file, it has to -be done in another class, namely factories. Each hierarchy of #eoOp#s should have its own -factory, which know how to build them from a description in a file. -@author GeNeura Team -@version 0.1 -@see eoOpFactory -*/ -//@{ - -/// -enum Arity { unary = 0, binary = 1, Nary = 2}; - -/** Abstract data types for EO operators. - * Genetic operators act on chromosomes, changing them. The type to instantiate them should - * be an eoObject, but in any case, they are type-specific; each kind of evolvable object - * can have its own operators - */ -template -class eoOp: public eoObject, public eoPrintable { -public: - - /// Ctor - eoOp( Arity _arity = unary ) - :arity( _arity ) {}; - - /// Copy Ctor - eoOp( const eoOp& _eop ) - :arity( _eop.arity ) {}; - - /// Needed virtual destructor - virtual ~eoOp(){}; - - /// Arity: number of operands - Arity readArity() const {return arity;}; - - /** @name Methods from eoObject */ - //@{ - - /** - * Write object. It's called printOn since it prints the object _on_ a stream. - * @param _os A ostream. - */ - virtual void printOn(ostream& _os) const { - _os << className(); - // _os << arity; - }; - - /** Inherited from eoObject - @see eoObject - */ - virtual string className() const {return "eoOp";}; - //@} - -private: - /// arity is the number of operands it takes - Arity arity; - -}; - -/** Binary genetic operator: subclasses eoOp, and defines -basically the operator() with two operands -*/ -template -class eoBinOp: public eoOp { -public: - - /// Ctor - eoBinOp() - :eoOp( binary ) {}; - - /// Copy Ctor - eoBinOp( const eoBinOp& _ebop ) - : eoOp( _ebop ){}; - - /// Dtor - ~eoBinOp () {}; - - /** applies operator, to the object. Modifies both operands. - */ - virtual void operator()( EOType& _eo1, EOType& _eo2 ) const = 0; - - /** @name Methods from eoObject - readFrom and printOn are directly inherited from eoObject - */ - //@{ - /** Inherited from eoObject - @see eoObject - */ - virtual string className() const {return "eoBinOp";}; - //@} - -}; - -/** eoMonOp is the monary operator: genetic operator that takes - only one EO -*/ -template -class eoMonOp: public eoOp { -public: - - /// Ctor - eoMonOp( ) - :eoOp( unary ) {}; - - /// Copy Ctor - eoMonOp( const eoMonOp& _emop ) - : eoOp( _emop ){}; - - /// Dtor - ~eoMonOp() {}; - - /** applies randomly operator, to the object. If arity is more than 1, - * keeps a copy of the operand in a cache. - */ - virtual void operator()( EOType& _eo1) const = 0; - - /** @name Methods from eoObject - readFrom and printOn are directly inherited from eoObject - */ - //@{ - /** Inherited from eoObject - @see eoObject - */ - virtual string className() const {return "eoMonOp";}; - //@} - - -}; - -#include -/** eoNaryOp is the N-ary operator: genetic operator that takes - several EOs. It could be called an {\em orgy} operator. It's a general operator - that takes any number of inputs and spits out any number of outputs -*/ -template -class eoNaryOp: public eoOp { -public: - - /// Ctor - eoNaryOp( ) - :eoOp( Nary ) {}; - - /// Copy Ctor - eoNaryOp( const eoNaryOp& _emop ) - : eoOp( _emop ){}; - - /// Dtor - ~eoNaryOp() {}; - - /** applies randomly operator, to the object. - */ - virtual void operator()( const eoPop & _in, eoPop _out ) const = 0; - - /** @name Methods from eoObject - readFrom and printOn are directly inherited from eoObject. - */ - //@{ - /** Inherited from eoObject - @see eoObject - */ - string className() const {return "eoNaryOp";}; - //@} - -}; -//@} - -#endif +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoOp.h +// (c) GeNeura Team, 1998 +/* + 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 _eoOp_H +#define _eoOp_H + +#include +#include + +/** @name Genetic operators + +What is a genetic algorithm without genetic operators? There is a genetic operator hierarchy, with +eoOp as father and eoMonOp (monary or unary operator) and eoBinOp and eoQuadraticOp (binary operators) +as siblings). Nobody should subclass eoOp, you should subclass eoGeneralOp, eoBinOp, eoQuadraticOp or eoMonOp, +those are the ones actually used here.\\ + +#eoOp#s are only printable objects, so if you want to build them from a file, it has to +be done in another class, namely factories. Each hierarchy of #eoOp#s should have its own +factory, which know how to build them from a description in a file. +@author GeNeura Team +@version 0.1 +@see eoOpFactory +*/ + + +/** Abstract data types for EO operators. + * Genetic operators act on chromosomes, changing them. The type to instantiate them should + * be an eoObject, but in any case, they are type-specific; each kind of evolvable object + * can have its own operators + */ +template +class eoOp: public eoObject, public eoPrintable { +public: + +//@{ + enum OpType { unary = 0, binary = 1, quadratic = 2, general = 3}; +/// + + /// Ctor + eoOp(OpType _type) + :opType( _type ) {}; + + /// Copy Ctor + eoOp( const eoOp& _eop ) + :opType( _eop.opType ) {}; + + /// Needed virtual destructor + virtual ~eoOp(){}; + + /// getType: number of operands it takes and individuals it produces + OpType getType() const {return opType;}; + + /** @name Methods from eoObject */ + //@{ + + /** + * Write object. It's called printOn since it prints the object _on_ a stream. + * @param _os A ostream. + */ + virtual void printOn(ostream& _os) const { + _os << className(); + // _os << arity; + }; + + /** Inherited from eoObject + @see eoObject + */ + virtual string className() const {return "eoOp";}; + //@} + +private: + /// OpType is the type of the operator: how many operands it takes and how many it produces + OpType opType; + +}; + +/** Binary genetic operator: subclasses eoOp, and defines +basically the operator() with two operands +*/ +template +class eoBinOp: public eoOp { +public: + + /// Ctor + eoBinOp() + :eoOp( binary ) {}; + + /// Copy Ctor + eoBinOp( const eoBinOp& _ebop ) + : eoOp( _ebop ){}; + + /// Dtor + ~eoBinOp () {}; + + /** applies operator, to the object. Modifies only the first operand. + */ + virtual void operator()( EOType& _eo1, const EOType& _eo2 ) const = 0; + + /** @name Methods from eoObject + readFrom and printOn are directly inherited from eoObject + */ + //@{ + /** Inherited from eoObject + @see eoObject + */ + virtual string className() const {return "eoBinOp";}; + //@} + +}; + +/** Quadratic genetic operator: subclasses eoOp, and defines +basically the operator() with two operands +*/ +template +class eoQuadraticOp: public eoOp { +public: + + /// Ctor + eoQuadraticOp() + :eoOp( eoOp::quadratic ) {}; + + /// Copy Ctor + eoQuadraticOp( const eoQuadraticOp& _ebop ) + : eoOp( _ebop ){}; + + /// Dtor + ~eoQuadraticOp() {}; + + /** applies operator, to the object. Modifies both operands. + */ + virtual void operator()( EOType& _eo1, EOType& _eo2 ) const = 0; + + /** @name Methods from eoObject + readFrom and printOn are directly inherited from eoObject + */ + //@{ + /** Inherited from eoObject + @see eoObject + */ + virtual string className() const {return "eoBinOp";}; + //@} + +}; + +/** eoMonOp is the monary operator: genetic operator that takes + only one EO +*/ +template +class eoMonOp: public eoOp { +public: + + /// Ctor + eoMonOp( ) + : eoOp( eoOp::unary ) {}; + + /// Copy Ctor + eoMonOp( const eoMonOp& _emop ) + : eoOp( _emop ){}; + + /// Dtor + ~eoMonOp() {}; + + /** applies randomly operator, to the object. If arity is more than 1, + * keeps a copy of the operand in a cache. + */ + virtual void operator()( EOType& _eo1) const = 0; + + /** @name Methods from eoObject + readFrom and printOn are directly inherited from eoObject + */ + //@{ + /** Inherited from eoObject + @see eoObject + */ + virtual string className() const {return "eoMonOp";}; + //@} +}; + +// some forward declarations +template +class eoIndiSelector; + +template +class eoInserter; + +/** + * eGeneralOp: General genetic operator; for objects used to transform sets + of EOs. Nary ("orgy") operators should be derived from this class +*/ +template +class eoGeneralOp: public eoOp +{ +public: + + /// Ctor that honors its superclass + eoGeneralOp(): eoOp( eoOp::general ) {}; + + /// Virtual dtor + virtual ~eoGeneralOp () {}; + + /** Method that really does the stuff. Applies the genetic operator + to a individuals dispensed by an eoIndividualSelector, + and puts the results in the eoIndividualInserter. + Any number of inputs can be requested and any number of outputs + can be produced. + */ + virtual void operator()( eoIndiSelector& _in, + eoInserter& _out) const = 0; + + virtual string className() const {return "eoGeneralOp";}; +}; + + +#endif diff --git a/eo/src/eoStochTournament.h b/eo/src/eoStochTournament.h index ad82e460..afc05d74 100644 --- a/eo/src/eoStochTournament.h +++ b/eo/src/eoStochTournament.h @@ -1,78 +1,68 @@ -// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - -//----------------------------------------------------------------------------- -// eoStochTournament.h -// (c) GeNeura Team, 1998 - EEAAX 1999 -/* - 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 - */ -//----------------------------------------------------------------------------- - -#ifndef eoStochTournament_h -#define eoStochTournament_h - -//----------------------------------------------------------------------------- - -#include // -#include // accumulate -#include // eoPop eoSelect MINFLOAT -#include - -//----------------------------------------------------------------------------- -/** eoStochTournament: a selection method that selects ONE individual by - binary stochastic tournament - -MS- 24/10/99 */ -//----------------------------------------------------------------------------- - -template class eoStochTournament: public eoSelectOne -{ - public: - - /// - eoStochTournament(float _Trate = 1.0 ):eoSelectOne(), Trate(_Trate) { - // consistency check - if (Trate < 0.5) { - cerr << "Warning, Tournament rate should be > 0.5\nAdjusted to 0.55\n"; - Trate = 0.55; - } - } - - /** DANGER: if you want to be able to minimize as well as maximizem - DON'T cast the fitness to a float, use the EOT comparator! */ - virtual const EOT& operator()(const eoPop& pop) { - unsigned i1 = rng.random(pop.size()), - i2 = rng.random(pop.size()); - - bool ok = ( rng.flip(Trate) ); - if (pop[i1] < pop[ i2 ] ) { - if (ok) return pop[ i2 ]; - else return pop[ i1 ]; - } - else { - if (ok) return pop[ i1 ]; - else return pop[ i2 ]; - } - } - -private: - float Trate; -}; - -//----------------------------------------------------------------------------- - -#endif eoDetTournament_h +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoStochTournament.h +// (c) GeNeura Team, 1998 - EEAAX 1999 +/* + 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 + */ +//----------------------------------------------------------------------------- + +#ifndef eoStochTournament_h +#define eoStochTournament_h + +//----------------------------------------------------------------------------- + +#include // +#include // accumulate +#include // eoPop eoSelect MINFLOAT +#include + +//----------------------------------------------------------------------------- +/** eoStochTournament: a selection method that selects ONE individual by + binary stochastic tournament + -MS- 24/10/99 */ +//----------------------------------------------------------------------------- + +template class eoStochTournament: public eoSelectOne +{ + public: + + /// + eoStochTournament(float _Trate = 1.0 ):eoSelectOne(), Trate(_Trate) { + // consistency check + if (Trate < 0.5) { + cerr << "Warning, Tournament rate should be > 0.5\nAdjusted to 0.55\n"; + Trate = 0.55; + } + } + + /** DANGER: if you want to be able to minimize as well as maximizem + DON'T cast the fitness to a float, use the EOT comparator! */ + virtual const EOT& operator()(const eoPop& pop) + { + return stochastic_tournament(pop, Trate)(); + } + +private: + float Trate; +}; + +//----------------------------------------------------------------------------- + +#endif eoDetTournament_h diff --git a/eo/src/eoUniformXOver.h b/eo/src/eoUniformXOver.h index cf4271c1..a2dfa5e2 100644 --- a/eo/src/eoUniformXOver.h +++ b/eo/src/eoUniformXOver.h @@ -1,90 +1,90 @@ -// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - -//----------------------------------------------------------------------------- -// eoUniformXOver.h -// (c) GeNeura Team, 1998 -/* - 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 _EOUNIFORMXOVER_h -#define _EOUNIFORMXOVER_h - - -// for swap -#if defined( __BORLANDC__ ) -#include -#else -#include -#endif - -// EO includes -#include -#include - -//----------------------------------------------------------------------------- -/** - * EOUniformCrossover: operator for binary chromosomes - * implementation of uniform crossover for EO - * swaps ranges of bits between the parents - */ -//----------------------------------------------------------------------------- - -template -class eoUniformXOver: public eoBinOp< EOT > -{ - public: - - /// - eoUniformXOver( float _rate = 0.5 ): - eoBinOp< EOT > ( ), rate( _rate ) { +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoUniformXOver.h +// (c) GeNeura Team, 1998 +/* + 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 _EOUNIFORMXOVER_h +#define _EOUNIFORMXOVER_h + + +// for swap +#if defined( __BORLANDC__ ) +#include +#else +#include +#endif + +// EO includes +#include +#include + +//----------------------------------------------------------------------------- +/** + * EOUniformCrossover: operator for binary chromosomes + * implementation of uniform crossover for EO + * swaps ranges of bits between the parents + */ +//----------------------------------------------------------------------------- + +template +class eoUniformXOver: public eoQuadraticOp< EOT > +{ + public: + + /// + eoUniformXOver( float _rate = 0.5 ): + eoQuadraticOp< EOT > ( ), rate( _rate ) { if (rate < 0 || rate > 1) - runtime_error("UxOver --> invalid rate"); - } - - - /// - void operator() ( EOT& chrom1, EOT& chrom2 ) const { - unsigned end = min(chrom1.length(),chrom2.length()) - 1; - // select bits to change - eoUniform rnd(0, 1); - - // aply changes - for (unsigned bit = 0; bit < end; bit++) - if (rnd() < rate) - swap(chrom1[ bit], chrom2[ bit]); - } - - /** @name Methods from eoObject - readFrom and printOn are directly inherited from eoOp - */ - //@{ - /** Inherited from eoObject - @see eoObject - */ - string className() const {return "eoUniformXOver";}; - //@} - -private: - float rate; /// rate of uniform crossover -}; - -//----------------------------------------------------------------------------- - - -#endif + runtime_error("UxOver --> invalid rate"); + } + + + /// + void operator() ( EOT& chrom1, EOT& chrom2 ) const { + unsigned end = min(chrom1.length(),chrom2.length()) - 1; + // select bits to change + eoUniform rnd(0, 1); + + // aply changes + for (unsigned bit = 0; bit < end; bit++) + if (rnd() < rate) + swap(chrom1[ bit], chrom2[ bit]); + } + + /** @name Methods from eoObject + readFrom and printOn are directly inherited from eoOp + */ + //@{ + /** Inherited from eoObject + @see eoObject + */ + string className() const {return "eoUniformXOver";}; + //@} + +private: + float rate; /// rate of uniform crossover +}; + +//----------------------------------------------------------------------------- + + +#endif diff --git a/eo/src/eoXOver2.h b/eo/src/eoXOver2.h index ca78bc20..ec67c399 100644 --- a/eo/src/eoXOver2.h +++ b/eo/src/eoXOver2.h @@ -1,106 +1,106 @@ -// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- - -//----------------------------------------------------------------------------- -// eoXOver2.h -// (c) GeNeura Team, 1998 -/* - 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 _EOXOVER2_h -#define _EOXOVER2_h - - -// for swap -#if defined( __BORLANDC__ ) -#include -#else -#include -#endif - -// EO includes -#include -#include - -/** 2-point crossover: takes the genes in the central section of two EOs -and interchanges it -*/ -template -class eoXOver2: public eoBinOp { -public: - /// - eoXOver2() - : eoBinOp< EOT >(){}; - - /// - virtual ~eoXOver2() {}; - - /// - virtual void operator()( EOT& _eo1, - EOT& _eo2 ) const { - unsigned len1 = _eo1.length(), len2 = _eo2.length(), - len= (len1 > len2)?len2:len1; - eoUniform uniform( 0, len ); - unsigned pos1 = uniform(), pos2 = uniform() ; - - applyAt( _eo1, _eo2, pos1, pos2 ); - - } - - /** @name Methods from eoObject - readFrom and printOn are directly inherited from eoOp - */ - //@{ - /** Inherited from eoObject - @see eoObject - */ - string className() const {return "eoXOver2";}; - //@} - -private: - -#ifdef _MSC_VER - typedef EOT::Type Type; -#else - typedef typename EOT::Type Type; -#endif - - /// applies operator to one gene in the EO - virtual void applyAt( EOT& _eo, EOT& _eo2, - unsigned _i, unsigned _j = 0) const { - - if ( _j < _i ) - swap( _i, _j ); - - unsigned len1 = _eo.length(), len2 = _eo2.length(), - len= (len1 > len2)?len2:len1; - - if ( (_j > len) || (_i> len ) ) - throw runtime_error( "xOver2: applying xOver past boundaries"); - - for ( unsigned i = _i; i < _j; i++ ) { - Type tmp = _eo.gene( i ); - _eo.gene( i ) = _eo2.gene( i ); - _eo2.gene( i ) = tmp ; - } - - } - -}; - -#endif +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoXOver2.h +// (c) GeNeura Team, 1998 +/* + 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 _EOXOVER2_h +#define _EOXOVER2_h + + +// for swap +#if defined( __BORLANDC__ ) +#include +#else +#include +#endif + +// EO includes +#include +#include + +/** 2-point crossover: takes the genes in the central section of two EOs +and interchanges it +*/ +template +class eoXOver2: public eoQuadraticOp { +public: + /// + eoXOver2() + : eoQuadraticOp< EOT >(){}; + + /// + virtual ~eoXOver2() {}; + + /// + virtual void operator()( EOT& _eo1, + EOT& _eo2 ) const { + unsigned len1 = _eo1.length(), len2 = _eo2.length(), + len= (len1 > len2)?len2:len1; + eoUniform uniform( 0, len ); + unsigned pos1 = uniform(), pos2 = uniform() ; + + applyAt( _eo1, _eo2, pos1, pos2 ); + + } + + /** @name Methods from eoObject + readFrom and printOn are directly inherited from eoOp + */ + //@{ + /** Inherited from eoObject + @see eoObject + */ + string className() const {return "eoXOver2";}; + //@} + +private: + +#ifdef _MSC_VER + typedef EOT::Type Type; +#else + typedef typename EOT::Type Type; +#endif + + /// applies operator to one gene in the EO + virtual void applyAt( EOT& _eo, EOT& _eo2, + unsigned _i, unsigned _j = 0) const { + + if ( _j < _i ) + swap( _i, _j ); + + unsigned len1 = _eo.length(), len2 = _eo2.length(), + len= (len1 > len2)?len2:len1; + + if ( (_j > len) || (_i> len ) ) + throw runtime_error( "xOver2: applying xOver past boundaries"); + + for ( unsigned i = _i; i < _j; i++ ) { + Type tmp = _eo.gene( i ); + _eo.gene( i ) = _eo2.gene( i ); + _eo2.gene( i ) = tmp ; + } + + } + +}; + +#endif