- Main Page
- Namespaces
- Classes
- Files

- Search for |

# order_xover.cpp

    00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
    00002
    00003 // "order_xover.cpp"
    00004
    00005 // (c) OPAC Team, LIFL, 2002-2006
    00006
    00007 /* LICENCE TEXT
    00008
    00009    Contact: paradiseo-help@lists.gforge.inria.fr
    00010 */
    00011
    00012 #include <assert.h>
    00013 #include <vector>
    00014
    00015 #include <utils/eoRNG.h>
    00016
    00017 #include "order_xover.h"
    00018 #include "route_valid.h"
    00019
    00020 void OrderXover :: cross (const Route & __par1, const Route & __par2, Route & __child)
    00021 {
    00022
    00023   unsigned int cut = rng.random (__par1.size ()) ;
    00024
    00025   /* To store vertices that have
    00026      already been crossed */
    00027   std::vector<bool> v;
    00028   v.resize(__par1.size());
    00029
    00030   for (unsigned int i = 0 ; i < __par1.size () ; i ++)
    00031     {
    00032       v [i] = false ;
    00033     }
    00034
    00035   /* Copy of the left partial
    00036      route of the first parent */
    00037   for (unsigned int i = 0 ; i < cut ; i ++)
    00038     {
    00039       __child [i] = __par1 [i] ;
    00040       v [__par1 [i]] = true ;
    00041     }
    00042
    00043   /* Searching the vertex of the second path, that ended
    00044      the previous first one */
    00045   unsigned int from = 0 ;
    00046   for (unsigned int i = 0 ; i < __par2.size () ; i ++)
    00047     {
    00048       if (__par2 [i] == __child [cut - 1])
    00049         {
    00050           from = i ;
    00051           break ;
    00052         }
    00053     }
    00054
    00055   /* Selecting a direction
    00056      Left or Right */
    00057   char direct = rng.flip () ? 1 : -1 ;
    00058
    00059   /* Copy of the left vertices from
    00060      the second parent path */
    00061   unsigned int l = cut ;
    00062
    00063   for (unsigned int i = 0 ; i < __par2.size () ; i ++)
    00064     {
    00065       unsigned int bidule /* :-) */ = (direct * i + from + __par2.size ()) % __par2.size () ;
    00066       if (! v [__par2 [bidule]])
    00067         {
    00068           __child [l ++] = __par2 [bidule] ;
    00069           v [__par2 [bidule]] = true ;
    00070         }
    00071     }
    00072
    00073   v.clear();
    00074 }
    00075
    00076 bool OrderXover :: operator () (Route & __route1, Route & __route2)
    00077 {
    00078
    00079   // Init. copy
    00080   Route par [2] ;
    00081   par [0] = __route1 ;
    00082   par [1] = __route2 ;
    00083
    00084   cross (par [0], par [1], __route1) ;
    00085   cross (par [1], par [0], __route2) ;
    00086
    00087   assert (valid (__route1)) ;
    00088   assert (valid (__route2)) ;
    00089
    00090   __route1.invalidate () ;
    00091   __route2.invalidate () ;
    00092
    00093   return true ;
    00094 }