New style for PEO

git-svn-id: svn://scm.gforge.inria.fr/svnroot/paradiseo@789 331e1502-861f-0410-8da2-ba01fb791d7f
This commit is contained in:
canape 2007-11-16 11:34:20 +00:00
commit 9c87b3b0c0
132 changed files with 3781 additions and 3396 deletions

View file

@ -1,4 +1,4 @@
/*
/*
* <city_swap.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -38,12 +38,13 @@
#include "city_swap.h"
bool CitySwap :: operator () (Route & __route) {
bool CitySwap :: operator () (Route & __route)
{
std :: swap (__route [rng.random (__route.size ())],
__route [rng.random (__route.size ())]) ;
__route [rng.random (__route.size ())]) ;
__route.invalidate () ;
return true ;
}

View file

@ -1,4 +1,4 @@
/*
/*
* <city_swap.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -43,12 +43,13 @@
/** Its swaps two vertices
randomly choosen */
class CitySwap : public eoMonOp <Route> {
public :
bool operator () (Route & __route) ;
} ;
class CitySwap : public eoMonOp <Route>
{
public :
bool operator () (Route & __route) ;
} ;
#endif

View file

@ -1,4 +1,4 @@
/*
/*
* <data.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -48,78 +48,84 @@
#define MAX_FIELD_LENGTH 1000
#define MAX_LINE_LENGTH 1000
static void getNextField (FILE * __f, char * __buff) {
char trash [MAX_TRASH_LENGTH];
static void getNextField (FILE * __f, char * __buff)
{
fscanf (__f, "%[ \t:\n]", trash); /* Discarding sep. */
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) {
static void getLine (FILE * __f, char * __buff)
{
char trash [MAX_TRASH_LENGTH];
char trash [MAX_TRASH_LENGTH];
fscanf (__f, "%[ \t:\n]", trash); /* Discarding sep. */
fscanf (__f, "%[ \t:\n]", trash); /* Discarding sep. */
fscanf (__f, "%[^\n]", __buff); /* Reading the line */
}
void loadData (const char * __filename) {
void loadData (const char * __filename)
{
FILE * f = fopen (__filename, "r");
if (f) {
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);
printf ("Loading '%s'.\n", __filename);
getNextField (f, field); /* Dimension */
assert (strstr (field, "DIMENSION"));
getNextField (f, field);
printf ("DIMENSION: %s.\n", field);
numNodes = atoi (field);
char field [MAX_FIELD_LENGTH];
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);
}
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) {
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

@ -1,4 +1,4 @@
/*
/*
* <data.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007

View file

@ -1,4 +1,4 @@
/*
/*
* <display.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -56,7 +56,7 @@ 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;
@ -67,23 +67,24 @@ 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) ;
/* 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) ;
gdk_gc_set_foreground (gc, & color_green) ;
for (int i = 0 ; i < (int) numNodes ; i ++) {
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]]);
}*/
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) {
void openMainWindow (const char * __filename)
{
filename = __filename;
@ -91,7 +92,7 @@ void openMainWindow (const char * __filename) {
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);
@ -100,46 +101,49 @@ void openMainWindow (const char * __filename) {
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);
}
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) ;
gc = XCreateGC (disp, win, GCForeground | GCBackground, & val) ;
XMapWindow (disp, win) ;
XFlush (disp) ;
while (true) {
XClearWindow (disp, win) ;
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]]);
/* 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) ;
}
XFlush (disp) ;
sleep (1) ;
}
}

View file

@ -1,4 +1,4 @@
/*
/*
* <display.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007

View file

@ -1,4 +1,4 @@
/*
/*
* <display_best_route.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -38,13 +38,14 @@
#include "display.h"
DisplayBestRoute :: DisplayBestRoute (eoPop <Route> & __pop
) : pop (__pop) {
) : pop (__pop)
{
}
void DisplayBestRoute :: operator () () {
void DisplayBestRoute :: operator () ()
{
displayRoute (pop.best_element ());
}

View file

@ -1,4 +1,4 @@
/*
/*
* <display_best_route.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -43,18 +43,19 @@
#include "route.h"
class DisplayBestRoute : public eoUpdater {
public :
class DisplayBestRoute : public eoUpdater
{
DisplayBestRoute (eoPop <Route> & __pop);
void operator () ();
public :
private :
eoPop <Route> & pop;
DisplayBestRoute (eoPop <Route> & __pop);
};
void operator () ();
private :
eoPop <Route> & pop;
};
#endif

View file

@ -1,4 +1,4 @@
/*
/*
* <edge_xover.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -41,104 +41,114 @@
#include "edge_xover.h"
void EdgeXover :: build_map (const Route & __par1, const Route & __par2) {
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]) ;
}
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] ;
void EdgeXover :: remove_entry (unsigned __vertex, std :: vector <std :: set <unsigned> > & __map)
{
for (std :: set <unsigned> :: iterator it = neigh.begin () ;
it != neigh.end () ;
it ++)
__map [* it].erase (__vertex) ;
}
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)
{
void EdgeXover :: add_vertex (unsigned __vertex, Route & __child) {
visited [__vertex] = true ;
__child.push_back (__vertex) ;
remove_entry (__vertex, _map) ; /* Removing entries */
__child.push_back (__vertex) ;
remove_entry (__vertex, _map) ; /* Removing entries */
}
void EdgeXover :: cross (const Route & __par1, const Route & __par2, Route & __child) {
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) ;
}
for (unsigned i = 1 ; i < len ; i ++)
{
cur_vertex = cand [rng.random (cand.size ())] ;
add_vertex (cur_vertex, __child) ;
}
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) {
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 () ;

View file

@ -1,4 +1,4 @@
/*
/*
* <edge_xover.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -45,27 +45,28 @@
#include "route.h"
/** Edge Crossover */
class EdgeXover : public eoQuadOp <Route> {
public :
bool operator () (Route & __route1, Route & __route2) ;
class EdgeXover : public eoQuadOp <Route>
{
private :
void cross (const Route & __par1, const Route & __par2, Route & __child) ; /* Binary */
public :
void remove_entry (unsigned __vertex, std :: vector <std :: set <unsigned> > & __map) ;
/* Updating the map of entries */
bool operator () (Route & __route1, Route & __route2) ;
void build_map (const Route & __par1, const Route & __par2) ;
private :
void add_vertex (unsigned __vertex, Route & __child) ;
void cross (const Route & __par1, const Route & __par2, Route & __child) ; /* Binary */
std :: vector <std :: set <unsigned> > _map ; /* The handled map */
void remove_entry (unsigned __vertex, std :: vector <std :: set <unsigned> > & __map) ;
/* Updating the map of entries */
std :: vector <bool> visited ; /* Vertices that are already visited */
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

@ -1,4 +1,4 @@
/*
/*
* <merge_route_eval.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -36,10 +36,11 @@
#include "merge_route_eval.h"
void MergeRouteEval :: operator () (Route & __route, const int & __part_fit) {
void MergeRouteEval :: operator () (Route & __route, const int & __part_fit)
{
int len = __route.fitness ();
len += __part_fit;
__route.fitness (len);
}

View file

@ -1,4 +1,4 @@
/*
/*
* <merge_route_eval.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -41,12 +41,13 @@
#include "route.h"
class MergeRouteEval : public peoAggEvalFunc <Route> {
public :
class MergeRouteEval : public peoAggEvalFunc <Route>
{
void operator () (Route & __route, const int & __part_fit) ;
};
public :
void operator () (Route & __route, const int & __part_fit) ;
};
#endif

View file

@ -1,4 +1,4 @@
/*
/*
* <mix.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -41,8 +41,9 @@
#include <utils/eoRNG.h>
template <class T> void mix (std :: vector <T> & __v) {
template <class T> void mix (std :: vector <T> & __v)
{
unsigned len = __v.size () ;
for (unsigned i = 0 ; i < len ; i ++)

View file

@ -1,4 +1,4 @@
/*
/*
* <node.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -47,59 +47,62 @@ double * X_coord, * Y_coord;
double X_min = MAXDOUBLE, X_max = MINDOUBLE, Y_min = MAXDOUBLE, Y_max = MINDOUBLE;
void loadNodes (FILE * __f) {
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];
}
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) {
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

@ -1,4 +1,4 @@
/*
/*
* <node.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -39,7 +39,7 @@
#include <stdio.h>
typedef unsigned Node;
typedef unsigned Node;
extern double X_min, X_max, Y_min, Y_max;

View file

@ -1,4 +1,4 @@
/*
/*
* <opt_route.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -40,88 +40,96 @@
#define MAX_FIELD_LENGTH 1000
#define MAX_LINE_LENGTH 1000
static void getNextField (FILE * __f, char * __buff) {
char trash [MAX_TRASH_LENGTH];
static void getNextField (FILE * __f, char * __buff)
{
fscanf (__f, "%[ \t:\n]", trash); /* Discarding sep. */
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) {
static void getLine (FILE * __f, char * __buff)
{
char trash [MAX_TRASH_LENGTH];
char trash [MAX_TRASH_LENGTH];
fscanf (__f, "%[ \t:\n]", trash); /* Discarding sep. */
fscanf (__f, "%[ \t:\n]", trash); /* Discarding sep. */
fscanf (__f, "%[^\n]", __buff); /* Reading the line */
}
static void loadBestRoute (FILE * __f) {
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);
}
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) {
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);
if (f)
{
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);
printf ("Loading '%s'.\n", __filename);
getNextField (f, field); /* Dimension */
assert (strstr (field, "DIMENSION"));
getNextField (f, field);
// printf ("DIMENSION: %s.\n", field);
numNodes = atoi (field);
char field [MAX_FIELD_LENGTH];
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);
}
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) {
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 ()))

View file

@ -1,4 +1,4 @@
/*
/*
* <opt_route.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007

View file

@ -1,4 +1,4 @@
/*
/*
* <order_xover.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -40,51 +40,57 @@
#include "order_xover.h"
void OrderXover :: cross (const Route & __par1, const Route & __par2, Route & __child) {
void OrderXover :: cross (const Route & __par1, const Route & __par2, Route & __child)
{
unsigned cut2 = 1 + rng.random (numNodes) ;
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 ;
}
/* 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 ;
}
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) {
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 () ;

View file

@ -1,4 +1,4 @@
/*
/*
* <order_xover.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -42,15 +42,16 @@
#include "route.h"
/** Order Crossover */
class OrderXover : public eoQuadOp <Route> {
public :
bool operator () (Route & __route1, Route & __route2) ;
class OrderXover : public eoQuadOp <Route>
{
private :
void cross (const Route & __par1, const Route & __par2, Route & __child) ;
} ;
public :
bool operator () (Route & __route1, Route & __route2) ;
private :
void cross (const Route & __par1, const Route & __par2, Route & __child) ;
} ;
#endif

View file

@ -1,4 +1,4 @@
/*
/*
* <param.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -39,10 +39,11 @@
#include "data.h"
#include "opt_route.h"
void loadParameters (int __argc, char * * __argv) {
void loadParameters (int __argc, char * * __argv)
{
eoParser parser (__argc, __argv);
loadData (parser);
loadOptimumRoute (parser);

View file

@ -1,4 +1,4 @@
/*
/*
* <param.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007

View file

@ -1,4 +1,4 @@
/*
/*
* <part_route_eval.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -38,21 +38,22 @@
#include "node.h"
PartRouteEval :: PartRouteEval (float __from,
float __to
) : from (__from),
to (__to) {
float __to
) : from (__from),
to (__to)
{
}
void PartRouteEval :: operator () (Route & __route) {
void PartRouteEval :: operator () (Route & __route)
{
unsigned len = 0 ;
for (unsigned i = (unsigned) (__route.size () * from) ;
i < (unsigned) (__route.size () * to) ;
i ++)
len += distance (__route [i], __route [(i + 1) % numNodes]) ;
__route.fitness (- (int) len) ;
}

View file

@ -1,4 +1,4 @@
/*
/*
* <part_route_eval.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -42,20 +42,21 @@
#include "route.h"
/** Route Evaluator */
class PartRouteEval : public eoEvalFunc <Route> {
public :
class PartRouteEval : public eoEvalFunc <Route>
{
/** Constructor */
PartRouteEval (float __from, float __to) ;
void operator () (Route & __route) ;
private :
public :
float from, to ;
/** Constructor */
PartRouteEval (float __from, float __to) ;
} ;
void operator () (Route & __route) ;
private :
float from, to ;
} ;
#endif

View file

@ -1,4 +1,4 @@
/*
/*
* <partial_mapped_xover.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -41,47 +41,50 @@
#include "partial_mapped_xover.h"
#include "mix.h"
void PartialMappedXover :: repair (Route & __route, unsigned __cut1, unsigned __cut2) {
void PartialMappedXover :: repair (Route & __route, unsigned __cut1, unsigned __cut2)
{
unsigned v [__route.size ()] ; // Number of times a cities are visited ...
for (unsigned i = 0 ; i < __route.size () ; i ++)
v [i] = 0 ;
for (unsigned i = 0 ; i < __route.size () ; i ++)
v [__route [i]] ++ ;
std :: vector <unsigned> vert ;
for (unsigned i = 0 ; i < __route.size () ; i ++)
if (! v [i])
vert.push_back (i) ;
mix (vert) ;
for (unsigned i = 0 ; i < __route.size () ; i ++)
if (i < __cut1 || i >= __cut2)
if (v [__route [i]] > 1) {
__route [i] = vert.back () ;
vert.pop_back () ;
}
if (v [__route [i]] > 1)
{
__route [i] = vert.back () ;
vert.pop_back () ;
}
}
bool PartialMappedXover :: operator () (Route & __route1, Route & __route2) {
bool PartialMappedXover :: operator () (Route & __route1, Route & __route2)
{
unsigned cut1 = rng.random (__route1.size ()), cut2 = rng.random (__route2.size ()) ;
if (cut2 < cut1)
std :: swap (cut1, cut2) ;
// Between the cuts
for (unsigned i = cut1 ; i < cut2 ; i ++)
std :: swap (__route1 [i], __route2 [i]) ;
// Outside the cuts
repair (__route1, cut1, cut2) ;
repair (__route2, cut1, cut2) ;
__route1.invalidate () ;
__route2.invalidate () ;

View file

@ -1,4 +1,4 @@
/*
/*
* <partial_mapped_xover.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -42,15 +42,16 @@
#include "route.h"
/** Partial Mapped Crossover */
class PartialMappedXover : public eoQuadOp <Route> {
public :
bool operator () (Route & __route1, Route & __route2) ;
class PartialMappedXover : public eoQuadOp <Route>
{
private :
void repair (Route & __route, unsigned __cut1, unsigned __cut2) ;
} ;
public :
bool operator () (Route & __route1, Route & __route2) ;
private :
void repair (Route & __route, unsigned __cut1, unsigned __cut2) ;
} ;
#endif

View file

@ -1,4 +1,4 @@
/*
/*
* <route.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -36,13 +36,14 @@
#include "route.h"
unsigned length (const Route & __route) {
unsigned length (const Route & __route)
{
unsigned len = 0 ;
for (unsigned i = 0; i < numNodes; i ++)
len += distance (__route [i], __route [(i + 1) % numNodes]) ;
len += distance (__route [i], __route [(i + 1) % numNodes]) ;
return len;
}

View file

@ -1,4 +1,4 @@
/*
/*
* <route.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -41,8 +41,8 @@
#include "node.h"
typedef eoVector <int, Node> Route;
typedef eoVector <int, Node> Route;
unsigned length (const Route & __route);
unsigned length (const Route & __route);
#endif

View file

@ -1,4 +1,4 @@
/*
/*
* <route_eval.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -36,6 +36,7 @@
#include "route_eval.h"
void RouteEval :: operator () (Route & __route) {
__route.fitness (- (int) length (__route));
void RouteEval :: operator () (Route & __route)
{
__route.fitness (- (int) length (__route));
}

View file

@ -1,4 +1,4 @@
/*
/*
* <route_eval.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -41,11 +41,12 @@
#include "route.h"
class RouteEval : public eoEvalFunc <Route> {
public :
void operator () (Route & __route) ;
} ;
class RouteEval : public eoEvalFunc <Route>
{
public :
void operator () (Route & __route) ;
} ;
#endif

View file

@ -1,4 +1,4 @@
/*
/*
* <route_init.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -39,13 +39,14 @@
#include "route_init.h"
#include "node.h"
void RouteInit :: operator () (Route & __route) {
void RouteInit :: operator () (Route & __route)
{
__route.clear ();
for (unsigned i = 0 ; i < numNodes ; i ++)
__route.push_back (i);
for (unsigned i = 0 ; i < numNodes ; i ++)
for (unsigned i = 0 ; i < numNodes ; i ++)
std :: swap (__route [i], __route [rng.random (numNodes)]);
}

View file

@ -1,4 +1,4 @@
/*
/*
* <route_init.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -41,11 +41,12 @@
#include "route.h"
class RouteInit : public eoInit <Route> {
public :
void operator () (Route & __route);
} ;
class RouteInit : public eoInit <Route>
{
public :
void operator () (Route & __route);
} ;
#endif

View file

@ -1,4 +1,4 @@
/*
/*
* <two_opt.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -36,13 +36,15 @@
#include "two_opt.h"
void TwoOpt :: operator () (Route & __route) {
unsigned i = 0;
void TwoOpt :: operator () (Route & __route)
{
while ((2 * i) < (second - first)) {
std :: swap (__route [first + i], __route [second - i]);
i ++;
}
unsigned i = 0;
while ((2 * i) < (second - first))
{
std :: swap (__route [first + i], __route [second - i]);
i ++;
}
}

View file

@ -1,4 +1,4 @@
/*
/*
* <two_opt.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -42,12 +42,13 @@
#include "route.h"
class TwoOpt : public moMove <Route>, public std :: pair <unsigned, unsigned> {
public :
void operator () (Route & __route);
class TwoOpt : public moMove <Route>, public std :: pair <unsigned, unsigned>
{
} ;
public :
void operator () (Route & __route);
} ;
#endif

View file

@ -1,4 +1,4 @@
/*
/*
* <two_opt_incr_eval.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -37,16 +37,17 @@
#include "two_opt_incr_eval.h"
#include "node.h"
int TwoOptIncrEval :: operator () (const TwoOpt & __move, const Route & __route) {
int TwoOptIncrEval :: operator () (const TwoOpt & __move, const Route & __route)
{
/* From */
Node v1 = __route [__move.first], v1_left = __route [(__move.first - 1 + numNodes) % numNodes];
/* To */
Node v2 = __route [__move.second], v2_right = __route [(__move.second + 1) % numNodes];
if (v1 == v2 || v2_right == v1)
return __route.fitness ();
else
else
return __route.fitness () - distance (v1_left, v2) - distance (v1, v2_right) + distance (v1_left, v1) + distance (v2, v2_right);
}

View file

@ -1,4 +1,4 @@
/*
/*
* <two_opt_incr_eval.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -40,12 +40,13 @@
#include <moMoveIncrEval.h>
#include "two_opt.h"
class TwoOptIncrEval : public moMoveIncrEval <TwoOpt> {
class TwoOptIncrEval : public moMoveIncrEval <TwoOpt>
{
public :
int operator () (const TwoOpt & __move, const Route & __route) ;
public :
} ;
int operator () (const TwoOpt & __move, const Route & __route) ;
} ;
#endif

View file

@ -1,4 +1,4 @@
/*
/*
* <two_opt_init.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -36,7 +36,8 @@
#include "two_opt_init.h"
void TwoOptInit :: operator () (TwoOpt & __move, const Route & __route) {
void TwoOptInit :: operator () (TwoOpt & __move, const Route & __route)
{
__move.first = __move.second = 0;
}

View file

@ -1,4 +1,4 @@
/*
/*
* <two_opt_init.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -41,12 +41,13 @@
#include "two_opt.h"
class TwoOptInit : public moMoveInit <TwoOpt> {
public :
void operator () (TwoOpt & __move, const Route & __route) ;
} ;
class TwoOptInit : public moMoveInit <TwoOpt>
{
public :
void operator () (TwoOpt & __move, const Route & __route) ;
} ;
#endif

View file

@ -1,4 +1,4 @@
/*
/*
* <two_opt_next.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -37,19 +37,22 @@
#include "two_opt_next.h"
#include "node.h"
bool TwoOptNext :: operator () (TwoOpt & __move, const Route & __route) {
bool TwoOptNext :: operator () (TwoOpt & __move, const Route & __route)
{
if (__move.first == numNodes - 1 && __move.second == numNodes - 1)
return false;
else {
__move.second ++;
if (__move.second == numNodes) {
__move.first ++;
__move.second = __move.first;
else
{
__move.second ++;
if (__move.second == numNodes)
{
__move.first ++;
__move.second = __move.first;
}
return true ;
}
return true ;
}
}

View file

@ -1,4 +1,4 @@
/*
/*
* <two_opt_next.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -41,12 +41,13 @@
#include "two_opt.h"
class TwoOptNext : public moNextMove <TwoOpt> {
class TwoOptNext : public moNextMove <TwoOpt>
{
public :
bool operator () (TwoOpt & __move, const Route & __route);
};
public :
bool operator () (TwoOpt & __move, const Route & __route);
};
#endif

View file

@ -1,4 +1,4 @@
/*
/*
* <two_opt_rand.cpp>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -37,13 +37,14 @@
#include <utils/eoRNG.h>
#include "two_opt_rand.h"
#include "node.h"
#include "node.h"
void TwoOptRand :: operator () (TwoOpt & __move, const Route & __route) {
void TwoOptRand :: operator () (TwoOpt & __move, const Route & __route)
{
__move.second = rng.random (numNodes);
__move.first = rng.random (__move.second);
}

View file

@ -1,4 +1,4 @@
/*
/*
* <two_opt_rand.h>
* Copyright (C) DOLPHIN Project-Team, INRIA Futurs, 2006-2007
* (C) OPAC Team, LIFL, 2002-2007
@ -41,12 +41,13 @@
#include "two_opt.h"
class TwoOptRand : public eoMoveRand <TwoOpt> {
public :
void operator () (TwoOpt & __move, const Route & __route) ;
} ;
class TwoOptRand : public eoMoveRand <TwoOpt>
{
public :
void operator () (TwoOpt & __move, const Route & __route) ;
} ;
#endif