WriteTour.c
Go to the documentation of this file.
1 #include "LKH.h"
2 
3 /*
4  * The WriteTour function writes a tour to file. The tour
5  * is written in TSPLIB format to file FileName.
6  *
7  * The tour is written in "normal form": starting at node 1,
8  * and continuing in direction of its lowest numbered
9  * neighbor.
10  *
11  * Nothing happens if FileName is 0.
12  */
13 
14 static char *FullName(char *Name, GainType Cost);
15 
16 void WriteTour(char *FileName, int *Tour, GainType Cost)
17 {
18  FILE *TourFile;
19  int i, j, n, Forwards;
20  char *FullFileName;
21  time_t Now;
22 
23  if (FileName == 0)
24  return;
25  FullFileName = FullName(FileName, Cost);
26  Now = time(&Now);
27  if (TraceLevel >= 1)
28  printff("Writing%s: \"%s\" ... ",
29  FileName == TourFileName ? " TOUR_FILE" :
30  FileName == OutputTourFileName ? " OUTPUT_TOUR_FILE" : "",
31  FullFileName);
32  assert(TourFile = fopen(FullFileName, "w"));
33  fprintf(TourFile, "NAME : %s." GainFormat ".tour\n", Name, Cost);
34  fprintf(TourFile, "COMMENT : Length = " GainFormat "\n", Cost);
35  fprintf(TourFile, "COMMENT : Found by GLKH [Keld Helsgaun] %s",
36  ctime(&Now));
37  fprintf(TourFile, "TYPE : TOUR\n");
38  n = ProblemType != ATSP ? Dimension : Dimension / 2;
39  fprintf(TourFile, "DIMENSION : %d\n", n);
40  fprintf(TourFile, "TOUR_SECTION\n");
41 
42  i = 1;
43  for (j = 1; j <= n; j++)
44  if (Tour[j] < Tour[i])
45  i = j;
46  Forwards = 1;
47  for (j = 1; j <= n; j++) {
48  fprintf(TourFile, "%d\n", Tour[i]);
49  if (Forwards) {
50  if (++i > n)
51  i = 1;
52  } else if (--i < 1)
53  i = n;
54  }
55  fprintf(TourFile, "-1\nEOF\n");
56  fclose(TourFile);
57  if (TraceLevel >= 1)
58  printff("done\n");
59  free(FullFileName);
60 }
61 
62 /*
63  * The FullName function returns a copy of the string Name where all
64  * occurrences of the character '$' have been replaced by Cost.
65  */
66 
67 static char *FullName(char *Name, GainType Cost)
68 {
69  char *NewName = 0, *CostBuffer, *Pos;
70 
71  if (!(Pos = strstr(Name, "$"))) {
72  assert(NewName = (char *) calloc(strlen(Name) + 1, 1));
73  strcpy(NewName, Name);
74  return NewName;
75  }
76  assert(CostBuffer = (char *) malloc(400));
77  sprintf(CostBuffer, GainFormat, Cost);
78  do {
79  free(NewName);
80  assert(NewName =
81  (char *) calloc(strlen(Name) + strlen(CostBuffer) + 1, 1));
82  strncpy(NewName, Name, Pos - Name);
83  strcat(NewName, CostBuffer);
84  strcat(NewName, Pos + 1);
85  Name = NewName;
86  }
87  while ((Pos = strstr(Name, "$")));
88  free(CostBuffer);
89  return NewName;
90 }
int Dimension
Definition: LKH.h:190
void printff(char *fmt,...)
Definition: printff.c:10
char * OutputTourFileName
Definition: LKH.h:284
Definition: LKH.h:41
void WriteTour(char *FileName, int *Tour, GainType Cost)
Definition: WriteTour.c:16
FILE * TourFile
Definition: LKH.h:302
int TraceLevel
Definition: LKH.h:274
static char * FullName(char *Name, GainType Cost)
Definition: WriteTour.c:67
char * TourFileName
Definition: LKH.h:284
char * Name
Definition: LKH.h:288
int ProblemType
Definition: LKH.h:290
#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