From 52a9c830c860bd40332ecae52e88e62824d991fb Mon Sep 17 00:00:00 2001 From: evomarc Date: Tue, 28 Nov 2000 17:41:41 +0000 Subject: [PATCH] I added this very simple instance of operator proportional selector for the tutorial - it is consistent with all other xxxCombined constructs. However, I am not sure that Occam's razor will not make it widely used... --- eo/src/eoProportionalCombinedOp.h | 185 ++++++++++++++++++++++++++++++ 1 file changed, 185 insertions(+) create mode 100644 eo/src/eoProportionalCombinedOp.h diff --git a/eo/src/eoProportionalCombinedOp.h b/eo/src/eoProportionalCombinedOp.h new file mode 100644 index 00000000..2c40393d --- /dev/null +++ b/eo/src/eoProportionalCombinedOp.h @@ -0,0 +1,185 @@ +// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- +//----------------------------------------------------------------------------- +// eoCombinedOp.h +// (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 _eoCombinedOp_H +#define _eoCombinedOp_H + +#include +#include +#include +#include +#include +/** +\defgroup PropCombined operators +Combination of same-type Genetic Operators - Proportional choice +*/ + +/** @name PropCombined Genetic operators + +This files contains the classes eoPropCombinedXXXOp (XXX in {Mon, Bin, Quad}) +that allow to use more than a single operator of a specific class +into an algorithm, chosing by a roulette wheel based on user-defined rates + +@author Marc Schoenauer +@version 0.1 +*/ + + + +/** eoMonOp is the monary operator: genetic operator that takes only one EO */ + +template +class eoPropCombinedMonOp: public eoMonOp +{ +public: + /// Ctor + eoPropCombinedMonOp(eoMonOp & _first, double _rate) + { + ops.push_back(&_first); + rates.push_back(_rate); + } + +virtual string className() const { return "eoPropCombinedMonOp"; } + +void add(eoMonOp & _op, double _rate, bool _verbose=false) + { + ops.push_back(&_op); + rates.push_back(_rate); + // compute the relative rates in percent - to warn the user! + if (_verbose) + { + double total = 0; + unsigned i; + for (i=0; iclassName() << " with rate " << 100*rates[i]/total << " %\n"; + } + } + + void operator()(EOT & _indi) + { + unsigned what = rng.roulette_wheel(rates); // choose one op + (*ops[what])(_indi); // apply it + } +private: +std::vector*> ops; +std::vector rates; +}; + + +/** COmbined Binary genetic operator: + * operator() has two operands, only the first one can be modified + */ +template +class eoPropCombinedBinOp: public eoBinOp +{ +public: + /// Ctor + eoPropCombinedBinOp(eoBinOp & _first, double _rate) + { + ops.push_back(&_first); + rates.push_back(_rate); + } + +virtual string className() const { return "eoPropCombinedBinOp"; } + +void add(eoBinOp & _op, double _rate, bool _verbose=false) + { + ops.push_back(&_op); + rates.push_back(_rate); + // compute the relative rates in percent - to warn the user! + if (_verbose) + { + double total = 0; + unsigned i; + for (i=0; iclassName() << " with rate " << 100*rates[i]/total << " %\n"; + } + } + + void operator()(EOT & _indi1, const EOT & _indi2) + { + unsigned what = rng.roulette_wheel(rates); // choose one op index + (*ops[what])(_indi1, _indi2); // apply it + } +private: +std::vector*> ops; +std::vector rates; +}; + + +/** Quadratic genetic operator: subclasses eoOp, and defines basically the + operator() with two operands, both can be modified. +*/ +/** COmbined Binary genetic operator: + * operator() has two operands, only the first one can be modified + */ +template +class eoPropCombinedQuadOp: public eoQuadraticOp +{ +public: + /// Ctor + eoPropCombinedQuadOp(eoQuadraticOp & _first, double _rate) + { + ops.push_back(&_first); + rates.push_back(_rate); + } + +virtual string className() const { return "eoPropCombinedQuadOp"; } + +void add(eoQuadraticOp & _op, double _rate, bool _verbose=false) + { + ops.push_back(&_op); + rates.push_back(_rate); + // compute the relative rates in percent - to warn the user! + if (_verbose) + { + double total = 0; + unsigned i; + for (i=0; iclassName() << " with rate " << 100*rates[i]/total << " %\n"; + } + } + + void operator()(EOT & _indi1, EOT & _indi2) + { + unsigned what = rng.roulette_wheel(rates); // choose one op index + (*ops[what])(_indi1, _indi2); // apply it + } +private: +std::vector*> ops; +std::vector rates; +}; + + +// for General Ops, it's another story - see eoGOpSelector +#endif +