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.

730 lines
24 KiB

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