-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_instance.h
246 lines (230 loc) · 8.64 KB
/
read_instance.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#pragma once
#include <iostream>
#include <sstream>
#include <type_traits>
#include <cstdlib>
#include <vector>
#include <string>
#include <fstream>
using namespace std;
int** distMatrix;
int ins_size;
int euc_2d(double x_i, double x_j, double y_i, double y_j) {
double x_d = x_i - x_j;
double y_d = y_i - y_j;
double pre_dist = sqrt(x_d * x_d + y_d * y_d);
int dist = (int)(pre_dist + 0.5);
return dist;
}
int ceil_2d(double x_i, double x_j, double y_i, double y_j) {
double x_d = x_i - x_j;
double y_d = y_i - y_j;
double pre_dist = sqrt(x_d * x_d + y_d * y_d);
int dist = (int)ceil(pre_dist);
return dist;
}
int euc_3d(double x_i, double x_j, double y_i, double y_j, double z_i, double z_j) {
double x_d = x_i - x_j;
double y_d = y_i - y_j;
double z_d = z_i - z_j;
double pre_dist = sqrt(x_d * x_d + y_d * y_d + z_d * z_d);
int dist = (int)(pre_dist + 0.5);
return dist;
}
int man_2d(double x_i, double x_j, double y_i, double y_j) {
double x_d = abs(x_i - x_j);
double y_d = abs(y_i - y_j);
double pre_dist = x_d + y_d;
int dist = (int)(pre_dist + 0.5);
return dist;
}
int man_3d(double x_i, double x_j, double y_i, double y_j, double z_i, double z_j) {
double x_d = abs(x_i - x_j);
double y_d = abs(y_i - y_j);
double z_d = abs(z_i - z_j);
double pre_dist = x_d + y_d + z_d;
int dist = (int)(pre_dist + 0.5);
return dist;
}
int max_2d(double x_i, double x_j, double y_i, double y_j) {
double x_d = abs(x_i - x_j);
double y_d = abs(y_i - y_j);
int dist = max((int)(x_d + 0.5), (int)(y_d + 0.5));
return dist;
}
int max_3d(double x_i, double x_j, double y_i, double y_j, double z_i, double z_j) {
double x_d = abs(x_i - x_j);
double y_d = abs(y_i - y_j);
double z_d = abs(z_i - z_j);
int pre_dist = max((int)(x_d + 0.5), (int)(y_d + 0.5));
int dist = max(pre_dist, (int)(z_d + 0.5));
return dist;
}
int att(double x_i, double x_j, double y_i, double y_j) { // psuedo-euclidean
double x_d = x_i - x_j;
double y_d = y_i - y_j;
double r = sqrt((x_d * x_d + y_d * y_d) / 10.0);
int t = (int)(r + 0.5);
int dist = (t < r) ? t + 1 : t;
return dist;
}
void process_explicit(vector<string> v_data, string edge_weight_type, string edge_weight_format, string dim) {
bool has_diag = edge_weight_format.find("DIAG") != string::npos ? true : false;
int n = stoi(dim);
ins_size = n;
int k = 0;
distMatrix = new int* [n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
distMatrix[i] = new int[n];
if (edge_weight_format.find("UPPER") != string::npos) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
distMatrix[i][j] = has_diag ? stoi(v_data[k++]) : 0;
if (distMatrix[i][j] > 0) {
cout << "Diagonal cell has an entry other than 0, that's an error...";
exit(-1);
}
}
else if (i > j)
distMatrix[i][j] = 0;
else
distMatrix[i][j] = stoi(v_data[k++]);
}
}
}
else if (edge_weight_format.find("LOWER") != string::npos) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
distMatrix[i][j] = has_diag ? stoi(v_data[k++]) : 0;
if (distMatrix[i][j] > 0) {
cout << "Diagonal cell has an entry other than 0, that's an error...";
exit(-1);
}
}
else if (i < j)
distMatrix[i][j] = 0;
else
distMatrix[i][j] = stoi(v_data[k++]);
}
}
}
else if (edge_weight_format.find("FULL") != string::npos) { // full matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
distMatrix[i][j] = stoi(v_data[k++]);
}
}
}
else {
cout << "No support for the edge weight format yet";
exit(-1);
}
}
/*
v_data := coordinates of the n cities of the instance
dim := number of cities
code := 0 (for euc_2d), 1 (for ceil_2d), 2 (for att)
*/
void process_euc_2d(vector<string> v_data, string dim, int code) {
int n = stoi(dim);
ins_size = n;
int k = 0;
distMatrix = new int* [n];
double** coord = new double* [n];
for (int i = 0; i < n; i++) {
coord[i] = new double[2];
for (int j = 0; j < 2; j++) {
int l = j == 0 ? k + 1 : k + 2;
coord[i][j] = stod(v_data[l]);
}
k += 3;
}
for (int i = 0; i < n; i++)
distMatrix[i] = new int[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j)
distMatrix[i][j] = 0;
else {
if (code == 0)
distMatrix[i][j] = euc_2d(coord[i][0], coord[j][0], coord[i][1], coord[j][1]);
else if (code == 1)
distMatrix[i][j] = ceil_2d(coord[i][0], coord[j][0], coord[i][1], coord[j][1]);
else
distMatrix[i][j] = att(coord[i][0], coord[j][0], coord[i][1], coord[j][1]);
distMatrix[j][i] = distMatrix[i][j];
}
}
}
}
void loadInstance() {
ifstream fin;
string instances[] = {"berlin52", "kroA100", "att48", "rat575", "a280", "eil101", "bayg29", "eil51", "eil76"};
string ins_name = instances[0]; // 6
string ins_path = ins_name + ".tsp";
cout << "TSP data: " << ins_name << endl;
//cin >> ins_name;
fin.open(ins_path);
if (!fin.fail()) {
string line = "";
int line_number = 0;
vector<string> raw_matrix_data;
bool data_section = false;
string edge_weight_type;
string dimension;
string edge_weight_format;
while (!fin.eof()) {
getline(fin, line);
line_number++;
if (line_number < 4)
continue;
if (data_section) {
stringstream stream(line);
//stream << line;
string next_token;
while (stream >> next_token) {
raw_matrix_data.push_back(next_token);
}
}
else if (dimension == "") {
int _start = line.find(": ") + strlen(": ");
string mysubtr = line.substr(_start, line.size() - _start); // get the type of edge weight.
dimension = mysubtr; // substring after removing the trailing spaces;
}
else if (line_number == 6 && edge_weight_type == "EXPLICIT") {
int _start = line.find(": ") + strlen(": ");
string mysubtr = line.substr(_start, line.size() - _start); // get the type of edge weight.
edge_weight_format = mysubtr; // substring after removing the trailing spaces;
}
else if (edge_weight_type == "") {
size_t found = line.find("EXPLICIT") || line.find("EUC_2D") || line.find("ATT") || line.find("EUC_3D") ||
line.find("MAX_2D") || line.find("MAX_3D") || line.find("MAN_2D") || line.find("MAN_3D") ||
line.find("CEIL_2D") || line.find("GEO");
if (found != string::npos) {
int _start = line.find(": ") + strlen(": ");
string mysubtr = line.substr(_start, line.size() - _start); // get the type of edge weight.
edge_weight_type = mysubtr; // substring after removing the trailing spaces;
}
}
else if (line.find("SECTION") != string::npos)
data_section = true;
}
if (edge_weight_type == "EXPLICIT")
process_explicit(raw_matrix_data, edge_weight_type, edge_weight_format, dimension);
else if (edge_weight_type == "EUC_2D")
process_euc_2d(raw_matrix_data, dimension, 0);
else if (edge_weight_type == "ATT")
process_euc_2d(raw_matrix_data, dimension, 2);
else {
cout << "No support for the data format of this instance yet..";
exit(-1);
}
}
else {
cout << "Issues with opening instance data file, confirm the name and location of the file and try again";
exit(-1);
}
}