diff options
Diffstat (limited to 'Solvers/ILP-Solver/hu.bme.mit.inf.dslreasoner.ilp.cbc/src/hu/bme')
3 files changed, 155 insertions, 0 deletions
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 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.ilp.cbc; | ||
2 | |||
3 | public class CbcException extends RuntimeException { | ||
4 | |||
5 | /** | ||
6 | * | ||
7 | */ | ||
8 | private static final long serialVersionUID = 2691773509078511887L; | ||
9 | |||
10 | public CbcException() { | ||
11 | super(); | ||
12 | } | ||
13 | |||
14 | public CbcException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { | ||
15 | super(message, cause, enableSuppression, writableStackTrace); | ||
16 | } | ||
17 | |||
18 | public CbcException(String message, Throwable cause) { | ||
19 | super(message, cause); | ||
20 | } | ||
21 | |||
22 | public CbcException(String message) { | ||
23 | super(message); | ||
24 | } | ||
25 | |||
26 | public CbcException(Throwable cause) { | ||
27 | super(cause); | ||
28 | } | ||
29 | |||
30 | } | ||
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 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.ilp.cbc; | ||
2 | |||
3 | public abstract class CbcResult { | ||
4 | public static final CbcResult SOLUTION_UNBOUNDED = new CbcResult() { | ||
5 | }; | ||
6 | |||
7 | public static final CbcResult UNSAT = new CbcResult() { | ||
8 | }; | ||
9 | |||
10 | public static final CbcResult ABANDONED = new CbcResult() { | ||
11 | }; | ||
12 | |||
13 | public static final CbcResult TIMEOUT = new CbcResult() { | ||
14 | }; | ||
15 | |||
16 | private CbcResult() { | ||
17 | } | ||
18 | |||
19 | public static class SolutionBounded extends CbcResult { | ||
20 | public final double value; | ||
21 | |||
22 | public SolutionBounded(double value) { | ||
23 | this.value = value; | ||
24 | } | ||
25 | |||
26 | public double getValue() { | ||
27 | return value; | ||
28 | } | ||
29 | |||
30 | @Override | ||
31 | public int hashCode() { | ||
32 | final int prime = 31; | ||
33 | int result = 1; | ||
34 | long temp; | ||
35 | temp = Double.doubleToLongBits(value); | ||
36 | result = prime * result + (int) (temp ^ (temp >>> 32)); | ||
37 | return result; | ||
38 | } | ||
39 | |||
40 | @Override | ||
41 | public boolean equals(Object obj) { | ||
42 | if (this == obj) | ||
43 | return true; | ||
44 | if (obj == null) | ||
45 | return false; | ||
46 | if (getClass() != obj.getClass()) | ||
47 | return false; | ||
48 | SolutionBounded other = (SolutionBounded) obj; | ||
49 | if (Double.doubleToLongBits(value) != Double.doubleToLongBits(other.value)) | ||
50 | return false; | ||
51 | return true; | ||
52 | } | ||
53 | } | ||
54 | } | ||
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..085d4448 --- /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 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.ilp.cbc; | ||
2 | |||
3 | public class CbcSolver { | ||
4 | private static int CBC_SOLUTION_BOUNDED = 0; | ||
5 | private static int CBC_SOLUTION_UNBOUNDED = 1; | ||
6 | private static int CBC_UNSAT = 2; | ||
7 | private static int CBC_ABANDONED = 3; | ||
8 | private static int CBC_TIMEOUT = 4; | ||
9 | private static int CBC_ERROR = 5; | ||
10 | |||
11 | private static boolean nativesLoaded = false; | ||
12 | |||
13 | private CbcSolver() { | ||
14 | throw new IllegalStateException("This is a static utility class and should not be instantiated directly."); | ||
15 | } | ||
16 | |||
17 | public static CbcResult solve(double[] columnLowerBounds, double[] columnUpperBounds, int[] rowStarts, | ||
18 | int[] columnIndices, double[] entries, double[] rowLowerBounds, double[] rowUpperBounds, double[] objective, | ||
19 | boolean lpRelaxation, double timeoutSeconds, boolean silent) { | ||
20 | loadNatives(); | ||
21 | validate(columnLowerBounds, columnUpperBounds, rowStarts, columnIndices, entries, rowLowerBounds, | ||
22 | rowUpperBounds, objective); | ||
23 | double[] output = new double[1]; | ||
24 | int result = solveIlpProblem(columnLowerBounds, columnUpperBounds, rowStarts, columnIndices, entries, | ||
25 | rowLowerBounds, rowUpperBounds, objective, output, lpRelaxation, timeoutSeconds, silent); | ||
26 | if (result == CBC_SOLUTION_BOUNDED) { | ||
27 | return new CbcResult.SolutionBounded(output[0]); | ||
28 | } else if (result == CBC_SOLUTION_UNBOUNDED) { | ||
29 | return CbcResult.SOLUTION_UNBOUNDED; | ||
30 | } else if (result == CBC_UNSAT) { | ||
31 | return CbcResult.UNSAT; | ||
32 | } else if (result == CBC_ABANDONED) { | ||
33 | return CbcResult.ABANDONED; | ||
34 | } else if (result == CBC_TIMEOUT) { | ||
35 | return CbcResult.TIMEOUT; | ||
36 | } else if (result == CBC_ERROR) { | ||
37 | throw new CbcException("Solver signalled error, but no exception was thrown"); | ||
38 | } else { | ||
39 | throw new CbcException("Unknown return value: " + result); | ||
40 | } | ||
41 | } | ||
42 | |||
43 | private static void loadNatives() { | ||
44 | if (!nativesLoaded) { | ||
45 | synchronized (CbcSolver.class) { | ||
46 | System.loadLibrary("viatracbc"); | ||
47 | nativesLoaded = true; | ||
48 | } | ||
49 | } | ||
50 | } | ||
51 | |||
52 | private static void validate(double[] columnLowerBounds, double[] columnUpperBounds, int[] rowStarts, | ||
53 | int[] columnIndices, double[] entries, double[] rowLowerBounds, double[] rowUpperBounds, | ||
54 | double[] objective) { | ||
55 | int numColumns = columnLowerBounds.length; | ||
56 | if (columnUpperBounds.length != numColumns) { | ||
57 | throw new CbcException("Lengths of columnLowerBounds and columnUpperBounds must match"); | ||
58 | } | ||
59 | if (objective.length != numColumns) { | ||
60 | throw new CbcException("Lengths of columnLowerBounds and objective must match"); | ||
61 | } | ||
62 | int numRows = rowLowerBounds.length; | ||
63 | if (rowUpperBounds.length != numRows) { | ||
64 | throw new CbcException("Lengths of rowLowerBounds and rowUpperBounds must match"); | ||
65 | } | ||
66 | } | ||
67 | |||
68 | private static native int solveIlpProblem(double[] columnLowerBounds, double[] columnUpperBounds, int[] rowStarts, | ||
69 | int[] columnIndices, double[] entries, double[] rowLowerBounds, double[] rowUpperBounds, double[] objective, | ||
70 | double[] output, boolean lpRelaxation, double timeoutSeconds, boolean silent); | ||
71 | } | ||