-
Notifications
You must be signed in to change notification settings - Fork 340
/
libvroom.cpp
293 lines (249 loc) · 9.2 KB
/
libvroom.cpp
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
290
291
292
293
#include <iostream>
#include "routing/osrm_routed_wrapper.h"
#include "structures/vroom/input/input.h"
#include "structures/vroom/job.h"
#include "structures/vroom/vehicle.h"
#include "utils/exception.h"
void log_solution(const vroom::Solution& sol, bool geometry) {
std::cout << "Total cost: " << sol.summary.cost << std::endl;
std::cout << "Unassigned: " << sol.summary.unassigned << std::endl;
// Log unassigned jobs if any.
std::cout << "Unassigned job ids: ";
for (const auto& j : sol.unassigned) {
std::cout << j.id << ", ";
}
std::cout << std::endl;
// Describe routes in solution.
for (const auto& route : sol.routes) {
std::cout << "Steps for vehicle " << route.vehicle
<< " (cost: " << route.cost;
std::cout << " - duration: " << route.duration;
std::cout << " - service: " << route.service;
if (geometry) {
std::cout << " - distance: " << route.distance;
}
std::cout << ")" << std::endl;
// Describe all route steps.
for (const auto& step : route.steps) {
std::string type;
switch (step.step_type) {
using enum vroom::STEP_TYPE;
case START:
type = "Start";
break;
case END:
type = "End";
break;
case BREAK:
type = "Break";
break;
case JOB: {
assert(step.job_type.has_value());
switch (step.job_type.value()) {
using enum vroom::JOB_TYPE;
case SINGLE:
type = "Job";
break;
case PICKUP:
type = "Pickup";
break;
case DELIVERY:
type = "Delivery";
break;
}
break;
}
}
std::cout << type;
// Add job/pickup/delivery/break ids.
if (step.step_type != vroom::STEP_TYPE::START &&
step.step_type != vroom::STEP_TYPE::END) {
std::cout << " " << step.id;
}
// Add location if known.
if (step.location.has_value()) {
const auto& loc = step.location.value();
if (loc.has_coordinates()) {
std::cout << " - " << loc.lon() << ";" << loc.lat();
}
}
std::cout << " - arrival: " << step.arrival;
std::cout << " - duration: " << step.duration;
std::cout << " - service: " << step.service;
// Add extra step info if geometry is required.
if (geometry) {
std::cout << " - distance: " << step.distance;
}
std::cout << std::endl;
}
}
}
void run_example_with_osrm() {
bool GEOMETRY = true;
const unsigned amount_dimension = 1;
// Set OSRM host and port for "car" profile.
vroom::io::Servers servers;
servers["car"] = vroom::Server("localhost", "5000");
vroom::Input problem_instance(servers, vroom::ROUTER::OSRM);
problem_instance.set_geometry(GEOMETRY); // Query for route geometry
// after solving.
// Create one-dimension capacity restrictions to model the situation
// where one vehicle can handle 4 jobs with deliveries.
vroom::Amount vehicle_capacity(amount_dimension);
vehicle_capacity[0] = 4;
vroom::TimeWindow vehicle_tw(28800, 43200); // Working hours.
// Default "zero" amount data structures with relevant dimension.
vroom::Amount job_delivery(amount_dimension);
vroom::Amount job_empty_delivery(amount_dimension);
job_delivery[0] = 1;
vroom::Amount job_pickup(amount_dimension);
vroom::Amount job_empty_pickup(amount_dimension);
job_pickup[0] = 1;
vroom::UserDuration setup = 0;
vroom::UserDuration service = 5 * 60; // 5 minutes
// Define vehicle breaks.
vroom::Break break_1(1, {vroom::TimeWindow(32400, 34200)}, 300);
vroom::Break break_2(2, {vroom::TimeWindow(34200, 36000)}, 300);
// Define vehicles (use std::nullopt for no start or no end).
vroom::Location depot(vroom::Coordinates{2.35044, 48.71764});
vroom::Vehicle v1(1, // id
depot, // start
depot, // end
"car", // profile
vehicle_capacity, // capacity
{1, 14}, // skills
vehicle_tw, // time window
{break_1}); // breaks
problem_instance.add_vehicle(v1);
vroom::Vehicle v2(2, // id
depot, // start
depot, // end
"car", // profile
vehicle_capacity, // capacity
{2, 14}, // skills
vehicle_tw, // time window
{break_2}); // breaks
problem_instance.add_vehicle(v2);
// Job to be done between 9 and 10 AM.
std::vector<vroom::TimeWindow> job_1_tws({{32400, 36000}});
// Set jobs id, location, service time, amount, required skills,
// priority and time windows. Constraints that are not required can
// be omitted.
std::vector<vroom::Job> jobs;
jobs.emplace_back(1,
vroom::Location(vroom::Coordinates{1.98935, 48.701}),
setup,
service,
job_delivery,
job_empty_pickup,
vroom::Skills{1}, // skills
0, // default priority
job_1_tws);
jobs.emplace_back(2,
vroom::Location(vroom::Coordinates{2.03655, 48.61128}),
setup,
service,
job_empty_delivery,
job_pickup,
vroom::Skills{1});
jobs.emplace_back(5,
vroom::Location(vroom::Coordinates{2.28325, 48.5958}),
setup,
service,
job_delivery,
job_empty_pickup,
vroom::Skills{14});
jobs.emplace_back(6,
vroom::Location(vroom::Coordinates{2.89357, 48.90736}),
setup,
service,
job_delivery,
job_empty_pickup,
vroom::Skills{14});
for (const auto& j : jobs) {
problem_instance.add_job(j);
}
// Define a shipment.
vroom::Skills pd_skills({2});
vroom::Amount pd_amount(amount_dimension);
pd_amount[0] = 1;
vroom::Job pickup(4,
vroom::JOB_TYPE::PICKUP,
vroom::Location(vroom::Coordinates{2.41808, 49.22619}),
setup,
service,
pd_amount,
pd_skills);
vroom::Job delivery(3,
vroom::JOB_TYPE::DELIVERY,
vroom::Location(vroom::Coordinates{2.39719, 49.07611}),
setup,
service,
pd_amount,
pd_skills);
problem_instance.add_shipment(pickup, delivery);
// Skills definitions set the following constraints:
// - jobs 1 and 2 can only be served by vehicle 1
// - jobs 3 and 4 can only be served by vehicle 2
// - jobs 5 and 6 can be served by either one of the vehicles
// Solve using exploration level as depth and number of searches.
auto sol =
problem_instance.solve(vroom::DEFAULT_EXPLORATION_LEVEL,
vroom::DEFAULT_EXPLORATION_LEVEL,
vroom::DEFAULT_THREADS_NUMBER); // Use 4 threads.
log_solution(sol, GEOMETRY);
}
void run_example_with_custom_matrix() {
bool GEOMETRY = false;
vroom::Input problem_instance;
// Define custom matrix and bypass OSRM call.
vroom::Matrix<vroom::UserDuration> matrix_input(4);
matrix_input[0][0] = 0;
matrix_input[0][1] = 2104;
matrix_input[0][2] = 197;
matrix_input[0][3] = 1299;
matrix_input[1][0] = 2103;
matrix_input[1][1] = 0;
matrix_input[1][2] = 2255;
matrix_input[1][3] = 3152;
matrix_input[2][0] = 197;
matrix_input[2][1] = 2256;
matrix_input[2][2] = 0;
matrix_input[2][3] = 1102;
matrix_input[3][0] = 1299;
matrix_input[3][1] = 3153;
matrix_input[3][2] = 1102;
matrix_input[3][3] = 0;
problem_instance.set_durations_matrix("car", std::move(matrix_input));
// Define vehicles (use std::nullopt for no start or no end).
vroom::Location v_start(0); // index in the provided matrix.
vroom::Location v_end(3); // index in the provided matrix.
vroom::Vehicle v(0, // id
v_start, // start
v_end); // end
problem_instance.add_vehicle(v);
// Define jobs with id and index of location in the matrix
// (coordinates are optional). Constraints that are not required can
// be omitted.
std::vector<vroom::Job> jobs;
jobs.emplace_back(1414, vroom::Location(1));
jobs.emplace_back(1515, vroom::Location(2));
for (const auto& j : jobs) {
problem_instance.add_job(j);
}
// Solve using exploration level as depth and number of searches.
auto sol =
problem_instance.solve(vroom::DEFAULT_EXPLORATION_LEVEL,
vroom::DEFAULT_EXPLORATION_LEVEL,
vroom::DEFAULT_THREADS_NUMBER); // Use 4 threads.
log_solution(sol, GEOMETRY);
}
int main() {
try {
run_example_with_osrm();
run_example_with_custom_matrix();
} catch (const vroom::Exception& e) {
std::cerr << "[Error] " << e.message << std::endl;
}
return 0;
}