alpar@1
|
1 |
/* glptsp.c */
|
alpar@1
|
2 |
|
alpar@1
|
3 |
/***********************************************************************
|
alpar@1
|
4 |
* This code is part of GLPK (GNU Linear Programming Kit).
|
alpar@1
|
5 |
*
|
alpar@1
|
6 |
* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
|
alpar@1
|
7 |
* 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
|
alpar@1
|
8 |
* Moscow Aviation Institute, Moscow, Russia. All rights reserved.
|
alpar@1
|
9 |
* E-mail: <mao@gnu.org>.
|
alpar@1
|
10 |
*
|
alpar@1
|
11 |
* GLPK is free software: you can redistribute it and/or modify it
|
alpar@1
|
12 |
* under the terms of the GNU General Public License as published by
|
alpar@1
|
13 |
* the Free Software Foundation, either version 3 of the License, or
|
alpar@1
|
14 |
* (at your option) any later version.
|
alpar@1
|
15 |
*
|
alpar@1
|
16 |
* GLPK is distributed in the hope that it will be useful, but WITHOUT
|
alpar@1
|
17 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
alpar@1
|
18 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
alpar@1
|
19 |
* License for more details.
|
alpar@1
|
20 |
*
|
alpar@1
|
21 |
* You should have received a copy of the GNU General Public License
|
alpar@1
|
22 |
* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
|
alpar@1
|
23 |
***********************************************************************/
|
alpar@1
|
24 |
|
alpar@1
|
25 |
#define _GLPSTD_ERRNO
|
alpar@1
|
26 |
#define _GLPSTD_STDIO
|
alpar@1
|
27 |
#include "glpenv.h"
|
alpar@1
|
28 |
#include "glptsp.h"
|
alpar@1
|
29 |
#define xfault xerror
|
alpar@1
|
30 |
|
alpar@1
|
31 |
/*----------------------------------------------------------------------
|
alpar@1
|
32 |
-- tsp_read_data - read TSP instance data.
|
alpar@1
|
33 |
--
|
alpar@1
|
34 |
-- *Synopsis*
|
alpar@1
|
35 |
--
|
alpar@1
|
36 |
-- #include "glptsp.h"
|
alpar@1
|
37 |
-- TSP *tsp_read_data(char *fname);
|
alpar@1
|
38 |
--
|
alpar@1
|
39 |
-- *Description*
|
alpar@1
|
40 |
--
|
alpar@1
|
41 |
-- The routine tsp_read_data reads a TSP (or related problem) instance
|
alpar@1
|
42 |
-- data from the text file, whose name is the character string fname.
|
alpar@1
|
43 |
--
|
alpar@1
|
44 |
-- For detailed description of the format recognized by the routine see
|
alpar@1
|
45 |
-- the report: G.Reinelt, TSPLIB 95.
|
alpar@1
|
46 |
--
|
alpar@1
|
47 |
-- *Returns*
|
alpar@1
|
48 |
--
|
alpar@1
|
49 |
-- If no error occurred, the routine tsp_read_data returns a pointer to
|
alpar@1
|
50 |
-- the TSP instance data block, which contains loaded data. In the case
|
alpar@1
|
51 |
-- of error the routine prints an error message and returns NULL. */
|
alpar@1
|
52 |
|
alpar@1
|
53 |
struct dsa
|
alpar@1
|
54 |
{ /* dynamic storage area used by the routine tsp_read_data */
|
alpar@1
|
55 |
char *fname;
|
alpar@1
|
56 |
/* name of the input text file */
|
alpar@1
|
57 |
FILE *fp;
|
alpar@1
|
58 |
/* stream assigned to the input text file */
|
alpar@1
|
59 |
int seqn;
|
alpar@1
|
60 |
/* line sequential number */
|
alpar@1
|
61 |
int c;
|
alpar@1
|
62 |
/* current character */
|
alpar@1
|
63 |
char token[255+1];
|
alpar@1
|
64 |
/* current token */
|
alpar@1
|
65 |
};
|
alpar@1
|
66 |
|
alpar@1
|
67 |
static int get_char(struct dsa *dsa)
|
alpar@1
|
68 |
{ dsa->c = fgetc(dsa->fp);
|
alpar@1
|
69 |
if (ferror(dsa->fp))
|
alpar@1
|
70 |
{ xprintf("%s:%d: read error - %s\n",
|
alpar@1
|
71 |
dsa->fname, dsa->seqn, strerror(errno));
|
alpar@1
|
72 |
return 1;
|
alpar@1
|
73 |
}
|
alpar@1
|
74 |
if (feof(dsa->fp))
|
alpar@1
|
75 |
dsa->c = EOF;
|
alpar@1
|
76 |
else if (dsa->c == '\n')
|
alpar@1
|
77 |
dsa->seqn++;
|
alpar@1
|
78 |
else if (isspace(dsa->c))
|
alpar@1
|
79 |
dsa->c = ' ';
|
alpar@1
|
80 |
else if (iscntrl(dsa->c))
|
alpar@1
|
81 |
{ xprintf("%s:%d: invalid control character 0x%02X\n",
|
alpar@1
|
82 |
dsa->fname, dsa->seqn, dsa->c);
|
alpar@1
|
83 |
return 1;
|
alpar@1
|
84 |
}
|
alpar@1
|
85 |
return 0;
|
alpar@1
|
86 |
}
|
alpar@1
|
87 |
|
alpar@1
|
88 |
static int skip_spaces(struct dsa *dsa, int across)
|
alpar@1
|
89 |
{ while (dsa->c == ' ' || (across && dsa->c == '\n'))
|
alpar@1
|
90 |
if (get_char(dsa)) return 1;
|
alpar@1
|
91 |
return 0;
|
alpar@1
|
92 |
}
|
alpar@1
|
93 |
|
alpar@1
|
94 |
static int scan_keyword(struct dsa *dsa)
|
alpar@1
|
95 |
{ int len = 0;
|
alpar@1
|
96 |
if (skip_spaces(dsa, 0)) return 1;
|
alpar@1
|
97 |
dsa->token[0] = '\0';
|
alpar@1
|
98 |
while (isalnum(dsa->c) || dsa->c == '_')
|
alpar@1
|
99 |
{ if (len == 31)
|
alpar@1
|
100 |
{ xprintf("%s:%d: keyword `%s...' too long\n", dsa->fname,
|
alpar@1
|
101 |
dsa->seqn, dsa->token);
|
alpar@1
|
102 |
return 1;
|
alpar@1
|
103 |
}
|
alpar@1
|
104 |
dsa->token[len++] = (char)dsa->c, dsa->token[len] = '\0';
|
alpar@1
|
105 |
if (get_char(dsa)) return 1;
|
alpar@1
|
106 |
}
|
alpar@1
|
107 |
if (len == 0)
|
alpar@1
|
108 |
{ xprintf("%s:%d: missing keyword\n", dsa->fname, dsa->seqn);
|
alpar@1
|
109 |
return 1;
|
alpar@1
|
110 |
}
|
alpar@1
|
111 |
return 0;
|
alpar@1
|
112 |
}
|
alpar@1
|
113 |
|
alpar@1
|
114 |
static int check_colon(struct dsa *dsa)
|
alpar@1
|
115 |
{ if (skip_spaces(dsa, 0)) return 1;
|
alpar@1
|
116 |
if (dsa->c != ':')
|
alpar@1
|
117 |
{ xprintf("%s:%d: missing colon after `%s'\n", dsa->fname,
|
alpar@1
|
118 |
dsa->seqn, dsa->token);
|
alpar@1
|
119 |
return 1;
|
alpar@1
|
120 |
}
|
alpar@1
|
121 |
if (get_char(dsa)) return 1;
|
alpar@1
|
122 |
return 0;
|
alpar@1
|
123 |
}
|
alpar@1
|
124 |
|
alpar@1
|
125 |
static int scan_token(struct dsa *dsa, int across)
|
alpar@1
|
126 |
{ int len = 0;
|
alpar@1
|
127 |
if (skip_spaces(dsa, across)) return 1;
|
alpar@1
|
128 |
dsa->token[0] = '\0';
|
alpar@1
|
129 |
while (!(dsa->c == EOF || dsa->c == '\n' || dsa->c == ' '))
|
alpar@1
|
130 |
{ if (len == 255)
|
alpar@1
|
131 |
{ dsa->token[31] = '\0';
|
alpar@1
|
132 |
xprintf("%s:%d: token `%s...' too long\n", dsa->fname,
|
alpar@1
|
133 |
dsa->seqn, dsa->token);
|
alpar@1
|
134 |
return 1;
|
alpar@1
|
135 |
}
|
alpar@1
|
136 |
dsa->token[len++] = (char)dsa->c, dsa->token[len] = '\0';
|
alpar@1
|
137 |
if (get_char(dsa)) return 1;
|
alpar@1
|
138 |
}
|
alpar@1
|
139 |
return 0;
|
alpar@1
|
140 |
}
|
alpar@1
|
141 |
|
alpar@1
|
142 |
static int check_newline(struct dsa *dsa)
|
alpar@1
|
143 |
{ if (skip_spaces(dsa, 0)) return 1;
|
alpar@1
|
144 |
if (!(dsa->c == EOF || dsa->c == '\n'))
|
alpar@1
|
145 |
{ xprintf("%s:%d: extra symbols detected\n", dsa->fname,
|
alpar@1
|
146 |
dsa->seqn);
|
alpar@1
|
147 |
return 1;
|
alpar@1
|
148 |
}
|
alpar@1
|
149 |
if (get_char(dsa)) return 1;
|
alpar@1
|
150 |
return 0;
|
alpar@1
|
151 |
}
|
alpar@1
|
152 |
|
alpar@1
|
153 |
static int scan_comment(struct dsa *dsa)
|
alpar@1
|
154 |
{ int len = 0;
|
alpar@1
|
155 |
if (skip_spaces(dsa, 0)) return 1;
|
alpar@1
|
156 |
dsa->token[0] = '\0';
|
alpar@1
|
157 |
while (!(dsa->c == EOF || dsa->c == '\n'))
|
alpar@1
|
158 |
{ if (len == 255)
|
alpar@1
|
159 |
{ xprintf("%s:%d: comment too long\n", dsa->fname, dsa->seqn)
|
alpar@1
|
160 |
;
|
alpar@1
|
161 |
return 1;
|
alpar@1
|
162 |
}
|
alpar@1
|
163 |
dsa->token[len++] = (char)dsa->c, dsa->token[len] = '\0';
|
alpar@1
|
164 |
if (get_char(dsa)) return 1;
|
alpar@1
|
165 |
}
|
alpar@1
|
166 |
return 0;
|
alpar@1
|
167 |
}
|
alpar@1
|
168 |
|
alpar@1
|
169 |
static int scan_integer(struct dsa *dsa, int across, int *val)
|
alpar@1
|
170 |
{ if (scan_token(dsa, across)) return 1;
|
alpar@1
|
171 |
if (strlen(dsa->token) == 0)
|
alpar@1
|
172 |
{ xprintf("%s:%d: missing integer\n", dsa->fname, dsa->seqn);
|
alpar@1
|
173 |
return 1;
|
alpar@1
|
174 |
}
|
alpar@1
|
175 |
if (str2int(dsa->token, val))
|
alpar@1
|
176 |
{ xprintf("%s:%d: integer `%s' invalid\n", dsa->fname, dsa->seqn
|
alpar@1
|
177 |
, dsa->token);
|
alpar@1
|
178 |
return 1;
|
alpar@1
|
179 |
}
|
alpar@1
|
180 |
return 0;
|
alpar@1
|
181 |
}
|
alpar@1
|
182 |
|
alpar@1
|
183 |
static int scan_number(struct dsa *dsa, int across, double *val)
|
alpar@1
|
184 |
{ if (scan_token(dsa, across)) return 1;
|
alpar@1
|
185 |
if (strlen(dsa->token) == 0)
|
alpar@1
|
186 |
{ xprintf("%s:%d: missing number\n", dsa->fname, dsa->seqn);
|
alpar@1
|
187 |
return 1;
|
alpar@1
|
188 |
}
|
alpar@1
|
189 |
if (str2num(dsa->token, val))
|
alpar@1
|
190 |
{ xprintf("%s:%d: number `%s' invalid\n", dsa->fname, dsa->seqn,
|
alpar@1
|
191 |
dsa->token);
|
alpar@1
|
192 |
return 1;
|
alpar@1
|
193 |
}
|
alpar@1
|
194 |
return 0;
|
alpar@1
|
195 |
}
|
alpar@1
|
196 |
|
alpar@1
|
197 |
TSP *tsp_read_data(char *fname)
|
alpar@1
|
198 |
{ struct dsa _dsa, *dsa = &_dsa;
|
alpar@1
|
199 |
TSP *tsp = NULL;
|
alpar@1
|
200 |
dsa->fname = fname;
|
alpar@1
|
201 |
xprintf("tsp_read_data: reading TSP data from `%s'...\n",
|
alpar@1
|
202 |
dsa->fname);
|
alpar@1
|
203 |
dsa->fp = fopen(dsa->fname, "r");
|
alpar@1
|
204 |
if (dsa->fp == NULL)
|
alpar@1
|
205 |
{ xprintf("tsp_read_data: unable to open `%s' - %s\n",
|
alpar@1
|
206 |
dsa->fname, strerror(errno));
|
alpar@1
|
207 |
goto fail;
|
alpar@1
|
208 |
}
|
alpar@1
|
209 |
tsp = xmalloc(sizeof(TSP));
|
alpar@1
|
210 |
tsp->name = NULL;
|
alpar@1
|
211 |
tsp->type = TSP_UNDEF;
|
alpar@1
|
212 |
tsp->comment = NULL;
|
alpar@1
|
213 |
tsp->dimension = 0;
|
alpar@1
|
214 |
tsp->edge_weight_type = TSP_UNDEF;
|
alpar@1
|
215 |
tsp->edge_weight_format = TSP_UNDEF;
|
alpar@1
|
216 |
tsp->display_data_type = TSP_UNDEF;
|
alpar@1
|
217 |
tsp->node_x_coord = NULL;
|
alpar@1
|
218 |
tsp->node_y_coord = NULL;
|
alpar@1
|
219 |
tsp->dply_x_coord = NULL;
|
alpar@1
|
220 |
tsp->dply_y_coord = NULL;
|
alpar@1
|
221 |
tsp->tour = NULL;
|
alpar@1
|
222 |
tsp->edge_weight = NULL;
|
alpar@1
|
223 |
dsa->seqn = 1;
|
alpar@1
|
224 |
if (get_char(dsa)) goto fail;
|
alpar@1
|
225 |
loop: if (scan_keyword(dsa)) goto fail;
|
alpar@1
|
226 |
if (strcmp(dsa->token, "NAME") == 0)
|
alpar@1
|
227 |
{ if (tsp->name != NULL)
|
alpar@1
|
228 |
{ xprintf("%s:%d: NAME entry multiply defined\n", dsa->fname,
|
alpar@1
|
229 |
dsa->seqn);
|
alpar@1
|
230 |
goto fail;
|
alpar@1
|
231 |
}
|
alpar@1
|
232 |
if (check_colon(dsa)) goto fail;
|
alpar@1
|
233 |
if (scan_token(dsa, 0)) goto fail;
|
alpar@1
|
234 |
if (strlen(dsa->token) == 0)
|
alpar@1
|
235 |
{ xprintf("%s:%d: NAME entry incomplete\n", dsa->fname,
|
alpar@1
|
236 |
dsa->seqn);
|
alpar@1
|
237 |
goto fail;
|
alpar@1
|
238 |
}
|
alpar@1
|
239 |
tsp->name = xmalloc(strlen(dsa->token) + 1);
|
alpar@1
|
240 |
strcpy(tsp->name, dsa->token);
|
alpar@1
|
241 |
xprintf("tsp_read_data: NAME: %s\n", tsp->name);
|
alpar@1
|
242 |
if (check_newline(dsa)) goto fail;
|
alpar@1
|
243 |
}
|
alpar@1
|
244 |
else if (strcmp(dsa->token, "TYPE") == 0)
|
alpar@1
|
245 |
{ if (tsp->type != TSP_UNDEF)
|
alpar@1
|
246 |
{ xprintf("%s:%d: TYPE entry multiply defined\n", dsa->fname,
|
alpar@1
|
247 |
dsa->seqn);
|
alpar@1
|
248 |
goto fail;
|
alpar@1
|
249 |
}
|
alpar@1
|
250 |
if (check_colon(dsa)) goto fail;
|
alpar@1
|
251 |
if (scan_keyword(dsa)) goto fail;
|
alpar@1
|
252 |
if (strcmp(dsa->token, "TSP") == 0)
|
alpar@1
|
253 |
tsp->type = TSP_TSP;
|
alpar@1
|
254 |
else if (strcmp(dsa->token, "ATSP") == 0)
|
alpar@1
|
255 |
tsp->type = TSP_ATSP;
|
alpar@1
|
256 |
else if (strcmp(dsa->token, "TOUR") == 0)
|
alpar@1
|
257 |
tsp->type = TSP_TOUR;
|
alpar@1
|
258 |
else
|
alpar@1
|
259 |
{ xprintf("%s:%d: data type `%s' not recognized\n",
|
alpar@1
|
260 |
dsa->fname, dsa->seqn, dsa->token);
|
alpar@1
|
261 |
goto fail;
|
alpar@1
|
262 |
}
|
alpar@1
|
263 |
xprintf("tsp_read_data: TYPE: %s\n", dsa->token);
|
alpar@1
|
264 |
if (check_newline(dsa)) goto fail;
|
alpar@1
|
265 |
}
|
alpar@1
|
266 |
else if (strcmp(dsa->token, "COMMENT") == 0)
|
alpar@1
|
267 |
{ if (tsp->comment != NULL)
|
alpar@1
|
268 |
{ xprintf("%s:%d: COMMENT entry multiply defined\n",
|
alpar@1
|
269 |
dsa->fname, dsa->seqn);
|
alpar@1
|
270 |
goto fail;
|
alpar@1
|
271 |
}
|
alpar@1
|
272 |
if (check_colon(dsa)) goto fail;
|
alpar@1
|
273 |
if (scan_comment(dsa)) goto fail;
|
alpar@1
|
274 |
tsp->comment = xmalloc(strlen(dsa->token) + 1);
|
alpar@1
|
275 |
strcpy(tsp->comment, dsa->token);
|
alpar@1
|
276 |
xprintf("tsp_read_data: COMMENT: %s\n", tsp->comment);
|
alpar@1
|
277 |
if (check_newline(dsa)) goto fail;
|
alpar@1
|
278 |
}
|
alpar@1
|
279 |
else if (strcmp(dsa->token, "DIMENSION") == 0)
|
alpar@1
|
280 |
{ if (tsp->dimension != 0)
|
alpar@1
|
281 |
{ xprintf("%s:%d: DIMENSION entry multiply defined\n",
|
alpar@1
|
282 |
dsa->fname, dsa->seqn);
|
alpar@1
|
283 |
goto fail;
|
alpar@1
|
284 |
}
|
alpar@1
|
285 |
if (check_colon(dsa)) goto fail;
|
alpar@1
|
286 |
if (scan_integer(dsa, 0, &tsp->dimension)) goto fail;
|
alpar@1
|
287 |
if (tsp->dimension < 1)
|
alpar@1
|
288 |
{ xprintf("%s:%d: invalid dimension\n", dsa->fname,
|
alpar@1
|
289 |
dsa->seqn);
|
alpar@1
|
290 |
goto fail;
|
alpar@1
|
291 |
}
|
alpar@1
|
292 |
xprintf("tsp_read_data: DIMENSION: %d\n", tsp->dimension);
|
alpar@1
|
293 |
if (check_newline(dsa)) goto fail;
|
alpar@1
|
294 |
}
|
alpar@1
|
295 |
else if (strcmp(dsa->token, "EDGE_WEIGHT_TYPE") == 0)
|
alpar@1
|
296 |
{ if (tsp->edge_weight_type != TSP_UNDEF)
|
alpar@1
|
297 |
{ xprintf("%s:%d: EDGE_WEIGHT_TYPE entry multiply defined\n",
|
alpar@1
|
298 |
dsa->fname, dsa->seqn);
|
alpar@1
|
299 |
goto fail;
|
alpar@1
|
300 |
}
|
alpar@1
|
301 |
if (check_colon(dsa)) goto fail;
|
alpar@1
|
302 |
if (scan_keyword(dsa)) goto fail;
|
alpar@1
|
303 |
if (strcmp(dsa->token, "GEO") == 0)
|
alpar@1
|
304 |
tsp->edge_weight_type = TSP_GEO;
|
alpar@1
|
305 |
else if (strcmp(dsa->token, "EUC_2D") == 0)
|
alpar@1
|
306 |
tsp->edge_weight_type = TSP_EUC_2D;
|
alpar@1
|
307 |
else if (strcmp(dsa->token, "ATT") == 0)
|
alpar@1
|
308 |
tsp->edge_weight_type = TSP_ATT;
|
alpar@1
|
309 |
else if (strcmp(dsa->token, "EXPLICIT") == 0)
|
alpar@1
|
310 |
tsp->edge_weight_type = TSP_EXPLICIT;
|
alpar@1
|
311 |
else if (strcmp(dsa->token, "CEIL_2D") == 0)
|
alpar@1
|
312 |
tsp->edge_weight_type = TSP_CEIL_2D;
|
alpar@1
|
313 |
else
|
alpar@1
|
314 |
{ xprintf("%s:%d: edge weight type `%s' not recognized\n",
|
alpar@1
|
315 |
dsa->fname, dsa->seqn, dsa->token);
|
alpar@1
|
316 |
goto fail;
|
alpar@1
|
317 |
}
|
alpar@1
|
318 |
xprintf("tsp_read_data: EDGE_WEIGHT_TYPE: %s\n", dsa->token);
|
alpar@1
|
319 |
if (check_newline(dsa)) goto fail;
|
alpar@1
|
320 |
}
|
alpar@1
|
321 |
else if (strcmp(dsa->token, "EDGE_WEIGHT_FORMAT") == 0)
|
alpar@1
|
322 |
{ if (tsp->edge_weight_format != TSP_UNDEF)
|
alpar@1
|
323 |
{ xprintf(
|
alpar@1
|
324 |
"%s:%d: EDGE_WEIGHT_FORMAT entry multiply defined\n",
|
alpar@1
|
325 |
dsa->fname, dsa->seqn);
|
alpar@1
|
326 |
goto fail;
|
alpar@1
|
327 |
}
|
alpar@1
|
328 |
if (check_colon(dsa)) goto fail;
|
alpar@1
|
329 |
if (scan_keyword(dsa)) goto fail;
|
alpar@1
|
330 |
if (strcmp(dsa->token, "UPPER_ROW") == 0)
|
alpar@1
|
331 |
tsp->edge_weight_format = TSP_UPPER_ROW;
|
alpar@1
|
332 |
else if (strcmp(dsa->token, "FULL_MATRIX") == 0)
|
alpar@1
|
333 |
tsp->edge_weight_format = TSP_FULL_MATRIX;
|
alpar@1
|
334 |
else if (strcmp(dsa->token, "FUNCTION") == 0)
|
alpar@1
|
335 |
tsp->edge_weight_format = TSP_FUNCTION;
|
alpar@1
|
336 |
else if (strcmp(dsa->token, "LOWER_DIAG_ROW") == 0)
|
alpar@1
|
337 |
tsp->edge_weight_format = TSP_LOWER_DIAG_ROW;
|
alpar@1
|
338 |
else
|
alpar@1
|
339 |
{ xprintf("%s:%d: edge weight format `%s' not recognized\n",
|
alpar@1
|
340 |
dsa->fname, dsa->seqn, dsa->token);
|
alpar@1
|
341 |
goto fail;
|
alpar@1
|
342 |
}
|
alpar@1
|
343 |
xprintf("tsp_read_data: EDGE_WEIGHT_FORMAT: %s\n", dsa->token);
|
alpar@1
|
344 |
if (check_newline(dsa)) goto fail;
|
alpar@1
|
345 |
}
|
alpar@1
|
346 |
else if (strcmp(dsa->token, "DISPLAY_DATA_TYPE") == 0)
|
alpar@1
|
347 |
{ if (tsp->display_data_type != TSP_UNDEF)
|
alpar@1
|
348 |
{ xprintf("%s:%d: DISPLAY_DATA_TYPE entry multiply defined\n",
|
alpar@1
|
349 |
dsa->fname, dsa->seqn);
|
alpar@1
|
350 |
goto fail;
|
alpar@1
|
351 |
}
|
alpar@1
|
352 |
if (check_colon(dsa)) goto fail;
|
alpar@1
|
353 |
if (scan_keyword(dsa)) goto fail;
|
alpar@1
|
354 |
if (strcmp(dsa->token, "COORD_DISPLAY") == 0)
|
alpar@1
|
355 |
tsp->display_data_type = TSP_COORD_DISPLAY;
|
alpar@1
|
356 |
else if (strcmp(dsa->token, "TWOD_DISPLAY") == 0)
|
alpar@1
|
357 |
tsp->display_data_type = TSP_TWOD_DISPLAY;
|
alpar@1
|
358 |
else
|
alpar@1
|
359 |
{ xprintf("%s:%d: display data type `%s' not recognized\n",
|
alpar@1
|
360 |
dsa->fname, dsa->seqn, dsa->token);
|
alpar@1
|
361 |
goto fail;
|
alpar@1
|
362 |
}
|
alpar@1
|
363 |
xprintf("tsp_read_data: DISPLAY_DATA_TYPE: %s\n", dsa->token);
|
alpar@1
|
364 |
if (check_newline(dsa)) goto fail;
|
alpar@1
|
365 |
}
|
alpar@1
|
366 |
else if (strcmp(dsa->token, "NODE_COORD_SECTION") == 0)
|
alpar@1
|
367 |
{ int n = tsp->dimension, k, node;
|
alpar@1
|
368 |
if (n == 0)
|
alpar@1
|
369 |
{ xprintf("%s:%d: DIMENSION entry not specified\n",
|
alpar@1
|
370 |
dsa->fname, dsa->seqn);
|
alpar@1
|
371 |
goto fail;
|
alpar@1
|
372 |
}
|
alpar@1
|
373 |
if (tsp->node_x_coord != NULL)
|
alpar@1
|
374 |
{ xprintf("%s:%d: NODE_COORD_SECTION multiply specified\n",
|
alpar@1
|
375 |
dsa->fname, dsa->seqn);
|
alpar@1
|
376 |
goto fail;
|
alpar@1
|
377 |
}
|
alpar@1
|
378 |
if (check_newline(dsa)) goto fail;
|
alpar@1
|
379 |
tsp->node_x_coord = xcalloc(1+n, sizeof(double));
|
alpar@1
|
380 |
tsp->node_y_coord = xcalloc(1+n, sizeof(double));
|
alpar@1
|
381 |
for (node = 1; node <= n; node++)
|
alpar@1
|
382 |
tsp->node_x_coord[node] = tsp->node_y_coord[node] = DBL_MAX;
|
alpar@1
|
383 |
for (k = 1; k <= n; k++)
|
alpar@1
|
384 |
{ if (scan_integer(dsa, 0, &node)) goto fail;
|
alpar@1
|
385 |
if (!(1 <= node && node <= n))
|
alpar@1
|
386 |
{ xprintf("%s:%d: invalid node number %d\n", dsa->fname,
|
alpar@1
|
387 |
dsa->seqn, node);
|
alpar@1
|
388 |
goto fail;
|
alpar@1
|
389 |
}
|
alpar@1
|
390 |
if (tsp->node_x_coord[node] != DBL_MAX)
|
alpar@1
|
391 |
{ xprintf("%s:%d: node number %d multiply specified\n",
|
alpar@1
|
392 |
dsa->fname, dsa->seqn, node);
|
alpar@1
|
393 |
goto fail;
|
alpar@1
|
394 |
}
|
alpar@1
|
395 |
if (scan_number(dsa, 0, &tsp->node_x_coord[node]))
|
alpar@1
|
396 |
goto fail;
|
alpar@1
|
397 |
if (scan_number(dsa, 0, &tsp->node_y_coord[node]))
|
alpar@1
|
398 |
goto fail;
|
alpar@1
|
399 |
if (check_newline(dsa)) goto fail;
|
alpar@1
|
400 |
}
|
alpar@1
|
401 |
}
|
alpar@1
|
402 |
else if (strcmp(dsa->token, "DISPLAY_DATA_SECTION") == 0)
|
alpar@1
|
403 |
{ int n = tsp->dimension, k, node;
|
alpar@1
|
404 |
if (n == 0)
|
alpar@1
|
405 |
{ xprintf("%s:%d: DIMENSION entry not specified\n",
|
alpar@1
|
406 |
dsa->fname, dsa->seqn);
|
alpar@1
|
407 |
goto fail;
|
alpar@1
|
408 |
}
|
alpar@1
|
409 |
if (tsp->dply_x_coord != NULL)
|
alpar@1
|
410 |
{ xprintf("%s:%d: DISPLAY_DATA_SECTION multiply specified\n",
|
alpar@1
|
411 |
dsa->fname, dsa->seqn);
|
alpar@1
|
412 |
goto fail;
|
alpar@1
|
413 |
}
|
alpar@1
|
414 |
if (check_newline(dsa)) goto fail;
|
alpar@1
|
415 |
tsp->dply_x_coord = xcalloc(1+n, sizeof(double));
|
alpar@1
|
416 |
tsp->dply_y_coord = xcalloc(1+n, sizeof(double));
|
alpar@1
|
417 |
for (node = 1; node <= n; node++)
|
alpar@1
|
418 |
tsp->dply_x_coord[node] = tsp->dply_y_coord[node] = DBL_MAX;
|
alpar@1
|
419 |
for (k = 1; k <= n; k++)
|
alpar@1
|
420 |
{ if (scan_integer(dsa, 0, &node)) goto fail;
|
alpar@1
|
421 |
if (!(1 <= node && node <= n))
|
alpar@1
|
422 |
{ xprintf("%s:%d: invalid node number %d\n", dsa->fname,
|
alpar@1
|
423 |
dsa->seqn, node);
|
alpar@1
|
424 |
goto fail;
|
alpar@1
|
425 |
}
|
alpar@1
|
426 |
if (tsp->dply_x_coord[node] != DBL_MAX)
|
alpar@1
|
427 |
{ xprintf("%s:%d: node number %d multiply specified\n",
|
alpar@1
|
428 |
dsa->fname, dsa->seqn, node);
|
alpar@1
|
429 |
goto fail;
|
alpar@1
|
430 |
}
|
alpar@1
|
431 |
if (scan_number(dsa, 0, &tsp->dply_x_coord[node]))
|
alpar@1
|
432 |
goto fail;
|
alpar@1
|
433 |
if (scan_number(dsa, 0, &tsp->dply_y_coord[node]))
|
alpar@1
|
434 |
goto fail;
|
alpar@1
|
435 |
if (check_newline(dsa)) goto fail;
|
alpar@1
|
436 |
}
|
alpar@1
|
437 |
}
|
alpar@1
|
438 |
else if (strcmp(dsa->token, "TOUR_SECTION") == 0)
|
alpar@1
|
439 |
{ int n = tsp->dimension, k, node;
|
alpar@1
|
440 |
if (n == 0)
|
alpar@1
|
441 |
{ xprintf("%s:%d: DIMENSION entry not specified\n",
|
alpar@1
|
442 |
dsa->fname, dsa->seqn);
|
alpar@1
|
443 |
goto fail;
|
alpar@1
|
444 |
}
|
alpar@1
|
445 |
if (tsp->tour != NULL)
|
alpar@1
|
446 |
{ xprintf("%s:%d: TOUR_SECTION multiply specified\n",
|
alpar@1
|
447 |
dsa->fname, dsa->seqn);
|
alpar@1
|
448 |
goto fail;
|
alpar@1
|
449 |
}
|
alpar@1
|
450 |
if (check_newline(dsa)) goto fail;
|
alpar@1
|
451 |
tsp->tour = xcalloc(1+n, sizeof(int));
|
alpar@1
|
452 |
for (k = 1; k <= n; k++)
|
alpar@1
|
453 |
{ if (scan_integer(dsa, 1, &node)) goto fail;
|
alpar@1
|
454 |
if (!(1 <= node && node <= n))
|
alpar@1
|
455 |
{ xprintf("%s:%d: invalid node number %d\n", dsa->fname,
|
alpar@1
|
456 |
dsa->seqn, node);
|
alpar@1
|
457 |
goto fail;
|
alpar@1
|
458 |
}
|
alpar@1
|
459 |
tsp->tour[k] = node;
|
alpar@1
|
460 |
}
|
alpar@1
|
461 |
if (scan_integer(dsa, 1, &node)) goto fail;
|
alpar@1
|
462 |
if (node != -1)
|
alpar@1
|
463 |
{ xprintf("%s:%d: extra node(s) detected\n", dsa->fname,
|
alpar@1
|
464 |
dsa->seqn);
|
alpar@1
|
465 |
goto fail;
|
alpar@1
|
466 |
}
|
alpar@1
|
467 |
if (check_newline(dsa)) goto fail;
|
alpar@1
|
468 |
}
|
alpar@1
|
469 |
else if (strcmp(dsa->token, "EDGE_WEIGHT_SECTION") == 0)
|
alpar@1
|
470 |
{ int n = tsp->dimension, i, j, temp;
|
alpar@1
|
471 |
if (n == 0)
|
alpar@1
|
472 |
{ xprintf("%s:%d: DIMENSION entry not specified\n",
|
alpar@1
|
473 |
dsa->fname, dsa->seqn);
|
alpar@1
|
474 |
goto fail;
|
alpar@1
|
475 |
}
|
alpar@1
|
476 |
if (tsp->edge_weight_format == TSP_UNDEF)
|
alpar@1
|
477 |
{ xprintf("%s:%d: EDGE_WEIGHT_FORMAT entry not specified\n",
|
alpar@1
|
478 |
dsa->fname, dsa->seqn);
|
alpar@1
|
479 |
goto fail;
|
alpar@1
|
480 |
}
|
alpar@1
|
481 |
if (tsp->edge_weight != NULL)
|
alpar@1
|
482 |
{ xprintf("%s:%d: EDGE_WEIGHT_SECTION multiply specified\n",
|
alpar@1
|
483 |
dsa->fname, dsa->seqn);
|
alpar@1
|
484 |
goto fail;
|
alpar@1
|
485 |
}
|
alpar@1
|
486 |
if (check_newline(dsa)) goto fail;
|
alpar@1
|
487 |
tsp->edge_weight = xcalloc(1+n*n, sizeof(int));
|
alpar@1
|
488 |
switch (tsp->edge_weight_format)
|
alpar@1
|
489 |
{ case TSP_FULL_MATRIX:
|
alpar@1
|
490 |
for (i = 1; i <= n; i++)
|
alpar@1
|
491 |
{ for (j = 1; j <= n; j++)
|
alpar@1
|
492 |
{ if (scan_integer(dsa, 1, &temp)) goto fail;
|
alpar@1
|
493 |
tsp->edge_weight[(i - 1) * n + j] = temp;
|
alpar@1
|
494 |
}
|
alpar@1
|
495 |
}
|
alpar@1
|
496 |
break;
|
alpar@1
|
497 |
case TSP_UPPER_ROW:
|
alpar@1
|
498 |
for (i = 1; i <= n; i++)
|
alpar@1
|
499 |
{ tsp->edge_weight[(i - 1) * n + i] = 0;
|
alpar@1
|
500 |
for (j = i + 1; j <= n; j++)
|
alpar@1
|
501 |
{ if (scan_integer(dsa, 1, &temp)) goto fail;
|
alpar@1
|
502 |
tsp->edge_weight[(i - 1) * n + j] = temp;
|
alpar@1
|
503 |
tsp->edge_weight[(j - 1) * n + i] = temp;
|
alpar@1
|
504 |
}
|
alpar@1
|
505 |
}
|
alpar@1
|
506 |
break;
|
alpar@1
|
507 |
case TSP_LOWER_DIAG_ROW:
|
alpar@1
|
508 |
for (i = 1; i <= n; i++)
|
alpar@1
|
509 |
{ for (j = 1; j <= i; j++)
|
alpar@1
|
510 |
{ if (scan_integer(dsa, 1, &temp)) goto fail;
|
alpar@1
|
511 |
tsp->edge_weight[(i - 1) * n + j] = temp;
|
alpar@1
|
512 |
tsp->edge_weight[(j - 1) * n + i] = temp;
|
alpar@1
|
513 |
}
|
alpar@1
|
514 |
}
|
alpar@1
|
515 |
break;
|
alpar@1
|
516 |
default:
|
alpar@1
|
517 |
goto fail;
|
alpar@1
|
518 |
}
|
alpar@1
|
519 |
if (check_newline(dsa)) goto fail;
|
alpar@1
|
520 |
}
|
alpar@1
|
521 |
else if (strcmp(dsa->token, "EOF") == 0)
|
alpar@1
|
522 |
{ if (check_newline(dsa)) goto fail;
|
alpar@1
|
523 |
goto done;
|
alpar@1
|
524 |
}
|
alpar@1
|
525 |
else
|
alpar@1
|
526 |
{ xprintf("%s:%d: keyword `%s' not recognized\n", dsa->fname,
|
alpar@1
|
527 |
dsa->seqn, dsa->token);
|
alpar@1
|
528 |
goto fail;
|
alpar@1
|
529 |
}
|
alpar@1
|
530 |
goto loop;
|
alpar@1
|
531 |
done: xprintf("tsp_read_data: %d lines were read\n", dsa->seqn-1);
|
alpar@1
|
532 |
fclose(dsa->fp);
|
alpar@1
|
533 |
return tsp;
|
alpar@1
|
534 |
fail: if (tsp != NULL)
|
alpar@1
|
535 |
{ if (tsp->name != NULL) xfree(tsp->name);
|
alpar@1
|
536 |
if (tsp->comment != NULL) xfree(tsp->comment);
|
alpar@1
|
537 |
if (tsp->node_x_coord != NULL) xfree(tsp->node_x_coord);
|
alpar@1
|
538 |
if (tsp->node_y_coord != NULL) xfree(tsp->node_y_coord);
|
alpar@1
|
539 |
if (tsp->dply_x_coord != NULL) xfree(tsp->dply_x_coord);
|
alpar@1
|
540 |
if (tsp->dply_y_coord != NULL) xfree(tsp->dply_y_coord);
|
alpar@1
|
541 |
if (tsp->tour != NULL) xfree(tsp->tour);
|
alpar@1
|
542 |
if (tsp->edge_weight != NULL) xfree(tsp->edge_weight);
|
alpar@1
|
543 |
xfree(tsp);
|
alpar@1
|
544 |
}
|
alpar@1
|
545 |
if (dsa->fp != NULL) fclose(dsa->fp);
|
alpar@1
|
546 |
return NULL;
|
alpar@1
|
547 |
}
|
alpar@1
|
548 |
|
alpar@1
|
549 |
/*----------------------------------------------------------------------
|
alpar@1
|
550 |
-- tsp_free_data - free TSP instance data.
|
alpar@1
|
551 |
--
|
alpar@1
|
552 |
-- *Synopsis*
|
alpar@1
|
553 |
--
|
alpar@1
|
554 |
-- #include "glptsp.h"
|
alpar@1
|
555 |
-- void tsp_free_data(TSP *tsp);
|
alpar@1
|
556 |
--
|
alpar@1
|
557 |
-- *Description*
|
alpar@1
|
558 |
--
|
alpar@1
|
559 |
-- The routine tsp_free_data frees all the memory allocated to the TSP
|
alpar@1
|
560 |
-- instance data block, which the parameter tsp points to. */
|
alpar@1
|
561 |
|
alpar@1
|
562 |
void tsp_free_data(TSP *tsp)
|
alpar@1
|
563 |
{ if (tsp->name != NULL) xfree(tsp->name);
|
alpar@1
|
564 |
if (tsp->comment != NULL) xfree(tsp->comment);
|
alpar@1
|
565 |
if (tsp->node_x_coord != NULL) xfree(tsp->node_x_coord);
|
alpar@1
|
566 |
if (tsp->node_y_coord != NULL) xfree(tsp->node_y_coord);
|
alpar@1
|
567 |
if (tsp->dply_x_coord != NULL) xfree(tsp->dply_x_coord);
|
alpar@1
|
568 |
if (tsp->dply_y_coord != NULL) xfree(tsp->dply_y_coord);
|
alpar@1
|
569 |
if (tsp->tour != NULL) xfree(tsp->tour);
|
alpar@1
|
570 |
if (tsp->edge_weight != NULL) xfree(tsp->edge_weight);
|
alpar@1
|
571 |
xfree(tsp);
|
alpar@1
|
572 |
return;
|
alpar@1
|
573 |
}
|
alpar@1
|
574 |
|
alpar@1
|
575 |
/*----------------------------------------------------------------------
|
alpar@1
|
576 |
-- tsp_distance - compute distance between two nodes.
|
alpar@1
|
577 |
--
|
alpar@1
|
578 |
-- *Synopsis*
|
alpar@1
|
579 |
--
|
alpar@1
|
580 |
-- #include "glptsp.h"
|
alpar@1
|
581 |
-- int tsp_distance(TSP *tsp, int i, int j);
|
alpar@1
|
582 |
--
|
alpar@1
|
583 |
-- *Description*
|
alpar@1
|
584 |
--
|
alpar@1
|
585 |
-- The routine tsp_distance computes the distance between i-th and j-th
|
alpar@1
|
586 |
-- nodes for the TSP instance, which tsp points to.
|
alpar@1
|
587 |
--
|
alpar@1
|
588 |
-- *Returns*
|
alpar@1
|
589 |
--
|
alpar@1
|
590 |
-- The routine tsp_distance returns the computed distance. */
|
alpar@1
|
591 |
|
alpar@1
|
592 |
#define nint(x) ((int)((x) + 0.5))
|
alpar@1
|
593 |
|
alpar@1
|
594 |
static double rad(double x)
|
alpar@1
|
595 |
{ /* convert input coordinate to longitude/latitude, in radians */
|
alpar@1
|
596 |
double pi = 3.141592, deg, min;
|
alpar@1
|
597 |
deg = (int)x;
|
alpar@1
|
598 |
min = x - deg;
|
alpar@1
|
599 |
return pi * (deg + 5.0 * min / 3.0) / 180.0;
|
alpar@1
|
600 |
}
|
alpar@1
|
601 |
|
alpar@1
|
602 |
int tsp_distance(TSP *tsp, int i, int j)
|
alpar@1
|
603 |
{ int n = tsp->dimension, dij;
|
alpar@1
|
604 |
if (!(tsp->type == TSP_TSP || tsp->type == TSP_ATSP))
|
alpar@1
|
605 |
xfault("tsp_distance: invalid TSP instance\n");
|
alpar@1
|
606 |
if (!(1 <= i && i <= n && 1 <= j && j <= n))
|
alpar@1
|
607 |
xfault("tsp_distance: node number out of range\n");
|
alpar@1
|
608 |
switch (tsp->edge_weight_type)
|
alpar@1
|
609 |
{ case TSP_UNDEF:
|
alpar@1
|
610 |
xfault("tsp_distance: edge weight type not specified\n");
|
alpar@1
|
611 |
case TSP_EXPLICIT:
|
alpar@1
|
612 |
if (tsp->edge_weight == NULL)
|
alpar@1
|
613 |
xfault("tsp_distance: edge weights not specified\n");
|
alpar@1
|
614 |
dij = tsp->edge_weight[(i - 1) * n + j];
|
alpar@1
|
615 |
break;
|
alpar@1
|
616 |
case TSP_EUC_2D:
|
alpar@1
|
617 |
if (tsp->node_x_coord == NULL || tsp->node_y_coord == NULL)
|
alpar@1
|
618 |
xfault("tsp_distance: node coordinates not specified\n");
|
alpar@1
|
619 |
{ double xd, yd;
|
alpar@1
|
620 |
xd = tsp->node_x_coord[i] - tsp->node_x_coord[j];
|
alpar@1
|
621 |
yd = tsp->node_y_coord[i] - tsp->node_y_coord[j];
|
alpar@1
|
622 |
dij = nint(sqrt(xd * xd + yd * yd));
|
alpar@1
|
623 |
}
|
alpar@1
|
624 |
break;
|
alpar@1
|
625 |
case TSP_CEIL_2D:
|
alpar@1
|
626 |
if (tsp->node_x_coord == NULL || tsp->node_y_coord == NULL)
|
alpar@1
|
627 |
xfault("tsp_distance: node coordinates not specified\n");
|
alpar@1
|
628 |
{ double xd, yd;
|
alpar@1
|
629 |
xd = tsp->node_x_coord[i] - tsp->node_x_coord[j];
|
alpar@1
|
630 |
yd = tsp->node_y_coord[i] - tsp->node_y_coord[j];
|
alpar@1
|
631 |
dij = (int)ceil(sqrt(xd * xd + yd * yd));
|
alpar@1
|
632 |
}
|
alpar@1
|
633 |
break;
|
alpar@1
|
634 |
case TSP_GEO:
|
alpar@1
|
635 |
if (tsp->node_x_coord == NULL || tsp->node_y_coord == NULL)
|
alpar@1
|
636 |
xfault("tsp_distance: node coordinates not specified\n");
|
alpar@1
|
637 |
{ double rrr = 6378.388;
|
alpar@1
|
638 |
double latitude_i = rad(tsp->node_x_coord[i]);
|
alpar@1
|
639 |
double latitude_j = rad(tsp->node_x_coord[j]);
|
alpar@1
|
640 |
double longitude_i = rad(tsp->node_y_coord[i]);
|
alpar@1
|
641 |
double longitude_j = rad(tsp->node_y_coord[j]);
|
alpar@1
|
642 |
double q1 = cos(longitude_i - longitude_j);
|
alpar@1
|
643 |
double q2 = cos(latitude_i - latitude_j);
|
alpar@1
|
644 |
double q3 = cos(latitude_i + latitude_j);
|
alpar@1
|
645 |
dij = (int)(rrr * acos(0.5 * ((1.0 + q1) * q2 -
|
alpar@1
|
646 |
(1.0 - q1) *q3)) + 1.0);
|
alpar@1
|
647 |
}
|
alpar@1
|
648 |
break;
|
alpar@1
|
649 |
case TSP_ATT:
|
alpar@1
|
650 |
if (tsp->node_x_coord == NULL || tsp->node_y_coord == NULL)
|
alpar@1
|
651 |
xfault("tsp_distance: node coordinates not specified\n");
|
alpar@1
|
652 |
{ int tij;
|
alpar@1
|
653 |
double xd, yd, rij;
|
alpar@1
|
654 |
xd = tsp->node_x_coord[i] - tsp->node_x_coord[j];
|
alpar@1
|
655 |
yd = tsp->node_y_coord[i] - tsp->node_y_coord[j];
|
alpar@1
|
656 |
rij = sqrt((xd * xd + yd * yd) / 10.0);
|
alpar@1
|
657 |
tij = nint(rij);
|
alpar@1
|
658 |
if (tij < rij) dij = tij + 1; else dij = tij;
|
alpar@1
|
659 |
}
|
alpar@1
|
660 |
break;
|
alpar@1
|
661 |
default:
|
alpar@1
|
662 |
xassert(tsp->edge_weight_type != tsp->edge_weight_type);
|
alpar@1
|
663 |
}
|
alpar@1
|
664 |
return dij;
|
alpar@1
|
665 |
}
|
alpar@1
|
666 |
|
alpar@1
|
667 |
/* eof */
|