Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
routing_flow.cc
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#include <math.h>
15
16#include <algorithm>
17#include <cstdint>
18#include <functional>
19#include <limits>
20#include <utility>
21#include <vector>
22
23#include "absl/algorithm/container.h"
24#include "absl/container/flat_hash_map.h"
25#include "absl/container/flat_hash_set.h"
26#include "absl/types/span.h"
35
36namespace operations_research {
37
38namespace {
39// Compute set of disjunctions involved in a pickup and delivery pair.
40template <typename Disjunctions>
41void AddDisjunctionsFromNodes(const RoutingModel& model,
42 absl::Span<const int64_t> nodes,
43 Disjunctions* disjunctions) {
44 for (int64_t node : nodes) {
45 for (const auto disjunction : model.GetDisjunctionIndices(node)) {
46 disjunctions->insert(disjunction);
47 }
48 }
49}
50} // namespace
51
53 // TODO(user): Support overlapping disjunctions and disjunctions with
54 // a cardinality > 1.
55 absl::flat_hash_set<int> disjunction_nodes;
56 for (DisjunctionIndex i(0); i < GetNumberOfDisjunctions(); ++i) {
57 if (GetDisjunctionMaxCardinality(i) > 1) return false;
58 for (int64_t node : GetDisjunctionNodeIndices(i)) {
59 if (!disjunction_nodes.insert(node).second) return false;
60 }
61 }
62 for (const auto& [pickups, deliveries] : GetPickupAndDeliveryPairs()) {
63 absl::flat_hash_set<DisjunctionIndex> disjunctions;
64 AddDisjunctionsFromNodes(*this, pickups, &disjunctions);
65 AddDisjunctionsFromNodes(*this, deliveries, &disjunctions);
66 // Pairs involving more than 2 disjunctions are not supported.
67 if (disjunctions.size() > 2) return false;
68 }
69 // Detect if a "unary" dimension prevents from having more than a single
70 // non-start/end node (or a single pickup and delivery pair) on a route.
71 // Binary dimensions are not considered because they would result in a
72 // quadratic check.
73 for (const RoutingDimension* const dimension : dimensions_) {
74 // TODO(user): Support vehicle-dependent dimension callbacks.
75 if (dimension->class_evaluators_.size() != 1) {
76 continue;
77 }
78 const TransitCallback1& transit =
79 UnaryTransitCallbackOrNull(dimension->class_evaluators_[0]);
80 if (transit == nullptr) {
81 continue;
82 }
83 int64_t max_vehicle_capacity = 0;
84 for (int64_t vehicle_capacity : dimension->vehicle_capacities()) {
85 max_vehicle_capacity = std::max(max_vehicle_capacity, vehicle_capacity);
86 }
87 std::vector<int64_t> transits(nexts_.size(),
88 std::numeric_limits<int64_t>::max());
89 for (int i = 0; i < nexts_.size(); ++i) {
90 if (!IsStart(i) && !IsEnd(i)) {
91 transits[i] = std::min(transits[i], transit(i));
92 }
93 }
94 int64_t min_transit = std::numeric_limits<int64_t>::max();
95 // Find the minimal accumulated value resulting from a pickup and delivery
96 // pair.
97 for (const auto& [pickups, deliveries] : GetPickupAndDeliveryPairs()) {
98 const auto transit_cmp = [&transits](int i, int j) {
99 return transits[i] < transits[j];
100 };
101 min_transit =
102 std::min(min_transit,
103 // Min transit from pickup.
104 transits[*absl::c_min_element(pickups, transit_cmp)] +
105 // Min transit from delivery.
106 transits[*absl::c_min_element(deliveries, transit_cmp)]);
107 }
108 // Find the minimal accumulated value resulting from a non-pickup/delivery
109 // node.
110 for (int i = 0; i < transits.size(); ++i) {
111 if (!IsPickup(i) && !IsDelivery(i)) {
112 min_transit = std::min(min_transit, transits[i]);
113 }
114 }
115 // If there cannot be more than one node or pickup and delivery, a matching
116 // problem has been detected.
117 if (CapProd(min_transit, 2) > max_vehicle_capacity) return true;
118 }
119 return false;
120}
121
122// Solve matching model using a min-cost flow. Here is the underlying flow:
123//
124// ---------- Source -------------
125// | (1,0) | (N,0)
126// V V
127// (vehicles) unperformed
128// | (1,cost) |
129// V |
130// (nodes/pickup/deliveries) | (1,penalty)
131// | (1,0) |
132// V |
133// disjunction <---------
134// | (1, 0)
135// V
136// Sink
137//
138// On arcs, (,) represents (capacity, cost).
139// N: number of disjunctions
140//
141
142namespace {
143struct FlowArc {
144 int64_t tail;
145 int64_t head;
146 int64_t capacity;
147 int64_t cost;
148};
149} // namespace
150
151bool RoutingModel::SolveMatchingModel(
152 Assignment* assignment, const RoutingSearchParameters& parameters) {
154 // We need to use LocalDimensionCumulOptimizers below, so we return false if
155 // LP scheduling is disabled.
156 return false;
157 }
158 VLOG(2) << "Solving with flow";
159 assignment->Clear();
160
161 // Collect dimensions with costs.
162 // TODO(user): If the costs are soft cumul upper (resp. lower) bounds only,
163 // do not use the LP model.
164 const std::vector<RoutingDimension*> dimensions =
166 std::vector<LocalDimensionCumulOptimizer> optimizers;
167 optimizers.reserve(dimensions.size());
168 for (RoutingDimension* dimension : dimensions) {
169 optimizers.emplace_back(
170 dimension, parameters.continuous_scheduling_solver(), &search_stats_);
171 }
172
173 int num_flow_nodes = 0;
174 std::vector<std::vector<int64_t>> disjunction_to_flow_nodes;
175 std::vector<int64_t> disjunction_penalties;
176 std::vector<bool> in_disjunction(Size(), false);
177 // Create pickup and delivery pair flow nodes.
178 // TODO(user): Check pair alternatives correspond exactly to at most two
179 // disjunctions.
180 absl::flat_hash_map<int, std::pair<int64_t, int64_t>> flow_to_pd;
181 for (const auto& [pickups, deliveries] : GetPickupAndDeliveryPairs()) {
182 disjunction_to_flow_nodes.push_back({});
183 absl::flat_hash_set<DisjunctionIndex> disjunctions;
184 AddDisjunctionsFromNodes(*this, pickups, &disjunctions);
185 AddDisjunctionsFromNodes(*this, deliveries, &disjunctions);
186 for (int64_t pickup : pickups) {
187 in_disjunction[pickup] = true;
188 for (int64_t delivery : deliveries) {
189 in_disjunction[delivery] = true;
190 flow_to_pd[num_flow_nodes] = {pickup, delivery};
191 disjunction_to_flow_nodes.back().push_back(num_flow_nodes);
192 num_flow_nodes++;
193 }
194 }
195 DCHECK_LE(disjunctions.size(), 2);
196 int64_t penalty = 0;
197 if (disjunctions.size() < 2) {
198 penalty = kNoPenalty;
199 } else {
200 for (DisjunctionIndex index : disjunctions) {
201 const int64_t d_penalty = GetDisjunctionPenalty(index);
202 if (d_penalty == kNoPenalty) {
203 penalty = kNoPenalty;
204 break;
205 }
206 penalty = CapAdd(penalty, d_penalty);
207 }
208 }
209 disjunction_penalties.push_back(penalty);
210 }
211 // Create non-pickup and delivery flow nodes.
212 absl::flat_hash_map<int, int64_t> flow_to_non_pd;
213 for (int node = 0; node < Size(); ++node) {
214 if (IsStart(node) || in_disjunction[node]) continue;
215 const std::vector<DisjunctionIndex>& disjunctions =
217 DCHECK_LE(disjunctions.size(), 1);
218 disjunction_to_flow_nodes.push_back({});
219 disjunction_penalties.push_back(
220 disjunctions.empty() ? kNoPenalty
221 : GetDisjunctionPenalty(disjunctions.back()));
222 if (disjunctions.empty()) {
223 in_disjunction[node] = true;
224 flow_to_non_pd[num_flow_nodes] = node;
225 disjunction_to_flow_nodes.back().push_back(num_flow_nodes);
226 num_flow_nodes++;
227 } else {
228 for (int n : GetDisjunctionNodeIndices(disjunctions.back())) {
229 in_disjunction[n] = true;
230 flow_to_non_pd[num_flow_nodes] = n;
231 disjunction_to_flow_nodes.back().push_back(num_flow_nodes);
232 num_flow_nodes++;
233 }
234 }
235 }
236
237 std::vector<FlowArc> arcs;
238
239 // Build a flow node for each disjunction and corresponding arcs.
240 // Each node exits to the sink through a node, for which the outgoing
241 // capacity is one (only one of the nodes in the disjunction is performed).
242 absl::flat_hash_map<int, int> flow_to_disjunction;
243 for (int i = 0; i < disjunction_to_flow_nodes.size(); ++i) {
244 const std::vector<int64_t>& flow_nodes = disjunction_to_flow_nodes[i];
245 if (flow_nodes.size() == 1) {
246 flow_to_disjunction[flow_nodes.back()] = i;
247 } else {
248 flow_to_disjunction[num_flow_nodes] = i;
249 for (int64_t flow_node : flow_nodes) {
250 arcs.push_back({flow_node, num_flow_nodes, 1, 0});
251 }
252 num_flow_nodes++;
253 }
254 }
255
256 // Build arcs from each vehicle to each non-vehicle flow node; the cost of
257 // each arc corresponds to:
258 // start(vehicle) -> pickup -> delivery -> end(vehicle)
259 // or
260 // start(vehicle) -> node -> end(vehicle)
261 std::vector<int> vehicle_to_flow;
262 absl::flat_hash_map<int, int> flow_to_vehicle;
263 for (int vehicle = 0; vehicle < vehicles(); ++vehicle) {
264 const int64_t start = Start(vehicle);
265 const int64_t end = End(vehicle);
266 for (const std::vector<int64_t>& flow_nodes : disjunction_to_flow_nodes) {
267 for (int64_t flow_node : flow_nodes) {
268 std::pair<int64_t, int64_t> pd_pair;
269 int64_t node = -1;
270 int64_t cost = 0;
271 bool add_arc = false;
272 if (gtl::FindCopy(flow_to_pd, flow_node, &pd_pair)) {
273 const int64_t pickup = pd_pair.first;
274 const int64_t delivery = pd_pair.second;
275 if (IsVehicleAllowedForIndex(vehicle, pickup) &&
276 IsVehicleAllowedForIndex(vehicle, delivery)) {
277 add_arc = true;
278 cost =
279 CapAdd(GetArcCostForVehicle(start, pickup, vehicle),
280 CapAdd(GetArcCostForVehicle(pickup, delivery, vehicle),
281 GetArcCostForVehicle(delivery, end, vehicle)));
282 const absl::flat_hash_map<int64_t, int64_t> nexts = {
283 {start, pickup}, {pickup, delivery}, {delivery, end}};
284 for (LocalDimensionCumulOptimizer& optimizer : optimizers) {
285 int64_t cumul_cost_value = 0;
286 // TODO(user): if the result is RELAXED_OPTIMAL_ONLY, do a
287 // second pass with an MP solver.
288 if (optimizer.ComputeRouteCumulCostWithoutFixedTransits(
289 vehicle, /*solve_duration_ratio=*/1.0,
290 [&nexts](int64_t node) {
291 return nexts.find(node)->second;
292 },
293 /*resource=*/nullptr, &cumul_cost_value) !=
295 cost = CapAdd(cost, cumul_cost_value);
296 } else {
297 add_arc = false;
298 break;
299 }
300 }
301 }
302 } else if (gtl::FindCopy(flow_to_non_pd, flow_node, &node)) {
303 if (IsVehicleAllowedForIndex(vehicle, node)) {
304 add_arc = true;
305 cost = CapAdd(GetArcCostForVehicle(start, node, vehicle),
306 GetArcCostForVehicle(node, end, vehicle));
307 const absl::flat_hash_map<int64_t, int64_t> nexts = {{start, node},
308 {node, end}};
309 for (LocalDimensionCumulOptimizer& optimizer : optimizers) {
310 int64_t cumul_cost_value = 0;
311 // TODO(user): if the result is RELAXED_OPTIMAL_ONLY, do a
312 // second pass with an MP solver.
313 if (optimizer.ComputeRouteCumulCostWithoutFixedTransits(
314 vehicle, /*solve_duration_ratio=*/1.0,
315 [&nexts](int64_t node) {
316 return nexts.find(node)->second;
317 },
318 /*resource=*/nullptr, &cumul_cost_value) !=
320 cost = CapAdd(cost, cumul_cost_value);
321 } else {
322 add_arc = false;
323 break;
324 }
325 }
326 }
327 } else {
328 DCHECK(false);
329 }
330 if (add_arc) {
331 arcs.push_back({num_flow_nodes, flow_node, 1, cost});
332 }
333 }
334 }
335 flow_to_vehicle[num_flow_nodes] = vehicle;
336 vehicle_to_flow.push_back(num_flow_nodes);
337 num_flow_nodes++;
338 }
339 // Create flow source and sink nodes.
340 const int source = num_flow_nodes + 1;
341 const int sink = source + 1;
342 // Source connected to vehicle nodes.
343 for (int vehicle = 0; vehicle < vehicles(); ++vehicle) {
344 arcs.push_back({source, vehicle_to_flow[vehicle], 1, 0});
345 }
346 // Handle unperformed nodes.
347 // Create a node to catch unperformed nodes and connect it to source.
348 const int unperformed = num_flow_nodes;
349 const int64_t flow_supply = disjunction_to_flow_nodes.size();
350 arcs.push_back({source, unperformed, flow_supply, 0});
351 for (const auto& flow_disjunction_element : flow_to_disjunction) {
352 const int flow_node = flow_disjunction_element.first;
353 const int64_t penalty =
354 disjunction_penalties[flow_disjunction_element.second];
355 if (penalty != kNoPenalty) {
356 arcs.push_back({unperformed, flow_node, 1, penalty});
357 }
358 // Connect non-vehicle flow nodes to sinks.
359 arcs.push_back({flow_node, sink, 1, 0});
360 }
361
362 // Rescale costs for min-cost flow; assuming max cost resulting from the
363 // push-relabel flow algorithm is max_arc_cost * (num_nodes+1) * (num_nodes+1)
364 // (cost-scaling multiplies arc costs by num_nodes+1 and the flow itself can
365 // accumulate num_nodes+1 such arcs (with capacity being 1 for costed arcs)).
366 int64_t scale_factor = 1;
367 const FlowArc& arc_with_max_cost = *std::max_element(
368 arcs.begin(), arcs.end(),
369 [](const FlowArc& a, const FlowArc& b) { return a.cost < b.cost; });
370 // SimpleMinCostFlow adds a source and a sink node, so actual number of
371 // nodes to consider is num_flow_nodes + 3.
372 const int actual_flow_num_nodes = num_flow_nodes + 3;
373 if (log(static_cast<double>(arc_with_max_cost.cost) + 1) +
374 2 * log(actual_flow_num_nodes) >
375 log(std::numeric_limits<int64_t>::max())) {
376 scale_factor = CapProd(actual_flow_num_nodes, actual_flow_num_nodes);
377 }
378
379 SimpleMinCostFlow flow;
380 // Add arcs to flow.
381 for (const FlowArc& arc : arcs) {
382 flow.AddArcWithCapacityAndUnitCost(arc.tail, arc.head, arc.capacity,
383 arc.cost / scale_factor);
384 }
385
386 // Set flow supply (number of non-vehicle nodes or pairs).
387 flow.SetNodeSupply(source, flow_supply);
388 flow.SetNodeSupply(sink, -flow_supply);
389
390 // TODO(user): Take time limit into account.
391 search_stats_.num_min_cost_flow_calls++;
392 if (flow.Solve() != SimpleMinCostFlow::OPTIMAL) {
393 return false;
394 }
395
396 // Map the flow result to assignment, only setting next variables.
397 std::vector<bool> used_vehicles(vehicles(), false);
398 absl::flat_hash_set<int> used_nodes;
399 for (int i = 0; i < flow.NumArcs(); ++i) {
400 if (flow.Flow(i) > 0 && flow.Tail(i) != source && flow.Head(i) != sink) {
401 std::vector<int> nodes;
402 std::pair<int64_t, int64_t> pd_pair;
403 int node = -1;
404 int index = -1;
405 if (gtl::FindCopy(flow_to_pd, flow.Head(i), &pd_pair)) {
406 nodes.push_back(pd_pair.first);
407 nodes.push_back(pd_pair.second);
408 } else if (gtl::FindCopy(flow_to_non_pd, flow.Head(i), &node)) {
409 nodes.push_back(node);
410 } else if (gtl::FindCopy(flow_to_disjunction, flow.Head(i), &index)) {
411 for (int64_t flow_node : disjunction_to_flow_nodes[index]) {
412 if (gtl::FindCopy(flow_to_pd, flow_node, &pd_pair)) {
413 nodes.push_back(pd_pair.first);
414 nodes.push_back(pd_pair.second);
415 } else if (gtl::FindCopy(flow_to_non_pd, flow_node, &node)) {
416 nodes.push_back(node);
417 }
418 }
419 }
420 int vehicle = -1;
421 if (flow.Tail(i) == unperformed) {
422 // Head is unperformed.
423 for (int node : nodes) {
424 assignment->Add(NextVar(node))->SetValue(node);
425 used_nodes.insert(node);
426 }
427 } else if (gtl::FindCopy(flow_to_vehicle, flow.Tail(i), &vehicle)) {
428 // Head is performed on a vehicle.
429 used_vehicles[vehicle] = true;
430 int current = Start(vehicle);
431 for (int node : nodes) {
432 assignment->Add(NextVar(current))->SetValue(node);
433 used_nodes.insert(node);
434 current = node;
435 }
436 assignment->Add(NextVar(current))->SetValue(End(vehicle));
437 }
438 }
439 }
440 // Adding unused nodes.
441 for (int node = 0; node < Size(); ++node) {
442 if (!IsStart(node) && used_nodes.count(node) == 0) {
443 assignment->Add(NextVar(node))->SetValue(node);
444 }
445 }
446 // Adding unused vehicles.
447 for (int vehicle = 0; vehicle < vehicles(); ++vehicle) {
448 if (!used_vehicles[vehicle]) {
449 assignment->Add(NextVar(Start(vehicle)))->SetValue(End(vehicle));
450 }
451 }
452 return true;
453}
454
455} // namespace operations_research
IntVarElement * Add(IntVar *var)
int64_t Size() const
Returns the number of next variables in the model.
Definition routing.h:2104
int GetNumberOfDisjunctions() const
Returns the number of node disjunctions in the model.
Definition routing.h:965
int64_t Start(int vehicle) const
Definition routing.h:1817
RoutingDisjunctionIndex DisjunctionIndex
Definition routing.h:271
operations_research::IntVar * NextVar(int64_t index) const
Definition routing.h:1859
int64_t GetDisjunctionPenalty(DisjunctionIndex index) const
Returns the penalty of the node disjunction of index 'index'.
Definition routing.h:950
int64_t End(int vehicle) const
Returns the variable index of the ending node of a vehicle route.
Definition routing.h:1819
const std::vector< PickupDeliveryPair > & GetPickupAndDeliveryPairs() const
Returns pickup and delivery pairs currently in the model.
Definition routing.h:1076
bool IsMatchingModel() const
Returns true if a vehicle/node matching problem is detected.
bool IsStart(int64_t index) const
Returns true if 'index' represents the first node of a route.
Definition routing.h:1821
bool IsPickup(int64_t node_index) const
Returns whether the node is a pickup (resp. delivery).
Definition routing.h:1055
bool IsEnd(int64_t index) const
Returns true if 'index' represents the last node of a route.
Definition routing.h:1823
bool IsDelivery(int64_t node_index) const
Definition routing.h:1058
const std::vector< int64_t > & GetDisjunctionNodeIndices(DisjunctionIndex index) const
Definition routing.h:944
int vehicles() const
Returns the number of vehicle routes in the model.
Definition routing.h:2102
static const int64_t kNoPenalty
Constant used to express a hard constraint instead of a soft penalty.
Definition routing.h:613
const TransitCallback1 & UnaryTransitCallbackOrNull(int callback_index) const
Definition routing.h:665
int64_t GetArcCostForVehicle(int64_t from_index, int64_t to_index, int64_t vehicle) const
Definition routing.cc:4298
std::vector< RoutingDimension * > GetDimensionsWithSoftOrSpanCosts() const
Returns dimensions with soft or vehicle span costs.
Definition routing.cc:5758
bool IsVehicleAllowedForIndex(int vehicle, int64_t index) const
Returns true if a vehicle is allowed to visit a given node.
Definition routing.h:1012
RoutingTransitCallback1 TransitCallback1
Definition routing.h:274
const std::vector< DisjunctionIndex > & GetDisjunctionIndices(int64_t index) const
Returns the indices of the disjunctions to which an index belongs.
Definition routing.h:923
int64_t GetDisjunctionMaxCardinality(DisjunctionIndex index) const
Definition routing.h:955
::operations_research::RoutingSearchParameters_SchedulingSolver continuous_scheduling_solver() const
bool FindCopy(const Collection &collection, const Key &key, Value *const value)
Definition map_util.h:190
OR-Tools root namespace.
int64_t CapAdd(int64_t x, int64_t y)
ClosedInterval::Iterator end(ClosedInterval interval)
int64_t CapProd(int64_t x, int64_t y)
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...