Repository navigation
Expand file tree
/
Copy pathProblemLoader.cpp
More file actions
198 lines (160 loc) · 4.11 KB
/
Copy pathProblemLoader.cpp
File metadata and controls
198 lines (160 loc) · 4.11 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
#include "ProblemLoader.hpp"
#include <fstream>
#include <iostream>
#include <random>
#include <algorithm>
using namespace LcVRPContest;
using namespace std;
ProblemLoader::ProblemLoader(const string& dir, const string& name, bool forceRandPermutation)
: randomizePermutation_(forceRandPermutation)
{
string separator = (dir.empty() || dir.back() == '/') ? "" : "/";
string basePath = "data/lcvrp/" + dir + separator;
filePath_ = basePath + name;
if (filePath_.find(".lcvrp") == string::npos) {
filePath_ += ".lcvrp";
}
}
ProblemData ProblemLoader::loadData() {
ProblemData data;
ifstream file(filePath_);
readFile(file, data);
file.close();
data.computeDistanceMatrix();
if (randomizePermutation_) {
generateRandomPermutation(data);
}
return data;
}
void ProblemLoader::readFile(ifstream& file, ProblemData& data) {
if (!file.is_open()) {
cerr << "[Reader] Critical error: File not found at " << filePath_ << endl;
return;
}
string token;
bool end = false;
while (file >> token && !end) {
if (token == "NAME") {
skipDelimiters(file);
string name;
file >> name;
data.setInstanceName(name);
}
else if (token == "DIMENSION") {
skipDelimiters(file);
int dim;
file >> dim;
data.setNodeCount(dim);
}
else if (token == "CAPACITY") {
skipDelimiters(file);
int cap;
file >> cap;
data.setCapacity(cap);
}
else if (token == "EDGE_WEIGHT_TYPE") {
skipDelimiters(file);
string type;
file >> type;
data.setWeightType(type);
}
else if (token == "NUM_GROUPS") {
skipDelimiters(file);
int groups;
file >> groups;
data.setNumGroups(groups);
}
else if (token == "NODE_COORD_SECTION") {
parseNodeCoords(file, data);
}
else if (token == "DEMAND_SECTION") {
parseDemands(file, data);
}
else if (token == "EDGE_WEIGHT_SECTION") {
parseEdgeWeights(file, data);
}
else if (token == "DEPOT_SECTION") {
parseDepot(file, data);
}
else if (token == "PERMUTATION") {
parsePermutation(file, data);
}
else if (token == "EOF") {
end = true;
}
}
}
void ProblemLoader::skipDelimiters(ifstream& file) {
char peek = file.peek();
while (peek == ' ' || peek == '\t' || peek == ':') {
file.get();
peek = file.peek();
}
}
void ProblemLoader::parseNodeCoords(ifstream& file, ProblemData& data) {
int n = data.getTotalNodes();
vector<Coordinate> locations(n);
for (int i = 0; i < n; i++) {
int id;
double x, y;
file >> id >> x >> y;
locations[id - 1] = Coordinate(x, y);
}
data.setLocations(locations);
}
void ProblemLoader::parseDemands(ifstream& file, ProblemData& data) {
int n = data.getTotalNodes();
vector<int> demands(n);
for (int i = 0; i < n; i++) {
int id, demand;
file >> id >> demand;
demands[id - 1] = demand;
}
data.setDemands(demands);
}
void ProblemLoader::parseEdgeWeights(ifstream& file, ProblemData& data) {
int dimension = data.getTotalNodes();
vector<vector<double>> edgeWeights(dimension, vector<double>(dimension, 0.0));
for (int i = 1; i < dimension; i++) {
for (int j = 0; j < i; j++) {
double weight;
file >> weight;
edgeWeights[i][j] = weight;
edgeWeights[j][i] = weight;
}
}
data.setDistanceMatrix(edgeWeights);
}
void ProblemLoader::parseDepot(ifstream& file, ProblemData& data) {
int id;
file >> id;
data.setDepotId(id);
int trash;
while (file >> trash && trash != -1);
}
void ProblemLoader::parsePermutation(ifstream& file, ProblemData& data) {
skipDelimiters(file);
int numCust = data.getNumCustomers();
vector<int> permutation;
permutation.reserve(numCust);
for (int i = 0; i < numCust; i++) {
int val;
file >> val;
permutation.push_back(val);
}
data.setPermutation(permutation);
}
void ProblemLoader::generateRandomPermutation(ProblemData& data) const {
int num_customers = data.getNumCustomers();
vector<int> permutation;
permutation.reserve(num_customers);
for (int i = 2; i <= num_customers + 1; ++i) {
permutation.push_back(i);
}
mt19937 gen(RANDOM_PERMUTATION_SEED);
shuffle(permutation.begin(), permutation.end(), gen);
data.setPermutation(permutation);
for (int i = 0; i < permutation.size(); i++) {
cout << permutation[i] << endl;
}
}