From 1528945dbc77cf23d23583af9d98a14fb1a5a9f5 Mon Sep 17 00:00:00 2001 From: Mizux Seiha Date: Wed, 1 Dec 2021 13:20:14 +0100 Subject: [PATCH] java: Fix tests when CBC not available --- examples/contrib/Issue173.java | 7 +++++-- examples/contrib/KnapsackMIP.java | 15 +++++---------- examples/contrib/MultiThreadTest.java | 20 +++++++++++++------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/examples/contrib/Issue173.java b/examples/contrib/Issue173.java index a86489ab23..f6799b3d87 100644 --- a/examples/contrib/Issue173.java +++ b/examples/contrib/Issue173.java @@ -14,8 +14,11 @@ public class Issue173 { } private static void solveLP() { - MPSolver solver = - new MPSolver("test", MPSolver.OptimizationProblemType.CBC_MIXED_INTEGER_PROGRAMMING); + MPSolver solver = MPSolver.createSolver("CBC"); + if (solver == null) { + System.out.println("Could not create solver CBC"); + return; + } MPVariable x = solver.makeNumVar(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, "x"); final MPObjective objective = solver.objective(); diff --git a/examples/contrib/KnapsackMIP.java b/examples/contrib/KnapsackMIP.java index 4ff5df047d..740118a399 100644 --- a/examples/contrib/KnapsackMIP.java +++ b/examples/contrib/KnapsackMIP.java @@ -26,17 +26,12 @@ import com.google.ortools.Loader; import com.google.ortools.linearsolver.*; public class KnapsackMIP { - private static MPSolver createSolver(String solverType) { - try { - return new MPSolver("MIPDiet", MPSolver.OptimizationProblemType.valueOf(solverType)); - } catch (java.lang.IllegalArgumentException e) { - System.err.println("Bad solver type: " + e); - return null; - } - } - private static void solve(String solverType) { - MPSolver solver = createSolver(solverType); + MPSolver solver = MPSolver.createSolver(solverType); + if (solver == null) { + System.out.println("Could not create solver"); + return; + } /** variables */ int itemCount = 12; diff --git a/examples/contrib/MultiThreadTest.java b/examples/contrib/MultiThreadTest.java index 6a62fcc997..e5a2e74ce6 100644 --- a/examples/contrib/MultiThreadTest.java +++ b/examples/contrib/MultiThreadTest.java @@ -63,8 +63,11 @@ public class MultiThreadTest { } private static MPSolver makeProblem() { - MPSolver solver = new MPSolver( - UUID.randomUUID().toString(), OptimizationProblemType.CBC_MIXED_INTEGER_PROGRAMMING); + MPSolver solver = MPSolver.createSolver("CBC"); + if (solver == null) { + System.out.println("Could not create solver CBC"); + return solver; + } double infinity = MPSolver.infinity(); @@ -97,11 +100,14 @@ public class MultiThreadTest { @Override public ResultStatus call() throws Exception { MPSolver solver = makeProblem(); - statusSolver = solver.solve(); - - // Check that the problem has an optimal solution. - if (MPSolver.ResultStatus.OPTIMAL.equals(statusSolver)) { - throw new RuntimeException("Non OPTIMAL status after solve."); + if (solver == null) { + statusSolver = MPSolver.ResultStatus.NOT_SOLVED; + } else { + statusSolver = solver.solve(); + // Check that the problem has an optimal solution. + if (MPSolver.ResultStatus.OPTIMAL.equals(statusSolver)) { + throw new RuntimeException("Non OPTIMAL status after solve."); + } } return statusSolver; }