-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblemLoader.cpp
More file actions
177 lines (156 loc) · 6.05 KB
/
Copy pathProblemLoader.cpp
File metadata and controls
177 lines (156 loc) · 6.05 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
#include "ProblemLoader.hpp"
#include <fstream>
#include <sstream>
#include <iostream>
using namespace LcVRPContest;
ProblemLoader::ProblemLoader(const string& folder_name, const string& instance_name)
: folder_name_(folder_name),
instance_name_(instance_name)
{
base_path_ = "data/lcvrp/" + folder_name_ + "/";
}
ProblemData ProblemLoader::LoadProblem() {
ProblemData problem_data;
string file_path = base_path_ + instance_name_ + ".lcvrp";
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();
}
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(1);
}
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("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, 0.0));
// 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;
// (again) node ids start from 1, array indices from 0 - i hate this
coordinates[node_id - 1] = 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;
// (same as above)
demands[node_id - 1] = 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;
}