// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- //----------------------------------------------------------------------------- // eoRealOp.h // (c) EEAAX 2000 - Maarten Keijzer 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: Marc.Schoenauer@polytechnique.fr mak@dhi.dk */ //----------------------------------------------------------------------------- #ifndef eoRealOp_h #define eoRealOp_h //----------------------------------------------------------------------------- #include // swap_ranges #include #include //----------------------------------------------------------------------------- /** eoUniformMutation --> changes all values of the vector by uniform choice with range epsilon with probability p_change per variable \class eoUniformMutation eoRealOp.h Tutorial/eoRealOp.h \ingroup parameteric */ template class eoUniformMutation: public eoMonOp { public: /** * (Default) Constructor. * @param _epsilon the range for uniform nutation * @param _p_change the probability to change a given coordinate */ eoUniformMutation(const double& _epsilon, const double& _p_change = 1.0): epsilon(_epsilon), p_change(_p_change) {} /// The class name. string className() const { return "eoUniformMutation"; } /** * Do it! * @param chrom The cromosome undergoing the mutation */ void operator()(Chrom& chrom) { bool hasChanged=false; for (unsigned lieu=0; lieu changes exactly k values of the vector by uniform choice with range epsilon \class eoDetUniformMutation eoRealOp.h Tutorial/eoRealOp.h \ingroup parameteric */ template class eoDetUniformMutation: public eoMonOp { public: /** * (Default) Constructor. * @param _epsilon the range for uniform nutation * @param number of coordinate to modify */ eoDetUniformMutation(const double& _epsilon, const unsigned& _no = 1): epsilon(_epsilon), no(_no) {} /// The class name. string className() const { return "eoDetUniformMutation"; } /** * Do it! * @param chrom The cromosome undergoing the mutation */ void operator()(Chrom& chrom) { chrom.invalidate(); for (unsigned i=0; i class eoNormalMutation: public eoMonOp { public: /** * (Default) Constructor. * @param _epsilon the range for uniform nutation * @param _p_change the probability to change a given coordinate */ eoNormalMutation(const double& _epsilon, const double& _p_change = 1.0): epsilon(_epsilon), p_change(_p_change) {} /// The class name. string className() const { return "eoNormalMutation"; } /** * Do it! * @param chrom The cromosome undergoing the mutation */ void operator()(Chrom& chrom) { bool hasChanged=false; for (unsigned lieu=0; lieu uniform choice in segment == arithmetical with same value along all coordinates \class eoSegmentCrossover eoRealOp.h Tutorial/eoRealOp.h \ingroup parameteric */ template class eoSegmentCrossover: public eoQuadraticOp { public: /** * (Default) Constructor. * @param _alpha the amount of exploration OUTSIDE the parents * as in BLX-alpha notation (Eshelman and Schaffer) * 0 == contractive application */ eoSegmentCrossover(const double& _alpha = 0.0) : alpha(_alpha), range(1+2*alpha) {} /// The class name. string className() const { return "eoSegmentCrossover"; } /** * segment crossover - modifies both parents * @param chrom1 The first parent * @param chrom2 The first parent */ void operator()(Chrom& chrom1, Chrom& chrom2) { unsigned i; double r1, r2, fact; fact = rng.uniform(range); // in [0,range) for (i=0; i uniform choice in hypercube == arithmetical with different values for each coordinate \class eoArithmeticCrossover eoRealOp.h Tutorial/eoRealOp.h \ingroup parameteric */ template class eoArithmeticCrossover: public eoQuadraticOp { public: /** * (Default) Constructor. * @param _alpha the amount of exploration OUTSIDE the parents * as in BLX-alpha notation (Eshelman and Schaffer) * 0 == contractive application */ eoArithmeticCrossover(const double& _alpha = 0.0): alpha(_alpha), range(1+2*alpha) {} /// The class name. string className() const { return "eoArithmeticCrossover"; } /** * arithmetical crossover - modifies both parents * @param chrom1 The first parent * @param chrom2 The first parent */ void operator()(Chrom& chrom1, Chrom& chrom2) { unsigned i; double r1, r2, fact; for (i=0; i Uniform crossover, also termed intermediate crossover \class eoRealUxOver eoRealOp.h Tutorial/eoRealOp.h \ingroup parameteric */ template class eoRealUxOver: public eoQuadraticOp { public: /** * (Default) Constructor. * @param _preference bias in the choice (usually, no bias == 0.5) */ eoRealUxOver(const float& _preference = 0.5): preference(_preference) { if ( (_preference <= 0.0) || (_preference >= 1.0) ) runtime_error("UxOver --> invalid preference"); } /// The class name. string className() const { return "eoRealUxOver"; } /** * Uniform crossover for real vectors * @param chrom1 The first parent * @param chrom2 The second parent * @runtime_error if sizes don't match */ void operator()(Chrom& chrom1, Chrom& chrom2) { if ( chrom1.size() != chrom2.size()) runtime_error("UxOver --> chromosomes sizes don't match" ); bool changed = false; for (unsigned int i=0; i