From b217dfc7e7bd7beb73c8cc23ad82383309ceb697 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Thu, 18 Jul 2019 15:21:56 +0200 Subject: Implement Coin-OR CBC polyhedron saturation operator --- .../mit/inf/dslreasoner/ilp/cbc/CbcException.java | 30 +++++++++ .../bme/mit/inf/dslreasoner/ilp/cbc/CbcResult.java | 54 ++++++++++++++++ .../bme/mit/inf/dslreasoner/ilp/cbc/CbcSolver.java | 71 ++++++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 Solvers/ILP-Solver/hu.bme.mit.inf.dslreasoner.ilp.cbc/src/hu/bme/mit/inf/dslreasoner/ilp/cbc/CbcException.java create mode 100644 Solvers/ILP-Solver/hu.bme.mit.inf.dslreasoner.ilp.cbc/src/hu/bme/mit/inf/dslreasoner/ilp/cbc/CbcResult.java create mode 100644 Solvers/ILP-Solver/hu.bme.mit.inf.dslreasoner.ilp.cbc/src/hu/bme/mit/inf/dslreasoner/ilp/cbc/CbcSolver.java (limited to 'Solvers/ILP-Solver/hu.bme.mit.inf.dslreasoner.ilp.cbc/src') diff --git a/Solvers/ILP-Solver/hu.bme.mit.inf.dslreasoner.ilp.cbc/src/hu/bme/mit/inf/dslreasoner/ilp/cbc/CbcException.java b/Solvers/ILP-Solver/hu.bme.mit.inf.dslreasoner.ilp.cbc/src/hu/bme/mit/inf/dslreasoner/ilp/cbc/CbcException.java new file mode 100644 index 00000000..26846958 --- /dev/null +++ b/Solvers/ILP-Solver/hu.bme.mit.inf.dslreasoner.ilp.cbc/src/hu/bme/mit/inf/dslreasoner/ilp/cbc/CbcException.java @@ -0,0 +1,30 @@ +package hu.bme.mit.inf.dslreasoner.ilp.cbc; + +public class CbcException extends RuntimeException { + + /** + * + */ + private static final long serialVersionUID = 2691773509078511887L; + + public CbcException() { + super(); + } + + public CbcException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + + public CbcException(String message, Throwable cause) { + super(message, cause); + } + + public CbcException(String message) { + super(message); + } + + public CbcException(Throwable cause) { + super(cause); + } + +} diff --git a/Solvers/ILP-Solver/hu.bme.mit.inf.dslreasoner.ilp.cbc/src/hu/bme/mit/inf/dslreasoner/ilp/cbc/CbcResult.java b/Solvers/ILP-Solver/hu.bme.mit.inf.dslreasoner.ilp.cbc/src/hu/bme/mit/inf/dslreasoner/ilp/cbc/CbcResult.java new file mode 100644 index 00000000..dae3a447 --- /dev/null +++ b/Solvers/ILP-Solver/hu.bme.mit.inf.dslreasoner.ilp.cbc/src/hu/bme/mit/inf/dslreasoner/ilp/cbc/CbcResult.java @@ -0,0 +1,54 @@ +package hu.bme.mit.inf.dslreasoner.ilp.cbc; + +public abstract class CbcResult { + public static final CbcResult SOLUTION_UNBOUNDED = new CbcResult() { + }; + + public static final CbcResult UNSAT = new CbcResult() { + }; + + public static final CbcResult ABANDONED = new CbcResult() { + }; + + public static final CbcResult TIMEOUT = new CbcResult() { + }; + + private CbcResult() { + } + + public static class SolutionBounded extends CbcResult { + public final double value; + + public SolutionBounded(double value) { + this.value = value; + } + + public double getValue() { + return value; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(value); + result = prime * result + (int) (temp ^ (temp >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + SolutionBounded other = (SolutionBounded) obj; + if (Double.doubleToLongBits(value) != Double.doubleToLongBits(other.value)) + return false; + return true; + } + } +} diff --git a/Solvers/ILP-Solver/hu.bme.mit.inf.dslreasoner.ilp.cbc/src/hu/bme/mit/inf/dslreasoner/ilp/cbc/CbcSolver.java b/Solvers/ILP-Solver/hu.bme.mit.inf.dslreasoner.ilp.cbc/src/hu/bme/mit/inf/dslreasoner/ilp/cbc/CbcSolver.java new file mode 100644 index 00000000..39b9d537 --- /dev/null +++ b/Solvers/ILP-Solver/hu.bme.mit.inf.dslreasoner.ilp.cbc/src/hu/bme/mit/inf/dslreasoner/ilp/cbc/CbcSolver.java @@ -0,0 +1,71 @@ +package hu.bme.mit.inf.dslreasoner.ilp.cbc; + +public class CbcSolver { + private static int CBC_SOLUTION_BOUNDED = 0; + private static int CBC_SOLUTION_UNBOUNDED = 1; + private static int CBC_UNSAT = 2; + private static int CBC_ABANDONED = 3; + private static int CBC_TIMEOUT = 4; + private static int CBC_ERROR = 5; + + private static boolean nativesLoaded = false; + + private CbcSolver() { + throw new IllegalStateException("This is a static utility class and should not be instantiated directly."); + } + + public static CbcResult solve(double[] columnLowerBounds, double[] columnUpperBounds, int[] rowStarts, + int[] columnIndices, double[] entries, double[] rowLowerBounds, double[] rowUpperBounds, + double[] objective, double timeoutSeconds, boolean silent) { + loadNatives(); + validate(columnLowerBounds, columnUpperBounds, rowStarts, columnIndices, entries, rowLowerBounds, + rowUpperBounds, objective); + double[] output = new double[1]; + int result = solveIlpProblem(columnLowerBounds, columnUpperBounds, rowStarts, columnIndices, entries, + rowLowerBounds, rowUpperBounds, objective, output, timeoutSeconds, silent); + if (result == CBC_SOLUTION_BOUNDED) { + return new CbcResult.SolutionBounded(output[0]); + } else if (result == CBC_SOLUTION_UNBOUNDED) { + return CbcResult.SOLUTION_UNBOUNDED; + } else if (result == CBC_UNSAT) { + return CbcResult.UNSAT; + } else if (result == CBC_ABANDONED) { + return CbcResult.ABANDONED; + } else if (result == CBC_TIMEOUT) { + return CbcResult.TIMEOUT; + } else if (result == CBC_ERROR) { + throw new CbcException("Solver signalled error, but no exception was thrown"); + } else { + throw new CbcException("Unknown return value: " + result); + } + } + + private static void loadNatives() { + if (!nativesLoaded) { + synchronized (CbcSolver.class) { + System.loadLibrary("viatracbc"); + nativesLoaded = true; + } + } + } + + private static void validate(double[] columnLowerBounds, double[] columnUpperBounds, int[] rowStarts, + int[] columnIndices, double[] entries, double[] rowLowerBounds, double[] rowUpperBounds, + double[] objective) { + int numColumns = columnLowerBounds.length; + if (columnUpperBounds.length != numColumns) { + throw new CbcException("Lengths of columnLowerBounds and columnUpperBounds must match"); + } + if (objective.length != numColumns) { + throw new CbcException("Lengths of columnLowerBounds and objective must match"); + } + int numRows = rowLowerBounds.length; + if (rowUpperBounds.length != numRows) { + throw new CbcException("Lengths of rowLowerBounds and rowUpperBounds must match"); + } + } + + private static native int solveIlpProblem(double[] columnLowerBounds, double[] columnUpperBounds, int[] rowStarts, + int[] columnIndices, double[] entries, double[] rowLowerBounds, double[] rowUpperBounds, double[] objective, + double[] output, double timeoutSeconds, boolean silent); +} -- cgit v1.2.3-70-g09d2