Repository navigation
Expand file tree
/
Copy pathProblemLoader.cpp
More file actions
224 lines (198 loc) · 7.85 KB
/
Copy pathProblemLoader.cpp
File metadata and controls
224 lines (198 loc) · 7.85 KB
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
#include "ProblemLoader.hpp"
#include <fstream>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <random>
using namespace LcVRPContest;
const string ProblemLoader::DATA_RELATIVE_PREFIX = "../";
const string ProblemLoader::DATA_DIRECTORY = "data/lcvrp/";
const string ProblemLoader::DATA_FILE_EXTENSION = ".lcvrp";
static bool FileExists(const string &path) {
ifstream file(path);
return file.is_open();
}
static string BuildDataPath(const string &prefix, const string &folder_name, const string &instance_name) {
return prefix + ProblemLoader::DATA_DIRECTORY + folder_name + "/" + instance_name + ProblemLoader::DATA_FILE_EXTENSION;
}
static string FindDataFilePath(const string &folder_name, const string &instance_name) {
string prefix;
for (int depth = 0; depth <= ProblemLoader::MAX_DATA_SEARCH_DEPTH; ++depth) {
string candidate = BuildDataPath(prefix, folder_name, instance_name);
if (FileExists(candidate)) {
return candidate;
}
prefix += ProblemLoader::DATA_RELATIVE_PREFIX;
}
return BuildDataPath("", folder_name, instance_name);
}
ProblemLoader::ProblemLoader(const string& folder_name, const string& instance_name, bool use_random_permutation)
: folder_name_(folder_name),
instance_name_(instance_name),
use_random_permutation_(use_random_permutation)
{
base_path_ = FindDataFilePath(folder_name_, instance_name_);
}
ProblemData ProblemLoader::LoadProblem() {
ProblemData problem_data;
string file_path = base_path_;
ParseLcVrpFile(file_path, problem_data);
// we need to build the edge weight matrix if EUC_2D
if (problem_data.GetEdgeWeightType() == "EUC_2D") {
problem_data.BuildEdgeWeightMatrix();
}
// generate random permutation if needed (from some fixed seed)
if (use_random_permutation_) {
int num_customers = problem_data.GetNumCustomers();
vector<int> permutation;
// min: first customer ID, max: num_customers + depot index offset
for (int i = ProblemLoader::FIRST_CUSTOMER_ID; i <= num_customers + ProblemLoader::DEPOT_INDEX_OFFSET; ++i) {
permutation.push_back(i);
}
mt19937 gen(RANDOM_PERMUTATION_SEED);
shuffle(permutation.begin(), permutation.end(), gen);
problem_data.SetPermutation(permutation);
}
return problem_data;
}
void ProblemLoader::ParseLcVrpFile(const string& file_path, ProblemData& problem_data) {
ifstream file(file_path);
if (!file.is_open()) {
cout << "Error: Cannot open file: " << file_path << endl;
exit(ProblemLoader::FILE_OPEN_ERROR_CODE);
}
string line;
while (getline(file, line)) {
// skip empty lines
if (line.empty()) continue;
// parse header fields
if (line.find("NAME") != string::npos) {
size_t colon_pos = line.find(':');
if (colon_pos != string::npos) {
string name = line.substr(colon_pos + 1);
// remove all kind of whitespaces
size_t start = name.find_first_not_of(" \t\r\n");
if (start != string::npos) {
name = name.substr(start);
size_t end = name.find_last_not_of(" \t\r\n");
name = name.substr(0, end + 1);
}
problem_data.SetName(name);
}
}
else if (line.find("DIMENSION") != string::npos) {
size_t colon_pos = line.find(':');
if (colon_pos != string::npos) {
int dimension = stoi(line.substr(colon_pos + 1));
problem_data.SetDimension(dimension);
}
}
else if (line.find("CAPACITY") != string::npos) {
size_t colon_pos = line.find(':');
if (colon_pos != string::npos) {
int capacity = stoi(line.substr(colon_pos + 1));
problem_data.SetCapacity(capacity);
}
}
else if (line.find("DISTANCE") != string::npos) {
size_t colon_pos = line.find(':');
if (colon_pos != string::npos) {
double distance = stod(line.substr(colon_pos + 1));
problem_data.SetDistance(distance);
}
}
else if (line.find("EDGE_WEIGHT_TYPE") != string::npos) {
size_t colon_pos = line.find(':');
if (colon_pos != string::npos) {
string type = line.substr(colon_pos + 1);
size_t start = type.find_first_not_of(" \t\r\n");
if (start != string::npos) {
type = type.substr(start);
size_t end = type.find_last_not_of(" \t\r\n");
type = type.substr(0, end + 1);
}
problem_data.SetEdgeWeightType(type);
}
}
else if (line.find("EDGE_WEIGHT_SECTION") != string::npos) {
ParseEdgeWeightSection(file, problem_data);
}
else if (line.find("NODE_COORD_SECTION") != string::npos) {
ParseNodeCoordSection(file, problem_data);
}
else if (line.find("DEMAND_SECTION") != string::npos) {
ParseDemandSection(file, problem_data);
}
else if (line.find("DEPOT_SECTION") != string::npos) {
ParseDepotSection(file, problem_data);
}
else if (line.find("NUM_GROUPS") != string::npos) {
size_t colon_pos = line.find(':');
if (colon_pos != string::npos) {
int num_groups = stoi(line.substr(colon_pos + 1));
problem_data.SetNumGroups(num_groups);
}
}
else if (line.find("PERMUTATION") != string::npos) {
size_t colon_pos = line.find(':');
if (colon_pos != string::npos) {
vector<int> permutation;
string perm_data = line.substr(colon_pos + 1);
istringstream iss(perm_data);
int customer_id;
while (iss >> customer_id) {
permutation.push_back(customer_id);
}
problem_data.SetPermutation(permutation);
}
}
else if (line.find("EOF") != string::npos) {
break;
}
}
file.close();
}
void ProblemLoader::ParseEdgeWeightSection(ifstream& file, ProblemData& problem_data) {
int dimension = problem_data.GetDimension();
vector<vector<double>> edge_weights(dimension, vector<double>(dimension, ProblemLoader::DEFAULT_EDGE_WEIGHT));
// for reading triangular matrix
for (int i = 1; i < dimension; ++i) {
for (int j = 0; j < i; ++j) {
double weight;
file >> weight;
edge_weights[i][j] = weight;
edge_weights[j][i] = weight; // we assume its symmetric
}
}
problem_data.SetEdgeWeights(edge_weights);
}
void ProblemLoader::ParseNodeCoordSection(ifstream& file, ProblemData& problem_data) {
int dimension = problem_data.GetDimension();
vector<Coordinate> coordinates(dimension);
for (int i = 0; i < dimension; ++i) {
int node_id;
double x, y;
file >> node_id >> x >> y;
// node ids start from 1, array indices start from 0
coordinates[node_id - ProblemLoader::DEPOT_INDEX_OFFSET] = Coordinate(x, y);
}
problem_data.SetCoordinates(coordinates);
}
void ProblemLoader::ParseDemandSection(ifstream& file, ProblemData& problem_data) {
int dimension = problem_data.GetDimension();
vector<int> demands(dimension);
for (int i = 0; i < dimension; ++i) {
int node_id, demand;
file >> node_id >> demand;
demands[node_id - ProblemLoader::DEPOT_INDEX_OFFSET] = demand;
}
problem_data.SetDemands(demands);
}
void ProblemLoader::ParseDepotSection(ifstream& file, ProblemData& problem_data) {
int depot;
file >> depot;
problem_data.SetDepot(depot);
// read -1 terminator
int terminator;
file >> terminator;
}