- Main Page
- Classes
- Files

- Search for |

# partial_mapped_xover.cpp

    00001 // "partial_mapped_xover.cpp"
    00002
    00003 // (c) OPAC Team, LIFL, 2003
    00004
    00005 /*
    00006    Contact: paradiseo-help@lists.gforge.inria.fr
    00007 */
    00008
    00009 #include <assert.h>
    00010
    00011 #include <utils/eoRNG.h>
    00012
    00013 #include "partial_mapped_xover.h"
    00014 #include "mix.h"
    00015
    00016 void PartialMappedXover :: repair (Route & __route, unsigned __cut1, unsigned __cut2) {
    00017
    00018   unsigned v [__route.size ()] ; // Number of times a cities are visited ...
    00019
    00020   for (unsigned i = 0 ; i < __route.size () ; i ++)
    00021     v [i] = 0 ;
    00022
    00023   for (unsigned i = 0 ; i < __route.size () ; i ++)
    00024     v [__route [i]] ++ ;
    00025
    00026   std :: vector <unsigned> vert ;
    00027
    00028   for (unsigned i = 0 ; i < __route.size () ; i ++)
    00029     if (! v [i])
    00030       vert.push_back (i) ;
    00031
    00032   mix (vert) ;
    00033
    00034   for (unsigned i = 0 ; i < __route.size () ; i ++)
    00035     if (i < __cut1 || i >= __cut2)
    00036       if (v [__route [i]] > 1) {
    00037         __route [i] = vert.back () ;
    00038         vert.pop_back () ;
    00039       }
    00040 }
    00041
    00042 bool PartialMappedXover :: operator () (Route & __route1, Route & __route2) {
    00043
    00044   unsigned cut1 = rng.random (__route1.size ()), cut2 = rng.random (__route2.size ()) ;
    00045
    00046   if (cut2 < cut1)
    00047     std :: swap (cut1, cut2) ;
    00048
    00049   // Between the cuts
    00050   for (unsigned i = cut1 ; i < cut2 ; i ++)
    00051     std :: swap (__route1 [i], __route2 [i]) ;
    00052
    00053   // Outside the cuts
    00054   repair (__route1, cut1, cut2) ;
    00055   repair (__route2, cut1, cut2) ;
    00056
    00057   __route1.invalidate () ;
    00058   __route2.invalidate () ;
    00059
    00060   return true ;
    00061 }