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 00038 template<class Chrom> class eoSwapMutation: public eoMonOp<Chrom> 00039 { 00040 public: 00041 00043 eoSwapMutation(const unsigned _howManySwaps=1): howManySwaps(_howManySwaps) 00044 { 00045 // consistency check 00046 if(howManySwaps < 1) 00047 throw std::runtime_error("Invalid number of swaps in eoSwapMutation"); 00048 } 00049 00051 virtual std::string className() const { return "eoSwapMutation"; } 00052 00057 bool operator()(Chrom& chrom) 00058 { 00059 unsigned i, j; 00060 00061 for(unsigned int swap = 0; swap < howManySwaps; swap++) 00062 { 00063 // generate two different indices 00064 i=eo::rng.random(chrom.size()); 00065 do j = eo::rng.random(chrom.size()); while (i == j); 00066 00067 // swap 00068 std::swap(chrom[i],chrom[j]); 00069 } 00070 return true; 00071 } 00072 00073 private: 00074 unsigned int howManySwaps; 00075 }; 00076 00077 //----------------------------------------------------------------------------- 00078 #endif 00079