java: Fix tests when CBC not available

This commit is contained in:
Mizux Seiha
2021-12-01 13:20:14 +01:00
parent d4a40bbd4c
commit 1528945dbc
3 changed files with 23 additions and 19 deletions

View File

@@ -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();

View File

@@ -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;

View File

@@ -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;
}