- Main Page
- Namespaces
- Classes
- Files

- Search for |

# 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-2006
    00006
    00007 /* TEXT LICENCE
    00008
    00009    Contact: paradiseo-help@lists.gforge.inria.fr
    00010 */
    00011
    00012 #include <assert.h>
    00013
    00014 #include <vector>
    00015
    00016 #include <utils/eoRNG.h>
    00017
    00018 #include "partial_mapped_xover.h"
    00019 #include "route_valid.h"
    00020 #include "mix.h"
    00021
    00022 void PartialMappedXover :: repair (Route & __route, unsigned __cut1, unsigned __cut2)
    00023 {
    00024
    00025   std::vector<unsigned int> v; // Number of times a cities are visited ...
    00026
    00027   v.resize(__route.size ());
    00028
    00029   for (unsigned int i = 0 ; i < __route.size () ; i ++)
    00030     {
    00031       v [i] = 0 ;
    00032     }
    00033
    00034   for (unsigned int i = 0 ; i < __route.size () ; i ++)
    00035     {
    00036       v [__route [i]] ++ ;
    00037     }
    00038
    00039   std :: vector <unsigned int> vert ;
    00040
    00041   for (unsigned int i = 0 ; i < __route.size () ; i ++)
    00042     {
    00043       if (! v [i])
    00044         {
    00045           vert.push_back (i) ;
    00046         }
    00047     }
    00048
    00049   mix (vert) ;
    00050
    00051   for (unsigned int i = 0 ; i < __route.size () ; i ++)
    00052     {
    00053       if (i < __cut1 || i >= __cut2)
    00054         {
    00055           if (v [__route [i]] > 1)
    00056             {
    00057               __route [i] = vert.back () ;
    00058               vert.pop_back () ;
    00059             }
    00060         }
    00061    }
    00062
    00063   v.clear();
    00064 }
    00065
    00066 bool PartialMappedXover :: operator () (Route & __route1, Route & __route2)
    00067 {
    00068   unsigned int cut1 = rng.random (__route1.size ()), cut2 = rng.random (__route2.size ()) ;
    00069
    00070   if (cut2 < cut1)
    00071     {
    00072       std :: swap (cut1, cut2) ;
    00073     }
    00074
    00075   // Between the cuts
    00076   for (unsigned int i = cut1 ; i < cut2 ; i ++)
    00077     {
    00078       std :: swap (__route1 [i], __route2 [i]) ;
    00079     }
    00080
    00081   // Outside the cuts
    00082   repair (__route1, cut1, cut2) ;
    00083   repair (__route2, cut1, cut2) ;
    00084
    00085   // Debug
    00086   assert (valid (__route1)) ;
    00087   assert (valid (__route2)) ;
    00088
    00089   __route1.invalidate () ;
    00090   __route2.invalidate () ;
    00091
    00092   return true ;
    00093 }