diff --git a/ortools/java/com/google/ortools/Loader.java b/ortools/java/com/google/ortools/Loader.java index 0c2dbadd76..a1243d0839 100644 --- a/ortools/java/com/google/ortools/Loader.java +++ b/ortools/java/com/google/ortools/Loader.java @@ -26,14 +26,16 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; +import java.util.Arrays; import java.util.Collections; +import java.util.List; import java.util.Objects; -/** Load native libraries needed for using ortools-java.*/ +/** Load native libraries needed for using ortools-java. */ public class Loader { private static final String RESOURCE_PATH = "ortools-" + Platform.RESOURCE_PREFIX + "/"; - /** Try to locate the native libraries directory.*/ + /** Try to locate the native libraries directory. */ private static URI getNativeResourceURI() throws IOException { ClassLoader loader = Loader.class.getClassLoader(); URL resourceURL = loader.getResource(RESOURCE_PATH); @@ -56,6 +58,7 @@ public class Loader { /** * Extract native resources in a temp directory. + * * @param resourceURI Native resource location. * @return The directory path containing all extracted libraries. */ @@ -98,34 +101,38 @@ public class Loader { return tempPath; } - /** Unpack and Load the native libraries needed for using ortools-java.*/ + /** Unpack and Load the native libraries needed for using ortools-java. */ private static boolean loaded = false; public static synchronized void loadNativeLibraries() { - if (!loaded) { - try { - // prints the name of the Operating System - // System.out.println("OS: " + System.getProperty("os.name")); - // System.out.println("Library: " + System.mapLibraryName("jniortools")); - - System.loadLibrary("jniortools"); - loaded = true; - return; - } catch (UnsatisfiedLinkError exception) { - // Do nothing. - } - try { - URI resourceURI = getNativeResourceURI(); - Path tempPath = unpackNativeResources(resourceURI); - // Load the native library - System.load(tempPath.resolve(RESOURCE_PATH) - .resolve(System.mapLibraryName("jniortools")) - .toAbsolutePath() - .toString()); - loaded = true; - } catch (IOException e) { - throw new RuntimeException(e); - } + // prints the name of the Operating System + // System.out.println("OS: " + System.getProperty("os.name")); + if (loaded) { + return; + } + try { + // System.out.println("System.loadLibrary(\"jniortools\")"); + System.loadLibrary("jniortools"); + loaded = true; + return; + } catch (UnsatisfiedLinkError e) { + // Do nothing. + // System.out.println("Can't System.loadLibrary(jniortools)"); + } + try { + URI resourceURI = getNativeResourceURI(); + Path tempPath = unpackNativeResources(resourceURI); + // Load the native library + // System.out.println("System.load(" + System.mapLibraryName("jniortools") + ")"); + System.load(tempPath.resolve(RESOURCE_PATH) + .resolve(System.mapLibraryName("jniortools")) + .toAbsolutePath() + .toString()); + loaded = true; + return; + } catch (IOException | UnsatisfiedLinkError e) { + // System.out.println("Can't System.load(jniortools)"); + throw new RuntimeException(e); } } } diff --git a/ortools/java/com/google/ortools/sat/CpModel.java b/ortools/java/com/google/ortools/sat/CpModel.java index 72f86014e9..d740325277 100644 --- a/ortools/java/com/google/ortools/sat/CpModel.java +++ b/ortools/java/com/google/ortools/sat/CpModel.java @@ -943,6 +943,17 @@ public final class CpModel { modelBuilder.getSolutionHintBuilder().addValues(value); } + /** Adds hinting to a literal */ + public void addHint(Literal lit, boolean value) { + if (isPositive(lit)) { + modelBuilder.getSolutionHintBuilder().addVars(lit.getIndex()); + modelBuilder.getSolutionHintBuilder().addValues(value ? 1 : 0); + } else { + modelBuilder.getSolutionHintBuilder().addVars(negated(lit.getIndex())); + modelBuilder.getSolutionHintBuilder().addValues(value ? 0 : 1); + } + } + /** Remove all solution hints */ public void clearHints() { modelBuilder.clearSolutionHint(); @@ -1098,6 +1109,10 @@ public final class CpModel { return -index - 1; } + public boolean isPositive(Literal lit) { + return lit.getIndex() >= 0; + } + /** Returns the model builder. */ public CpModelProto.Builder getBuilder() { return modelBuilder; diff --git a/ortools/java/com/google/ortools/sat/DoubleLinearExpr.java b/ortools/java/com/google/ortools/sat/DoubleLinearExpr.java index 4850a25957..9da8333109 100644 --- a/ortools/java/com/google/ortools/sat/DoubleLinearExpr.java +++ b/ortools/java/com/google/ortools/sat/DoubleLinearExpr.java @@ -20,39 +20,39 @@ public class DoubleLinearExpr { private double offset; /** Creates a sum expression. */ - static DoubleLinearExpr sum(IntVar[] variables) { + public static DoubleLinearExpr sum(IntVar[] variables) { return sumWithOffset(variables, 0.0); } /** Creates a sum expression. */ - static DoubleLinearExpr sum(Literal[] literals) { + public static DoubleLinearExpr sum(Literal[] literals) { // We need the scalar product for the negative coefficient of negated Boolean variables. return sumWithOffset(literals, 0.0); } /** Creates a sum expression with a double offset. */ - static DoubleLinearExpr sumWithOffset(IntVar[] variables, double offset) { + public static DoubleLinearExpr sumWithOffset(IntVar[] variables, double offset) { return new DoubleLinearExpr(variables, offset); } /** Creates a sum expression with a double offset. */ - static DoubleLinearExpr sumWithOffset(Literal[] literals, double offset) { + public static DoubleLinearExpr sumWithOffset(Literal[] literals, double offset) { // We need the scalar product for the negative coefficient of negated Boolean variables. return new DoubleLinearExpr(literals, offset); } /** Creates a scalar product. */ - static DoubleLinearExpr weightedSum(IntVar[] variables, double[] coefficients) { + public static DoubleLinearExpr weightedSum(IntVar[] variables, double[] coefficients) { return weightedSumWithOffset(variables, coefficients, 0.0); } /** Creates a scalar product. */ - static DoubleLinearExpr weightedSum(Literal[] literals, double[] coefficients) { + public static DoubleLinearExpr weightedSum(Literal[] literals, double[] coefficients) { return weightedSumWithOffset(literals, coefficients, 0.0); } /** Creates a scalar product. */ - static DoubleLinearExpr weightedSumWithOffset( + public static DoubleLinearExpr weightedSumWithOffset( IntVar[] variables, double[] coefficients, double offset) { if (variables.length != coefficients.length) { throw new CpModel.MismatchedArrayLengths( @@ -62,7 +62,7 @@ public class DoubleLinearExpr { } /** Creates a scalar product with a double offset. */ - static DoubleLinearExpr weightedSumWithOffset( + public static DoubleLinearExpr weightedSumWithOffset( Literal[] literals, double[] coefficients, double offset) { if (literals.length != coefficients.length) { throw new CpModel.MismatchedArrayLengths( @@ -72,27 +72,27 @@ public class DoubleLinearExpr { } /** Creates a linear term (var * coefficient). */ - static DoubleLinearExpr term(IntVar variable, double coefficient) { + public static DoubleLinearExpr term(IntVar variable, double coefficient) { return new DoubleLinearExpr(variable, coefficient, 0.0); } /** Creates a linear term (lit * coefficient). */ - static DoubleLinearExpr term(Literal lit, double coefficient) { + public static DoubleLinearExpr term(Literal lit, double coefficient) { return new DoubleLinearExpr(lit, coefficient, 0.0); } /** Creates an affine expression (var * coefficient + offset). */ - static DoubleLinearExpr affine(IntVar variable, double coefficient, double offset) { + public static DoubleLinearExpr affine(IntVar variable, double coefficient, double offset) { return new DoubleLinearExpr(variable, coefficient, offset); } /** Creates an affine expression (lit * coefficient + offset). */ - static DoubleLinearExpr affine(Literal lit, double coefficient, double offset) { + public static DoubleLinearExpr affine(Literal lit, double coefficient, double offset) { return new DoubleLinearExpr(lit, coefficient, offset); } - /** Creates an constant expression. */ - static DoubleLinearExpr constant(double value) { + /** Creates a constant expression. */ + public static DoubleLinearExpr constant(double value) { return new DoubleLinearExpr(new IntVar[0], value); }