Skip to content

Commit a334c0f

Browse files
committed
routing: test AllowedVehicles in Java and .Net
1 parent d55eeda commit a334c0f

4 files changed

Lines changed: 71 additions & 2 deletions

File tree

ortools/javatests/com/google/ortools/routing/RoutingModelTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import static com.google.common.truth.Truth.assertThat;
1717
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertFalse;
1819
import static org.junit.jupiter.api.Assertions.assertNotNull;
1920
import static org.junit.jupiter.api.Assertions.assertTrue;
2021

@@ -325,6 +326,37 @@ public void testRoutingModel_addDisjunction() {
325326
assertEquals(8, solution.objectiveValue());
326327
}
327328

329+
@Test
330+
public void testAllowedVehicles() {
331+
final IndexManager manager = new IndexManager(2, 2, 0);
332+
assertNotNull(manager);
333+
final Model model = new Model(manager);
334+
assertNotNull(model);
335+
336+
// out of range vehicle index is allowed.
337+
model.setAllowedVehiclesForIndex(new int[] {13}, 1);
338+
assertFalse(model.isVehicleAllowedForIndex(0, 1));
339+
assertFalse(model.isVehicleAllowedForIndex(1, 1));
340+
assertTrue(model.isVehicleAllowedForIndex(13, 1));
341+
342+
// empty array means any vehicles are allowed.
343+
model.setAllowedVehiclesForIndex(new int[] {}, 1);
344+
assertTrue(model.isVehicleAllowedForIndex(0, 1));
345+
assertTrue(model.isVehicleAllowedForIndex(1, 1));
346+
assertTrue(model.isVehicleAllowedForIndex(42, 1));
347+
348+
// only allow first vehicle for node 1.
349+
model.setAllowedVehiclesForIndex(new int[] {0}, 1);
350+
assertTrue(model.isVehicleAllowedForIndex(0, 1));
351+
assertFalse(model.isVehicleAllowedForIndex(1, 1));
352+
assertFalse(model.isVehicleAllowedForIndex(2, 1));
353+
354+
final Assignment solution = model.solve(null);
355+
assertNotNull(solution);
356+
assertEquals(1, solution.value(model.nextVar(model.start(0))));
357+
assertEquals(2, model.getVehicleClassesCount());
358+
}
359+
328360
@Test
329361
public void testRoutingModel_addConstantDimension() {
330362
final IndexManager manager = new IndexManager(10, 1, 0);

ortools/routing/csharp/RoutingModelTests.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,38 @@ long SolveObjective(bool addRouteConstraint)
260260
Assert.Equal(8, baselineObjective);
261261
Assert.Equal(18, constrainedObjective);
262262
}
263+
264+
[Fact]
265+
public void TestAllowedVehicles()
266+
{
267+
IndexManager manager = new IndexManager(2, 2, 0);
268+
Assert.NotNull(manager);
269+
Model model = new Model(manager);
270+
Assert.NotNull(model);
271+
272+
// out of range vehicle index is allowed.
273+
model.SetAllowedVehiclesForIndex(new int[] { 13 }, 1);
274+
Assert.False(model.IsVehicleAllowedForIndex(0, 1));
275+
Assert.False(model.IsVehicleAllowedForIndex(1, 1));
276+
Assert.True(model.IsVehicleAllowedForIndex(13, 1));
277+
278+
// empty array means any vehicles are allowed.
279+
model.SetAllowedVehiclesForIndex(new int[] { }, 1);
280+
Assert.True(model.IsVehicleAllowedForIndex(0, 1));
281+
Assert.True(model.IsVehicleAllowedForIndex(1, 1));
282+
Assert.True(model.IsVehicleAllowedForIndex(42, 1));
283+
284+
// only allow first vehicle for node 1.
285+
model.SetAllowedVehiclesForIndex(new int[] { 0 }, 1);
286+
Assert.True(model.IsVehicleAllowedForIndex(0, 1));
287+
Assert.False(model.IsVehicleAllowedForIndex(1, 1));
288+
Assert.False(model.IsVehicleAllowedForIndex(2, 1));
289+
290+
Assignment solution = model.Solve(null);
291+
Assert.NotNull(solution);
292+
Assert.Equal(1, solution.Value(model.NextVar(model.Start(0))));
293+
Assert.Equal(2, model.GetVehicleClassesCount());
294+
}
263295
}
264296

265297
// TODO(user): Add tests for Routing[Cost|Vehicle|Resource]ClassIndex

ortools/routing/csharp/routing.i

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,6 @@ using Domain = Google.OrTools.Util.Domain;
449449
%unignore Model::RegisterUnaryTransitVector;
450450
%unignore Model::ResourceVar;
451451
%unignore Model::RestoreAssignment;
452-
%unignore Model::SetAllowedVehiclesForIndex;
453452
%unignore Model::SetAllowedVehiclesForIndex(const std::vector<int>&, int64_t);
454453
%unignore Model::SetAmortizedCostFactorsOfAllVehicles;
455454
%unignore Model::SetAmortizedCostFactorsOfVehicle;

ortools/routing/java/routing.swig

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,13 @@ import java.util.function.LongUnaryOperator;
347347

348348
%rename (restoreAssignment) Model::RestoreAssignment;
349349
%rename (routesToAssignment) Model::RoutesToAssignment;
350-
%rename (setAllowedVehiclesForIndex) Model::SetAllowedVehiclesForIndex;
350+
%rename (setAllowedVehiclesForIndex) Model::SetAllowedVehiclesForIndex(const std::vector<int>&, int64_t);
351+
%extend Model {
352+
void SetAllowedVehiclesForIndex(const std::vector<int>& vehicles,
353+
int64_t index) {
354+
$self->SetAllowedVehiclesForIndex(absl::Span<const int>(vehicles), index);
355+
}
356+
}
351357
%rename (setAmortizedCostFactorsOfAllVehicles) Model::SetAmortizedCostFactorsOfAllVehicles;
352358
%rename (setAmortizedCostFactorsOfVehicle) Model::SetAmortizedCostFactorsOfVehicle;
353359
%rename (setArcCostEvaluatorOfAllVehicles) Model::SetArcCostEvaluatorOfAllVehicles;

0 commit comments

Comments
 (0)