-
Notifications
You must be signed in to change notification settings - Fork 1
/
tsptw_data_dt.h
289 lines (241 loc) · 7.23 KB
/
tsptw_data_dt.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#ifndef LOCALSOLVER_TUTORIALS_CPLUSPLUS_TSPTW_DATA_DT_H
#define LOCALSOLVER_TUTORIALS_CPLUSPLUS_TSPTW_DATA_DT_H
#include <ostream>
#include <iostream>
#include <iomanip>
#include <vector>
#include <inttypes.h>
#include <fstream>
#include <math.h>
#include <string>
#include "localsolver_vrp.pb.h"
#define CUSTOM_MAX_INT (int) pow(2,30)
enum ShiftPref { ForceStart = 2, ForceEnd = 1, MinimizeSpan = 0 };
namespace std {
class TSPTWDataDT {
public:
explicit TSPTWDataDT(string filename) {
LoadInstance(filename);
}
void LoadInstance(const string & filename);
vector<int> Demands() const {
return Demands_;
}
vector<int> Durations() const {
return Durations_;
}
vector<int> CapaVecs() const {
return CapaVec_;
}
int SizeMissions() const {
return size_missions_;
}
int NbVehicles() const {
return nbVecs_;
}
vector<vector<float>> Matrice() const {
return matrice_;
}
vector<float> DistWarehouse() const {
return distWare;
}
vector<float> DistWarehouseReturn() const {
return distWare2;
}
vector<int> TimeWindowStarts1() const {
return timewindow_start_1 ;
}
vector<int> TimeWindowEnds1() const {
return timewindow_end_1 ;
}
vector<int> TimeWindowStarts2() const {
return timewindow_start_2 ;
}
vector<int> TimeWindowEnds2() const {
return timewindow_end_2 ;
}
// vector<vector<int>> TimeWindowStarts() const {
// return timewindow_start_ ;
// }
// vector<vector<int>> TimeWindowEnds() const {
// return timewindow_end_ ;
// }
vector<vector<int>> IndiceMultipleTW() const {
return indiceMultipleTW_;
}
int SizeMissionsMultipleTW() const {
return size_missions_multipleTW_;
}
vector<int> TwStartCar() const {
return tw_start_car_;
}
vector<int> TwEndCar() const {
return tw_end_car_;
}
vector<int> Start_index() const {
return start_index_;
}
vector<int> End_index() const {
return end_index_;
}
private:
void ProcessNewLine(char* const line);
int size_missions_;
int size_missions_multipleTW_;
int nbVecs_;
vector<int> CapaVec_;
vector<int> Demands_;
vector<int> Durations_;
vector<int> tw_start_car_;
vector<int> tw_end_car_;
// vector<vector<int>> timewindow_start_;
// vector<vector<int>> timewindow_end_;
vector<int> timewindow_start_1;
vector<int> timewindow_end_1;
vector<int> timewindow_start_2;
vector<int> timewindow_end_2;
vector<int> start_index_;
vector<int> end_index_;
vector<vector<float>> matrice_;
vector<float> distWare;
vector<float> distWare2;
vector<vector<int>> indiceMultipleTW_;
vector<int> indice_;
};
void TSPTWDataDT::LoadInstance(const string & filename) {
GOOGLE_PROTOBUF_VERIFY_VERSION;
localsolver_vrp::Problem problem;
{
fstream input(filename, ios::in | ios::binary);
if (!problem.ParseFromIstream(&input)) {
cout << "Failed to parse pbf." << endl;
}
}
int nbService=0;
for (const localsolver_vrp::Service& service: problem.services()) {
nbService++;
}
size_missions_ = nbService;
size_missions_multipleTW_ = size_missions_;
int nbVehicle = 0;
for (const localsolver_vrp::Vehicle& vehicle: problem.vehicles()) {
nbVehicle +=1;
}
nbVecs_=nbVehicle;
int quant=0;
int j=0;
int tws = 0;
vector<int> start;
vector<int> end;
for (const localsolver_vrp::Service& service: problem.services()) {
j+=1;
Durations_.push_back(service.duration());
for (const float& quantity: service.quantities()) {
Demands_.push_back(quantity);
}
if (service.time_windows().size() != 0 && service.time_windows().size() != 1){
for (const localsolver_vrp::TimeWindow& tw: service.time_windows()) {
start.push_back(tw.start());
if (tw.end() == 0) {
end.push_back(5000000);
} else {
end.push_back(tw.end());
}
}
timewindow_start_1.push_back(start[0]);
timewindow_end_1.push_back(end[0]);
timewindow_start_2.push_back(start[1]);
timewindow_end_2.push_back(end[1]);
// }
start.clear();
end.clear();
} else if (service.time_windows().size() == 1) {
for (const localsolver_vrp::TimeWindow& tw: service.time_windows()) {
start.push_back(tw.start());
if (tw.end() == 0) {
end.push_back(5000000);
} else {
end.push_back(tw.end());
}
}
start.push_back(-2);
end.push_back(-1);
timewindow_start_1.push_back(start[0]);
timewindow_end_1.push_back(end[0]);
timewindow_start_2.push_back(start[1]);
timewindow_end_2.push_back(end[1]);
start.clear();
end.clear();
}
else {
for (int i=0; i<2; i++) {
if (i == 0) {
start.push_back(0);
end.push_back(5000000);
} else {
start.push_back(-2);
end.push_back(-1);
}
}
timewindow_start_1.push_back(start[0]);
timewindow_end_1.push_back(end[0]);
timewindow_start_2.push_back(start[1]);
timewindow_end_2.push_back(end[1]);
start.clear();
end.clear();
}
}
for (const localsolver_vrp::Vehicle& vehicle: problem.vehicles()) {
for (const localsolver_vrp::Capacity& capacity: vehicle.capacities()) {
CapaVec_.push_back(capacity.limit()/1000);
}
tw_start_car_.push_back(vehicle.time_window().start());
tw_end_car_.push_back(vehicle.time_window().end());
start_index_.push_back(vehicle.start_index());
end_index_.push_back(vehicle.end_index());
}
for (const localsolver_vrp::Matrix& matrix: problem.matrices()) {
int size_matrix5 = sqrt(matrix.time().size());
if (size_matrix5 == size_missions_ + 2) {
int size_matrix = sqrt(matrix.time().size());
for (int i=0; i<size_matrix; i++) {
vector<float> tab;
if (i==size_matrix-2) {
for (int j=0; j<size_matrix-2; j++) {
distWare.push_back(static_cast<float>(matrix.time(i * (size_matrix) + j)));
}
} else if (i==size_matrix-1) {
for (int j=0; j<size_matrix-2; j++) {
distWare2.push_back(static_cast<float>(matrix.time(i * (size_matrix) + j)));
}
} else {
for (int j=0; j<size_matrix-2; j++) {
tab.push_back(static_cast<float>(matrix.time(i * (size_matrix) + j)));
}
matrice_.push_back(tab);
}
}
} else {
int size_matrix = sqrt(matrix.time().size()) - (CapaVec_.size()-1)*2;
for (int i=0; i<size_matrix; i++) {
vector<float> tab;
if (i==size_matrix-2) {
for (int j=0; j<size_matrix-2; j++) {
distWare.push_back(static_cast<float>(matrix.time(i * (size_matrix + (CapaVec_.size()-1)*2) + j)));
}
} else if (i==size_matrix-1) {
for (int j=0; j<size_matrix-2; j++) {
distWare2.push_back(static_cast<float>(matrix.time(i * (size_matrix + (CapaVec_.size()-1)*2) + j)));
}
} else {
for (int j=0; j<size_matrix-2; j++) {
tab.push_back(static_cast<float>(matrix.time(i * (size_matrix + (CapaVec_.size()-1)*2) + j)));
}
matrice_.push_back(tab);
}
}
}
}
}
} // namespace operations_research
#endif // LOCALSOLVER_TUTORIALS_CPLUSPLUS_TSP_DATA_DT_H