edge_xover.cpp

00001 /* 
00002 * <edge_xover.cpp>
00003 * Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
00004 * (C) OPAC Team, LIFL, 2002-2007
00005 *
00006 * Sebastien Cahon, Alexandru-Adrian Tantar
00007 *
00008 * This software is governed by the CeCILL license under French law and
00009 * abiding by the rules of distribution of free software.  You can  use,
00010 * modify and/ or redistribute the software under the terms of the CeCILL
00011 * license as circulated by CEA, CNRS and INRIA at the following URL
00012 * "http://www.cecill.info".
00013 *
00014 * As a counterpart to the access to the source code and  rights to copy,
00015 * modify and redistribute granted by the license, users are provided only
00016 * with a limited warranty  and the software's author,  the holder of the
00017 * economic rights,  and the successive licensors  have only  limited liability.
00018 *
00019 * In this respect, the user's attention is drawn to the risks associated
00020 * with loading,  using,  modifying and/or developing or reproducing the
00021 * software by the user in light of its specific status of free software,
00022 * that may mean  that it is complicated to manipulate,  and  that  also
00023 * therefore means  that it is reserved for developers  and  experienced
00024 * professionals having in-depth computer knowledge. Users are therefore
00025 * encouraged to load and test the software's suitability as regards their
00026 * requirements in conditions enabling the security of their systems and/or
00027 * data to be ensured and,  more generally, to use and operate it in the
00028 * same conditions as regards security.
00029 * The fact that you are presently reading this means that you have had
00030 * knowledge of the CeCILL license and that you accept its terms.
00031 *
00032 * ParadisEO WebSite : http://paradiseo.gforge.inria.fr
00033 * Contact: paradiseo-help@lists.gforge.inria.fr
00034 *
00035 */
00036 
00037 #include <assert.h>
00038 #include <values.h>
00039 
00040 #include <utils/eoRNG.h>
00041 
00042 #include "edge_xover.h"
00043 
00044 void EdgeXover :: build_map (const Route & __par1, const Route & __par2) {
00045   
00046   unsigned len = __par1.size () ;
00047   
00048   /* Initialization */
00049   _map.clear () ;
00050   _map.resize (len) ;
00051   
00052   for (unsigned i = 0 ; i < len ; i ++) {
00053     _map [__par1 [i]].insert (__par1 [(i + 1) % len]) ;
00054     _map [__par2 [i]].insert (__par2 [(i + 1) % len]) ;
00055     _map [__par1 [i]].insert (__par1 [(i - 1 + len) % len]) ;
00056     _map [__par2 [i]].insert (__par2 [(i - 1 + len) % len]) ;
00057   }
00058   
00059   visited.clear () ;
00060   visited.resize (len, false) ;
00061 }
00062 
00063 void EdgeXover :: remove_entry (unsigned __vertex, std :: vector <std :: set <unsigned> > & __map) {
00064   
00065   std :: set <unsigned> & neigh = __map [__vertex] ;
00066 
00067   for (std :: set <unsigned> :: iterator it = neigh.begin () ;
00068        it != neigh.end () ;
00069        it ++)
00070     __map [* it].erase (__vertex) ; 
00071       
00072 }
00073 
00074 void EdgeXover :: add_vertex (unsigned __vertex, Route & __child) {
00075   
00076   visited [__vertex] = true ;
00077   __child.push_back (__vertex) ;    
00078   remove_entry (__vertex, _map) ; /* Removing entries */    
00079 }
00080 
00081 void EdgeXover :: cross (const Route & __par1, const Route & __par2, Route & __child) {
00082   
00083   build_map (__par1, __par2) ;
00084   
00085   unsigned len = __par1.size () ;
00086  
00087   /* Go ! */
00088   __child.clear () ;
00089   
00090   unsigned cur_vertex = rng.random (len) ;
00091   
00092   add_vertex (cur_vertex, __child) ;
00093 
00094   for (unsigned i = 1 ; i < len ; i ++) {
00095     
00096     unsigned len_min_entry = MAXINT ;
00097     
00098     std :: set <unsigned> & neigh = _map [cur_vertex] ;
00099     
00100     for (std :: set <unsigned> :: iterator it = neigh.begin () ;
00101          it != neigh.end () ;
00102          it ++) {      
00103       unsigned l = _map [* it].size () ;
00104       if (len_min_entry > l)
00105         len_min_entry = l ;
00106     }
00107     
00108     std :: vector <unsigned> cand ; /* Candidates */
00109     
00110     for (std :: set <unsigned> :: iterator it = neigh.begin () ;
00111          it != neigh.end () ;
00112          it ++) {      
00113       unsigned l = _map [* it].size () ;
00114       if (len_min_entry == l)
00115         cand.push_back (* it) ;
00116     }
00117        
00118     if (! cand.size ()) {
00119       
00120       /* Oh no ! Implicit mutation */      
00121       for (unsigned j = 0 ; j < len ; j ++)
00122         if (! visited [j])
00123           cand.push_back (j) ;
00124     }
00125 
00126     cur_vertex = cand [rng.random (cand.size ())] ;
00127     
00128     add_vertex (cur_vertex, __child) ;
00129   } 
00130 }
00131 
00132 bool EdgeXover :: operator () (Route & __route1, Route & __route2) {
00133   
00134   // Init. copy
00135   Route par [2] ;
00136   par [0] = __route1 ;
00137   par [1] = __route2 ;
00138   
00139   cross (par [0], par [1], __route1) ;
00140   cross (par [1], par [0], __route2) ;
00141   
00142   __route1.invalidate () ;
00143   __route2.invalidate () ;
00144 
00145   return true ;
00146 }

Generated on Mon Oct 8 11:16:45 2007 for ParadisEO-PEOMovingObjects by  doxygen 1.4.7