java: Fix tests when CBC not available
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user