Distance.c
Go to the documentation of this file.
1 #include "LKH.h"
2 
3 /*
4  * Functions for computing distances (see TSPLIB).
5  *
6  * The appropriate function is referenced by the function pointer Distance.
7  */
8 
9 int Distance_1(Node * Na, Node * Nb)
10 {
11  return 1;
12 }
13 
14 int Distance_ATSP(Node * Na, Node * Nb)
15 {
16  int n = DimensionSaved;
17  if ((Na->Id <= n) == (Nb->Id <= n))
18  return M;
19  if (abs(Na->Id - Nb->Id) == n)
20  return 0;
21  return Na->Id < Nb->Id ? Na->C[Nb->Id - n] : Nb->C[Na->Id - n];
22 }
23 
24 int Distance_ATT(Node * Na, Node * Nb)
25 {
26  double xd = Na->X - Nb->X, yd = Na->Y - Nb->Y;
27  return (int) ceil(sqrt((xd * xd + yd * yd) / 10.0));
28 }
29 
30 int Distance_CEIL_2D(Node * Na, Node * Nb)
31 {
32  double xd = Na->X - Nb->X, yd = Na->Y - Nb->Y;
33  return (int) ceil(sqrt(xd * xd + yd * yd));
34 }
35 
36 int Distance_CEIL_3D(Node * Na, Node * Nb)
37 {
38  double xd = Na->X - Nb->X, yd = Na->Y - Nb->Y, zd = Na->Z - Nb->Z;
39  return (int) ceil(sqrt(xd * xd + yd * yd + zd * zd));
40 }
41 
42 int Distance_EUC_2D(Node * Na, Node * Nb)
43 {
44  double xd = Na->X - Nb->X, yd = Na->Y - Nb->Y;
45  return (int) (sqrt(xd * xd + yd * yd) + 0.5);
46 }
47 
48 int Distance_EUC_3D(Node * Na, Node * Nb)
49 {
50  double xd = Na->X - Nb->X, yd = Na->Y - Nb->Y, zd = Na->Z - Nb->Z;
51  return (int) (sqrt(xd * xd + yd * yd + zd * zd) + 0.5);
52 }
53 
54 int Distance_EXPLICIT(Node * Na, Node * Nb)
55 {
56  return Na->Id < Nb->Id ? Nb->C[Na->Id] : Na->C[Nb->Id];
57 }
58 
59 #define PI 3.141592
60 #define RRR 6378.388
61 
62 int Distance_GEO(Node * Na, Node * Nb)
63 {
64  int deg;
65  double NaLatitude, NaLongitude, NbLatitude, NbLongitude, min, q1, q2,
66  q3;
67  deg = (int) Na->X;
68  min = Na->X - deg;
69  NaLatitude = PI * (deg + 5.0 * min / 3.0) / 180.0;
70  deg = (int) Na->Y;
71  min = Na->Y - deg;
72  NaLongitude = PI * (deg + 5.0 * min / 3.0) / 180.0;
73  deg = (int) Nb->X;
74  min = Nb->X - deg;
75  NbLatitude = PI * (deg + 5.0 * min / 3.0) / 180.0;
76  deg = (int) Nb->Y;
77  min = Nb->Y - deg;
78  NbLongitude = PI * (deg + 5.0 * min / 3.0) / 180.0;
79  q1 = cos(NaLongitude - NbLongitude);
80  q2 = cos(NaLatitude - NbLatitude);
81  q3 = cos(NaLatitude + NbLatitude);
82  return (int) (RRR * acos(0.5 * ((1.0 + q1) * q2 - (1.0 - q1) * q3)) +
83  1.0);
84 }
85 
86 #undef M_PI
87 #define M_PI 3.14159265358979323846264
88 #define M_RRR 6378388.0
89 
90 int Distance_GEOM(Node * Na, Node * Nb)
91 {
92  double lati = M_PI * (Na->X / 180.0);
93  double latj = M_PI * (Nb->X / 180.0);
94  double longi = M_PI * (Na->Y / 180.0);
95  double longj = M_PI * (Nb->Y / 180.0);
96  double q1 = cos(latj) * sin(longi - longj);
97  double q3 = sin((longi - longj) / 2.0);
98  double q4 = cos((longi - longj) / 2.0);
99  double q2 = sin(lati + latj) * q3 * q3 - sin(lati - latj) * q4 * q4;
100  double q5 = cos(lati - latj) * q4 * q4 - cos(lati + latj) * q3 * q3;
101  return (int) (M_RRR * atan2(sqrt(q1 * q1 + q2 * q2), q5) + 1.0);
102 }
103 
104 int Distance_MAN_2D(Node * Na, Node * Nb)
105 {
106  return (int) (fabs(Na->X - Nb->X) + fabs(Na->Y - Nb->Y) + 0.5);
107 }
108 
109 int Distance_MAN_3D(Node * Na, Node * Nb)
110 {
111  return (int) (fabs(Na->X - Nb->X) +
112  fabs(Na->Y - Nb->Y) + fabs(Na->Z - Nb->Z) + 0.5);
113 }
114 
115 int Distance_MAX_2D(Node * Na, Node * Nb)
116 {
117  int dx = (int) (fabs(Na->X - Nb->X) + 0.5),
118  dy = (int) (fabs(Na->Y - Nb->Y) + 0.5);
119  return dx > dy ? dx : dy;
120 }
121 
122 int Distance_MAX_3D(Node * Na, Node * Nb)
123 {
124  int dx = (int) (fabs(Na->X - Nb->X) + 0.5),
125  dy = (int) (fabs(Na->Y - Nb->Y) + 0.5),
126  dz = (int) (fabs(Na->Z - Nb->Z) + 0.5);
127  if (dy > dx)
128  dx = dy;
129  return dx > dz ? dx : dz;
130 }
131 
132 /* Function for computing the distance in kilometers between two points on
133  the Earth's surface, based on the high accuracy method by H. Andoyer,
134  as described in
135 
136  "Astronomical Algorithms (2nd Ed.)", pg. 85, Jean Meeus (2000).
137 */
138 
139 static double Meeus(double lat1, double lon1, double lat2, double lon2)
140 {
141  const double a = 6378.137; /* equator earth radius */
142  const double fl = 1 / 298.257; /* earth flattening */
143  double f, g, l, sg, sl, sf, s, c, w, r, d, h1, h2;
144 
145  if (lat1 == lat2 && lon1 == lon2)
146  return 0;
147  f = (lat1 + lat2) / 2;
148  g = (lat1 - lat2) / 2;
149  l = (lon1 - lon2) / 2;
150  sg = sin(g);
151  sl = sin(l);
152  sf = sin(f);
153  sg = sg * sg;
154  sl = sl * sl;
155  sf = sf * sf;
156  s = sg * (1 - sl) + (1 - sf) * sl;
157  c = (1 - sg) * (1 - sl) + sf * sl;
158  w = atan(sqrt(s / c));
159  r = sqrt(s * c) / w;
160  d = 2 * w * a;
161  h1 = (3 * r - 1) / 2 / c;
162  h2 = (3 * r + 1) / 2 / s;
163  return (int) (d *
164  (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg)));
165 }
166 
167 int Distance_GEO_MEEUS(Node * Na, Node * Nb)
168 {
169  double lat1 =
170  M_PI * ((int) Na->X + 5 * (Na->X - (int) Na->X) / 3) / 180;
171  double lon1 =
172  M_PI * ((int) Na->Y + 5 * (Na->Y - (int) Na->Y) / 3) / 180;
173  double lat2 =
174  M_PI * ((int) Nb->X + 5 * (Nb->X - (int) Nb->X) / 3) / 180;
175  double lon2 =
176  M_PI * ((int) Nb->Y + 5 * (Nb->Y - (int) Nb->Y) / 3) / 180;
177  return (int) (Meeus(lat1, lon1, lat2, lon2) + 0.5);
178 }
179 
181 {
182  double lat1 = M_PI * (Na->X / 180);
183  double lon1 = M_PI * (Na->Y / 180);
184  double lat2 = M_PI * (Nb->X / 180);
185  double lon2 = M_PI * (Nb->Y / 180);
186  return (int) (1000 * Meeus(lat1, lon1, lat2, lon2) + 0.5);
187 }
d
int Distance_EXPLICIT(Node *Na, Node *Nb)
Definition: Distance.c:54
static double Meeus(double lat1, double lon1, double lat2, double lon2)
Definition: Distance.c:139
int Distance_MAN_2D(Node *Na, Node *Nb)
Definition: Distance.c:104
int Distance_GEOM(Node *Na, Node *Nb)
Definition: Distance.c:90
int Distance_MAX_2D(Node *Na, Node *Nb)
Definition: Distance.c:115
double Y
Definition: LKH.h:123
int Distance_1(Node *Na, Node *Nb)
Definition: Distance.c:9
int Distance_GEO_MEEUS(Node *Na, Node *Nb)
Definition: Distance.c:167
f
int Distance_MAN_3D(Node *Na, Node *Nb)
Definition: Distance.c:109
int Distance_ATSP(Node *Na, Node *Nb)
Definition: Distance.c:14
XmlRpcServer s
Definition: LKH.h:68
int Id
Definition: LKH.h:69
double X
Definition: LKH.h:123
int Distance_MAX_3D(Node *Na, Node *Nb)
Definition: Distance.c:122
int Distance_GEO(Node *Na, Node *Nb)
Definition: Distance.c:62
int Distance_EUC_3D(Node *Na, Node *Nb)
Definition: Distance.c:48
double Z
Definition: LKH.h:123
int Distance_EUC_2D(Node *Na, Node *Nb)
Definition: Distance.c:42
static int a
Definition: Random.c:41
#define PI
Definition: Distance.c:59
int DimensionSaved
Definition: LKH.h:191
int * C
Definition: LKH.h:88
#define M_RRR
Definition: Distance.c:88
int Distance_GEOM_MEEUS(Node *Na, Node *Nb)
Definition: Distance.c:180
int Distance_CEIL_3D(Node *Na, Node *Nb)
Definition: Distance.c:36
int Distance_CEIL_2D(Node *Na, Node *Nb)
Definition: Distance.c:30
int Distance_ATT(Node *Na, Node *Nb)
Definition: Distance.c:24
int M
Definition: LKH.h:218
CostFunction c
Definition: LKH.h:304
r
#define RRR
Definition: Distance.c:60
#define M_PI
Definition: Distance.c:87


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