-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtesting.c
384 lines (336 loc) · 9.29 KB
/
testing.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
#include "tsp.h"
#ifdef TESTING
#include "testing.h"
#include <Windows.h>
#include <time.h>
extern char benchmark;
int main(int argc, char *argv[])
{
if (argc < 2)
{
print_help();
return 1;
}
if (VERB_PRINT_CLINE_ARGUMENTS)
for (int i = 1; i < argc; i++)
printf("arg %i\t: %s\n", i, argv[i]);
benchmark = 1;
testset ts;
memset(&ts, 0, sizeof(testset));
parse_test_command_line(argc, argv, &ts);
init_testset(&ts);
if (ts.comparing_heuristics)
{
read_opt_costs(&ts);
compare_heuristics(&ts);
}
else if (ts.comparing_times)
{
compare_exec_time(&ts);
}
else
test_all(&ts);
close_testset(&ts);
return 0;
}
void close_testset(testset * ts)
{
for (int i = 0; i < ts->size; i++)
close_instance(&ts->insts[i]);
free(ts->insts);
if (ts->comparing_heuristics)
free(ts->opt_costs);
}
void init_testset(testset * ts)
{
WIN32_FIND_DATA fdFile;
HANDLE hFind = NULL;
char path[STRING_MAX_SIZE + 1];
char** inst_names = (char**)calloc(TESTSET_MAX_SIZE, sizeof(char**));
int tsize = 0;
sprintf(path, "%s\\%s", ts->directory, ts->testset_filename);
hFind = FindFirstFile(path, &fdFile);
if (hFind != INVALID_HANDLE_VALUE)
{
FILE* f = fopen(path, "r");
char line[STRING_MAX_SIZE + 1];
int last;
if (f != NULL)
{
while (fgets(line, sizeof(line), f) != NULL)
{
last = strlen(line) - 1;
if (line[0] == '#') // comments starts with '#'
continue;
// removing CR, LF and blanks from the tail
while (line[last] == '\r' ||
line[last] == '\n' ||
line[last] == ' ')
line[last--] = '\0';
if (last != -1) // found a new valid file name
{
inst_names[tsize] = (char*)calloc(STRING_MAX_SIZE + 1, sizeof(char));
sprintf(inst_names[tsize], "%s\\%s", ts->directory, line);
tsize++;
}
}
fclose(f);
}
else
print_error("can't read \"testset.txt\"");
}
FindClose(hFind);
if (tsize == 0)
{
sprintf(path, "%s\\*.tsp", ts->directory);
hFind = FindFirstFile(path, &fdFile);
if (hFind == INVALID_HANDLE_VALUE)
print_error("Invalid test directory");
do
{
if (!(fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
inst_names[tsize] = (char*)calloc(STRING_MAX_SIZE + 1, sizeof(char));
sprintf(inst_names[tsize], "%s\\%s", ts->directory, fdFile.cFileName);
tsize++;
}
} while (FindNextFile(hFind, &fdFile));
FindClose(hFind);
}
ts->size = tsize;
ts->insts = (instance*)calloc(tsize, sizeof(instance));
for (int i = 0; i < tsize; i++)
{
strcpy(ts->insts[i].input_file, inst_names[i]);
free(inst_names[i]);
}
free(inst_names);
}
void read_opt_costs(testset* ts)
{
WIN32_FIND_DATA fdFile;
HANDLE hFind = NULL;
char path[STRING_MAX_SIZE + 1];
double* opt_costs = (char**)calloc(ts->size, sizeof(double*));
int tsize = 0;
sprintf(path, "%s\\%s", ts->directory, ts->opt_filename);
hFind = FindFirstFile(path, &fdFile);
if (hFind != INVALID_HANDLE_VALUE)
{
FILE* f = fopen(path, "r");
char line[STRING_MAX_SIZE + 1];
int last;
if (f != NULL)
{
while (fgets(line, sizeof(line), f) != NULL && tsize < ts->size)
{
last = strlen(line) - 1;
if (line[0] == '#') // comments starts with '#'
continue;
// removing CR, LF and blanks from the tail
while (line[last] == '\r' ||
line[last] == '\n' ||
line[last] == ' ')
line[last--] = '\0';
if (last != -1) // found a new valid file name
{
// removing the name at the beginning of the line
int i = 0;
while (line[i++] != ',')
if (i >= STRING_MAX_SIZE)
print_error("invalid opt cost file");
opt_costs[tsize] = atof(&line[i]);
tsize++;
}
}
fclose(f);
}
else
print_error("can't read \"opt.txt\"");
}
FindClose(hFind);
ts->opt_costs = opt_costs;
if (tsize < ts->size)
printf("WARNING: opt costs file doesn't contains all of the optimal costs!\n");
}
void parse_test_command_line(int argc, char* argv[], testset * ts)
{
for (int i = 1; i < argc; i++)
{
if (strcmp(argv[i], "-test_dir") == 0)
{
strcpy(ts->directory, argv[++i]);
}
else if (strcmp(argv[i], "-test_models") == 0)
{
strcpy(ts->model_types, argv[++i]);
}
else if (strcmp(argv[i], "-heuristics") == 0)
{
ts->comparing_heuristics = 1;
}
else if (strcmp(argv[i], "-times") == 0)
{
ts->comparing_times = 1;
}
else if (strcmp(argv[i], "-ts_file") == 0)
{
strcpy(ts->testset_filename, argv[++i]);
}
else if (strcmp(argv[i], "-opt_file") == 0)
{
strcpy(ts->opt_filename, argv[++i]);
}
else if (strcmp(argv[i], "-seed") == 0)
{
ts->random_seed = atoi(argv[++i]);
}
}
}
void test_all(testset * ts)
{
double timer, sec;
int* models = (int*)calloc(strlen(ts->model_types), sizeof(int));
int nmodels = 0;
for (int m = 0; m < strlen(ts->model_types); m++) // select which model types to test with
{
if (ts->model_types[m] >= '0' && ts->model_types[m] <= '9')
models[nmodels++] = ts->model_types[m] - '0';
}
FILE* output = fopen("benchmark.txt", "w");
if (output == NULL)
print_error("can't create benchmark file");
// header: column number and names
fprintf(output, "%i,", nmodels);
for (int m = 0; m < nmodels; m++)
fprintf(output, "%s%s", get_model_name(models[m]), (m + 1 < nmodels) ? "," : "\n");
solution sol;
for (int i = 0; i < ts->size; i++)
{
printf("\nTesting problem %03i\t: %s\n", i, ts->insts[i].input_file);
memset(&sol, 0x00, sizeof(solution));
read_input(&ts->insts[i]);
ts->insts[i].random_seed = ts->random_seed;
ts->insts[i].time_limit = 3600;
fprintf(output, "%s, ", ts->insts[i].input_file);
for (int m = 0; m < nmodels; m++) // for each model type
{
sol.inst = &ts->insts[i];
sol.inst->model_type = models[m];
printf(" using model %i %-20s ... ", models[m], get_model_name(models[m]));
fflush(stdout);
timer = -clock();
if (TSPopt(&sol))
print_error(" error within TSPopt()");
timer += clock();
sec = timer / CLOCKS_PER_SEC;
printf("\tsolved in %02i:%02i:%02i (%.3lf s)\n",
(int)(sec / 3600),
(int)((int)sec % 3600) / 60,
(int)sec % 60,
sec);
fprintf(output, "%10.3lf%s", sec, (m + 1 < nmodels) ? "," : "\n");
fflush(output);
if (sol.comp)
free(sol.comp);
if (sol.succ)
free(sol.succ);
if (sol.nodes_order)
free(sol.nodes_order);
memset(&sol, 0x00, sizeof(solution));
}
}
fclose(output);
free(models);
}
void compare_heuristics(testset *ts)
{
solution sol;
memset(&sol, 0x00, sizeof(solution));
const int M_FIRST_NN = 9;
const int M_GRASP = 11;
const int M_INSERTION = 12;
const int M_HARD_FIXING = 7;
const int M_LOCAL_BRANCHING = 8;
const int M_SIMULATED_ANNEALING = 13;
const int M_VNS = 14;
FILE* output = fopen("heuristics_comparison.txt", "w");
if (output == NULL)
print_error("can't create comparison file");
double time;
for (int i = 0; i<ts->size; i++)
{
solution sol;
memset(&sol, 0x00, sizeof(solution));
read_input(&ts->insts[i]);
sol.inst = &ts->insts[i];
printf("\nTesting problem %03i\t: %s\n", i, ts->insts[i].input_file);
fprintf(output, "%s ", sol.inst->input_file);
printf("\t%s\t\t...\n", get_model_name(M_VNS));
sol.inst->time_limit = 3600;
sol.inst->model_type = M_VNS;
double time = -clock();
VNS(&sol);
time += clock();
printf("apx: %.2lf%%\n", (sol.latest_cost / ts->opt_costs[i] - 1.0) * 100.0);
printf(" TIME %7.3lf \n", time / (double)CLOCKS_PER_SEC);
fprintf(output, "%7.3lf ", (sol.latest_cost / ts->opt_costs[i] - 1.0) * 100.0);
fprintf(output, "%7.3lf ", time / (double)CLOCKS_PER_SEC);
fprintf(output, "%7.3lf \n", sol.latest_improvement);
fflush(output);
sol.inst = NULL;
close_solution(&sol);
sol.inst = &ts->insts[i];
}
fclose(output);
}
void compare_exec_time(testset* ts)
{
solution sol;
memset(&sol, 0x00, sizeof(solution));
const int M_FIRST_NN = 9;
const int M_GRASP = 11;
const int M_INSERTION = 12;
FILE* output = fopen("time_comparison.txt", "w");
if (output == NULL)
print_error("can't create comparison file");
double time;
for (int i = 0; i < ts->size; i++)
{
solution sol;
memset(&sol, 0x00, sizeof(solution));
read_input(&ts->insts[i]);
sol.inst = &ts->insts[i];
int* aux = (int*)malloc(sol.inst->nnodes * sizeof(int));
printf("\nTesting problem %03i\t: %s\n", i, ts->insts[i].input_file);
fprintf(output, "%s ", sol.inst->input_file);
// ---> REFINEMENT TEST
printf("\t%s\t\t...\t", get_model_name(M_FIRST_NN));
first_nearest_neighbor(&sol);
double initial_cost = sol.latest_cost;
memcpy(aux, sol.succ, sol.inst->nnodes * sizeof(int));
double time;
time = -clock();
refine(&sol);
time += clock();
fprintf(output, "%7.3lf ", (initial_cost - sol.latest_cost) / initial_cost * 100.0);
fprintf(output, "%.3lf ", time / (double)CLOCKS_PER_SEC);
printf("%7.3lf%% ", (initial_cost - sol.latest_cost) / initial_cost * 100.0);
printf("%.3lf sec\n", time / (double)CLOCKS_PER_SEC);
memcpy(sol.succ, aux, sol.inst->nnodes * sizeof(int)); //restoring solution
time = -clock();
sol.latest_cost = initial_cost;
greedy_refinement_2opt(&sol);
time += clock();
fprintf(output, "%7.3lf ", (initial_cost - sol.latest_cost) / initial_cost * 100.0);
fprintf(output, "%.3lf\n", time / (double)CLOCKS_PER_SEC);
printf("%7.3lf%% ", (initial_cost - sol.latest_cost) / initial_cost * 100.0);
printf("%.3lf sec\n", time / (double)CLOCKS_PER_SEC);
sol.inst = NULL;
close_solution(&sol);
printf("\n");
free(aux);
}
fclose(output);
}
#endif