partial_mapped_xover.cpp

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 // "partial_mapped_xover.cpp"
00004 
00005 // (c) OPAC Team, LIFL, 2003
00006 
00007 /* This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011    
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016    
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with this library; if not, write to the Free Software
00019    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020    
00021    Contact: cahon@lifl.fr
00022 */
00023 
00024 #include <assert.h>
00025 
00026 #include <utils/eoRNG.h>
00027 
00028 #include "partial_mapped_xover.h"
00029 #include "mix.h"
00030 
00031 void PartialMappedXover :: repair (Route & __route, unsigned __cut1, unsigned __cut2) {
00032   
00033   unsigned v [__route.size ()] ; // Number of times a cities are visited ...
00034   
00035   for (unsigned i = 0 ; i < __route.size () ; i ++)
00036     v [i] = 0 ;
00037   
00038   for (unsigned i = 0 ; i < __route.size () ; i ++)
00039     v [__route [i]] ++ ;
00040   
00041   std :: vector <unsigned> vert ;
00042 
00043   for (unsigned i = 0 ; i < __route.size () ; i ++)
00044     if (! v [i])
00045       vert.push_back (i) ;
00046   
00047   mix (vert) ;
00048 
00049   for (unsigned i = 0 ; i < __route.size () ; i ++)
00050     if (i < __cut1 || i >= __cut2)
00051       if (v [__route [i]] > 1) {
00052         __route [i] = vert.back () ;
00053         vert.pop_back () ;
00054       }
00055 }
00056 
00057 bool PartialMappedXover :: operator () (Route & __route1, Route & __route2) {
00058     
00059   unsigned cut1 = rng.random (__route1.size ()), cut2 = rng.random (__route2.size ()) ;
00060   
00061   if (cut2 < cut1)
00062     std :: swap (cut1, cut2) ;
00063   
00064   // Between the cuts
00065   for (unsigned i = cut1 ; i < cut2 ; i ++)
00066     std :: swap (__route1 [i], __route2 [i]) ;
00067   
00068   // Outside the cuts
00069   repair (__route1, cut1, cut2) ;
00070   repair (__route2, cut1, cut2) ;
00071   
00072   __route1.invalidate () ;
00073   __route2.invalidate () ;
00074 
00075   return true ;
00076 }

Generated on Fri Dec 22 16:54:58 2006 for ParadisEO by  doxygen 1.4.7