-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsp_3opt.c
286 lines (246 loc) · 6.62 KB
/
tsp_3opt.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "tsp_tools.h"
int diff_len(int i1, int i2, int i3, int i4, int i5, int i6, int type, int *tour, int **dist)
{
int s1, s2, s3, s4, s5, s6;
s1 = tour[i1];
s2 = tour[i2];
s3 = tour[i3];
s4 = tour[i4];
s5 = tour[i5];
s6 = tour[i6];
if(type == 0)
{
return (dist[s1][s6] + dist[s5][s4] + dist[s2][s3] - dist[s1][s2] - dist[s3][s4] - dist[s5][s6]);
}else if(type == 1)
{
return (dist[s1][s5] + dist[s6][s4] + dist[s2][s3] - dist[s1][s2] - dist[s3][s4] - dist[s5][s6]);
}else if(type == 2)
{
return (dist[s1][s6] + dist[s5][s3] + dist[s2][s4] - dist[s1][s2] - dist[s3][s4] - dist[s5][s6]);
}else
{
return (dist[s1][s3] + dist[s5][s4] + dist[s2][s6] - dist[s1][s2] - dist[s3][s4] - dist[s5][s6]);
}
}
int tsp_3opt(int *min_tour, int *init, int **dist, int n)
{
int h,i,j,k;
int i1,i2,i3,i4,i5,i6;
int min_len;
int *sub_tour1, *sub_tour2, *sub_tour3;
int n1, n2, n3;
int diff, min_diff;
int min_neighbor[6];
int min_type;
int *tour;
tour = (int *) malloc(n*sizeof(int));
for(i=0;i<n;i++) min_tour[i] = init[i];
min_len = tour_length(min_tour, dist, n);
for(i=0;i<n;i++) tour[i] = min_tour[i];
sub_tour1 = (int *) malloc(n*sizeof(int)); /* sub tour 2 to 5 */
sub_tour2 = (int *) malloc(n*sizeof(int)); /* sub tour 6 to 3 */
sub_tour3 = (int *) malloc(n*sizeof(int)); /* sub tour 4 to 1 */
h = 0;
while(1)
{
/* initialize the next neighbor */
min_diff = 0;
min_type = 1;
min_neighbor[0] = 0;
min_neighbor[1] = 1;
min_neighbor[2] = 2;
min_neighbor[3] = 3;
min_neighbor[4] = 1;
min_neighbor[5] = 2;
/* find the minimum neighbor */
for(i=0;i<n;i++)
{
i1 = i;
i2 = (i1+1)%n;
for(j=0;j<n-2;j++)
{
i3 = (i2+1+j)%n;
i4 = (i3+1)%n;
for(k=0;k<=j;k++)
{
i5 = (i2+k)%n;
i6 = (i5+1)%n;
/* calculate the difference of distance between the current tour and a neighbor tour */
diff = diff_len(i1, i2, i3, i4, i5, i6, 0, tour, dist);
if(diff < min_diff)
{
min_diff = diff;
min_type = 0;
min_neighbor[0] = i1;
min_neighbor[1] = i2;
min_neighbor[2] = i3;
min_neighbor[3] = i4;
min_neighbor[4] = i5;
min_neighbor[5] = i6;
}
diff = diff_len(i1, i2, i3, i4, i5, i6, 1, tour, dist);
if(diff < min_diff)
{
min_diff = diff;
min_type = 1;
min_neighbor[0] = i1;
min_neighbor[1] = i2;
min_neighbor[2] = i3;
min_neighbor[3] = i4;
min_neighbor[4] = i5;
min_neighbor[5] = i6;
}
diff = diff_len(i1, i2, i3, i4, i5, i6, 2, tour, dist);
if(diff < min_diff)
{
min_diff = diff;
min_type = 2;
min_neighbor[0] = i1;
min_neighbor[1] = i2;
min_neighbor[2] = i3;
min_neighbor[3] = i4;
min_neighbor[4] = i5;
min_neighbor[5] = i6;
}
diff = diff_len(i1, i2, i3, i4, i5, i6, 3, tour, dist);
if(diff < min_diff)
{
min_diff = diff;
min_type = 3;
min_neighbor[0] = i1;
min_neighbor[1] = i2;
min_neighbor[2] = i3;
min_neighbor[3] = i4;
min_neighbor[4] = i5;
min_neighbor[5] = i6;
}
}
}
}
if(min_diff==0) break;
i1 = min_neighbor[0];
i2 = min_neighbor[1];
i3 = min_neighbor[2];
i4 = min_neighbor[3];
i5 = min_neighbor[4];
i6 = min_neighbor[5];
/* cut the sub tours */
if(i2 <= i5)
{
for(i=0;i<i5-i2+1;i++) sub_tour1[i] = tour[i2+i];
n1 = i5-i2+1;
}
else
{
for(i=0;i<n-i2;i++) sub_tour1[i] = tour[i2+i];
for(i=0;i<i5+1;i++) sub_tour1[i+n-i2] = tour[i];
n1 = i5-i2+n+1;
}
if(i6 <= i3)
{
for(i=0;i<i3-i6+1;i++) sub_tour2[i] = tour[i6+i];
n2 = i3-i6+1;
}
else
{
for(i=0;i<n-i6;i++) sub_tour2[i] = tour[i6+i];
for(i=0;i<i3+1;i++) sub_tour2[i+n-i6] = tour[i];
n2 = i3-i6+n+1;
}
if(i4 <= i1)
{
for(i=0;i<i1-i4+1;i++) sub_tour3[i] = tour[i4+i];
n3 = i1-i4+1;
}
else
{
for(i=0;i<n-i4;i++) sub_tour3[i] = tour[i4+i];
for(i=0;i<i1+1;i++) sub_tour3[i+n-i4] = tour[i];
n3 = i1-i4+n+1;
}
/*
printf("[");
for(i=0;i<n1;i++) printf("%d ", sub_tour1[i]+1);
printf("]\n");
printf("[");
for(i=0;i<n2;i++) printf("%d ", sub_tour2[i]+1);
printf("]\n");
printf("[");
for(i=0;i<n3;i++) printf("%d ", sub_tour3[i]+1);
printf("]\n");
*/
/* reconnect the sub tours */
if(min_type == 0)
{
for(i=0;i<n1;i++) min_tour[i] = sub_tour1[i];
for(i=0;i<n3;i++) min_tour[n1+i] = sub_tour3[i];
for(i=0;i<n2;i++) min_tour[n1+n3+i] = sub_tour2[i];
}
else if(min_type == 1)
{
for(i=0;i<n1;i++) min_tour[i] = sub_tour1[i];
for(i=0;i<n3;i++) min_tour[n1+i] = sub_tour3[n3-1-i];
for(i=0;i<n2;i++) min_tour[n1+n3+i] = sub_tour2[i];
}
else if(min_type == 2)
{
for(i=0;i<n1;i++) min_tour[i] = sub_tour1[i];
for(i=0;i<n2;i++) min_tour[n1+i] = sub_tour2[n2-1-i];
for(i=0;i<n3;i++) min_tour[n1+n2+i] = sub_tour3[n3-1-i];
}
else
{
for(i=0;i<n1;i++) min_tour[i] = sub_tour1[i];
for(i=0;i<n3;i++) min_tour[n1+i] = sub_tour3[i];
for(i=0;i<n2;i++) min_tour[n1+n3+i] = sub_tour2[n2-1-i];
}
min_len = tour_length(min_tour, dist, n);
printf("%d: diff = %d, obj = %d \n",h, -min_diff, min_len);
printf("replace_type = %d, replace_pos = [%d %d %d %d %d %d]\n", min_type, i1, i2, i3, i4, i5, i6);
printf("tour = [");
for(i=0;i<n;i++) printf("%d ", min_tour[i]+1);
printf("]\n\n");
for(i=0;i<n;i++) tour[i] = min_tour[i];
h = h+1;
}
free(tour);
free(sub_tour1);
free(sub_tour2);
free(sub_tour3);
return min_len;
}
int main(int argc, char *argv[])
{
int i;
char *fn;
char *fn_init;
int n;
float **points;
int **dist;
int min_len;
int *min_tour;
int *init;
clock_t begin, end;
double time_spent;
fn = argv[1]; /* tsp instance */
fn_init = argv[2]; /* initial tour for the 2-opt */
n = read_tsp(&points, fn); /* (points[i][0], points[i][1]): the coordinate of point i. */
/* for(i=0;i<n;i++) printf("%d %f %f\n", i+1, points[i][0], points[i][1]); */
distance_matrix(&dist, points, n); /* dist[i][j]: the distance of i and j. */
read_tour(&init, n, fn_init);
min_tour = (int *) malloc(n*sizeof(int));
begin = clock();
min_len = tsp_3opt(min_tour, init, dist, n);
end = clock();
time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("obj = %d\n", min_len);
printf("tour = [");
for(i=0;i<n;i++) printf("%d ", min_tour[i]+1);
printf("]\n");
printf("CPU time (s): %f\n", time_spent);
write_tour("a.tour", min_tour, n);
return 0;
}