tree modifications

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@512 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
jboisson 2007-07-04 14:05:19 +00:00
commit 61c0424eed
38 changed files with 395 additions and 458 deletions

73
trunk/paradiseo-mo/tutorial/examples/tsp/CMakeLists.txt Normal file → Executable file
View file

@ -1,8 +1,77 @@
######################################################################################
### 1) Where must cmake go now ?
### 1) Include the sources
######################################################################################
SUBDIRS(src)
INCLUDE_DIRECTORIES(${EO_SRC_DIR})
INCLUDE_DIRECTORIES(${MO_SRC_DIR})
######################################################################################
######################################################################################
### 2) Define your target(s): just the tsp here
######################################################################################
SET(TSP_LIB_OUTPUT_PATH ${TSP_EXAMPLE_DIR}/build)
SET(LIBRARY_OUTPUT_PATH ${TSP_LIB_OUTPUT_PATH})
SET (TSP_SOURCES graph.cpp
route_init.cpp
route_eval.cpp
part_route_eval.cpp
edge_xover.cpp
order_xover.cpp
route_valid.cpp
partial_mapped_xover.cpp
city_swap.cpp
two_opt.cpp
two_opt_init.cpp
two_opt_next.cpp
two_opt_incr_eval.cpp
two_opt_tabu_list.cpp
two_opt_rand.cpp)
# --> UNIX
IF(UNIX)
ADD_LIBRARY(tsp STATIC ${TSP_SOURCES})
# --> WIN
ELSEIF(WIN32)
ADD_LIBRARY(tsp STATIC ${TSP_SOURCES})
ENDIF(UNIX)
######################################################################################
######################################################################################
### 3) Optionnal: define your target(s)'s version: no effect for windows
######################################################################################
SET(TSP_VERSION "1.0.beta")
SET_TARGET_PROPERTIES(tsp PROPERTIES VERSION "${TSP_VERSION}")
######################################################################################
######################################################################################
### 4) Windows advanced config - especially for Microsoft Visual Studio 8
######################################################################################
IF(CMAKE_CXX_COMPILER MATCHES cl)
IF(NOT WITH_SHARED_LIBS)
IF(CMAKE_GENERATOR STREQUAL "Visual Studio 8 2005")
SET(CMAKE_CXX_FLAGS "/nologo /W3 /Gy")
SET(CMAKE_CXX_FLAGS_DEBUG "/MTd /Z7 /Od")
SET(CMAKE_CXX_FLAGS_RELEASE "/MT /O2")
SET(CMAKE_CXX_FLAGS_MINSIZEREL "/MT /O2")
SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/MTd /Z7 /Od")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:CONSOLE")
ENDIF(CMAKE_GENERATOR STREQUAL "Visual Studio 8 2005")
ENDIF(NOT WITH_SHARED_LIBS)
ENDIF(CMAKE_CXX_COMPILER MATCHES cl)
######################################################################################

View file

@ -0,0 +1,24 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "city_swap.cpp"
// (c) OPAC Team, LIFL, 2002-2006
/* TEXT LICENCE
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include <utils/eoRNG.h>
#include "city_swap.h"
bool CitySwap :: operator () (Route & __route) {
std :: swap (__route [rng.random (__route.size ())],
__route [rng.random (__route.size ())]) ;
__route.invalidate () ;
return true ;
}

View file

@ -0,0 +1,29 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "city_swap.h"
// (c) OPAC Team, LIFL, 2002-2006
/* TEXT LICENCE
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef city_swap_h
#define city_swap_h
#include <eoOp.h>
#include "route.h"
/** Its swaps two vertices
randomly choosen */
class CitySwap : public eoMonOp <Route> {
public :
bool operator () (Route & __route) ;
} ;
#endif

View file

@ -0,0 +1,142 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "edge_xover.cpp"
// (c) OPAC Team, LIFL, 2003-2006
/* LICENCE TEXT
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include <assert.h>
#include <utils/eoRNG.h>
#include "edge_xover.h"
#include "route_valid.h"
#define MAXINT 1000000
void
EdgeXover :: build_map (const Route & __par1, const Route & __par2)
{
unsigned int len = __par1.size () ;
/* Initialization */
_map.clear () ;
_map.resize (len) ;
for (unsigned int i = 0 ; i < len ; i ++)
{
_map [__par1 [i]].insert (__par1 [(i + 1) % len]) ;
_map [__par2 [i]].insert (__par2 [(i + 1) % len]) ;
_map [__par1 [i]].insert (__par1 [(i - 1 + len) % len]) ;
_map [__par2 [i]].insert (__par2 [(i - 1 + len) % len]) ;
}
visited.clear () ;
visited.resize (len, false) ;
}
void
EdgeXover :: remove_entry (unsigned int __vertex, std :: vector <std :: set <unsigned int> > & __map)
{
std :: set <unsigned int> & neigh = __map [__vertex] ;
for (std :: set <unsigned int> :: iterator it = neigh.begin () ; it != neigh.end () ; it ++)
{
__map [* it].erase (__vertex) ;
}
}
void
EdgeXover :: add_vertex (unsigned int __vertex, Route & __child)
{
visited [__vertex] = true ;
__child.push_back (__vertex) ;
remove_entry (__vertex, _map) ; /* Removing entries */
}
void
EdgeXover :: cross (const Route & __par1, const Route & __par2, Route & __child) {
build_map (__par1, __par2) ;
unsigned int len = __par1.size () ;
/* Go ! */
__child.clear () ;
unsigned int cur_vertex = rng.random (len) ;
add_vertex (cur_vertex, __child) ;
for (unsigned int i = 1 ; i < len ; i ++) {
unsigned int len_min_entry = MAXINT ;
std :: set <unsigned int> & neigh = _map [cur_vertex] ;
for (std :: set <unsigned int> :: iterator it = neigh.begin () ; it != neigh.end () ; it ++)
{
unsigned int l = _map [* it].size () ;
if (len_min_entry > l)
{
len_min_entry = l ;
}
}
std :: vector <unsigned int> cand ; /* Candidates */
for (std :: set <unsigned> :: iterator it = neigh.begin () ; it != neigh.end () ; it ++)
{
unsigned int l = _map [* it].size () ;
if (len_min_entry == l)
{
cand.push_back (* it) ;
}
}
if (! cand.size ())
{
/* Oh no ! Implicit mutation */
for (unsigned int j = 0 ; j < len ; j ++)
{
if (! visited [j])
{
cand.push_back (j) ;
}
}
}
cur_vertex = cand [rng.random (cand.size ())] ;
add_vertex (cur_vertex, __child) ;
}
}
bool
EdgeXover :: operator () (Route & __route1, Route & __route2)
{
// Init. copy
Route par [2] ;
par [0] = __route1 ;
par [1] = __route2 ;
cross (par [0], par [1], __route1) ;
cross (par [1], par [0], __route2) ;
assert (valid (__route1)) ;
assert (valid (__route2)) ;
__route1.invalidate () ;
__route2.invalidate () ;
return true ;
}

View file

@ -0,0 +1,47 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "edge_xover.h"
// (c) OPAC Team, LIFL, 2003-2006
/* TEXT LICENCE
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef edge_xover_h
#define edge_xover_h
#include <vector>
#include <set>
#include <eoOp.h>
#include "route.h"
/** Edge Crossover */
class EdgeXover : public eoQuadOp <Route>
{
public :
bool operator () (Route & __route1, Route & __route2) ;
private :
void cross (const Route & __par1, const Route & __par2, Route & __child) ; /* Binary */
void remove_entry (unsigned int __vertex, std :: vector <std :: set <unsigned> > & __map) ;
/* Updating the map of entries */
void build_map (const Route & __par1, const Route & __par2) ;
void add_vertex (unsigned int __vertex, Route & __child) ;
std :: vector <std :: set <unsigned int> > _map ; /* The handled map */
std :: vector <bool> visited ; /* Vertices that are already visited */
} ;
#endif

View file

@ -0,0 +1,90 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "graph.cpp"
// (c) OPAC Team, LIFL, 2003-2006
/* LICENCE TEXT
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include <fstream>
#include <iostream>
#include <math.h>
#include "graph.h"
namespace Graph {
static std :: vector <std :: pair <double, double> > vectCoord ; // Coordinates
static std :: vector <std :: vector <unsigned int> > dist ; // Distances Mat.
unsigned size ()
{
return dist.size () ;
}
void computeDistances ()
{
// Dim.
unsigned int numCities = vectCoord.size () ;
dist.resize (numCities) ;
for (unsigned int i = 0 ; i < dist.size () ; i ++)
{
dist [i].resize (numCities) ;
}
// Computations.
for (unsigned int i = 0 ; i < dist.size () ; i ++)
{
for (unsigned int j = i + 1 ; j < dist.size () ; j ++)
{
double distX = (double)(vectCoord [i].first - vectCoord [j].first) ;
double distY = (double)(vectCoord [i].second - vectCoord [j].second) ;
dist [i] [j] = dist [j] [i] = (unsigned) (sqrt ((float) (distX * distX + distY * distY)) + 0.5) ;
}
}
}
void load (const char * __fileName)
{
std :: ifstream f (__fileName) ;
std :: cout << ">> Loading [" << __fileName << "]" << std :: endl ;
if (f)
{
unsigned int num_vert ;
f >> num_vert ;
vectCoord.resize (num_vert) ;
for (unsigned int i = 0 ; i < num_vert ; i ++)
{
f >> vectCoord [i].first >> vectCoord [i].second ;
}
f.close () ;
computeDistances () ;
}
else
{
std :: cout << __fileName << " doesn't exist !!!" << std :: endl ;
// Bye !!!
exit (1) ;
}
}
float distance (unsigned int __from, unsigned int __to)
{
return (float)(dist [__from] [__to]) ;
}
}

View file

@ -0,0 +1,30 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "graph.h"
// (c) OPAC Team, LIFL, 2003-2006
/* LICENCE TEXT
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef graph_h
#define graph_h
#include <vector>
#include <utility>
namespace Graph
{
void load (const char * __file_name) ;
/* Loading cities
(expressed by their coordinates)
from the given file name */
float distance (unsigned int __from, unsigned int __to) ;
unsigned int size () ; // How many cities ?
}
#endif

View file

@ -0,0 +1,25 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "mix.h"
// (c) OPAC Team, LIFL, 2003-2006
/* LICENCE TEXT
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef mix_h
#define mix_h
#include <utils/eoRNG.h>
template <class T> void mix (std :: vector <T> & __vect)
{
for (unsigned int i = 0 ; i < __vect.size () ; i ++)
{
std :: swap (__vect [i], __vect [rng.random (__vect.size ())]) ;
}
}
#endif

View file

@ -0,0 +1,94 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "order_xover.cpp"
// (c) OPAC Team, LIFL, 2002-2006
/* LICENCE TEXT
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include <assert.h>
#include <vector>
#include <utils/eoRNG.h>
#include "order_xover.h"
#include "route_valid.h"
void OrderXover :: cross (const Route & __par1, const Route & __par2, Route & __child)
{
unsigned int cut = rng.random (__par1.size ()) ;
/* To store vertices that have
already been crossed */
std::vector<bool> v;
v.resize(__par1.size());
for (unsigned int i = 0 ; i < __par1.size () ; i ++)
{
v [i] = false ;
}
/* Copy of the left partial
route of the first parent */
for (unsigned int i = 0 ; i < cut ; i ++)
{
__child [i] = __par1 [i] ;
v [__par1 [i]] = true ;
}
/* Searching the vertex of the second path, that ended
the previous first one */
unsigned int from = 0 ;
for (unsigned int i = 0 ; i < __par2.size () ; i ++)
{
if (__par2 [i] == __child [cut - 1])
{
from = i ;
break ;
}
}
/* Selecting a direction
Left or Right */
char direct = rng.flip () ? 1 : -1 ;
/* Copy of the left vertices from
the second parent path */
unsigned int l = cut ;
for (unsigned int i = 0 ; i < __par2.size () ; i ++)
{
unsigned int bidule /* :-) */ = (direct * i + from + __par2.size ()) % __par2.size () ;
if (! v [__par2 [bidule]])
{
__child [l ++] = __par2 [bidule] ;
v [__par2 [bidule]] = true ;
}
}
v.clear();
}
bool OrderXover :: operator () (Route & __route1, Route & __route2)
{
// Init. copy
Route par [2] ;
par [0] = __route1 ;
par [1] = __route2 ;
cross (par [0], par [1], __route1) ;
cross (par [1], par [0], __route2) ;
assert (valid (__route1)) ;
assert (valid (__route2)) ;
__route1.invalidate () ;
__route2.invalidate () ;
return true ;
}

View file

@ -0,0 +1,32 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "order_xover.h"
// (c) OPAC Team, LIFL, 2003-2006
/* LICENCE TEXT
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef order_xover_h
#define order_xover_h
#include <eoOp.h>
#include "route.h"
/** Order Crossover */
class OrderXover : public eoQuadOp <Route>
{
public :
bool operator () (Route & __route1, Route & __route2) ;
private :
void cross (const Route & __par1, const Route & __par2, Route & __child) ;
} ;
#endif

View file

@ -0,0 +1,27 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "part_route_eval.cpp"
// (c) OPAC Team, LIFL, 2003-2006
/* LICENCE TEXT
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include "part_route_eval.h"
#include "graph.h"
PartRouteEval :: PartRouteEval (float __from, float __to) : from (__from), to (__to) {}
void PartRouteEval :: operator () (Route & __route)
{
float len = 0 ;
for (unsigned int i = (unsigned int) (__route.size () * from) ; i < (unsigned int ) (__route.size () * to) ; i ++)
{
len -= Graph :: distance (__route [i], __route [(i + 1) % Graph :: size ()]) ;
}
__route.fitness (len) ;
}

View file

@ -0,0 +1,37 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "part_route_eval.h"
// (c) OPAC Team, LIFL, 2003-2006
/* LICENCE TEXT
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef part_route_eval_h
#define part_route_eval_h
#include <eoEvalFunc.h>
#include "route.h"
/** Route Evaluator */
class PartRouteEval : public eoEvalFunc <Route>
{
public :
/** Constructor */
PartRouteEval (float __from, float __to) ;
void operator () (Route & __route) ;
private :
float from, to ;
} ;
#endif

View file

@ -0,0 +1,20 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "part_two_opt_init.cpp"
// (c) OPAC Team, LIFL, 2003-2006
/* LICENCE TEXT
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include <utils/eoRNG.h>
#include "part_two_opt_init.h"
void PartTwoOptInit :: operator () (TwoOpt & __move, const Route & __route)
{
__move.first = rng.random (__route.size () - 6) ;
__move.second = __move.first + 2 ;
}

View file

@ -0,0 +1,29 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "part_two_opt_init.h"
// (c) OPAC Team, LIFL, 2003-2006
/* LICENCE TEXT
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef part_two_opt_init_h
#define part_two_opt_init_h
#include <moMoveInit.h>
#include "two_opt.h"
/** It sets the first couple of edges */
class PartTwoOptInit : public moMoveInit <TwoOpt>
{
public :
void operator () (TwoOpt & __move, const Route & __route) ;
} ;
#endif

View file

@ -0,0 +1,32 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "part_two_opt_next.cpp"
// (c) OPAC Team, LIFL, 2003-2006
/* LICENCE TEXT
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include "part_two_opt_next.h"
#include "graph.h"
bool TwoOptNext :: operator () (TwoOpt & __move, const Route & __route)
{
if (__move.first == Graph :: size () - 4 && __move.second == __move.first + 2)
{
return false ;
}
else
{
__move.second ++ ;
if (__move.second == Graph :: size () - 1)
{
__move.first ++ ;
__move.second = __move.first + 2 ;
}
return true ;
}
}

View file

@ -0,0 +1,28 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "part_two_opt_next.h"
// (c) OPAC Team, LIFL, 2003-2006
/* LICENCE TEXT
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef part_two_opt_next_h
#define part_two_opt_next_h
#include <moNextMove.h>
#include "two_opt.h"
/** It updates a couple of edges */
class PartTwoOptNext : public moNextMove <TwoOpt>
{
public :
bool operator () (TwoOpt & __move, const Route & __route) ;
} ;
#endif

View file

@ -0,0 +1,93 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "partial_mapped_xover.cpp"
// (c) OPAC Team, LIFL, 2003-2006
/* TEXT LICENCE
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include <assert.h>
#include <vector>
#include <utils/eoRNG.h>
#include "partial_mapped_xover.h"
#include "route_valid.h"
#include "mix.h"
void PartialMappedXover :: repair (Route & __route, unsigned __cut1, unsigned __cut2)
{
std::vector<unsigned int> v; // Number of times a cities are visited ...
v.resize(__route.size ());
for (unsigned int i = 0 ; i < __route.size () ; i ++)
{
v [i] = 0 ;
}
for (unsigned int i = 0 ; i < __route.size () ; i ++)
{
v [__route [i]] ++ ;
}
std :: vector <unsigned int> vert ;
for (unsigned int i = 0 ; i < __route.size () ; i ++)
{
if (! v [i])
{
vert.push_back (i) ;
}
}
mix (vert) ;
for (unsigned int i = 0 ; i < __route.size () ; i ++)
{
if (i < __cut1 || i >= __cut2)
{
if (v [__route [i]] > 1)
{
__route [i] = vert.back () ;
vert.pop_back () ;
}
}
}
v.clear();
}
bool PartialMappedXover :: operator () (Route & __route1, Route & __route2)
{
unsigned int cut1 = rng.random (__route1.size ()), cut2 = rng.random (__route2.size ()) ;
if (cut2 < cut1)
{
std :: swap (cut1, cut2) ;
}
// Between the cuts
for (unsigned int i = cut1 ; i < cut2 ; i ++)
{
std :: swap (__route1 [i], __route2 [i]) ;
}
// Outside the cuts
repair (__route1, cut1, cut2) ;
repair (__route2, cut1, cut2) ;
// Debug
assert (valid (__route1)) ;
assert (valid (__route2)) ;
__route1.invalidate () ;
__route2.invalidate () ;
return true ;
}

View file

@ -0,0 +1,31 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "partial_mapped_xover.h"
// (c) OPAC Team, LIFL, 2003-2006
/* LICENCE TEXT
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef partial_mapped_xover_h
#define partial_mapped_xover_h
#include <eoOp.h>
#include "route.h"
/** Partial Mapped Crossover */
class PartialMappedXover : public eoQuadOp <Route> {
public :
bool operator () (Route & __route1, Route & __route2) ;
private :
void repair (Route & __route, unsigned __cut1, unsigned __cut2) ;
} ;
#endif

View file

@ -0,0 +1,19 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "route.h"
// (c) OPAC Team, LIFL, 2003-2006
/* LICENCE TEXT
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef route_h
#define route_h
#include <eoVector.h>
typedef eoVector <float, unsigned int> Route ; // [Fitness (- length), Gene (city)]
#endif

View file

@ -0,0 +1,26 @@
// -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
// "route_eval.cpp"
// (c) OPAC Team, LIFL, 2003-2006
/* TEXT LICENCE
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include "route_eval.h"
#include "graph.h"
void RouteEval :: operator () (Route & __route)
{
float len = 0 ;
for (unsigned int i = 0 ; i < Graph :: size () ; i ++)
{
len -= Graph :: distance (__route [i], __route [(i + 1) % Graph :: size ()]) ;
}
__route.fitness (len) ;
}

Some files were not shown because too many files have changed in this diff Show more