-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
routing_sat.cc
1205 lines (1148 loc) · 52.4 KB
/
routing_sat.cc
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2010-2024 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.
#include <algorithm>
#include <atomic>
#include <cstdint>
#include <functional>
#include <limits>
#include <memory>
#include <ostream>
#include <utility>
#include <vector>
#include "absl/container/btree_map.h"
#include "absl/container/flat_hash_map.h"
#include "absl/log/check.h"
#include "absl/time/time.h"
#include "absl/types/span.h"
#include "ortools/base/map_util.h"
#include "ortools/constraint_solver/constraint_solver.h"
#include "ortools/constraint_solver/routing.h"
#include "ortools/constraint_solver/routing_parameters.pb.h"
#include "ortools/constraint_solver/routing_types.h"
#include "ortools/sat/cp_model.pb.h"
#include "ortools/sat/cp_model_solver.h"
#include "ortools/sat/integer.h"
#include "ortools/sat/model.h"
#include "ortools/sat/sat_parameters.pb.h"
#include "ortools/util/bitset.h"
#include "ortools/util/optional_boolean.pb.h"
#include "ortools/util/saturated_arithmetic.h"
#include "ortools/util/time_limit.h"
namespace operations_research {
namespace sat {
namespace {
// As of 07/2019, TSPs and VRPs with homogeneous fleets of vehicles are
// supported.
// TODO(user): Support any type of constraints.
// TODO(user): Make VRPs properly support optional nodes.
bool RoutingModelCanBeSolvedBySat(const RoutingModel& model) {
return model.GetVehicleClassesCount() == 1;
}
// Adds an integer variable to a CpModelProto, returning its index in the proto.
int AddVariable(CpModelProto* cp_model, int64_t lb, int64_t ub) {
const int index = cp_model->variables_size();
IntegerVariableProto* const var = cp_model->add_variables();
var->add_domain(lb);
var->add_domain(ub);
return index;
}
// Adds a linear constraint, enforcing
// enforcement_literals -> lower_bound <= sum variable * coeff <= upper_bound.
void AddLinearConstraint(
CpModelProto* cp_model, int64_t lower_bound, int64_t upper_bound,
absl::Span<const std::pair<int, double>> variable_coeffs,
absl::Span<const int> enforcement_literals) {
CHECK_LE(lower_bound, upper_bound);
ConstraintProto* ct = cp_model->add_constraints();
for (const int enforcement_literal : enforcement_literals) {
ct->add_enforcement_literal(enforcement_literal);
}
LinearConstraintProto* arg = ct->mutable_linear();
arg->add_domain(lower_bound);
arg->add_domain(upper_bound);
for (const auto [var, coeff] : variable_coeffs) {
arg->add_vars(var);
arg->add_coeffs(coeff);
}
}
// Adds a linear constraint, enforcing
// lower_bound <= sum variable * coeff <= upper_bound.
void AddLinearConstraint(
CpModelProto* cp_model, int64_t lower_bound, int64_t upper_bound,
const std::vector<std::pair<int, double>>& variable_coeffs) {
AddLinearConstraint(cp_model, lower_bound, upper_bound, variable_coeffs, {});
}
// Returns the unique depot node used in the CP-SAT models (as of 01/2020).
int64_t GetDepotFromModel(const RoutingModel& model) { return model.Start(0); }
// Structure to keep track of arcs created.
struct Arc {
int tail;
int head;
friend bool operator==(const Arc& a, const Arc& b) {
return a.tail == b.tail && a.head == b.head;
}
friend bool operator!=(const Arc& a, const Arc& b) { return !(a == b); }
friend bool operator<(const Arc& a, const Arc& b) {
return a.tail == b.tail ? a.head < b.head : a.tail < b.tail;
}
friend std::ostream& operator<<(std::ostream& strm, const Arc& arc) {
return strm << "{" << arc.tail << ", " << arc.head << "}";
}
template <typename H>
friend H AbslHashValue(H h, const Arc& a) {
return H::combine(std::move(h), a.tail, a.head);
}
};
using ArcVarMap =
absl::btree_map<Arc, int>; // needs to be stable when iterating
void AddSoftCumulBounds(const RoutingDimension* dimension, int index, int cumul,
int64_t cumul_min, int64_t cumul_max,
CpModelProto* cp_model) {
{
const int64_t soft_ub_coef =
dimension->GetCumulVarSoftUpperBoundCoefficient(index);
if (soft_ub_coef != 0) {
const int64_t soft_ub = dimension->GetCumulVarSoftUpperBound(index);
const int soft_ub_var =
AddVariable(cp_model, 0, CapSub(cumul_max, soft_ub));
// soft_ub_var >= cumul - soft_ub
AddLinearConstraint(cp_model, std::numeric_limits<int64_t>::min(),
soft_ub, {{cumul, 1}, {soft_ub_var, -1}});
cp_model->mutable_objective()->add_vars(soft_ub_var);
cp_model->mutable_objective()->add_coeffs(soft_ub_coef);
}
}
{
const int64_t soft_lb_coef =
dimension->GetCumulVarSoftLowerBoundCoefficient(index);
if (soft_lb_coef != 0) {
const int64_t soft_lb = dimension->GetCumulVarSoftLowerBound(index);
const int soft_lb_var =
AddVariable(cp_model, 0, CapSub(soft_lb, cumul_min));
// soft_lb_var >= soft_lb - cumul
AddLinearConstraint(cp_model, soft_lb,
std::numeric_limits<int64_t>::max(),
{{cumul, 1}, {soft_lb_var, 1}});
cp_model->mutable_objective()->add_vars(soft_lb_var);
cp_model->mutable_objective()->add_coeffs(soft_lb_coef);
}
}
}
// Adds all dimensions to a CpModelProto. Only adds path cumul constraints and
// cumul bounds.
void AddDimensions(const RoutingModel& model, const ArcVarMap& arc_vars,
CpModelProto* cp_model) {
for (const RoutingDimension* dimension : model.GetDimensions()) {
// Only a single vehicle class.
const RoutingModel::TransitCallback2& transit =
dimension->transit_evaluator(0);
std::vector<int> cumuls(dimension->cumuls().size(), -1);
const int64_t min_start = dimension->cumuls()[model.Start(0)]->Min();
const int64_t max_end = std::min(dimension->cumuls()[model.End(0)]->Max(),
dimension->vehicle_capacities()[0]);
for (int i = 0; i < cumuls.size(); ++i) {
if (model.IsStart(i) || model.IsEnd(i)) continue;
// Reducing bounds supposing the triangular inequality.
const int64_t cumul_min =
std::max(sat::kMinIntegerValue.value(),
std::max(dimension->cumuls()[i]->Min(),
CapAdd(transit(model.Start(0), i), min_start)));
const int64_t cumul_max =
std::min(sat::kMaxIntegerValue.value(),
std::min(dimension->cumuls()[i]->Max(),
CapSub(max_end, transit(i, model.End(0)))));
cumuls[i] = AddVariable(cp_model, cumul_min, cumul_max);
AddSoftCumulBounds(dimension, i, cumuls[i], cumul_min, cumul_max,
cp_model);
}
for (const auto arc_var : arc_vars) {
const int tail = arc_var.first.tail;
const int head = arc_var.first.head;
if (tail == head || model.IsStart(tail) || model.IsStart(head)) continue;
// arc[tail][head] -> cumuls[head] >= cumuls[tail] + transit.
// This is a relaxation of the model as it does not consider slack max.
AddLinearConstraint(
cp_model, transit(tail, head), std::numeric_limits<int64_t>::max(),
{{cumuls[head], 1}, {cumuls[tail], -1}}, {arc_var.second});
}
}
}
std::vector<int> CreateRanks(const RoutingModel& model,
const ArcVarMap& arc_vars,
CpModelProto* cp_model) {
const int depot = GetDepotFromModel(model);
const int size = model.Size() + model.vehicles();
const int rank_size = model.Size() - model.vehicles();
std::vector<int> ranks(size, -1);
for (int i = 0; i < size; ++i) {
if (model.IsStart(i) || model.IsEnd(i)) continue;
ranks[i] = AddVariable(cp_model, 0, rank_size);
}
ranks[depot] = AddVariable(cp_model, 0, 0);
for (const auto arc_var : arc_vars) {
const int tail = arc_var.first.tail;
const int head = arc_var.first.head;
if (tail == head || head == depot) continue;
// arc[tail][head] -> ranks[head] == ranks[tail] + 1.
AddLinearConstraint(cp_model, 1, 1, {{ranks[head], 1}, {ranks[tail], -1}},
{arc_var.second});
}
return ranks;
}
// Vehicle variables do not actually represent the index of the vehicle
// performing a node, but we ensure that the values of two vehicle variables
// are the same if and only if the corresponding nodes are served by the same
// vehicle.
std::vector<int> CreateVehicleVars(const RoutingModel& model,
const ArcVarMap& arc_vars,
CpModelProto* cp_model) {
const int depot = GetDepotFromModel(model);
const int size = model.Size() + model.vehicles();
std::vector<int> vehicles(size, -1);
for (int i = 0; i < size; ++i) {
if (model.IsStart(i) || model.IsEnd(i)) continue;
vehicles[i] = AddVariable(cp_model, 0, size - 1);
}
for (const auto arc_var : arc_vars) {
const int tail = arc_var.first.tail;
const int head = arc_var.first.head;
if (tail == head || head == depot) continue;
if (tail == depot) {
// arc[depot][head] -> vehicles[head] == head.
AddLinearConstraint(cp_model, head, head, {{vehicles[head], 1}},
{arc_var.second});
continue;
}
// arc[tail][head] -> vehicles[head] == vehicles[tail].
AddLinearConstraint(cp_model, 0, 0,
{{vehicles[head], 1}, {vehicles[tail], -1}},
{arc_var.second});
}
return vehicles;
}
void AddPickupDeliveryConstraints(const RoutingModel& model,
const ArcVarMap& arc_vars,
CpModelProto* cp_model) {
if (model.GetPickupAndDeliveryPairs().empty()) return;
const std::vector<int> ranks = CreateRanks(model, arc_vars, cp_model);
const std::vector<int> vehicles =
CreateVehicleVars(model, arc_vars, cp_model);
for (const auto& [pickups, deliveries] : model.GetPickupAndDeliveryPairs()) {
const int64_t pickup = pickups[0];
const int64_t delivery = deliveries[0];
// ranks[pickup] + 1 <= ranks[delivery].
AddLinearConstraint(cp_model, 1, std::numeric_limits<int64_t>::max(),
{{ranks[delivery], 1}, {ranks[pickup], -1}});
// vehicles[pickup] == vehicles[delivery]
AddLinearConstraint(cp_model, 0, 0,
{{vehicles[delivery], 1}, {vehicles[pickup], -1}});
}
}
// Converts a RoutingModel to CpModelProto for models with multiple vehicles.
// All non-start/end nodes have the same index in both models. Start/end nodes
// map to a single depot index; its value is arbitrarly the index of the start
// node of the first vehicle in the RoutingModel.
// The map between CPModelProto arcs and their corresponding arc variable is
// returned.
ArcVarMap PopulateMultiRouteModelFromRoutingModel(const RoutingModel& model,
CpModelProto* cp_model) {
ArcVarMap arc_vars;
const int num_nodes = model.Nexts().size();
const int depot = GetDepotFromModel(model);
// Create "arc" variables and set their cost.
for (int tail = 0; tail < num_nodes; ++tail) {
const int tail_index = model.IsStart(tail) ? depot : tail;
std::unique_ptr<IntVarIterator> iter(
model.NextVar(tail)->MakeDomainIterator(false));
for (int head : InitAndGetValues(iter.get())) {
// Vehicle start and end nodes are represented as a single node in the
// CP-SAT model. We choose the start index of the first vehicle to
// represent both. We can also skip any head representing a vehicle start
// as the CP solver will reject those.
if (model.IsStart(head)) continue;
const int head_index = model.IsEnd(head) ? depot : head;
if (head_index == tail_index && head_index == depot) continue;
const int64_t cost = tail != head ? model.GetHomogeneousCost(tail, head)
: model.UnperformedPenalty(tail);
if (cost == std::numeric_limits<int64_t>::max()) continue;
const Arc arc = {tail_index, head_index};
if (arc_vars.contains(arc)) continue;
const int index = AddVariable(cp_model, 0, 1);
gtl::InsertOrDie(&arc_vars, arc, index);
cp_model->mutable_objective()->add_vars(index);
cp_model->mutable_objective()->add_coeffs(cost);
}
}
// Limit the number of routes to the maximum number of vehicles.
{
std::vector<std::pair<int, double>> variable_coeffs;
for (int node = 0; node < num_nodes; ++node) {
if (model.IsStart(node) || model.IsEnd(node)) continue;
int* const var = gtl::FindOrNull(arc_vars, {depot, node});
if (var == nullptr) continue;
variable_coeffs.push_back({*var, 1});
}
AddLinearConstraint(
cp_model, 0,
std::min(model.vehicles(), model.GetMaximumNumberOfActiveVehicles()),
variable_coeffs);
}
AddPickupDeliveryConstraints(model, arc_vars, cp_model);
AddDimensions(model, arc_vars, cp_model);
// Create Routes constraint, ensuring circuits from and to the depot.
// This one is a bit tricky, because we need to remap the depot to zero.
// TODO(user): Make Routes constraints support optional nodes.
RoutesConstraintProto* routes_ct =
cp_model->add_constraints()->mutable_routes();
for (const auto arc_var : arc_vars) {
const int tail = arc_var.first.tail;
const int head = arc_var.first.head;
routes_ct->add_tails(tail == 0 ? depot : tail == depot ? 0 : tail);
routes_ct->add_heads(head == 0 ? depot : head == depot ? 0 : head);
routes_ct->add_literals(arc_var.second);
}
// Add demands and capacities to improve the LP relaxation and cuts. These are
// based on the first "unary" dimension in the model if it exists.
// TODO(user): We might want to try to get demand lower bounds from
// non-unary dimensions if no unary exist.
const RoutingDimension* primary_dimension = nullptr;
for (const RoutingDimension* dimension : model.GetDimensions()) {
// Only a single vehicle class is supported.
if (dimension->GetUnaryTransitEvaluator(0) != nullptr) {
primary_dimension = dimension;
break;
}
}
if (primary_dimension != nullptr) {
const RoutingModel::TransitCallback1& transit =
primary_dimension->GetUnaryTransitEvaluator(0);
for (int node = 0; node < num_nodes; ++node) {
// Tricky: demand is added for all nodes in the sat model; this means
// start/end nodes other than the one used for the depot must be ignored.
if (!model.IsEnd(node) && (!model.IsStart(node) || node == depot)) {
routes_ct->add_demands(transit(node));
}
}
DCHECK_EQ(routes_ct->demands_size(), num_nodes + 1 - model.vehicles());
routes_ct->set_capacity(primary_dimension->vehicle_capacities()[0]);
}
return arc_vars;
}
// Converts a RoutingModel with a single vehicle to a CpModelProto.
// The mapping between CPModelProto arcs and their corresponding arc variables
// is returned.
ArcVarMap PopulateSingleRouteModelFromRoutingModel(const RoutingModel& model,
CpModelProto* cp_model) {
ArcVarMap arc_vars;
const int num_nodes = model.Nexts().size();
CircuitConstraintProto* circuit =
cp_model->add_constraints()->mutable_circuit();
for (int tail = 0; tail < num_nodes; ++tail) {
std::unique_ptr<IntVarIterator> iter(
model.NextVar(tail)->MakeDomainIterator(false));
for (int head : InitAndGetValues(iter.get())) {
// Vehicle start and end nodes are represented as a single node in the
// CP-SAT model. We choose the start index to represent both. We can also
// skip any head representing a vehicle start as the CP solver will reject
// those.
if (model.IsStart(head)) continue;
if (model.IsEnd(head)) head = model.Start(0);
const int64_t cost = tail != head ? model.GetHomogeneousCost(tail, head)
: model.UnperformedPenalty(tail);
if (cost == std::numeric_limits<int64_t>::max()) continue;
const int index = AddVariable(cp_model, 0, 1);
circuit->add_literals(index);
circuit->add_tails(tail);
circuit->add_heads(head);
cp_model->mutable_objective()->add_vars(index);
cp_model->mutable_objective()->add_coeffs(cost);
gtl::InsertOrDie(&arc_vars, {tail, head}, index);
}
}
AddPickupDeliveryConstraints(model, arc_vars, cp_model);
AddDimensions(model, arc_vars, cp_model);
return arc_vars;
}
// Converts a RoutingModel to a CpModelProto.
// The mapping between CPModelProto arcs and their corresponding arc variables
// is returned.
ArcVarMap PopulateModelFromRoutingModel(const RoutingModel& model,
CpModelProto* cp_model) {
if (model.vehicles() == 1) {
return PopulateSingleRouteModelFromRoutingModel(model, cp_model);
}
return PopulateMultiRouteModelFromRoutingModel(model, cp_model);
}
void ConvertObjectiveToSolution(const CpSolverResponse& response,
const CpObjectiveProto& objective,
const RoutingModel& model,
Assignment* solution) {
if (response.status() == CpSolverStatus::OPTIMAL) {
// If the solution was proven optimal by CP-SAT, add the objective value to
// the solution; it will be a proper lower bound of the routing objective.
// Recomputing the objective value to avoid rounding errors due to scaling.
// Note: We could use inner_objective_lower_bound if we were sure
// absolute_gap_limit was 0 (which is not guaranteed).
int64_t cost_value = 0;
for (int i = 0; i < objective.coeffs_size(); ++i) {
cost_value = CapAdd(
cost_value,
CapProd(objective.coeffs(i), response.solution(objective.vars(i))));
}
solution->AddObjective(model.CostVar());
solution->SetObjectiveValue(cost_value);
} else if (response.status() == CpSolverStatus::FEASIBLE) {
// If the solution is feasible only, add the lower bound of the objective to
// the solution; it will be a proper lower bound of the routing objective.
solution->AddObjective(model.CostVar());
solution->SetObjectiveValue(response.inner_objective_lower_bound());
}
}
// Converts a CpSolverResponse to an Assignment containing next variables.
bool ConvertToSolution(const CpSolverResponse& response,
const CpObjectiveProto& objective,
const RoutingModel& model, const ArcVarMap& arc_vars,
Assignment* solution) {
solution->Clear();
if (response.status() != CpSolverStatus::OPTIMAL &&
response.status() != CpSolverStatus::FEASIBLE) {
return false;
}
const int depot = GetDepotFromModel(model);
int vehicle = 0;
for (const auto& arc_var : arc_vars) {
if (response.solution(arc_var.second) != 0) {
const int tail = arc_var.first.tail;
const int head = arc_var.first.head;
if (head == depot) continue;
if (tail != depot) {
solution->Add(model.NextVar(tail))->SetValue(head);
} else {
solution->Add(model.NextVar(model.Start(vehicle)))->SetValue(head);
++vehicle;
}
}
}
// Close open routes.
for (int v = 0; v < model.vehicles(); ++v) {
int current = model.Start(v);
while (!model.IsEnd(current) &&
solution->Contains(model.NextVar(current))) {
current = solution->Value(model.NextVar(current));
}
if (model.IsEnd(current)) continue;
solution->Add(model.NextVar(current))->SetValue(model.End(v));
}
ConvertObjectiveToSolution(response, objective, model, solution);
return true;
}
// Adds dimensions to a CpModelProto for heterogeneous fleet. Adds path
// cumul constraints and cumul bounds.
void AddGeneralizedDimensions(
const RoutingModel& model, const ArcVarMap& arc_vars,
absl::Span<const absl::flat_hash_map<int, int>> vehicle_performs_node,
absl::Span<const absl::flat_hash_map<int, int>> vehicle_class_performs_arc,
CpModelProto* cp_model) {
const int num_cp_nodes = model.Nexts().size() + model.vehicles() + 1;
for (const RoutingDimension* dimension : model.GetDimensions()) {
// Initialize cumuls.
std::vector<int> cumuls(num_cp_nodes, -1);
for (int cp_node = 1; cp_node < num_cp_nodes; ++cp_node) {
const int node = cp_node - 1;
int64_t cumul_min = dimension->cumuls()[node]->Min();
int64_t cumul_max = dimension->cumuls()[node]->Max();
if (model.IsStart(node) || model.IsEnd(node)) {
const int vehicle = model.VehicleIndex(node);
cumul_max =
std::min(cumul_max, dimension->vehicle_capacities()[vehicle]);
}
cumuls[cp_node] = AddVariable(cp_model, cumul_min, cumul_max);
AddSoftCumulBounds(dimension, node, cumuls[cp_node], cumul_min, cumul_max,
cp_model);
}
// Constrain cumuls with vehicle capacities.
for (int vehicle = 0; vehicle < model.vehicles(); vehicle++) {
for (int cp_node = 1; cp_node < num_cp_nodes; cp_node++) {
if (!vehicle_performs_node[vehicle].contains(cp_node)) continue;
const int64_t vehicle_capacity =
dimension->vehicle_capacities()[vehicle];
AddLinearConstraint(cp_model, std::numeric_limits<int64_t>::min(),
vehicle_capacity, {{cumuls[cp_node], 1}},
{vehicle_performs_node[vehicle].at(cp_node)});
}
}
for (auto vehicle_class = RoutingVehicleClassIndex(0);
vehicle_class < model.GetVehicleClassesCount(); vehicle_class++) {
std::vector<int> slack(num_cp_nodes, -1);
const int64_t slack_cost = CapAdd(
dimension->GetSpanCostCoefficientForVehicleClass(vehicle_class),
dimension->GetSlackCostCoefficientForVehicleClass(vehicle_class));
for (const auto [arc, arc_var] : arc_vars) {
const auto [cp_tail, cp_head] = arc;
if (cp_tail == cp_head || cp_tail == 0 || cp_head == 0) continue;
if (!vehicle_class_performs_arc[vehicle_class.value()].contains(
arc_var)) {
continue;
}
// Create slack variable and add span cost to the objective.
if (slack[cp_tail] == -1) {
const int64_t slack_max =
cp_tail - 1 < dimension->slacks().size()
? dimension->slacks()[cp_tail - 1]->Max()
: 0;
slack[cp_tail] = AddVariable(cp_model, 0, slack_max);
if (slack_max > 0 && slack_cost > 0) {
cp_model->mutable_objective()->add_vars(slack[cp_tail]);
cp_model->mutable_objective()->add_coeffs(slack_cost);
}
}
const int64_t transit = dimension->class_transit_evaluator(
vehicle_class)(cp_tail - 1, cp_head - 1);
// vehicle_class_performs_arc[vehicle][arc_var] = 1 ->
// cumuls[cp_head] - cumuls[cp_tail] - slack[cp_tail] = transit
AddLinearConstraint(
cp_model, transit, transit,
{{cumuls[cp_head], 1}, {cumuls[cp_tail], -1}, {slack[cp_tail], -1}},
{vehicle_class_performs_arc[vehicle_class.value()].at(arc_var)});
}
}
// Constrain cumuls with span limits.
for (int vehicle = 0; vehicle < model.vehicles(); vehicle++) {
const int64_t span_limit =
dimension->vehicle_span_upper_bounds()[vehicle];
if (span_limit == std::numeric_limits<int64_t>::max()) continue;
int cp_start = model.Start(vehicle) + 1;
int cp_end = model.End(vehicle) + 1;
AddLinearConstraint(cp_model, std::numeric_limits<int64_t>::min(),
span_limit,
{{cumuls[cp_end], 1}, {cumuls[cp_start], -1}});
}
// Set soft span upper bound costs.
if (dimension->HasSoftSpanUpperBounds()) {
for (int vehicle = 0; vehicle < model.vehicles(); vehicle++) {
const auto [bound, cost] =
dimension->GetSoftSpanUpperBoundForVehicle(vehicle);
const int cp_start = model.Start(vehicle) + 1;
const int cp_end = model.End(vehicle) + 1;
const int extra =
AddVariable(cp_model, 0,
std::min(dimension->cumuls()[model.End(vehicle)]->Max(),
dimension->vehicle_capacities()[vehicle]));
// -inf <= cumuls[cp_end] - cumuls[cp_start] - extra <= bound
AddLinearConstraint(
cp_model, std::numeric_limits<int64_t>::min(), bound,
{{cumuls[cp_end], 1}, {cumuls[cp_start], -1}, {extra, -1}});
// Add extra * cost to objective.
cp_model->mutable_objective()->add_vars(extra);
cp_model->mutable_objective()->add_coeffs(cost);
}
}
}
}
std::vector<int> CreateGeneralizedRanks(const RoutingModel& model,
const ArcVarMap& arc_vars,
const std::vector<int>& is_unperformed,
CpModelProto* cp_model) {
const int depot = 0;
const int num_cp_nodes = model.Nexts().size() + model.vehicles() + 1;
// Maximum length of a single route (excluding the depot & vehicle end nodes).
const int max_rank = num_cp_nodes - 2 * model.vehicles();
std::vector<int> ranks(num_cp_nodes, -1);
ranks[depot] = AddVariable(cp_model, 0, 0);
for (int cp_node = 1; cp_node < num_cp_nodes; cp_node++) {
if (model.IsEnd(cp_node - 1)) continue;
ranks[cp_node] = AddVariable(cp_model, 0, max_rank);
// For unperformed nodes rank is 0.
AddLinearConstraint(cp_model, 0, 0, {{ranks[cp_node], 1}},
{is_unperformed[cp_node]});
}
for (const auto [arc, arc_var] : arc_vars) {
const auto [cp_tail, cp_head] = arc;
if (cp_head == 0 || model.IsEnd(cp_head - 1)) continue;
if (cp_tail == cp_head || cp_head == depot) continue;
// arc[tail][head] -> ranks[head] == ranks[tail] + 1.
AddLinearConstraint(cp_model, 1, 1,
{{ranks[cp_head], 1}, {ranks[cp_tail], -1}}, {arc_var});
}
return ranks;
}
void AddGeneralizedPickupDeliveryConstraints(
const RoutingModel& model, const ArcVarMap& arc_vars,
const std::vector<absl::flat_hash_map<int, int>>& vehicle_performs_node,
const std::vector<int>& is_unperformed, CpModelProto* cp_model) {
if (model.GetPickupAndDeliveryPairs().empty()) return;
const std::vector<int> ranks =
CreateGeneralizedRanks(model, arc_vars, is_unperformed, cp_model);
for (const auto& [pickups, deliveries] : model.GetPickupAndDeliveryPairs()) {
for (const int delivery : deliveries) {
const int cp_delivery = delivery + 1;
for (int vehicle = 0; vehicle < model.vehicles(); vehicle++) {
const Arc vehicle_start_delivery_arc = {
static_cast<int>(model.Start(vehicle) + 1), cp_delivery};
if (arc_vars.contains(vehicle_start_delivery_arc)) {
// Forbid vehicle_start -> delivery arc.
AddLinearConstraint(cp_model, 0, 0,
{{arc_vars.at(vehicle_start_delivery_arc), 1}});
}
}
for (const int pickup : pickups) {
const int cp_pickup = pickup + 1;
const Arc delivery_pickup_arc = {cp_delivery, cp_pickup};
if (arc_vars.contains(delivery_pickup_arc)) {
// Forbid delivery -> pickup arc.
AddLinearConstraint(cp_model, 0, 0,
{{arc_vars.at(delivery_pickup_arc), 1}});
}
DCHECK_GE(is_unperformed[cp_delivery], 0);
DCHECK_GE(is_unperformed[cp_pickup], 0);
// A negative index i refers to NOT the literal at index -i - 1.
// -i - 1 ~ NOT i, if value of i in [0, 1] (boolean).
const int delivery_performed = -is_unperformed[cp_delivery] - 1;
const int pickup_performed = -is_unperformed[cp_pickup] - 1;
// The same vehicle performs pickup and delivery.
for (int vehicle = 0; vehicle < model.vehicles(); vehicle++) {
// delivery_performed & pickup_performed ->
// vehicle_performs_node[vehicle][cp_delivery] -
// vehicle_performs_node[vehicle][cp_pickup] = 0
AddLinearConstraint(
cp_model, 0, 0,
{{vehicle_performs_node[vehicle].at(cp_delivery), 1},
{vehicle_performs_node[vehicle].at(cp_pickup), -1}},
{delivery_performed, pickup_performed});
}
}
}
std::vector<std::pair<int, double>> ranks_difference;
// -SUM(pickup)ranks[pickup].
for (const int pickup : pickups) {
const int cp_pickup = pickup + 1;
ranks_difference.push_back({ranks[cp_pickup], -1});
}
// SUM(delivery)ranks[delivery].
for (const int delivery : deliveries) {
const int cp_delivery = delivery + 1;
ranks_difference.push_back({ranks[cp_delivery], 1});
}
// SUM(delivery)ranks[delivery] - SUM(pickup)ranks[pickup] >= 1
AddLinearConstraint(cp_model, 1, std::numeric_limits<int64_t>::max(),
ranks_difference);
}
}
// Converts a RoutingModel to CpModelProto for models with multiple
// vehicles. The node 0 is depot. All nodes in CpModel have index increased
// by 1 in comparison to the RoutingModel. Each start node has only 1
// incoming arc (from depot), each end node has only 1 outgoing arc (to
// depot). The mapping from CPModelProto arcs to their corresponding arc
// variable is returned.
ArcVarMap PopulateGeneralizedRouteModelFromRoutingModel(
const RoutingModel& model, CpModelProto* cp_model) {
ArcVarMap arc_vars;
const int depot = 0;
const int num_nodes = model.Nexts().size();
const int num_cp_nodes = num_nodes + model.vehicles() + 1;
// vehicle_performs_node[vehicle][node] equals to 1 if the vehicle performs
// the node, and 0 otherwise.
std::vector<absl::flat_hash_map<int, int>> vehicle_performs_node(
model.vehicles());
// Connect vehicles start and end nodes to depot.
for (int vehicle = 0; vehicle < model.vehicles(); vehicle++) {
const int cp_start = model.Start(vehicle) + 1;
const Arc start_arc = {depot, cp_start};
const int start_arc_var = AddVariable(cp_model, 1, 1);
DCHECK(!arc_vars.contains(start_arc));
arc_vars.insert({start_arc, start_arc_var});
const int cp_end = model.End(vehicle) + 1;
const Arc end_arc = {cp_end, depot};
const int end_arc_var = AddVariable(cp_model, 1, 1);
DCHECK(!arc_vars.contains(end_arc));
arc_vars.insert({end_arc, end_arc_var});
vehicle_performs_node[vehicle][cp_start] = start_arc_var;
vehicle_performs_node[vehicle][cp_end] = end_arc_var;
}
// is_unperformed[node] variable equals to 1 if visit is unperformed, and 0
// otherwise.
std::vector<int> is_unperformed(num_cp_nodes, -1);
// Initialize is_unperformed variables for nodes that must be performed.
for (int node = 0; node < num_nodes; node++) {
const int cp_node = node + 1;
// Forced active and nodes that are not involved in any disjunctions are
// always performed.
const std::vector<RoutingDisjunctionIndex>& disjunction_indices =
model.GetDisjunctionIndices(node);
if (disjunction_indices.empty() || model.ActiveVar(node)->Min() == 1) {
is_unperformed[cp_node] = AddVariable(cp_model, 0, 0);
continue;
}
// Check if the node is in a forced active disjunction.
for (RoutingDisjunctionIndex disjunction_index : disjunction_indices) {
const int num_nodes =
model.GetDisjunctionNodeIndices(disjunction_index).size();
const int64_t penalty = model.GetDisjunctionPenalty(disjunction_index);
const int64_t max_cardinality =
model.GetDisjunctionMaxCardinality(disjunction_index);
if (num_nodes == max_cardinality &&
(penalty < 0 || penalty == std::numeric_limits<int64_t>::max())) {
// Nodes in this disjunction are forced active.
is_unperformed[cp_node] = AddVariable(cp_model, 0, 0);
break;
}
}
}
// Add alternative visits. Create self-looped arc variables. Set penalty for
// not performing disjunctions.
for (RoutingDisjunctionIndex disjunction_index(0);
disjunction_index < model.GetNumberOfDisjunctions();
disjunction_index++) {
const std::vector<int64_t>& disjunction_indices =
model.GetDisjunctionNodeIndices(disjunction_index);
const int disjunction_size = disjunction_indices.size();
const int64_t penalty = model.GetDisjunctionPenalty(disjunction_index);
const int64_t max_cardinality =
model.GetDisjunctionMaxCardinality(disjunction_index);
// Case when disjunction involves only 1 node, the node is only present in
// this disjunction, and the node can be unperformed.
if (disjunction_size == 1 &&
model.GetDisjunctionIndices(disjunction_indices[0]).size() == 1 &&
is_unperformed[disjunction_indices[0] + 1] == -1) {
const int cp_node = disjunction_indices[0] + 1;
const Arc arc = {cp_node, cp_node};
DCHECK(!arc_vars.contains(arc));
is_unperformed[cp_node] = AddVariable(cp_model, 0, 1);
arc_vars.insert({arc, is_unperformed[cp_node]});
cp_model->mutable_objective()->add_vars(is_unperformed[cp_node]);
cp_model->mutable_objective()->add_coeffs(penalty);
continue;
}
// num_performed + SUM(node)is_unperformed[node] = disjunction_size
const int num_performed = AddVariable(cp_model, 0, max_cardinality);
std::vector<std::pair<int, double>> var_coeffs;
var_coeffs.push_back({num_performed, 1});
for (const int node : disjunction_indices) {
const int cp_node = node + 1;
// Node can be unperformed.
if (is_unperformed[cp_node] == -1) {
const Arc arc = {cp_node, cp_node};
DCHECK(!arc_vars.contains(arc));
is_unperformed[cp_node] = AddVariable(cp_model, 0, 1);
arc_vars.insert({arc, is_unperformed[cp_node]});
}
var_coeffs.push_back({is_unperformed[cp_node], 1});
}
AddLinearConstraint(cp_model, disjunction_size, disjunction_size,
var_coeffs);
// When penalty is negative or max int64_t (forced active), num_violated is
// 0.
if (penalty < 0 || penalty == std::numeric_limits<int64_t>::max()) {
AddLinearConstraint(cp_model, max_cardinality, max_cardinality,
{{num_performed, 1}});
continue;
}
// If number of active indices is less than max_cardinality, then for each
// violated index 'penalty' is paid.
const int num_violated = AddVariable(cp_model, 0, max_cardinality);
cp_model->mutable_objective()->add_vars(num_violated);
cp_model->mutable_objective()->add_coeffs(penalty);
// num_performed + num_violated = max_cardinality
AddLinearConstraint(cp_model, max_cardinality, max_cardinality,
{{num_performed, 1}, {num_violated, 1}});
}
// Create "arc" variables.
for (int tail = 0; tail < num_nodes; ++tail) {
const int cp_tail = tail + 1;
std::unique_ptr<IntVarIterator> iter(
model.NextVar(tail)->MakeDomainIterator(false));
for (int head : InitAndGetValues(iter.get())) {
const int cp_head = head + 1;
if (model.IsStart(head)) continue;
// Arcs for unperformed visits have already been created.
if (tail == head) continue;
// Direct arcs from start to end nodes should exist only if they are
// for the same vehicle.
if (model.IsStart(tail) && model.IsEnd(head) &&
model.VehicleIndex(tail) != model.VehicleIndex(head)) {
continue;
}
bool feasible = false;
for (int vehicle = 0; vehicle < model.vehicles(); vehicle++) {
if (model.GetArcCostForVehicle(tail, head, vehicle) !=
std::numeric_limits<int64_t>::max()) {
feasible = true;
break;
}
}
if (!feasible) continue;
const Arc arc = {cp_tail, cp_head};
DCHECK(!arc_vars.contains(arc));
const int arc_var = AddVariable(cp_model, 0, 1);
arc_vars.insert({arc, arc_var});
}
}
// Set literals for vehicle performing node.
for (int cp_node = 1; cp_node < num_cp_nodes; cp_node++) {
const int routing_index = cp_node - 1;
// For starts and ends nodes vehicle_performs_node variables already set.
if (model.IsStart(routing_index) || model.IsEnd(routing_index)) continue;
// Each node should be performed by 1 vehicle, or be unperformed.
// SUM(vehicle)(vehicle_performs_node[vehicle][cp_node]) + loop(cp_node) = 1
std::vector<std::pair<int, double>> var_coeffs;
for (int vehicle = 0; vehicle < model.vehicles(); vehicle++) {
vehicle_performs_node[vehicle][cp_node] =
model.VehicleVar(routing_index)->Contains(vehicle)
? AddVariable(cp_model, 0, 1)
: AddVariable(cp_model, 0, 0);
var_coeffs.push_back({vehicle_performs_node[vehicle][cp_node], 1});
}
var_coeffs.push_back({is_unperformed[cp_node], 1});
AddLinearConstraint(cp_model, 1, 1, var_coeffs);
}
const int num_vehicle_classes = model.GetVehicleClassesCount();
// vehicle_class_performs_node[vehicle_class][node] equals to 1 if the
// vehicle of vehicle_class performs the node, and 0 otherwise.
std::vector<absl::flat_hash_map<int, int>> vehicle_class_performs_node(
num_vehicle_classes);
for (int cp_node = 1; cp_node < num_cp_nodes; cp_node++) {
const int node = cp_node - 1;
for (int vehicle_class = 0; vehicle_class < num_vehicle_classes;
vehicle_class++) {
if (model.IsStart(node) || model.IsEnd(node)) {
const int vehicle = model.VehicleIndex(node);
vehicle_class_performs_node[vehicle_class][cp_node] =
vehicle_class ==
model.GetVehicleClassIndexOfVehicle(vehicle).value()
? AddVariable(cp_model, 1, 1)
: AddVariable(cp_model, 0, 0);
continue;
}
vehicle_class_performs_node[vehicle_class][cp_node] =
AddVariable(cp_model, 0, 1);
std::vector<std::pair<int, double>> var_coeffs;
for (int vehicle = 0; vehicle < model.vehicles(); vehicle++) {
if (model.GetVehicleClassIndexOfVehicle(vehicle).value() ==
vehicle_class) {
var_coeffs.push_back({vehicle_performs_node[vehicle][cp_node], 1});
// vehicle_performs_node -> vehicle_class_performs_node
AddLinearConstraint(
cp_model, 1, 1,
{{vehicle_class_performs_node[vehicle_class][cp_node], 1}},
{vehicle_performs_node[vehicle][cp_node]});
}
}
// vehicle_class_performs_node -> exactly one vehicle from this class
// performs node.
AddLinearConstraint(
cp_model, 1, 1, var_coeffs,
{vehicle_class_performs_node[vehicle_class][cp_node]});
}
}
// vehicle_class_performs_arc[vehicle_class][arc_var] equals to 1 if the
// vehicle of vehicle_class performs the arc, and 0 otherwise.
std::vector<absl::flat_hash_map<int, int>> vehicle_class_performs_arc(
num_vehicle_classes);
// Set "arc" costs.
for (const auto [arc, arc_var] : arc_vars) {
const auto [cp_tail, cp_head] = arc;
if (cp_tail == depot || cp_head == depot) continue;
const int tail = cp_tail - 1;
const int head = cp_head - 1;
// Costs for unperformed arcs have already been set.
if (tail == head) continue;
for (int vehicle = 0; vehicle < model.vehicles(); vehicle++) {
// The arc can't be performed by the vehicle when vehicle can't perform
// arc nodes.
if (!vehicle_performs_node[vehicle].contains(cp_tail) ||
!vehicle_performs_node[vehicle].contains(cp_head)) {
continue;
}
int64_t cost = model.GetArcCostForVehicle(tail, head, vehicle);
// Arcs with int64_t's max cost are infeasible.
if (cost == std::numeric_limits<int64_t>::max()) continue;
const int vehicle_class =
model.GetVehicleClassIndexOfVehicle(vehicle).value();
if (!vehicle_class_performs_arc[vehicle_class].contains(arc_var)) {
vehicle_class_performs_arc[vehicle_class][arc_var] =
AddVariable(cp_model, 0, 1);
// Create constraints to set vehicle_class_performs_arc.
// vehicle_class_performs_arc ->
// vehicle_class_performs_tail & vehicle_class_performs_head &
// arc_is_performed
ConstraintProto* ct = cp_model->add_constraints();
ct->add_enforcement_literal(
vehicle_class_performs_arc[vehicle_class][arc_var]);
BoolArgumentProto* bool_and = ct->mutable_bool_and();
bool_and->add_literals(
vehicle_class_performs_node[vehicle_class][cp_tail]);
bool_and->add_literals(
vehicle_class_performs_node[vehicle_class][cp_head]);
bool_and->add_literals(arc_var);
// Don't add arcs with zero cost to the objective.
if (cost != 0) {
cp_model->mutable_objective()->add_vars(
vehicle_class_performs_arc[vehicle_class][arc_var]);
cp_model->mutable_objective()->add_coeffs(cost);
}
}
// (arc_is_performed & vehicle_performs_tail) ->
// (vehicle_class_performs_arc & vehicle_performs_head)
ConstraintProto* ct_arc_tail = cp_model->add_constraints();
ct_arc_tail->add_enforcement_literal(arc_var);
ct_arc_tail->add_enforcement_literal(
vehicle_performs_node[vehicle][cp_tail]);
ct_arc_tail->mutable_bool_and()->add_literals(
vehicle_class_performs_arc[vehicle_class][arc_var]);
ct_arc_tail->mutable_bool_and()->add_literals(
vehicle_performs_node[vehicle][cp_head]);
// (arc_is_performed & vehicle_performs_head) ->
// (vehicle_class_performs_arc & vehicle_performs_tail)
ConstraintProto* ct_arc_head = cp_model->add_constraints();
ct_arc_head->add_enforcement_literal(arc_var);
ct_arc_head->add_enforcement_literal(
vehicle_performs_node[vehicle][cp_head]);
ct_arc_head->mutable_bool_and()->add_literals(
vehicle_class_performs_arc[vehicle_class][arc_var]);
ct_arc_head->mutable_bool_and()->add_literals(
vehicle_performs_node[vehicle][cp_tail]);
}
}
AddGeneralizedPickupDeliveryConstraints(
model, arc_vars, vehicle_performs_node, is_unperformed, cp_model);
AddGeneralizedDimensions(model, arc_vars, vehicle_performs_node,
vehicle_class_performs_arc, cp_model);
// Create Routes constraint, ensuring circuits from and to the depot.
RoutesConstraintProto* routes_ct =
cp_model->add_constraints()->mutable_routes();
for (const auto [arc, arc_var] : arc_vars) {
const int tail = arc.tail;
const int head = arc.head;
routes_ct->add_tails(tail);
routes_ct->add_heads(head);
routes_ct->add_literals(arc_var);
}
// Add demands and capacities to improve the LP relaxation and cuts. These
// are based on the first "unary" dimension in the model if it exists.
// TODO(user): We might want to try to get demand lower bounds from
// non-unary dimensions if no unary exist.
const RoutingDimension* primary_dimension = nullptr;
for (const RoutingDimension* dimension : model.GetDimensions()) {
bool is_unary = true;
for (int vehicle = 0; vehicle < model.vehicles(); vehicle++) {
if (dimension->GetUnaryTransitEvaluator(vehicle) == nullptr) {
is_unary = false;
break;
}
}
if (is_unary) {
primary_dimension = dimension;
break;
}
}
if (primary_dimension != nullptr) {
for (int cp_node = 0; cp_node < num_cp_nodes; ++cp_node) {
int64_t min_transit = std::numeric_limits<int64_t>::max();
if (cp_node != 0 && !model.IsEnd(cp_node - 1)) {
for (int vehicle = 0; vehicle < model.vehicles(); vehicle++) {