forked from google/or-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdptw_with_alternatives.cc
More file actions
377 lines (355 loc) · 16.5 KB
/
pdptw_with_alternatives.cc
File metadata and controls
377 lines (355 loc) · 16.5 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright 2010-2025 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Pickup and Delivery Problem with Time Windows and Alternatives.
// This is a variant of the mode in pdptw.cc (see that file for more details
// on pickup and delivery models). In this model both pickups and deliveries
// have alternative locations, of which one of each has to be selected. As in
// the standard pickup and delivery problem, pickups must happen before
// deliveries and must be on the same route.
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <string>
#include <utility>
#include <vector>
#include "absl/base/log_severity.h"
#include "absl/flags/flag.h"
#include "absl/functional/bind_front.h"
#include "absl/log/check.h"
#include "absl/log/globals.h"
#include "absl/log/log.h"
#include "absl/strings/numbers.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/text_format.h"
#include "ortools/base/helpers.h"
#include "ortools/base/init_google.h"
#include "ortools/base/options.h"
#include "ortools/constraint_solver/constraint_solver.h"
#include "ortools/routing/index_manager.h"
#include "ortools/routing/parameters.h"
#include "ortools/routing/parameters.pb.h"
#include "ortools/routing/routing.h"
ABSL_FLAG(std::string, pdp_file, "",
"File containing the Pickup and Delivery Problem to solve.");
ABSL_FLAG(int, pdp_force_vehicles, 0,
"Force the number of vehicles used (maximum number of routes.");
ABSL_FLAG(bool, reduce_vehicle_cost_model, true,
"Overrides the homonymous field of "
"DefaultRoutingModelParameters().");
ABSL_FLAG(std::string, routing_search_parameters,
"first_solution_strategy:ALL_UNPERFORMED "
"local_search_operators { use_node_pair_swap_active:BOOL_FALSE }",
"Text proto RoutingSearchParameters (possibly partial) that will "
"override the DefaultRoutingSearchParameters()");
using ::absl::bind_front;
namespace operations_research::routing {
// Scaling factor used to scale up distances, allowing a bit more precision
// from Euclidean distances.
const int64_t kScalingFactor = 1000;
// Vector of (x,y) node coordinates, *unscaled*, in some imaginary planar,
// metric grid.
typedef std::vector<std::pair<int, int>> Coordinates;
// Returns the scaled Euclidean distance between two nodes, coords holding the
// coordinates of the nodes.
int64_t Travel(const Coordinates* const coords,
RoutingIndexManager::NodeIndex from,
RoutingIndexManager::NodeIndex to) {
DCHECK(coords != nullptr);
const int xd = coords->at(from.value()).first - coords->at(to.value()).first;
const int yd =
coords->at(from.value()).second - coords->at(to.value()).second;
return static_cast<int64_t>(kScalingFactor *
std::sqrt(1.0L * xd * xd + yd * yd));
}
// Returns the scaled service time at a given node, service_times holding the
// service times.
int64_t ServiceTime(const std::vector<int64_t>* const service_times,
RoutingIndexManager::NodeIndex node) {
return kScalingFactor * service_times->at(node.value());
}
// Returns the scaled (distance plus service time) between two indices, coords
// holding the coordinates of the nodes and service_times holding the service
// times.
// The service time is the time spent to execute a delivery or a pickup.
int64_t TravelPlusServiceTime(const RoutingIndexManager& manager,
const Coordinates* const coords,
const std::vector<int64_t>* const service_times,
int64_t from_index, int64_t to_index) {
const RoutingIndexManager::NodeIndex from = manager.IndexToNode(from_index);
const RoutingIndexManager::NodeIndex to = manager.IndexToNode(to_index);
return ServiceTime(service_times, from) + Travel(coords, from, to);
}
// Returns the demand (quantity picked up or delivered) of an index, demands
// holds the demand of each node.
int64_t Demand(const RoutingIndexManager& manager,
const std::vector<int64_t>* const demands, int64_t from_index,
int64_t to_index) {
(void)to_index;
return demands->at(manager.IndexToNode(from_index).value());
}
// Outputs a solution to the current model in a string.
std::string VerboseOutput(const RoutingModel& routing,
const RoutingIndexManager& manager,
const Assignment& assignment,
const Coordinates& coords,
const std::vector<int64_t>& service_times) {
std::string output;
const RoutingDimension& time_dimension = routing.GetDimensionOrDie("time");
const RoutingDimension& load_dimension = routing.GetDimensionOrDie("demand");
for (int i = 0; i < routing.vehicles(); ++i) {
absl::StrAppendFormat(&output, "Vehicle %d: ", i);
int64_t index = routing.Start(i);
if (routing.IsEnd(assignment.Value(routing.NextVar(index)))) {
output.append("empty");
} else {
while (!routing.IsEnd(index)) {
absl::StrAppendFormat(&output, "%d ",
manager.IndexToNode(index).value());
const IntVar* vehicle = routing.VehicleVar(index);
absl::StrAppendFormat(&output, "Vehicle(%d) ",
assignment.Value(vehicle));
const IntVar* arrival = time_dimension.CumulVar(index);
absl::StrAppendFormat(&output, "Time(%d..%d) ", assignment.Min(arrival),
assignment.Max(arrival));
const IntVar* load = load_dimension.CumulVar(index);
absl::StrAppendFormat(&output, "Load(%d..%d) ", assignment.Min(load),
assignment.Max(load));
const int64_t next_index = assignment.Value(routing.NextVar(index));
absl::StrAppendFormat(
&output, "Transit(%d) ",
TravelPlusServiceTime(manager, &coords, &service_times, index,
next_index));
index = next_index;
}
output.append("Route end ");
const IntVar* vehicle = routing.VehicleVar(index);
absl::StrAppendFormat(&output, "Vehicle(%d) ", assignment.Value(vehicle));
const IntVar* arrival = time_dimension.CumulVar(index);
absl::StrAppendFormat(&output, "Time(%d..%d) ", assignment.Min(arrival),
assignment.Max(arrival));
const IntVar* load = load_dimension.CumulVar(index);
absl::StrAppendFormat(&output, "Load(%d..%d) ", assignment.Min(load),
assignment.Max(load));
}
output.append("\n");
}
return output;
}
namespace {
// An inefficient but convenient method to parse a whitespace-separated list
// of integers. Returns true iff the input string was entirely valid and parsed.
bool SafeParseInt64Array(const std::string& str,
std::vector<int64_t>* parsed_int) {
static const char kWhiteSpaces[] = " \t\n\v\f\r";
parsed_int->clear();
for (absl::string_view token :
absl::StrSplit(str, absl::ByAnyChar(kWhiteSpaces), absl::SkipEmpty())) {
int value;
if (!absl::SimpleAtoi(token, &value)) return false;
parsed_int->push_back(value);
}
return true;
}
} // namespace
// Builds and solves a model from a file in the format defined by Li & Lim
// (https://www.sintef.no/projectweb/top/pdptw/li-lim-benchmark/documentation/).
bool LoadAndSolve(const std::string& pdp_file,
const RoutingModelParameters& model_parameters,
const RoutingSearchParameters& search_parameters) {
// Load all the lines of the file in RAM (it shouldn't be too large anyway).
std::vector<std::string> lines;
{
std::string contents;
CHECK_OK(file::GetContents(pdp_file, &contents, file::Defaults()));
const int64_t kMaxInputFileSize = 1 << 30; // 1GB
if (contents.size() >= kMaxInputFileSize) {
LOG(WARNING) << "Input file '" << pdp_file << "' is too large (>"
<< kMaxInputFileSize << " bytes).";
return false;
}
lines = absl::StrSplit(contents, '\n', absl::SkipEmpty());
}
// Reading header.
if (lines.empty()) {
LOG(WARNING) << "Empty file: " << pdp_file;
return false;
}
// Parse file header.
std::vector<int64_t> parsed_int;
if (!SafeParseInt64Array(lines[0], &parsed_int) || parsed_int.size() != 3 ||
parsed_int[0] < 0 || parsed_int[1] < 0 || parsed_int[2] < 0) {
LOG(WARNING) << "Malformed header: " << lines[0];
return false;
}
const int num_vehicles = absl::GetFlag(FLAGS_pdp_force_vehicles) > 0
? absl::GetFlag(FLAGS_pdp_force_vehicles)
: parsed_int[0];
const int64_t capacity = parsed_int[1];
// We do not care about the 'speed' field, in third position.
// Parse order data.
std::vector<int> customer_ids;
std::vector<std::pair<int, int>> coords;
std::vector<int64_t> demands;
std::vector<int64_t> open_times;
std::vector<int64_t> close_times;
std::vector<int64_t> service_times;
std::vector<RoutingIndexManager::NodeIndex> pickups;
std::vector<RoutingIndexManager::NodeIndex> deliveries;
int64_t horizon = 0;
RoutingIndexManager::NodeIndex depot(0);
for (int line_index = 1; line_index < lines.size(); ++line_index) {
if (!SafeParseInt64Array(lines[line_index], &parsed_int) ||
parsed_int.size() != 9 || parsed_int[0] < 0 || parsed_int[4] < 0 ||
parsed_int[5] < 0 || parsed_int[6] < 0 || parsed_int[7] < 0 ||
parsed_int[8] < 0) {
LOG(WARNING) << "Malformed line #" << line_index << ": "
<< lines[line_index];
return false;
}
const int customer_id = parsed_int[0];
const int x = parsed_int[1];
const int y = parsed_int[2];
const int64_t demand = parsed_int[3];
const int64_t open_time = parsed_int[4];
const int64_t close_time = parsed_int[5];
const int64_t service_time = parsed_int[6];
const int pickup = parsed_int[7];
const int delivery = parsed_int[8];
customer_ids.push_back(customer_id);
coords.push_back(std::make_pair(x, y));
demands.push_back(demand);
open_times.push_back(open_time);
close_times.push_back(close_time);
service_times.push_back(service_time);
pickups.push_back(RoutingIndexManager::NodeIndex(pickup));
deliveries.push_back(RoutingIndexManager::NodeIndex(delivery));
if (pickup == 0 && delivery == 0) {
depot = RoutingIndexManager::NodeIndex(pickups.size() - 1);
}
horizon = std::max(horizon, close_time);
}
// Build pickup and delivery model.
const int num_nodes = customer_ids.size();
RoutingIndexManager manager(num_nodes, num_vehicles, depot);
RoutingModel routing(manager, model_parameters);
const int vehicle_cost = routing.RegisterTransitCallback(
[&coords, &manager](int64_t i, int64_t j) {
return Travel(const_cast<const Coordinates*>(&coords),
manager.IndexToNode(i), manager.IndexToNode(j));
});
routing.SetArcCostEvaluatorOfAllVehicles(vehicle_cost);
routing.AddDimension(
routing.RegisterTransitCallback(absl::bind_front(
TravelPlusServiceTime, manager,
const_cast<const Coordinates*>(&coords),
const_cast<const std::vector<int64_t>*>(&service_times))),
kScalingFactor * horizon, kScalingFactor * horizon,
/*fix_start_cumul_to_zero=*/true, "time");
const RoutingDimension& time_dimension = routing.GetDimensionOrDie("time");
Solver* const solver = routing.solver();
// Collect pickup and delivery pairs and set time windows.
std::vector<std::pair<int64_t, int64_t>> pickup_delivery_pairs;
for (RoutingIndexManager::NodeIndex order(0); order < routing.nodes();
++order) {
const int64_t index = manager.NodeToIndex(order);
IntVar* const cumul = time_dimension.CumulVar(index);
cumul->SetMin(kScalingFactor * open_times[order.value()]);
cumul->SetMax(kScalingFactor * close_times[order.value()]);
RoutingIndexManager::NodeIndex delivery = deliveries[order.value()];
if (pickups[order.value()] == 0 && delivery != 0) {
pickup_delivery_pairs.push_back({index, manager.NodeToIndex(delivery)});
}
}
// Build groups of pickup and delivery pairs representing the alternatives of
// pickup and delivery locations for a given shipment, and add the
// corresponding constraints.
const int kGroupSize = 4;
const int64_t kPenalty = 10000000;
// Collecting demands per group computed as the average demand for the group.
std::vector<int64_t> group_demands(demands.size());
for (int pair_index = 0; pair_index < pickup_delivery_pairs.size();) {
std::vector<int64_t> pickup_indices;
std::vector<int64_t> delivery_indices;
std::vector<IntVar*> pickup_vehicle_variables;
std::vector<IntVar*> delivery_vehicle_variables;
int64_t demand_sum = 0;
int pair_start = pair_index;
for (int i = 0; i < kGroupSize && pair_index < pickup_delivery_pairs.size();
++i, ++pair_index) {
const int64_t pickup = pickup_delivery_pairs[pair_index].first;
const int64_t delivery = pickup_delivery_pairs[pair_index].second;
pickup_indices.push_back(pickup);
delivery_indices.push_back(delivery);
pickup_vehicle_variables.push_back(routing.VehicleVar(pickup));
delivery_vehicle_variables.push_back(routing.VehicleVar(delivery));
demand_sum += demands[manager.IndexToNode(pickup).value()];
}
// Computing demand average.
int64_t demand_avg = demand_sum / (pair_index - pair_start);
for (int i = pair_start; i < pair_index; ++i) {
group_demands[pickup_delivery_pairs[i].first] = demand_avg;
group_demands[pickup_delivery_pairs[i].second] = -demand_avg;
}
// Unperformed pickups or deliveries will have their vehicle variable set
// to -1. Therefore the vehicle performing the performed pickup (resp. the
// performed delivery) is the maximum of the vehicle variables of the
// pickups (resp. deliveries). Using this to ensure the performed pickup
// and delivery are on the same route.
solver->AddConstraint(
solver->MakeEquality(solver->MakeMax(pickup_vehicle_variables),
solver->MakeMax(delivery_vehicle_variables)));
// Only one pickup and one delivery must be performed and notify the solver
// about the pickup and delivery alternatives.
routing.AddPickupAndDeliverySets(
routing.AddDisjunction(pickup_indices, kPenalty),
routing.AddDisjunction(delivery_indices, kPenalty));
}
// Add demand dimension where the demand corresponds to the average demand
// of the group.
routing.AddDimension(
routing.RegisterTransitCallback(absl::bind_front(
Demand, manager,
const_cast<const std::vector<int64_t>*>(&group_demands))),
0, capacity, /*fix_start_cumul_to_zero=*/true, "demand");
// Solve pickup and delivery problem.
const Assignment* assignment = routing.SolveWithParameters(search_parameters);
LOG(INFO) << routing.solver()->LocalSearchProfile();
if (nullptr != assignment) {
LOG(INFO) << "Cost: " << assignment->ObjectiveValue();
LOG(INFO) << VerboseOutput(routing, manager, *assignment, coords,
service_times);
return true;
}
return false;
}
} // namespace operations_research::routing
int main(int argc, char** argv) {
absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
InitGoogle(argv[0], &argc, &argv, true);
// Set up model and search parameters.
operations_research::routing::RoutingModelParameters model_parameters =
operations_research::routing::DefaultRoutingModelParameters();
model_parameters.set_reduce_vehicle_cost_model(
absl::GetFlag(FLAGS_reduce_vehicle_cost_model));
operations_research::routing::RoutingSearchParameters search_parameters =
operations_research::routing::DefaultRoutingSearchParameters();
CHECK(google::protobuf::TextFormat::MergeFromString(
absl::GetFlag(FLAGS_routing_search_parameters), &search_parameters));
if (!operations_research::routing::LoadAndSolve(
absl::GetFlag(FLAGS_pdp_file), model_parameters, search_parameters)) {
LOG(INFO) << "Error solving " << absl::GetFlag(FLAGS_pdp_file);
}
return 0;
}