Skip to content

Commit a46a2d5

Browse files
committed
routing(python): fix SetAllowedVehiclesForIndex (#4982)
1 parent cc2b905 commit a46a2d5

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

ortools/constraint_solver/python/pywraprouting_test.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,33 @@ def testMatrixDimensionVRP(self):
564564
manager.IndexToNode(index)
565565
]
566566

567+
def testAllowedVehicles(self):
568+
manager = pywrapcp.RoutingIndexManager(
569+
2, 2, 0 # num_nodes # num_vehicles # depot
570+
)
571+
self.assertIsNotNone(manager)
572+
model = pywrapcp.RoutingModel(manager)
573+
self.assertIsNotNone(model)
574+
# out of range vehicle index is allowed.
575+
model.SetAllowedVehiclesForIndex(vehicles=[13], index=1)
576+
self.assertFalse(model.IsVehicleAllowedForIndex(vehicle=0, index=1))
577+
self.assertFalse(model.IsVehicleAllowedForIndex(1, 1))
578+
self.assertTrue(model.IsVehicleAllowedForIndex(13, 1))
579+
# empty list means any vehicles are allowed.
580+
model.SetAllowedVehiclesForIndex([], 1)
581+
self.assertTrue(model.IsVehicleAllowedForIndex(0, 1))
582+
self.assertTrue(model.IsVehicleAllowedForIndex(1, 1))
583+
self.assertTrue(model.IsVehicleAllowedForIndex(42, 1))
584+
# only allow first vehicle for node 1.
585+
model.SetAllowedVehiclesForIndex([0], 1)
586+
self.assertTrue(model.IsVehicleAllowedForIndex(0, 1))
587+
self.assertFalse(model.IsVehicleAllowedForIndex(1, 1))
588+
self.assertFalse(model.IsVehicleAllowedForIndex(2, 1))
589+
assignment = model.Solve()
590+
self.assertIsNotNone(assignment)
591+
self.assertEqual(1, assignment.Value(model.NextVar(model.Start(0))))
592+
self.assertEqual(model.GetVehicleClassesCount(), 2)
593+
567594
def testDisjunctionTSP(self):
568595
# Create routing model
569596
manager = pywrapcp.RoutingIndexManager(10, 1, 0)

ortools/constraint_solver/python/routing.swig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ DEFINE_INDEX_TYPE_TYPEDEF(
7171
%ignore operations_research::RoutingModel::AddDimensionDependentDimensionWithVehicleCapacity;
7272
%ignore operations_research::RoutingModel::AddResourceGroup;
7373
%ignore operations_research::RoutingModel::GetResourceGroups;
74+
%ignore operations_research::RoutingModel::SetAllowedVehiclesForIndex(absl::Span<const int>, int64_t);
75+
%extend operations_research::RoutingModel {
76+
void SetAllowedVehiclesForIndex(
77+
const std::vector<int>& vehicles,
78+
int64_t index) {
79+
$self->SetAllowedVehiclesForIndex(absl::Span<const int>(vehicles), index);
80+
}
81+
}
7482

7583
PY_PROTO_TYPEMAP(ortools.constraint_solver.routing_parameters_pb2,
7684
RoutingModelParameters,

0 commit comments

Comments
 (0)