-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTSP.c
601 lines (527 loc) · 16.2 KB
/
TSP.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/*
AAAA CCCC OOOO TTTTTT SSSSS PPPPP
AA AA CC OO OO TT SS PP PP
AAAAAA CC OO OO TT SSSS PPPPP
AA AA CC OO OO TT SS PP
AA AA CCCC OOOO TT SSSSS PP
######################################################
########## ACO algorithms for the TSP ##########
######################################################
Version: 1.0
File: TSP.c
Author: Thomas Stuetzle
Purpose: TSP related procedures, distance computation, neighbour lists
Check: README and gpl.txt
Copyright (C) 2002 Thomas Stuetzle
*/
/***************************************************************************
Program's name: acotsp
Ant Colony Optimization algorithms (AS, ACS, EAS, RAS, MMAS, BWAS) for the
symmetric TSP
Copyright (C) 2004 Thomas Stuetzle
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
email: stuetzle no@spam ulb.ac.be
mail address: Universite libre de Bruxelles
IRIDIA, CP 194/6
Av. F. Roosevelt 50
B-1050 Brussels
Belgium
***************************************************************************/
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "InOut.h"
#include "TSP.h"
#include "ants.h"
#include "ls.h"
#include "utilities.h"
#define M_PI 3.14159265358979323846264
#define MAXCOORD 1000000
long int n; /* number of cities in the instance to be solved */
static int *key; /* array of keys to generate random distance matrices */
static int param; /* parameter to generate random distance matrices */
static double factor; /* factor value to generate random distance matrices */
struct problem instance;
static double dtrunc (double x)
{
int k;
k = (int) x;
x = (double) k;
return x;
}
void initializeKeys(int instanceSeed)
/*
FUNCTION: initialize factor, param and key values to calculate the
random distances for explicit instances
INPUT: seed used to generate the instance
OUTPUT: none
COMMENTS: implemented according to the portmgen TSPLIB instance generator
*/
{
int i;
factor = MAXCOORD/2147483648.;
param = 104*instanceSeed + 1;
key = (int *) calloc (n+1, sizeof(int));
for (i=1; i<=n; i++) key[i] = 0x12345672*i + 1;
}
long int (*distance)(long int, long int); /* function pointer */
/*
FUNCTION: the following four functions implement different ways of
computing distances for TSPLIB instances
INPUT: two node indices
OUTPUT: distance between the two nodes
*/
long int round_distance (long int i, long int j)
/*
FUNCTION: compute Euclidean distances between two nodes rounded to next
integer for TSPLIB instances
INPUT: two node indices
OUTPUT: distance between the two nodes
COMMENTS: for the definition of how to compute this distance see TSPLIB
*/
{
double xd = instance.nodeptr[i].x - instance.nodeptr[j].x;
double yd = instance.nodeptr[i].y - instance.nodeptr[j].y;
double r = sqrt(xd*xd + yd*yd) + 0.5;
return (long int) r;
}
long int ceil_distance (long int i, long int j)
/*
FUNCTION: compute ceiling distance between two nodes rounded to next
integer for TSPLIB instances
INPUT: two node indices
OUTPUT: distance between the two nodes
COMMENTS: for the definition of how to compute this distance see TSPLIB
*/
{
double xd = instance.nodeptr[i].x - instance.nodeptr[j].x;
double yd = instance.nodeptr[i].y - instance.nodeptr[j].y;
double r = sqrt(xd*xd + yd*yd);
return (long int)(ceil (r));
}
long int geo_distance (long int i, long int j)
/*
FUNCTION: compute geometric distance between two nodes rounded to next
integer for TSPLIB instances
INPUT: two node indices
OUTPUT: distance between the two nodes
COMMENTS: adapted from concorde code
for the definition of how to compute this distance see TSPLIB
*/
{
double deg, min;
double lati, latj, longi, longj;
double q1, q2, q3;
long int dd;
double x1 = instance.nodeptr[i].x, x2 = instance.nodeptr[j].x,
y1 = instance.nodeptr[i].y, y2 = instance.nodeptr[j].y;
deg = dtrunc (x1);
min = x1 - deg;
lati = M_PI * (deg + 5.0 * min / 3.0) / 180.0;
deg = dtrunc (x2);
min = x2 - deg;
latj = M_PI * (deg + 5.0 * min / 3.0) / 180.0;
deg = dtrunc (y1);
min = y1 - deg;
longi = M_PI * (deg + 5.0 * min / 3.0) / 180.0;
deg = dtrunc (y2);
min = y2 - deg;
longj = M_PI * (deg + 5.0 * min / 3.0) / 180.0;
q1 = cos (longi - longj);
q2 = cos (lati - latj);
q3 = cos (lati + latj);
dd = (int) (6378.388 * acos (0.5 * ((1.0 + q1) * q2 - (1.0 - q1) * q3)) + 1.0);
return dd;
}
long int att_distance (long int i, long int j)
/*
FUNCTION: compute ATT distance between two nodes rounded to next
integer for TSPLIB instances
INPUT: two node indices
OUTPUT: distance between the two nodes
COMMENTS: for the definition of how to compute this distance see TSPLIB
*/
{
double xd = instance.nodeptr[i].x - instance.nodeptr[j].x;
double yd = instance.nodeptr[i].y - instance.nodeptr[j].y;
double rij = sqrt ((xd * xd + yd * yd) / 10.0);
double tij = dtrunc (rij);
long int dij;
if (tij < rij)
dij = (int) tij + 1;
else
dij = (int) tij;
return dij;
}
long int explicit_distance (long int i, long int j)
/*
FUNCTION: compute EXPLICIT random distance between two nodes
INPUT: two node indices
OUTPUT: distance between the two nodes
COMMENTS: for the definition of how to compute this distance see TSPLIB
*/
{
int x, y, z;
if (i == j) return 0;
i = key[i+1];
j = key[j+1];
x = i&j;
y = i|j;
z = param;
x *= z;
y *= x;
z *= y;
z ^= param;
x *= z;
y *= x;
z *= y;
x = ((i+j)^z) & 0x7fffffff;
return (int)(x*factor);
}
long int ** compute_distances(void)
/*
FUNCTION: computes the matrix of all intercity distances
INPUT: none
OUTPUT: pointer to distance matrix, has to be freed when program stops
*/
{
long int i, j;
long int **matrix;
if((matrix = malloc(sizeof(long int) * n * n +
sizeof(long int *) * n )) == NULL){
fprintf(stderr,"Out of memory, exit.");
exit(1);
}
for ( i = 0 ; i < n ; i++ ) {
matrix[i] = (long int *)(matrix + n) + i*n;
for ( j = 0 ; j < n ; j++ ) {
matrix[i][j] = distance(i, j);
}
}
return matrix;
}
long int ** compute_nn_lists(struct problem *instance)
/*
FUNCTION: computes nearest neighbor lists of depth nn for each city
INPUT: none
OUTPUT: pointer to the nearest neighbor lists
*/
{
long int i, node, nn;
long int *distance_vector;
long int *help_vector;
long int **m_nnear;
trace_print("\n computing nearest neighbor lists, ");
nn = MAX(nn_ls, nn_ants);
if ( nn >= n )
nn = n - 1;
DEBUG ( assert( n > nn ) );
trace_print("nn = %ld ... \n",nn);
if ((m_nnear = malloc(sizeof(long int) * n * nn
+ n * sizeof(long int *))) == NULL){
exit(EXIT_FAILURE);
}
distance_vector = calloc(n, sizeof(long int));
help_vector = calloc(n, sizeof(long int));
for ( node = 0 ; node < n ; node++ ) { /* compute cnd-sets for all node */
m_nnear[node] = (long int *)(m_nnear + n) + node * nn;
for ( i = 0 ; i < n ; i++ ) { /* Copy distances from nodes to the others */
distance_vector[i] = instance->distance[node][i];
help_vector[i] = i;
}
distance_vector[node] = LONG_MAX; /* city is not nearest neighbour */
sort2(distance_vector, help_vector, 0, n-1);
for ( i = 0 ; i < nn ; i++ ) {
m_nnear[node][i] = help_vector[i];
}
}
free(distance_vector);
free(help_vector);
trace_print("\n .. done\n");
instance->nn_list = m_nnear;
return instance->nn_list;
}
long int compute_tour_length(const long int *t )
/*
FUNCTION: compute the tour length of tour t
INPUT: pointer to tour t
OUTPUT: tour length of tour t
*/
{
int i;
long int tour_length = 0;
for ( i = 0 ; i < n ; i++ ) {
tour_length += instance.distance[t[i]][t[i+1]];
}
return tour_length;
}
bool check_solution(const long int *t)
{
int i;
const int size = n;
if (!check_permutation (t, size))
goto error;
if (t[0] != t[size]) {
fprintf(stderr,"\n%s:error: permutation is not a closed tour.", __FUNCTION__);
goto error;
}
return true;
error:
fprintf(stderr,"\n%s:error: solution_vector:", __FUNCTION__);
for (i = 0; i < size; i++)
fprintf(stderr, " %ld", t[i]);
fprintf(stderr,"\n");
return false;
}
struct point * read_etsp(const char *tsp_file_name)
/*
FUNCTION: parse and read instance file
INPUT: instance name
OUTPUT: list of coordinates for all nodes
COMMENTS: Instance files have to be in TSPLIB format, otherwise procedure fails
*/
{
FILE *tsp_file;
char buf[LINE_BUF_LEN];
long int i, j;
struct point *nodeptr;
tsp_file = fopen(tsp_file_name, "r");
if ( tsp_file == NULL ) {
fprintf(stderr,"No instance file specified, abort\n");
exit(1);
}
assert(tsp_file != NULL);
printf("\nreading tsp-file %s ... \n\n", tsp_file_name);
int instanceSeed = 1234567;
fscanf(tsp_file,"%s", buf);
while ( (strcmp("NODE_COORD_SECTION", buf) != 0) && (strcmp("EDGE_WEIGHT_SECTION", buf) != 0)) {
if ( strcmp("NAME", buf) == 0 ) {
fscanf(tsp_file, "%s", buf);
trace_print("%s ", buf);
fscanf(tsp_file, "%s", buf);
strcpy(instance.name, buf);
trace_print("%s \n", instance.name);
buf[0]=0;
}
else if ( strcmp("NAME:", buf) == 0 ) {
fscanf(tsp_file, "%s", buf);
strcpy(instance.name, buf);
trace_print("%s \n", instance.name);
buf[0]=0;
}
else if ( strcmp("COMMENT", buf) == 0 ){
fgets(buf, LINE_BUF_LEN, tsp_file);
trace_print("%s", buf);
char* strSeed = strstr(buf, "seed");
if (strSeed != NULL) {
strtok(strSeed, "=");
instanceSeed = atoi(strtok(NULL, "="));
}
buf[0]=0;
}
else if ( strcmp("COMMENT:", buf) == 0 ){
fgets(buf, LINE_BUF_LEN, tsp_file);
trace_print("%s", buf);
char* strSeed = strstr(buf, "seed");
if(strSeed != NULL) {
strtok(strSeed, "=");
instanceSeed = atoi(strtok(NULL, "="));
}
buf[0]=0;
}
else if ( strcmp("TYPE", buf) == 0 ) {
fscanf(tsp_file, "%s", buf);
trace_print("%s ", buf);
fscanf(tsp_file, "%s", buf);
trace_print("%s\n", buf);
if( strcmp("TSP", buf) != 0 ) {
fprintf(stderr,"\n Not a TSP instance in TSPLIB format !!\n");
exit(1);
}
buf[0]=0;
}
else if ( strcmp("TYPE:", buf) == 0 ) {
fscanf(tsp_file, "%s", buf);
trace_print("%s\n", buf);
if( strcmp("TSP", buf) != 0 ) {
fprintf(stderr,"\n Not a TSP instance in TSPLIB format !!\n");
exit(1);
}
buf[0]=0;
}
else if( strcmp("DIMENSION", buf) == 0 ){
fscanf(tsp_file, "%s", buf);
trace_print("%s ", buf);
fscanf(tsp_file, "%ld", &n);
instance.n = n;
trace_print("%ld\n", n);
assert ( n > 2 && n < 6000);
buf[0]=0;
}
else if ( strcmp("DIMENSION:", buf) == 0 ) {
fscanf(tsp_file, "%ld", &n);
instance.n = n;
trace_print("%ld\n", n);
assert ( n > 2 && n < 6000);
buf[0]=0;
}
else if( strcmp("DISPLAY_DATA_TYPE", buf) == 0 ){
fgets(buf, LINE_BUF_LEN, tsp_file);
trace_print("%s", buf);
buf[0]=0;
}
else if ( strcmp("DISPLAY_DATA_TYPE:", buf) == 0 ) {
fgets(buf, LINE_BUF_LEN, tsp_file);
trace_print("%s", buf);
buf[0]=0;
}
else if( strcmp("EDGE_WEIGHT_TYPE", buf) == 0 ){
/* set pointer to appropriate distance function; has to be one of
EUC_2D, CEIL_2D, GEO, ATT, or EXPLICIT. Everything else fails */
buf[0]=0;
fscanf(tsp_file, "%s", buf);
trace_print("%s ", buf);
buf[0]=0;
fscanf(tsp_file, "%s", buf);
trace_print("%s\n", buf);
if ( strcmp("EUC_2D", buf) == 0 ) {
distance = round_distance;
}
else if ( strcmp("CEIL_2D", buf) == 0 ) {
distance = ceil_distance;
}
else if ( strcmp("GEO", buf) == 0 ) {
distance = geo_distance;
}
else if ( strcmp("ATT", buf) == 0 ) {
distance = att_distance;
}
else if ( strcmp("EXPLICIT", buf) == 0 ) {
initializeKeys(instanceSeed);
distance = explicit_distance;
}
else
fprintf(stderr,"EDGE_WEIGHT_TYPE %s not implemented\n",buf);
strcpy(instance.edge_weight_type, buf);
buf[0]=0;
}
else if( strcmp("EDGE_WEIGHT_TYPE:", buf) == 0 ){
/* set pointer to appropriate distance function; has to be one of
EUC_2D, CEIL_2D, GEO, ATT, or EXPLICIT. Everything else fails */
buf[0]=0;
fscanf(tsp_file, "%s", buf);
trace_print("%s\n", buf);
printf("%s\n", buf);
printf("%s\n", buf);
if ( strcmp("EUC_2D", buf) == 0 ) {
distance = round_distance;
}
else if ( strcmp("CEIL_2D", buf) == 0 ) {
distance = ceil_distance;
}
else if ( strcmp("GEO", buf) == 0 ) {
distance = geo_distance;
}
else if ( strcmp("ATT", buf) == 0 ) {
distance = att_distance;
}
else if ( strcmp("EXPLICIT", buf) == 0 ) {
initializeKeys(instanceSeed);
distance = explicit_distance;
}
else {
fprintf(stderr,"EDGE_WEIGHT_TYPE %s not implemented\n",buf);
exit(1);
}
strcpy(instance.edge_weight_type, buf);
buf[0]=0;
}
buf[0]=0;
fscanf(tsp_file,"%s", buf);
}
if( (strcmp("NODE_COORD_SECTION", buf) == 0) || (strcmp("EDGE_WEIGHT_SECTION", buf) == 0) ){
trace_print("found section contaning the node coordinates\n");
}
else{
fprintf(stderr,"\n\nSome error ocurred finding start of coordinates from tsp file !!\n");
exit(1);
}
if( (nodeptr = malloc(sizeof(struct point) * n)) == NULL )
exit(EXIT_FAILURE);
else {
for ( i = 0 ; i < n ; i++ ) {
fscanf(tsp_file,"%ld %lf %lf", &j, &nodeptr[i].x, &nodeptr[i].y );
}
}
trace_print("number of cities is %ld\n",n);
trace_print("\n... done\n");
return (nodeptr);
}
void
read_instance (const char* filename, struct problem *instance)
{
instance->nodeptr = read_etsp(filename);
printf("calculating distance matrix ..\n\n");
instance->distance = compute_distances();
printf(" .. done\n");
}
void free_instance (struct problem *instance)
{
free( instance->distance );
free( instance->nn_list );
}
const char * get_instance_name(const struct problem *instance)
{
return instance->name;
}
void printHeur(void)
/*
FUNCTION: print heuristic information
INPUT: none
OUTPUT: none
*/
{
long int i, j;
printf("Heuristic information:\n");
for ( i = 0 ; i < n ; i++) {
printf("From %ld: ",i);
for ( j = 0 ; j < n - 1 ; j++ ) {
printf(" %.3f ", HEURISTIC(i,j));
}
printf(" %.3f\n", HEURISTIC(i,j));
printf("\n");
}
printf("\n");
}
void printDist(void)
/*
FUNCTION: print distance matrix
INPUT: none
OUTPUT: none
*/
{
long int i,j;
printf("Distance Matrix:\n");
for ( i = 0 ; i < n ; i++) {
printf("From %ld: ",i);
for ( j = 0 ; j < n - 1 ; j++ ) {
printf(" %ld", instance.distance[i][j]);
}
printf(" %ld\n", instance.distance[i][n-1]);
printf("\n");
}
printf("\n");
}