-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_c_api.c
More file actions
149 lines (133 loc) · 5 KB
/
Copy pathtest_c_api.c
File metadata and controls
149 lines (133 loc) · 5 KB
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
/* Copyright 2017 Stephen L. Smith and Frank Imeson
* Licensed under the Apache License, Version 2.0. See LICENSE.
*
* C API smoke test: solves the tiny 6-vertex instance from examples/tiny.gtsp
* (built in memory) and one instance from a file.
*/
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "glns/glns.h"
static int failures = 0;
#define CHECK(cond) \
do { \
if (!(cond)) { \
printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); \
++failures; \
} \
} while (0)
int main(int argc, char** argv) {
CHECK(GLNS_VERSION_MAJOR == 0);
CHECK(strcmp(glns_version(), GLNS_VERSION_STRING) == 0);
glns_params_init(NULL);
/* the tiny.gtsp instance: 6 vertices, 3 sets, full matrix */
const int64_t dist[36] = {
9999, 3, 4, 7, 8, 2,
3, 9999, 6, 5, 3, 9,
4, 6, 9999, 4, 2, 6,
7, 5, 4, 9999, 1, 7,
8, 3, 2, 1, 9999, 4,
2, 9, 6, 7, 4, 9999,
};
const int set_sizes[3] = {2, 2, 2};
const int set_vertices[6] = {0, 1, 2, 3, 4, 5}; /* 0-indexed */
glns_params params;
glns_params_init(¶ms);
params.seed = 42;
params.has_seed = 1;
params.insertion_algs = "randpdf";
params.removal_algs = "classic";
glns_solution solution;
char err[256];
int rc = glns_solve(6, 3, dist, set_sizes, set_vertices, ¶ms, &solution, err,
sizeof(err));
CHECK(rc == 0);
if (rc == 0) {
CHECK(solution.tour_len == 3);
CHECK(solution.cost == 9); /* known optimum for tiny.gtsp */
glns_solution_free(&solution);
CHECK(solution.tour == NULL);
} else {
printf("error: %s\n", err);
}
/* error path: vertex in two sets */
const int bad_vertices[6] = {0, 1, 1, 3, 4, 5};
rc = glns_solve(6, 3, dist, set_sizes, bad_vertices, ¶ms, &solution, err,
sizeof(err));
CHECK(rc != 0);
CHECK(strlen(err) > 0);
CHECK(solution.tour == NULL);
CHECK(solution.tour_len == 0);
glns_solution_free(&solution);
/* error path: invalid solver parameter */
glns_params_init(¶ms);
params.trials = 0;
rc = glns_solve(6, 3, dist, set_sizes, set_vertices, ¶ms, &solution, err,
sizeof(err));
CHECK(rc != 0);
CHECK(strstr(err, "trials") != NULL);
/* error path: negative edge cost */
int64_t negative_dist[36];
memcpy(negative_dist, dist, sizeof(dist));
negative_dist[1] = -1;
glns_params_init(¶ms);
rc = glns_solve(6, 3, negative_dist, set_sizes, set_vertices, ¶ms, &solution,
err, sizeof(err));
CHECK(rc != 0);
CHECK(strstr(err, "negative") != NULL);
/* error path: invalid set size */
const int empty_set_sizes[3] = {2, 0, 4};
rc = glns_solve(6, 3, dist, empty_set_sizes, set_vertices, ¶ms, &solution,
err, sizeof(err));
CHECK(rc != 0);
CHECK(strstr(err, "set size") != NULL);
glns_params_init(¶ms);
params.seed = 42;
params.has_seed = 1;
/* Nonfinite values are errors, not negative-value default sentinels. */
const double nonfinite[] = {NAN, INFINITY, -INFINITY};
const char* fields[] = {"max_time", "reopt", "epsilon"};
for (int field = 0; field < 3; ++field) {
for (int value = 0; value < 3; ++value) {
glns_params_init(¶ms);
params.trials = 1;
params.restarts = 0;
params.seed = 1;
params.has_seed = 1;
if (field == 0) params.max_time = nonfinite[value];
if (field == 1) params.reopt = nonfinite[value];
if (field == 2) params.epsilon = nonfinite[value];
rc = glns_solve(6, 3, dist, set_sizes, set_vertices, ¶ms, &solution,
err, sizeof(err));
CHECK(rc != 0);
CHECK(strstr(err, fields[field]) != NULL);
CHECK(solution.tour == NULL);
glns_solution_free(&solution);
}
}
glns_params_init(¶ms);
params.seed = 42;
params.has_seed = 1;
/* file path, if an examples directory was given */
if (argc > 1) {
char path[512];
snprintf(path, sizeof(path), "%s/39rat195.gtsp", argv[1]);
rc = glns_solve_file(path, ¶ms, &solution, err, sizeof(err));
CHECK(rc == 0);
if (rc == 0) {
CHECK(solution.tour_len == 39);
CHECK(solution.cost > 0);
printf("39rat195 via C API: cost=%lld time=%.2fs\n",
(long long)solution.cost, solution.solve_time);
glns_solution_free(&solution);
} else {
printf("error: %s\n", err);
}
}
if (failures == 0) {
printf("C API TESTS PASSED\n");
return 0;
}
printf("%d FAILURES\n", failures);
return 1;
}