SolveTSP.c
Go to the documentation of this file.
1 #include "LKH.h"
2 #include <unistd.h>
3 
4 /*
5  * The SolveTSP function solves a TSP instancei using LKH.
6  *
7  * Parameters
8  * Dimension: The number of nodes in the instance.
9  * ParFileName: Name of the paramter file.
10  * TourFileName: Name of a temporary tour file.
11  * Tour: The solution tour.
12  * Optimum: A known optimum.
13  * Deduction: Value to be subtracted from the tour cost found by LKH.
14  *
15  * The return value is the cost of the solution tour.
16  */
17 
18 GainType SolveTSP(int Dimension, char *ParFileName,
19  char *TourFileName, int *Tour, GainType Optimum,
20  GainType Deduction)
21 {
22  GainType Cost;
23  FILE *p, *TourFile;
24  int i;
25  char Command[256], Key[256], Buffer[256], *Line, *Keyword;
26  char Delimiters[] = " :=\n\t\r\f\v\xef\xbb\xbf";
27 
28  sprintf(Command, "rosrun lkh_solver lkh_solver %s", ParFileName);
29  assert(p = popen(Command, "r"));
30  Cost = PLUS_INFINITY;
31  while (fgets(Buffer, sizeof(Buffer), p)) {
32  if (TraceLevel > 1)
33  printff("%s", Buffer);
34  if (sscanf(Buffer, "%s", Key) > 0) {
35  GainType LocalCost;
36  int LocalTrial;
37  double LocalTime;
38  if (!strcmp(Key, "Cost.min")) {
39  char *cp = strchr(Buffer + strlen(Key), '=');
40  sscanf(cp + 1, GainInputFormat, &Cost);
41  Cost -= Deduction;
42  } else if (TraceLevel > 0 && Dimension != GTSPSets) {
43  if (!strcmp(Key, "Run")) {
44  char *cp = Buffer + strlen(Key);
45  sscanf(cp + 1, "%d", &Run);
46  sscanf(cp + 1, "%d", &LocalTrial);
47  cp = strchr(cp + 1, '=');
48  sscanf(cp + 1, GainInputFormat, &LocalCost);
49  LocalCost -= Deduction;
50  cp = strchr(cp + 1, 'T');
51  cp = strchr(cp + 1, '=');
52  sscanf(cp + 1, "%lf", &LocalTime);
53  printff("Run %d: Cost = " GainFormat ", ",
54  Run, LocalCost);
55  if (Optimum != MINUS_INFINITY && Optimum != 0)
56  printf("Gap = %0.4f%%, ",
57  100.0 * (LocalCost - Optimum) / Optimum);
58  printff("Time = %0.2f sec.\n\n", LocalTime);
59  } else if (!strcmp(Key, "*")) {
60  char *cp = Buffer + strlen(Key);
61  if (sscanf(cp + 1, "%d", &LocalTrial) > 0) {
62  cp = strchr(cp + 1, '=');
63  sscanf(cp + 1, GainInputFormat, &LocalCost);
64  LocalCost -= Deduction;
65  cp = strchr(cp + 1, 'T');
66  cp = strchr(cp + 1, '=');
67  sscanf(cp + 1, "%lf", &LocalTime);
68  printff("# %d: Cost = " GainFormat ", ",
69  LocalTrial, LocalCost);
70  if (Optimum != MINUS_INFINITY && Optimum != 0)
71  printf("Gap = %0.4f%%, ",
72  100.0 * (LocalCost - Optimum) / Optimum);
73  printff("Time = %0.2f sec.\n", LocalTime);
74  }
75  }
76  } else if (!strcmp(Key, "Run")) {
77  char *cp = Buffer + strlen(Key);
78  sscanf(cp + 1, "%d", &Run);
79  }
80  }
81  }
82  pclose(p);
83  TourFile = fopen(TourFileName, "r");
84  if (!TourFile)
85  return PLUS_INFINITY;
86  while ((Line = ReadLine(TourFile))) {
87  if (!(Keyword = strtok(Line, Delimiters)))
88  continue;
89  for (i = 0; i < strlen(Keyword); i++)
90  Keyword[i] = (char) toupper(Keyword[i]);
91  if (!strcmp(Keyword, "TOUR_SECTION"))
92  break;
93  }
94  for (i = 1; i <= Dimension; i++)
95  fscanf(TourFile, "%d", &Tour[i]);
96  Tour[0] = Tour[Dimension];
97  fclose(TourFile);
98  unlink(TourFileName);
99  return Cost;
100 }
int Dimension
Definition: LKH.h:190
static char * Buffer
Definition: ReadLine.c:9
int GTSPSets
Definition: LKH.h:209
#define GainInputFormat
Definition: GainType.h:23
void printff(char *fmt,...)
Definition: printff.c:10
#define MINUS_INFINITY
Definition: GainType.h:21
GainType Optimum
Definition: LKH.h:241
static char Delimiters[]
FILE * TourFile
Definition: LKH.h:302
int TraceLevel
Definition: LKH.h:274
char * TourFileName
Definition: LKH.h:284
int Run
Definition: LKH.h:257
char * ReadLine(FILE *InputFile)
Definition: ReadLine.c:23
GainType SolveTSP(int Dimension, char *ParFileName, char *TourFileName, int *Tour, GainType Optimum, GainType Deduction)
Definition: SolveTSP.c:18
#define PLUS_INFINITY
Definition: GainType.h:20
#define GainFormat
Definition: GainType.h:22
long long GainType
Definition: GainType.h:13


glkh_solver
Author(s): Francisco Suarez-Ruiz
autogenerated on Mon Jun 10 2019 13:50:27