glptsp.c 24 KB

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