From 3a8d9dd7a9e038b70900d857a857eb18d30dd1d8 Mon Sep 17 00:00:00 2001 From: evomarc Date: Sat, 2 Aug 2003 06:42:33 +0000 Subject: [PATCH] Adding the generic operators for FIXED-LENGTH ORDERED genotypes eoFlOrXxxOp.h --- eo/src/eoFlOrBinOp.h | 219 +++++++++++++++++++++++++++++++++++++++++ eo/src/eoFlOrMonOp.h | 124 +++++++++++++++++++++++ eo/src/eoFlOrQuadOp.h | 213 +++++++++++++++++++++++++++++++++++++++ eo/src/ga/eoBoolFlip.h | 46 +++++++++ 4 files changed, 602 insertions(+) create mode 100644 eo/src/eoFlOrBinOp.h create mode 100644 eo/src/eoFlOrMonOp.h create mode 100644 eo/src/eoFlOrQuadOp.h create mode 100644 eo/src/ga/eoBoolFlip.h diff --git a/eo/src/eoFlOrBinOp.h b/eo/src/eoFlOrBinOp.h new file mode 100644 index 00000000..124ca4fd --- /dev/null +++ b/eo/src/eoFlOrBinOp.h @@ -0,0 +1,219 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoFlOrBinOp.h +// (c) Marc Schoenauer - Maarten Keijzer 2000-2003 +/* + 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: Marc.Schoenauer@inria.fr + mkeijzer@cs.vu.nl + */ +//----------------------------------------------------------------------------- + +#ifndef _eoFlOrBinOp_h +#define _eoFlOrBinOp_h + +#include +#include + +/** Generic eoBinOps on fixed length genotypes. + * Contains exchange crossovers (1pt and uniform) + * and crossovers that applies an Atom crossover + * to all components with given rate, or + * to a fixed prescribed nb of components + * + * Example: the standard bitstring 1-point and uniform crossovers + * could be implemented as resp. eoFlOr1ptBinOp and eoFlOrUniformBinOp +*/ + +////////////////////////////////////////////////////////////////////// +// eoFlOrAllAtomBinOp +////////////////////////////////////////////////////////////////////// +/** Bin Crossover using an Atom Crossover + * that is applied to a ALL components with given rate + */ +template +class eoFlOrAllAtomBinOp : public eoBinOp +{ +public : + + typedef typename EOT::AtomType AtomType; + + /** default ctor: requires an Atom BinOp */ + eoFlOrAllAtomBinOp( eoBinOp& _op, float _rate = 1.0): + op(_op), rate( _rate ) {} + + /** applies Atom crossover to ALL components with given rate */ + bool operator()(EOT & _eo1, const EOT & _eo2) + { + if (_eo1.size() != _eo2.size()) + { + string s = "Operand size don't match in " + className(); + throw runtime_error(s); + } + bool changed = false; + for ( unsigned i = 0; i < _eo1.size(); i++ ) { + if ( rng.flip( rate ) ) { + bool changedHere = op( _eo1[i], _eo2[i] ); + changed |= changedHere; + } + } + return changed; + } + + /** inherited className()*/ + virtual string className() const { return "eoFlOrAllAtomBinOp"; } + +private: + double rate; + eoBinOp & op; +}; + +////////////////////////////////////////////////////////////////////// +// eoFlOrKAtomBinOp +////////////////////////////////////////////////////////////////////// +/** Bin Crossover using an Atom Crossover + * that is applied to a FIXED NB of components + */ +template +class eoFlOrKAtomBinOp : public eoBinOp +{ +public : + + typedef typename EOT::AtomType AtomType; + + /** default ctor: requires an Atom BinOp and an unsigned */ + eoFlOrAtomBinOp( eoBinOp& _op, unsigned _k = 1): + op(_op), k( _k ) {} + + /** applies the Atom BinOp to some components */ + bool operator()(EOT & _eo1, const EOT & _eo2) + { + if (_eo1.size() != _eo2.size()) + { + string s = "Operand size don't match in " + className(); + throw runtime_error(s); + } + + bool changed = false; + for ( unsigned i = 0; i < k; i++ ) // TODO: check that we don't do twice the same + { + unsigned where = eo::rng.random(_eo1.size()); + bool changedHere = op( _eo1[where], _eo2[where] ); + changed |= changedHere; + } + return changed; + } + + /** inherited className()*/ + virtual string className() const { return "eoFlOrKAtomBinOp"; } + +private: + unsigned k; + eoBinOp & op; +}; + + +////////////////////////////////////////////////////////////////////// +// eoFlOrUniformBinOp +////////////////////////////////////////////////////////////////////// + +/** The uniform crossover - exchanges atoms uniformly ! */ +template +class eoFlOrUniformBinOp : public eoBinOp +{ +public : + + typedef typename EOT::AtomType AtomType; + + /** default ctor: requires a rate - 0.5 by default */ + eoFlOrUniformBinOp(double _rate=0.5) : eoBinOp(_size), + rate(_rate) {} + + /** excahnges atoms at given rate */ + bool operator()(EOT & _eo1, const EOT & _eo2) + { + unsigned i; + Atom tmp; + if (_eo1.size() != _eo2.size()) + { + string s = "Operand size don't match in " + className(); + throw runtime_error(s); + } + bool hasChanged = false; + for (unsigned i=0; i<_eo1.size(); i++) + { + if ( (_eo1[i]!=_eo2[i]) && (eo::rng.filp(rate)) ) + { + _eo1[i] = _eo2[i]; + hasChanged = true; + } + } + return hasChanged; + } + + /** inherited className()*/ + virtual string className() const { return "eoFlOrUniformBinOp"; } + +private: + double rate; +}; + +////////////////////////////////////////////////////////////////////// +// eoFlOr1ptBinOp +////////////////////////////////////////////////////////////////////// + +/** The 1pt crossover (just in case someone wants it some day!) */ +template +class eoFlOr1ptBinOp : public eoBinOp +{ +public : + + typedef typename EOT::AtomType AtomType; + + /** default ctor: no argument */ + eoVlUniformBinOp() {} + + /** exchanges first and second parts of the vectors of Atoms */ + bool operator()(EOT & _eo1, EOT & _eo2) + { + unsigned i; + Atom tmp; + if (_eo1.size() != _eo2.size()) + { + string s = "Operand size don't match in " + className(); + throw runtime_error(s); + } + bool hasChanged = false; + unsigned where = eo::rng.random(_eo1.size()-1); + for (unsigned i=where+1; i<_eo1.size(); i++) + { + if ( (_eo1[i]!=_eo2[i]) ) + { + _eo1[i] = _eo2[i]; + hasChanged = true; + } + } + return hasChanged; + } + + /** inherited className()*/ + virtual string className() const { return "eoFlOr1ptBinOp"; } + +}; + + +#endif diff --git a/eo/src/eoFlOrMonOp.h b/eo/src/eoFlOrMonOp.h new file mode 100644 index 00000000..982dc7e8 --- /dev/null +++ b/eo/src/eoFlOrMonOp.h @@ -0,0 +1,124 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoFlOrMonOp.h +// (c) Marc Schoenauer - Maarten Keijzer 2000-2003 +/* + 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: Marc.Schoenauer@polytechnique.fr + mkeijzer@cs.vu.nl + */ +//----------------------------------------------------------------------------- + +#ifndef _eoFlOrMonOp_h +#define _eoFlOrMonOp_h + +#include +#include +#include + +/** Base classes for generic mutations on fixed length chromosomes. + * COntains 2 classes that both use an atomic mutation + * eoFlOrAllMutation applies the atom mutation to all components with given rate + * eoFlOrKMutation applies the atom mutation to a fixed nb of components + * + * Remark: the standard bit-flip mutation is an eoFlOrAllMutation + * with atom mutation == bitflipping + */ + +/** applies an atomic mutation to all the components with a given rate + */ +template +class eoFlOrAllMutation : public eoMonOp +{ +public : + + typedef typename EOT::AtomType AtomType; + + /** default ctor: requires an Atom mutation and a rate */ + eoFlOrAllMutation(eoMonOp & _atomMutation, double _rate=1.0) : + atomMutation(_atomMutation), rate(_rate) {} + + /** applies the atom mutation to all components with given rate */ + bool operator()(EOT & _eo) + { + bool modified=false; + for (unsigned i=0; i<_eo.size(); i++) + if (eo::rng.flip(rate)) + if (atomMutation(_eo[i])) + modified = true; + + return modified; + } + + /** inherited className() */ + virtual string className() const + { + char s[1024]; + ostrstream os(s, 1022); + os << "eoFlOrAllMutation(" << atomMutation.className() << ")"; + return string(s); + } + +private: + eoMonOp & atomMutation; // the atom mutation + double rate; // the mutation rate PER ATOM +}; + +/** Applies an atomic mutation to a fixed + number of components (1 by default) + */ +template +class eoFlOrKMutation : public eoMonOp +{ +public : + + typedef typename EOT::AtomType AtomType; + + /** default ctor: requires an Atom mutation */ + eoFlOrKMutation(eoMonOp & _atomMutation, unsigned _nb=1) : + nb(_nb), atomMutation(_atomMutation) {} + + + /** applies the atom mutation to K randomly selected components */ + bool operator()(EOT & _eo) + { + bool modified=false; + for (unsigned k=0; k & atomMutation; // the atom mutation +}; + + +#endif diff --git a/eo/src/eoFlOrQuadOp.h b/eo/src/eoFlOrQuadOp.h new file mode 100644 index 00000000..1ad873c4 --- /dev/null +++ b/eo/src/eoFlOrQuadOp.h @@ -0,0 +1,213 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoFlOrQuadOp.h +// (c) Marc Schoenauer - Maarten Keijzer 2000-2003 +/* + 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: Marc.Schoenauer@polytechnique.fr + mkeijzer@cs.vu.nl + */ +//----------------------------------------------------------------------------- + +#ifndef _eoFlOrQuadOp_h +#define _eoFlOrQuadOp_h + +#include +#include + +/** Generic eoQuadOps on fixed length genotypes. + * Contains exchange crossovers (1pt and uniform) + * and crossovers that applies an Atom crossover + * to all components with given rate, or + * to a fixed prescribed nb of components +*/ + +////////////////////////////////////////////////////////////////////// +// eoFlOrAllAtomQuadOp +////////////////////////////////////////////////////////////////////// + +/** Quad Crossover using an Atom Crossover + */ +template +class eoFlOrAllAtomQuadOp : public eoQuadOp +{ +public : + + typedef typename EOT::AtomType AtomType; + + /** default ctor: requires an Atom QuadOp */ + eoFlOrAllAtomQuadOp( eoQuadOp& _op, double _rate = 1): + op(_op), rate( _rate ) {} + + /** applies Atom crossover to ALL components with given rate */ + bool operator()(EOT & _eo1, EOT & _eo2) + { + bool changed = false; + for ( unsigned i = 0; i < _eo1.size(); i++ ) { + if ( rng.flip( rate ) ) { + bool changedHere = op( _eo1[i], _eo2[i] ); + changed |= changedHere; + } + } + return changed; + } + + /** inherited className()*/ + virtual string className() const { return "eoFlOrAllAtomQuadOp"; } + +private: + double rate; + eoQuadOp & op; +}; + +////////////////////////////////////////////////////////////////////// +// eoFlOrKAtomQuadOp +////////////////////////////////////////////////////////////////////// +/** Quad Crossover using an Atom Crossover + * that is applied to a FIXED NB of components + */ +template +class eoFlOrKAtomQuadOp : public eoQuadOp +{ +public : + + typedef typename EOT::AtomType AtomType; + + /** default ctor: requires an Atom QuadOp and an unsigned */ + eoFlOrAtomQuadOp( eoQuadOp& _op, unsigned _k = 1): + op(_op), k( _k ) {} + + /** applies the Atom QuadOp to some components */ + bool operator()(EOT & _eo1, const EOT & _eo2) + { + if (_eo1.size() != _eo2.size()) + { + string s = "Operand size don't match in " + className(); + throw runtime_error(s); + } + + bool changed = false; + for ( unsigned i = 0; i < k; i++ ) // TODO: check that we don't do twice the same + { + unsigned where = eo::rng.random(_eo1.size()); + bool changedHere = op( _eo1[where], _eo2[where] ); + changed |= changedHere; + } + return changed; + } + + /** inherited className()*/ + virtual string className() const { return "eoFlOrKAtomQuadOp"; } + +private: + unsigned k; + eoQuadOp & op; +}; + + +////////////////////////////////////////////////////////////////////// +// eoFlOrUniformQuadOp +////////////////////////////////////////////////////////////////////// +/** The uniform crossover - exchanges atoms uniformly ! */ +template +class eoFlOrUniformQuadOp : public eoQuadOp +{ +public : + + typedef typename EOT::AtomType AtomType; + + /** default ctor: requires a rate - 0.5 by default */ + eoVlUniformQuadOp(double _rate=0.5) : eoQuadOp(_size), + rate(_rate) {} + + /** excahnges atoms at given rate */ + bool operator()(EOT & _eo1, EOT & _eo2) + { + unsigned i; + Atom tmp; + if (_eo1.size() != _eo2.size()) + { + string s = "Operand size don't match in " + className(); + throw runtime_error(s); + } + bool hasChanged = false; + for (unsigned i=0; i<_eo1.size(); i++) + { + if ( (_eo1[i]!=_eo2[i]) && (eo::rng.filp(rate)) ) + { + tmp = _eo1[i]; + _eo1[i] = _eo2[i]; + _eo2[i] = tmp; + hasChanged = true; + } + } + return hasChanged; + } + + /** inherited className()*/ + virtual string className() const { return "eoFlOrUniformQuadOp"; } + +private: + double rate; +}; + +////////////////////////////////////////////////////////////////////// +// eoFlOr1ptQuadOp +////////////////////////////////////////////////////////////////////// +/** The 1pt crossover (just in case someone wants it some day!) */ +template +class eoFlOr1ptQuadOp : public eoQuadOp +{ +public : + + typedef typename EOT::AtomType AtomType; + + /** default ctor: no argument */ + eoVlUniformQuadOp() {} + + /** exchanges first and second parts of the vectors of Atoms */ + bool operator()(EOT & _eo1, EOT & _eo2) + { + unsigned i; + Atom tmp; + if (_eo1.size() != _eo2.size()) + { + string s = "Operand size don't match in " + className(); + throw runtime_error(s); + } + bool hasChanged = false; + unsigned where = eo::rng.random(_eo1.size()-1); + for (unsigned i=where+1; i<_eo1.size(); i++) + { + if ( (_eo1[i]!=_eo2[i]) ) + { + tmp = _eo1[i]; + _eo1[i] = _eo2[i]; + _eo2[i] = tmp; + hasChanged = true; + } + } + return hasChanged; + } + + /** inherited className()*/ + virtual string className() const { return "eoFlOr1ptQuadOp"; } + +}; + + +#endif diff --git a/eo/src/ga/eoBoolFlip.h b/eo/src/ga/eoBoolFlip.h new file mode 100644 index 00000000..cf226eba --- /dev/null +++ b/eo/src/ga/eoBoolFlip.h @@ -0,0 +1,46 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- + +//----------------------------------------------------------------------------- +// eoBoolFlip.h +// (c) Marc Schoenauer, 2003 +/* + 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: Marc.Schoenauer@inria.fr + */ +//----------------------------------------------------------------------------- + +#ifndef _eoBoolFlip_h +#define _eoBoolFlip_h + +#include + +/** a simple boolean mutation - to be used in generic eoOp's + */ +class eoBoolFlip : public eoMonOp { +public: + /** simply flips the boolean argument */ + bool operator()(bool & _b) + { + _b = !_b; + return true; + } + + /** inherited className() */ + virtual string className() const {return "boolFlip";} + +}; + +#endif