order_xover.cpp

00001 // "order_xover.cpp"
00002 
00003 // (c) OPAC Team, LIFL, 2002
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 "order_xover.h"
00014 
00015 void OrderXover :: cross (const Route & __par1, const Route & __par2, Route & __child) {
00016 
00017   unsigned cut2 = 1 + rng.random (numNodes) ;    
00018   unsigned cut1 = rng.random (cut2);
00019   unsigned l = 0;
00020 
00021   /* To store vertices that have already been crossed */
00022   std :: vector <bool> v (numNodes, false);
00023 
00024   /* Copy of the left partial route of the first parent */ 
00025   for (unsigned i = cut1 ; i < cut2 ; i ++) {
00026     __child [l ++] = __par1 [i] ; 
00027     v [__par1 [i]] = true ;
00028   }
00029    
00030   /* Searching the vertex of the second path, that ended the previous first one */
00031   unsigned from = 0 ;
00032   for (unsigned i = 0; i < numNodes; i ++)
00033     if (__par2 [i] == __child [cut2 - 1]) { 
00034       from = i ;
00035       break ;
00036     }
00037   
00038   /* Selecting a direction (Left or Right) */
00039   char direct = rng.flip () ? 1 : -1 ;
00040       
00041   for (unsigned i = 0; i < numNodes + 1; i ++) {
00042     unsigned bidule = (direct * i + from + numNodes) % numNodes;
00043     if (! v [__par2 [bidule]]) {
00044       __child [l ++] = __par2 [bidule] ;
00045       v [__par2 [bidule]] = true ;
00046     }
00047   }
00048 } 
00049 
00050 bool OrderXover :: operator () (Route & __route1, Route & __route2) {
00051   
00052   // Init. copy
00053   Route par [2] ;
00054   par [0] = __route1 ;
00055   par [1] = __route2 ;
00056   
00057   cross (par [0], par [1], __route1) ;
00058   cross (par [1], par [0], __route2) ;
00059   
00060   __route1.invalidate () ;
00061   __route2.invalidate () ;
00062 
00063   return true ;
00064 }

Generated on Tue Jan 9 15:47:41 2007 for ParadisEO-PEO - Lessons by  doxygen 1.4.7