java: Fix RoutingModel::add[Matrix|Vector]Dimension()

This commit is contained in:
Mizux Seiha
2021-02-16 16:05:20 +01:00
parent 9fa90eb8c1
commit 6747f7dfb3
2 changed files with 169 additions and 17 deletions

View File

@@ -21,7 +21,9 @@ import com.google.ortools.constraintsolver.RoutingIndexManager;
import com.google.ortools.constraintsolver.RoutingModel;
import com.google.ortools.constraintsolver.RoutingSearchParameters;
import com.google.ortools.constraintsolver.main;
import com.google.ortools.constraintsolver.IntBoolPair;
import java.util.logging.Logger;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
@@ -29,6 +31,41 @@ import org.junit.jupiter.params.provider.ValueSource;
public class RoutingSolverTest {
private static final Logger logger = Logger.getLogger(RoutingSolverTest.class.getName());
@Test
public void testRoutingTransitMatrix() {
Loader.loadNativeLibraries();
logger.info("testRoutingTransitMatrix...");
// Create Routing Index Manager
RoutingIndexManager manager =
new RoutingIndexManager(5 /*location*/, 1 /*vehicle*/, 0 /*depot*/);
// Create Routing Model.
RoutingModel routing = new RoutingModel(manager);
// Define cost of each arc.
int transitCallbackIndex;
long[][] matrix = {
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
};
transitCallbackIndex = routing.registerTransitMatrix(matrix);
routing.setArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// Setting first solution heuristic.
RoutingSearchParameters searchParameters =
main.defaultRoutingSearchParameters()
.toBuilder()
.setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
.build();
// Solve the problem.
Assignment solution = routing.solveWithParameters(searchParameters);
if (null == solution)
throw new AssertionError("null == solution");
if (5 != solution.objectiveValue())
throw new AssertionError("5 != objective");
logger.info("testRoutingTransitMatrix...DONE");
}
@ParameterizedTest
@ValueSource(booleans = {false, true})
public void testRoutingTransitCallback(boolean enableGC) {
@@ -64,10 +101,78 @@ public class RoutingSolverTest {
if (null == solution)
throw new AssertionError("null == solution");
if (8 != solution.objectiveValue())
throw new AssertionError("5 != objective");
throw new AssertionError("8 != objective");
logger.info("testRoutingTransitCallback (enable gc:" + enableGC + ")...DONE");
}
@Test
public void testRoutingMatrixDimension() {
Loader.loadNativeLibraries();
logger.info("testRoutingMatrixDimension...");
// Create Routing Index Manager
RoutingIndexManager manager =
new RoutingIndexManager(5 /*location*/, 1 /*vehicle*/, 0 /*depot*/);
// Create Routing Model.
RoutingModel routing = new RoutingModel(manager);
// Define cost of each arc.
int transitCallbackIndex;
long[][] matrix = {
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
{1, 1, 1, 1, 1},
};
IntBoolPair result = routing.addMatrixDimension(
matrix,
/*capacity=*/10,
/*fix_start_cumul_to_zero=*/true,
"Dimension");
routing.setArcCostEvaluatorOfAllVehicles(result.getFirst());
// Setting first solution heuristic.
RoutingSearchParameters searchParameters =
main.defaultRoutingSearchParameters()
.toBuilder()
.setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
.build();
// Solve the problem.
Assignment solution = routing.solveWithParameters(searchParameters);
if (null == solution)
throw new AssertionError("null == solution");
if (5 != solution.objectiveValue())
throw new AssertionError("5 != objective");
logger.info("testRoutingMatrixDimension...DONE");
}
@Test
public void testRoutingUnaryTransitVector() {
Loader.loadNativeLibraries();
logger.info("testRoutingUnaryTransitVector...");
// Create Routing Index Manager
RoutingIndexManager manager =
new RoutingIndexManager(5 /*location*/, 1 /*vehicle*/, 0 /*depot*/);
// Create Routing Model.
RoutingModel routing = new RoutingModel(manager);
// Define cost of each arc.
int transitCallbackIndex;
long[] vector = {1, 1, 1, 1, 1};
transitCallbackIndex = routing.registerUnaryTransitVector(vector);
routing.setArcCostEvaluatorOfAllVehicles(transitCallbackIndex);
// Setting first solution heuristic.
RoutingSearchParameters searchParameters =
main.defaultRoutingSearchParameters()
.toBuilder()
.setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
.build();
// Solve the problem.
Assignment solution = routing.solveWithParameters(searchParameters);
if (null == solution)
throw new AssertionError("null == solution");
if (5 != solution.objectiveValue())
throw new AssertionError("5 != objective");
logger.info("testRoutingUnaryTransitVector...DONE");
}
@ParameterizedTest
@ValueSource(booleans = {false, true})
public void testRoutingUnaryTransitCallback(boolean enableGC) {
@@ -102,7 +207,40 @@ public class RoutingSolverTest {
if (null == solution)
throw new AssertionError("null == solution");
if (10 != solution.objectiveValue())
throw new AssertionError("5 != objective");
throw new AssertionError("10 != objective");
logger.info("testRoutingUnaryTransitCallback (enable gc:" + enableGC + ")...DONE");
}
@Test
public void testRoutingVectorDimension() {
Loader.loadNativeLibraries();
logger.info("testRoutingVectorDimension...");
// Create Routing Index Manager
RoutingIndexManager manager =
new RoutingIndexManager(5 /*location*/, 1 /*vehicle*/, 0 /*depot*/);
// Create Routing Model.
RoutingModel routing = new RoutingModel(manager);
// Define cost of each arc.
int transitCallbackIndex;
long[] vector = {1, 1, 1, 1, 1};
IntBoolPair result = routing.addVectorDimension(
vector,
/*capacity=*/10,
/*fix_start_cumul_to_zero=*/true,
"Dimension");
routing.setArcCostEvaluatorOfAllVehicles(result.getFirst());
// Setting first solution heuristic.
RoutingSearchParameters searchParameters =
main.defaultRoutingSearchParameters()
.toBuilder()
.setFirstSolutionStrategy(FirstSolutionStrategy.Value.PATH_CHEAPEST_ARC)
.build();
// Solve the problem.
Assignment solution = routing.solveWithParameters(searchParameters);
if (null == solution)
throw new AssertionError("null == solution");
if (5 != solution.objectiveValue())
throw new AssertionError("5 != objective");
logger.info("testRoutingMatrixDimension...DONE");
}
}