move tsp source files

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@519 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
legrand 2007-07-05 08:37:11 +00:00
commit 2dc84beef3
43 changed files with 0 additions and 9 deletions

View file

@ -1,9 +0,0 @@
######################################################################################
### 1) Where must cmake go now ?
######################################################################################
SUBDIRS(src)
######################################################################################

View file

@ -0,0 +1,21 @@
// "city_swap.cpp"
// (c) OPAC Team, LIFL, 2002
/*
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,26 @@
// "city_swap.h"
// (c) OPAC Team, LIFL, 2002
/*
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,98 @@
// "data.cpp"
// (c) OPAC Team, LIFL, January 2006
/*
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <utils/eoParser.h>
#include "data.h"
#include "node.h"
#define MAX_TRASH_LENGTH 1000
#define MAX_FIELD_LENGTH 1000
#define MAX_LINE_LENGTH 1000
static void getNextField (FILE * __f, char * __buff) {
char trash [MAX_TRASH_LENGTH];
fscanf (__f, "%[ \t:\n]", trash); /* Discarding sep. */
fscanf (__f, "%[^:\n]", __buff); /* Reading the field */
fgetc (__f);
}
static void getLine (FILE * __f, char * __buff) {
char trash [MAX_TRASH_LENGTH];
fscanf (__f, "%[ \t:\n]", trash); /* Discarding sep. */
fscanf (__f, "%[^\n]", __buff); /* Reading the line */
}
void loadData (const char * __filename) {
FILE * f = fopen (__filename, "r");
if (f) {
printf ("Loading '%s'.\n", __filename);
char field [MAX_FIELD_LENGTH];
getNextField (f, field); /* Name */
assert (strstr (field, "NAME"));
getNextField (f, field);
printf ("NAME: %s.\n", field);
getNextField (f, field); /* Comment */
assert (strstr (field, "COMMENT"));
getLine (f, field);
printf ("COMMENT: %s.\n", field);
getNextField (f, field); /* Type */
assert (strstr (field, "TYPE"));
getNextField (f, field);
printf ("TYPE: %s.\n", field);
getNextField (f, field); /* Dimension */
assert (strstr (field, "DIMENSION"));
getNextField (f, field);
printf ("DIMENSION: %s.\n", field);
numNodes = atoi (field);
getNextField (f, field); /* Edge weight type */
assert (strstr (field, "EDGE_WEIGHT_TYPE"));
getNextField (f, field);
printf ("EDGE_WEIGHT_TYPE: %s.\n", field);
getNextField (f, field); /* Node coord section */
assert (strstr (field, "NODE_COORD_SECTION"));
loadNodes (f);
getNextField (f, field); /* End of file */
assert (strstr (field, "EOF"));
printf ("EOF.\n");
}
else {
fprintf (stderr, "Can't open '%s'.\n", __filename);
exit (1);
}
}
void loadData (eoParser & __parser) {
/* Getting the path of the instance */
eoValueParam <std :: string> param ("", "inst", "Path of the instance") ;
__parser.processParam (param) ;
loadData (param.value ().c_str ());
}

View file

@ -0,0 +1,18 @@
// "data.h"
// (c) OPAC Team, LIFL, January 2006
/*
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef __data_h
#define __data_h
#include <utils/eoParser.h>
extern void loadData (const char * __filename);
extern void loadData (eoParser & __parser);
#endif

View file

@ -0,0 +1,117 @@
// "display.cpp"
// (c) OPAC Team, LIFL, January 2006
/*
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include <iostream>
#include <fstream>
#include <X11/Xlib.h>
#include "display.h"
#include "node.h"
#include "opt_route.h"
#define BORDER 20
#define RATIO 0.5
#define screen_width 1024
#define screen_height 768
static const char * filename;
/* Computed coordinates */
static unsigned * X_new_coord, * Y_new_coord ;
/* this variable will contain the handle to the returned graphics context. */
static GC gc;
/* this variable will contain the pointer to the Display structure */
static Display* disp;
/* this variable will store the ID of the newly created window. */
static Window win;
static int screen;
/* Create a new backing pixmap of the appropriate size */
/* Best tour */
/*
gdk_gc_set_line_attributes (gc, 2, GDK_LINE_ON_OFF_DASH, GDK_CAP_NOT_LAST, GDK_JOIN_MITER) ;
gdk_gc_set_foreground (gc, & color_green) ;
for (int i = 0 ; i < (int) numNodes ; i ++) {
gdk_draw_line (pixmap, gc,
X_new_coord [opt_route [i]],
Y_new_coord [opt_route [i]],
X_new_coord [opt_route [(i + 1) % numNodes]],
Y_new_coord [opt_route [(i + 1) % numNodes]]);
}*/
void openMainWindow (const char * __filename) {
filename = __filename;
/* Map */
int map_width = (int) (X_max - X_min);
int map_height = (int) (Y_max - Y_min);
int map_side = std :: max (map_width, map_height);
/* Calculate the window's width and height. */
int win_width = (int) (screen_width * RATIO * map_width / map_side);
int win_height = (int) (screen_height * RATIO * map_height / map_side);
/* Computing the coordinates */
X_new_coord = new unsigned [numNodes];
Y_new_coord = new unsigned [numNodes];
for (unsigned i = 0; i < numNodes; i ++) {
X_new_coord [i] = (unsigned) (win_width * (1.0 - (X_coord [i] - X_min) / map_width) + BORDER);
Y_new_coord [i] = (unsigned) (win_height * (1.0 - (Y_coord [i] - Y_min) / map_height) + BORDER);
}
/* Initialisation */
XGCValues val ;
disp = XOpenDisplay (NULL) ;
screen = DefaultScreen (disp) ;
win = XCreateSimpleWindow (disp, RootWindow (disp, screen), 0, 0, win_width + 2 * BORDER, win_height + 2 * BORDER, 2, BlackPixel (disp, screen), WhitePixel (disp, screen)) ;
val.foreground = BlackPixel(disp, screen) ;
val.background = WhitePixel(disp, screen) ;
gc = XCreateGC (disp, win, GCForeground | GCBackground, & val) ;
XMapWindow (disp, win) ;
XFlush (disp) ;
while (true) {
XClearWindow (disp, win) ;
/* Vertices as circles */
for (unsigned i = 1 ; i < numNodes ; i ++)
XDrawArc (disp, win, gc, X_new_coord [i] - 1, Y_new_coord [i] - 1, 3, 3, 0, 364 * 64) ;
/* New tour */
std :: ifstream f (filename);
if (f) {
Route route;
f >> route;
f.close ();
for (int i = 0; i < (int) numNodes; i ++)
XDrawLine (disp, win, gc,
X_new_coord [route [i]],
Y_new_coord [route [i]],
X_new_coord [route [(i + 1) % numNodes]],
Y_new_coord [route [(i + 1) % numNodes]]);
}
XFlush (disp) ;
sleep (1) ;
}
}

View file

@ -0,0 +1,16 @@
// "display.h"
// (c) OPAC Team, LIFL, January 2006
/*
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef __display_h
#define __display_h
#include "route.h"
extern void openMainWindow (const char * __filename);
#endif

View file

@ -0,0 +1,22 @@
// "display_best_route.cpp"
// (c) OPAC Team, LIFL, January 2006
/*
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include "display_best_route.h"
#include "display.h"
DisplayBestRoute :: DisplayBestRoute (eoPop <Route> & __pop
) : pop (__pop) {
}
void DisplayBestRoute :: operator () () {
displayRoute (pop.best_element ());
}

View file

@ -0,0 +1,32 @@
// "display_best_route.h"
// (c) OPAC Team, LIFL, January 2006
/*
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef __display_best_route_h
#define __display_best_route_h
#include <utils/eoUpdater.h>
#include <eoPop.h>
#include "route.h"
class DisplayBestRoute : public eoUpdater {
public :
DisplayBestRoute (eoPop <Route> & __pop);
void operator () ();
private :
eoPop <Route> & pop;
};
#endif

View file

@ -0,0 +1,118 @@
// "edge_xover.cpp"
// (c) OPAC Team, LIFL, 2003
/*
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include <assert.h>
#include <values.h>
#include <utils/eoRNG.h>
#include "edge_xover.h"
void EdgeXover :: build_map (const Route & __par1, const Route & __par2) {
unsigned len = __par1.size () ;
/* Initialization */
_map.clear () ;
_map.resize (len) ;
for (unsigned 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 __vertex, std :: vector <std :: set <unsigned> > & __map) {
std :: set <unsigned> & neigh = __map [__vertex] ;
for (std :: set <unsigned> :: iterator it = neigh.begin () ;
it != neigh.end () ;
it ++)
__map [* it].erase (__vertex) ;
}
void EdgeXover :: add_vertex (unsigned __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 len = __par1.size () ;
/* Go ! */
__child.clear () ;
unsigned cur_vertex = rng.random (len) ;
add_vertex (cur_vertex, __child) ;
for (unsigned i = 1 ; i < len ; i ++) {
unsigned len_min_entry = MAXINT ;
std :: set <unsigned> & neigh = _map [cur_vertex] ;
for (std :: set <unsigned> :: iterator it = neigh.begin () ;
it != neigh.end () ;
it ++) {
unsigned l = _map [* it].size () ;
if (len_min_entry > l)
len_min_entry = l ;
}
std :: vector <unsigned> cand ; /* Candidates */
for (std :: set <unsigned> :: iterator it = neigh.begin () ;
it != neigh.end () ;
it ++) {
unsigned l = _map [* it].size () ;
if (len_min_entry == l)
cand.push_back (* it) ;
}
if (! cand.size ()) {
/* Oh no ! Implicit mutation */
for (unsigned 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) ;
__route1.invalidate () ;
__route2.invalidate () ;
return true ;
}

View file

@ -0,0 +1,43 @@
// "edge_xover.h"
// (c) OPAC Team, LIFL, 2003
/*
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 __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 __vertex, Route & __child) ;
std :: vector <std :: set <unsigned> > _map ; /* The handled map */
std :: vector <bool> visited ; /* Vertices that are already visited */
} ;
#endif

View file

@ -0,0 +1,17 @@
// "merge_route_eval.cpp"
// (c) OPAC Team, LIFL, 2005
/*
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include "merge_route_eval.h"
void MergeRouteEval :: operator () (Route & __route, const int & __part_fit) {
int len = __route.fitness ();
len += __part_fit;
__route.fitness (len);
}

View file

@ -0,0 +1,24 @@
// "merge_route_eval.h"
// (c) OPAC Team, LIFL, 2005
/*
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef __merge_route_eval_h
#define __merge_route_eval_h
#include <peoAggEvalFunc.h>
#include "route.h"
class MergeRouteEval : public peoAggEvalFunc <Route> {
public :
void operator () (Route & __route, const int & __part_fit) ;
};
#endif

View file

@ -0,0 +1,25 @@
/*
file: 'mix.h'
author: S. CAHON
mail: paradiseo-help@lists.gforge.inria.fr
date: dec. 2005
*/
#ifndef __mix_h
#define __mix_h
#include <vector>
#include <utils/eoRNG.h>
template <class T> void mix (std :: vector <T> & __v) {
unsigned len = __v.size () ;
for (unsigned i = 0 ; i < len ; i ++)
std :: swap (__v [i], __v [rng.random (len)]) ;
}
#endif

View file

@ -0,0 +1,77 @@
// "node.cpp"
// (c) OPAC Team, LIFL, January 2006
/*
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include <math.h>
#include <values.h>
#include "node.h"
unsigned numNodes; /* Number of nodes */
//static unsigned * * dist; /* Square matrix of distances */
double * X_coord, * Y_coord;
double X_min = MAXDOUBLE, X_max = MINDOUBLE, Y_min = MAXDOUBLE, Y_max = MINDOUBLE;
void loadNodes (FILE * __f) {
/* Coord */
X_coord = new double [numNodes];
Y_coord = new double [numNodes];
unsigned num;
for (unsigned i = 0; i < numNodes; i ++) {
fscanf (__f, "%u%lf%lf", & num, X_coord + i, Y_coord + i);
if (X_coord [i] < X_min)
X_min = X_coord [i];
if (X_coord [i] > X_max)
X_max = X_coord [i];
if (Y_coord [i] < Y_min)
Y_min = Y_coord [i];
if (Y_coord [i] > Y_max)
Y_max = Y_coord [i];
}
/* Allocation */
/*
dist = new unsigned * [numNodes];
for (unsigned i = 0; i < numNodes; i ++)
dist [i] = new unsigned [numNodes];
*/
/* Computation of the distances */
/*
for (unsigned i = 0; i < numNodes; i ++) {
dist [i] [i] = 0;
for (unsigned j = 0; j < numNodes; j ++) {
double dx = X_coord [i] - X_coord [j], dy = Y_coord [i] - Y_coord [j];
dist [i] [j] = dist [j] [i] = (unsigned) (sqrt (dx * dx + dy * dy) + 0.5) ;
}
}*/
}
unsigned distance (Node __from, Node __to) {
// return dist [__from] [__to];
double dx = X_coord [__from] - X_coord [__to], dy = Y_coord [__from] - Y_coord [__to];
return (unsigned) (sqrt (dx * dx + dy * dy) + 0.5) ;
}

View file

@ -0,0 +1,26 @@
// "node.h"
// (c) OPAC Team, LIFL, January 2006
/*
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef __node_h
#define __node_h
#include <stdio.h>
typedef unsigned Node;
extern double X_min, X_max, Y_min, Y_max;
extern double * X_coord, * Y_coord;
extern unsigned numNodes; /* Number of nodes */
extern void loadNodes (FILE * __f);
extern unsigned distance (Node __from, Node __to);
#endif

View file

@ -0,0 +1,107 @@
// "opt_route.cpp"
// (c) OPAC Team, LIFL, January 2006
/*
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include "opt_route.h"
#define MAX_TRASH_LENGTH 1000
#define MAX_FIELD_LENGTH 1000
#define MAX_LINE_LENGTH 1000
static void getNextField (FILE * __f, char * __buff) {
char trash [MAX_TRASH_LENGTH];
fscanf (__f, "%[ \t:\n]", trash); /* Discarding sep. */
fscanf (__f, "%[^:\n]", __buff); /* Reading the field */
fgetc (__f);
}
static void getLine (FILE * __f, char * __buff) {
char trash [MAX_TRASH_LENGTH];
fscanf (__f, "%[ \t:\n]", trash); /* Discarding sep. */
fscanf (__f, "%[^\n]", __buff); /* Reading the line */
}
static void loadBestRoute (FILE * __f) {
opt_route.clear ();
for (unsigned i = 0; i < numNodes; i ++) {
Node node;
fscanf (__f, "%u", & node);
opt_route.push_back (node - 1);
}
int d; /* -1 ! */
fscanf (__f, "%d", & d);
}
void loadOptimumRoute (const char * __filename) {
FILE * f = fopen (__filename, "r");
if (f) {
printf ("Loading '%s'.\n", __filename);
char field [MAX_FIELD_LENGTH];
getNextField (f, field); /* Name */
assert (strstr (field, "NAME"));
getNextField (f, field);
//printf ("NAME: %s.\n", field);
getNextField (f, field); /* Comment */
assert (strstr (field, "COMMENT"));
getLine (f, field);
// printf ("COMMENT: %s.\n", field);
getNextField (f, field); /* Type */
assert (strstr (field, "TYPE"));
getNextField (f, field);
//printf ("TYPE: %s.\n", field);
getNextField (f, field); /* Dimension */
assert (strstr (field, "DIMENSION"));
getNextField (f, field);
// printf ("DIMENSION: %s.\n", field);
numNodes = atoi (field);
getNextField (f, field); /* Tour section */
assert (strstr (field, "TOUR_SECTION"));
loadBestRoute (f);
getNextField (f, field); /* End of file */
assert (strstr (field, "EOF"));
//printf ("EOF.\n");
printf ("The length of the best route is %u.\n", length (opt_route));
}
else {
fprintf (stderr, "Can't open '%s'.\n", __filename);
exit (1);
}
}
void loadOptimumRoute (eoParser & __parser) {
/* Getting the path of the instance */
eoValueParam <std :: string> param ("", "optimumTour", "Optimum tour") ;
__parser.processParam (param) ;
if (strlen (param.value ().c_str ()))
loadOptimumRoute (param.value ().c_str ());
else
opt_route.fitness (0);
}
Route opt_route; /* Optimum route */

View file

@ -0,0 +1,23 @@
// "opt_route.h"
// (c) OPAC Team, LIFL, January 2006
/*
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#ifndef __opt_route_h
#define __opt_route_h
#include <cassert>
#include <utils/eoParser.h>
#include "route.h"
extern void loadOptimumRoute (const char * __filename);
extern void loadOptimumRoute (eoParser & __parser);
extern Route opt_route; /* Optimum route */
#endif

View file

@ -0,0 +1,64 @@
// "order_xover.cpp"
// (c) OPAC Team, LIFL, 2002
/*
Contact: paradiseo-help@lists.gforge.inria.fr
*/
#include <assert.h>
#include <utils/eoRNG.h>
#include "order_xover.h"
void OrderXover :: cross (const Route & __par1, const Route & __par2, Route & __child) {
unsigned cut2 = 1 + rng.random (numNodes) ;
unsigned cut1 = rng.random (cut2);
unsigned l = 0;
/* To store vertices that have already been crossed */
std :: vector <bool> v (numNodes, false);
/* Copy of the left partial route of the first parent */
for (unsigned i = cut1 ; i < cut2 ; i ++) {
__child [l ++] = __par1 [i] ;
v [__par1 [i]] = true ;
}
/* Searching the vertex of the second path, that ended the previous first one */
unsigned from = 0 ;
for (unsigned i = 0; i < numNodes; i ++)
if (__par2 [i] == __child [cut2 - 1]) {
from = i ;
break ;
}
/* Selecting a direction (Left or Right) */
char direct = rng.flip () ? 1 : -1 ;
for (unsigned i = 0; i < numNodes + 1; i ++) {
unsigned bidule = (direct * i + from + numNodes) % numNodes;
if (! v [__par2 [bidule]]) {
__child [l ++] = __par2 [bidule] ;
v [__par2 [bidule]] = true ;
}
}
}
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) ;
__route1.invalidate () ;
__route2.invalidate () ;
return true ;
}

View file

@ -0,0 +1,28 @@
// "order_xover.h"
// (c) OPAC Team, LIFL, 2003
/*
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

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