You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

667 lines
24 KiB

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