EvolvingObjects
|
00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*- 00002 00003 //----------------------------------------------------------------------------- 00004 // eoSwapMutation.h 00005 // (c) GeNeura Team, 2000 - EEAAX 2000 - Maarten Keijzer 2000 00006 // (c) INRIA Futurs - Dolphin Team - Thomas Legrand 2007 00007 /* 00008 This library is free software; you can redistribute it and/or 00009 modify it under the terms of the GNU Lesser General Public 00010 License as published by the Free Software Foundation; either 00011 version 2 of the License, or (at your option) any later version. 00012 00013 This library is distributed in the hope that it will be useful, 00014 but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 Lesser General Public License for more details. 00017 00018 You should have received a copy of the GNU Lesser General Public 00019 License along with this library; if not, write to the Free Software 00020 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00021 00022 Contact: todos@geneura.ugr.es, http://geneura.ugr.es 00023 thomas.legrand@lifl.fr 00024 Marc.Schoenauer@polytechnique.fr 00025 mak@dhi.dk 00026 */ 00027 //----------------------------------------------------------------------------- 00028 00029 #ifndef eoSwapMutation_h 00030 #define eoSwapMutation_h 00031 00032 //----------------------------------------------------------------------------- 00033 00034 00040 template<class Chrom> class eoSwapMutation: public eoMonOp<Chrom> 00041 { 00042 public: 00043 00045 eoSwapMutation(const unsigned _howManySwaps=1): howManySwaps(_howManySwaps) 00046 { 00047 // consistency check 00048 if(howManySwaps < 1) 00049 throw std::runtime_error("Invalid number of swaps in eoSwapMutation"); 00050 } 00051 00053 virtual std::string className() const { return "eoSwapMutation"; } 00054 00059 bool operator()(Chrom& chrom) 00060 { 00061 unsigned i, j; 00062 00063 for(unsigned int swap = 0; swap < howManySwaps; swap++) 00064 { 00065 // generate two different indices 00066 i=eo::rng.random(chrom.size()); 00067 do j = eo::rng.random(chrom.size()); while (i == j); 00068 00069 // swap 00070 std::swap(chrom[i],chrom[j]); 00071 } 00072 return true; 00073 } 00074 00075 private: 00076 unsigned int howManySwaps; 00077 }; 00081 //----------------------------------------------------------------------------- 00082 #endif