PageRenderTime 89ms CodeModel.GetById 41ms app.highlight 41ms RepoModel.GetById 1ms app.codeStats 0ms

/farmR/src/glptsp.c

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