-
Notifications
You must be signed in to change notification settings - Fork 0
/
lk_main.c
294 lines (255 loc) · 7.02 KB
/
lk_main.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
#ifdef CONCORDE
#include "machdefs.h"
#include "linkern.h"
#include "util.h"
#include "kdtree.h"
#include "edgegen.h"
#include "macrorus.h"
#include "tsp.h"
void
randcycle ( int ncount, int *cyc, CCrandstate * rstate )
{
int i, k, temp;
for ( i = 0; i < ncount; i++ )
cyc[i] = i;
for ( i = ncount; i > 1; i-- )
{
k = CCutil_lprand ( rstate ) % i;
CC_SWAP ( cyc[i - 1], cyc[k], temp );
}
}
void
fill_dat ( int ncount, CCdatagroup * dat, int **adj )
{
int i, j;
CCutil_init_datagroup ( dat );
CCutil_dat_setnorm ( dat, CC_MATRIXNORM );
dat->adj = CC_SAFE_MALLOC ( ncount, int * );
dat->adjspace = CC_SAFE_MALLOC ( ncount * ( ncount + 1 ) / 2, int );
if ( dat->adj == ( int ** ) NULL || dat->adjspace == ( int * ) NULL )
{
CC_IFFREE ( dat->adj, int * );
CC_IFFREE ( dat->adjspace, int );
}
for ( i = 0, j = 0; i < ncount; i++ )
{
dat->adj[i] = dat->adjspace + j;
j += ( i + 1 );
}
for ( i = 0; i < ncount; i++ )
{
for ( j = 0; j <= i; j++ )
{
dat->adj[i][j] = adj[i][j];
}
}
return;
}
int
chlinkern ( int ncount, int **adj, int *tour, int *incycle, int *outcycle )
{
double val;
int tempcount, *templist;
#if 0
int *incycle = ( int * ) NULL, *outcycle = ( int * ) NULL;
#endif
CCdatagroup dat;
CCrandstate rstate;
int attempt = 1;
int norm;
int seed;
int nearnum;
int quadtry = 2;
int run_silently = 1;
int in_repeater;
double time_bound = -1.0;
double length_bound = -1.0;
int i, genes;
int kick_type = CC_LK_RANDOM_KICK;
fill_dat ( ncount, &dat, adj );
seed = ( int ) CCutil_real_zeit ( );
CCutil_sprand ( seed, &rstate );
#if TSPOUT
printf ( "Chained Lin-Kernighan with seed %d\n", seed );
fflush ( outfile );
#endif
CCutil_dat_getnorm ( &dat, &norm );
in_repeater = ncount;
nearnum = 4 * quadtry;
if ( CCedgegen_junk_k_nearest ( ncount, nearnum, &dat,
( double * ) NULL, 1,
&tempcount, &templist, run_silently ) )
{
fprintf ( stderr, "CCedgegen_junk_k_nearest failed\n" );
val = -1.0;
goto CLEANUP;
}
#if 0
randcycle ( ncount, incycle, &rstate );
#else
if ( CCedgegen_junk_nearest_neighbor_tour
( ncount, CCutil_lprand ( &rstate ) % ncount, &dat, incycle,
&val, run_silently ) )
{
fprintf ( stderr, "CCedgegen_junk_nearest_neighbor_tour failed\n" );
val = -1.0;
goto CLEANUP;
}
#endif
do
{
if ( CClinkern_tour ( ncount, &dat, tempcount, templist, 10000000,
in_repeater, incycle, outcycle, &val,
run_silently, time_bound, length_bound,
/*saveit_name */ ( char * ) NULL, kick_type,
&rstate ) )
{
fprintf ( stderr, "CClinkern_tour failed\n" );
val = -1.0;
goto CLEANUP;
}
if ( length_bound != -1 && val > length_bound )
{
printf ( "Cycle of value %.0f - did not reach %.0f\n",
val, length_bound );
printf ( "Try again. Number of attempts: %d\n", ++attempt );
}
}
while ( length_bound != -1 && val > length_bound );
#if 1
/* Fill tour */
genes = ncount / 2;
if ( ( outcycle[0] == outcycle[1] - genes ) ||
( outcycle[1] == outcycle[0] - genes ) )
{
for ( i = 0; i < genes; i++ )
{
tour[i] = 1 + outcycle[2 * i];
if ( tour[i] > genes )
{
tour[i] = -( tour[i] - genes );
}
}
}
else
{
for ( i = 0; i < genes; i++ )
{
tour[i] = 1 + outcycle[( 2 * i + 1 ) % ncount];
if ( tour[i] > genes )
tour[i] = -( tour[i] - genes );
}
}
#endif
#if TSPOUT
printf ( "Final Cycle: %.0f\n", val );
fflush ( outfile );
#endif
CLEANUP:
#ifndef BIG_PROBLEM
CC_IFFREE ( templist, int );
#endif
CCutil_freedatagroup ( &dat );
return ( ( int ) val );
}
int
greedylk ( int ncount, int **adj, int *tour, int *incycle, int *outcycle )
{
double val;
int tempcount, *templist;
#if 0
int *incycle = ( int * ) NULL, *outcycle = ( int * ) NULL;
#endif
CCdatagroup dat;
int rval = 0;
CCrandstate rstate;
int run_silently = 1;
double time_bound = 0.0;
double length_bound = 0.0;
int i, genes;
int kick_type = CC_LK_RANDOM_KICK;
CCedgegengroup plan;
int ecount;
int *elist = ( int * ) NULL;
int *elen = ( int * ) NULL;
double *x;
int seed;
fill_dat ( ncount, &dat, adj );
#if TSPOUT
printf ( "Greedy + Lin-Kernighan\n" );
fflush ( outfile );
#endif
ecount = ( ncount * ( ncount - 1 ) ) / 2;
seed = ( int ) CCutil_real_zeit ( );
CCutil_sprand ( seed, &rstate );
CCutil_genedgelist ( ncount, ecount, &elist, &elen, &dat, -1, &rstate );
x = CC_SAFE_MALLOC ( ecount, double );
for ( i = 0; i < ecount; i++ )
x[i] = ( double ) elen[i];
rval = CCtsp_x_greedy_tour ( &dat, ncount, ecount, elist, x, incycle,
&val, run_silently );
if ( rval )
{
fprintf ( stderr, "CCtsp_x_greedy_tour failed\n" );
goto CLEANUP;
}
CCedgegen_init_edgegengroup ( &plan );
plan.quadnearest = 2;
rval =
CCedgegen_edges ( &plan, ncount, &dat, ( double * ) NULL, &tempcount,
&templist, run_silently, &rstate );
if ( rval )
{
fprintf ( stderr, "CCedgegen_edges failed\n" );
goto CLEANUP;
}
rval = CClinkern_tour ( ncount, &dat, tempcount, templist, /* ncount */ 0,
/* ncount > 1000 ? 500 : ncount/2 */ 0,
incycle, outcycle, &val,
run_silently, time_bound, length_bound,
( char * ) NULL, kick_type, &rstate );
if ( rval )
{
fprintf ( stderr, "CClinkern_tour failed\n" );
goto CLEANUP;
}
#if 1
/* Fill tour */
genes = ncount / 2;
if ( ( outcycle[0] == outcycle[1] - genes ) ||
( outcycle[1] == outcycle[0] - genes ) )
{
for ( i = 0; i < genes; i++ )
{
tour[i] = 1 + outcycle[2 * i];
if ( tour[i] > genes )
{
tour[i] = -( tour[i] - genes );
}
}
}
else
{
for ( i = 0; i < genes; i++ )
{
tour[i] = 1 + outcycle[( 2 * i + 1 ) % ncount];
if ( tour[i] > genes )
tour[i] = -( tour[i] - genes );
}
}
#endif
#if TSPOUT
printf ( "Final Cycle: %.0f\n", val );
fflush ( outfile );
#endif
CLEANUP:
#ifndef BIG_PROBLEM
CC_IFFREE ( templist, int );
#endif
CCutil_freedatagroup ( &dat );
CC_IFFREE ( elist, int );
CC_IFFREE ( elen, int );
CC_IFFREE ( x, double );
return ( ( int ) val );
}
#endif