This commit is contained in:
Laurent Perron
2022-01-03 19:29:15 +01:00
parent 6144215236
commit de6ea0da5f
4 changed files with 101 additions and 43 deletions

View File

@@ -37,7 +37,7 @@ public final class CpModelTest {
final IntVar y = model.newIntVarFromDomain(Domain.fromValues(new long[] {0, 1, 2, 5}), "y");
final IntVar z =
model.newIntVarFromDomain(Domain.fromIntervals(new long[][] {{0, 2}, {5}}), "");
final IntVar t = model.newBoolVar("t");
final BoolVar t = model.newBoolVar("t");
final IntVar u = model.newConstant(5);
assertThat(x.getName()).isEqualTo("x");
@@ -85,9 +85,9 @@ public final class CpModelTest {
public void testCpModel_addBoolOr() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
final IntVar y = model.newBoolVar("y");
final IntVar z = model.newBoolVar("z");
final BoolVar x = model.newBoolVar("x");
final BoolVar y = model.newBoolVar("y");
final BoolVar z = model.newBoolVar("z");
model.addBoolOr(new Literal[] {x, y.not(), z});
assertThat(model.model().getConstraintsCount()).isEqualTo(1);
@@ -99,9 +99,9 @@ public final class CpModelTest {
public void testCpModel_addAtLeastOne() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
final IntVar y = model.newBoolVar("y");
final IntVar z = model.newBoolVar("z");
final BoolVar x = model.newBoolVar("x");
final BoolVar y = model.newBoolVar("y");
final BoolVar z = model.newBoolVar("z");
model.addAtLeastOne(new Literal[] {x, y.not(), z});
assertThat(model.model().getConstraintsCount()).isEqualTo(1);
@@ -113,9 +113,9 @@ public final class CpModelTest {
public void testCpModel_addAtMostOne() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
final IntVar y = model.newBoolVar("y");
final IntVar z = model.newBoolVar("z");
final BoolVar x = model.newBoolVar("x");
final BoolVar y = model.newBoolVar("y");
final BoolVar z = model.newBoolVar("z");
model.addAtMostOne(new Literal[] {x, y.not(), z});
assertThat(model.model().getConstraintsCount()).isEqualTo(1);
@@ -127,9 +127,9 @@ public final class CpModelTest {
public void testCpModel_addExactlyOne() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
final IntVar y = model.newBoolVar("y");
final IntVar z = model.newBoolVar("z");
final BoolVar x = model.newBoolVar("x");
final BoolVar y = model.newBoolVar("y");
final BoolVar z = model.newBoolVar("z");
model.addExactlyOne(new Literal[] {x, y.not(), z});
assertThat(model.model().getConstraintsCount()).isEqualTo(1);
@@ -141,9 +141,9 @@ public final class CpModelTest {
public void testCpModel_addBoolAnd() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
final IntVar y = model.newBoolVar("y");
final IntVar z = model.newBoolVar("z");
final BoolVar x = model.newBoolVar("x");
final BoolVar y = model.newBoolVar("y");
final BoolVar z = model.newBoolVar("z");
model.addBoolAnd(new Literal[] {x, y.not(), z});
assertThat(model.model().getConstraintsCount()).isEqualTo(1);
@@ -155,9 +155,9 @@ public final class CpModelTest {
public void testCpModel_addBoolXor() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
final IntVar y = model.newBoolVar("y");
final IntVar z = model.newBoolVar("z");
final BoolVar x = model.newBoolVar("x");
final BoolVar y = model.newBoolVar("y");
final BoolVar z = model.newBoolVar("z");
model.addBoolXor(new Literal[] {x, y.not(), z});
assertThat(model.model().getConstraintsCount()).isEqualTo(1);
@@ -169,8 +169,8 @@ public final class CpModelTest {
public void testCpModel_addImplication() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
final IntVar y = model.newBoolVar("y");
final BoolVar x = model.newBoolVar("x");
final BoolVar y = model.newBoolVar("y");
model.addImplication(x, y);
assertThat(model.model().getConstraintsCount()).isEqualTo(1);
@@ -182,22 +182,22 @@ public final class CpModelTest {
public void testCpModel_addLinear() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
final IntVar y = model.newBoolVar("y");
final BoolVar x = model.newBoolVar("x");
final BoolVar y = model.newBoolVar("y");
model.addEquality(LinearExpr.newBuilder().add(x).add(y).build(), 1);
assertThat(model.model().getConstraintsCount()).isEqualTo(1);
assertThat(model.model().getConstraints(0).hasLinear()).isTrue();
assertThat(model.model().getConstraints(0).getLinear().getVarsCount()).isEqualTo(2);
final IntVar b = model.newBoolVar("b");
final BoolVar b = model.newBoolVar("b");
model.addEquality(LinearExpr.newBuilder().add(x).add(y).build(), 2).onlyEnforceIf(b.not());
assertThat(model.model().getConstraintsCount()).isEqualTo(2);
assertThat(model.model().getConstraints(1).hasLinear()).isTrue();
assertThat(model.model().getConstraints(1).getEnforcementLiteralCount()).isEqualTo(1);
assertThat(model.model().getConstraints(1).getEnforcementLiteral(0)).isEqualTo(-3);
final IntVar c = model.newBoolVar("c");
final BoolVar c = model.newBoolVar("c");
model.addEquality(LinearExpr.newBuilder().add(x).add(y).build(), 3)
.onlyEnforceIf(new Literal[] {b, c});
assertThat(model.model().getConstraintsCount()).isEqualTo(3);
@@ -207,13 +207,23 @@ public final class CpModelTest {
assertThat(model.model().getConstraints(2).getEnforcementLiteral(1)).isEqualTo(3);
}
@Test
public void testLinearExpr_addEquality_literal() {
final CpModel model = new CpModel();
assertNotNull(model);
final Literal x = model.newBoolVar("x");
final Literal y = model.newBoolVar("y").not();
model.addEquality(x, y);
}
@Test
public void testCpModel_addMinEquality() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
final IntVar y = model.newBoolVar("y");
final IntVar t = model.newBoolVar("t");
final BoolVar x = model.newBoolVar("x");
final BoolVar y = model.newBoolVar("y");
final BoolVar t = model.newBoolVar("t");
model.addMinEquality(t, new IntVar[] {x, y});
assertThat(model.model().getConstraintsCount()).isEqualTo(1);
@@ -235,9 +245,9 @@ public final class CpModelTest {
public void testCpModel_addMaxEquality() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
final IntVar y = model.newBoolVar("y");
final IntVar t = model.newBoolVar("t");
final BoolVar x = model.newBoolVar("x");
final BoolVar y = model.newBoolVar("y");
final BoolVar t = model.newBoolVar("t");
model.addMaxEquality(t, new IntVar[] {x, y});
assertThat(model.model().getConstraintsCount()).isEqualTo(1);
@@ -451,8 +461,8 @@ public final class CpModelTest {
public void testCpModel_modelStats() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
final IntVar y = model.newBoolVar("y");
final BoolVar x = model.newBoolVar("x");
final BoolVar y = model.newBoolVar("y");
model.addImplication(x, y);
String stats = model.modelStats();
@@ -463,8 +473,8 @@ public final class CpModelTest {
public void testCpModel_validateOk() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
final IntVar y = model.newBoolVar("y");
final BoolVar x = model.newBoolVar("x");
final BoolVar y = model.newBoolVar("y");
model.addImplication(x, y);
String stats = model.validate();

View File

@@ -133,8 +133,8 @@ public final class CpSolverTest {
public void testCpSolver_booleanValue() throws Exception {
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
final IntVar y = model.newBoolVar("y");
final BoolVar x = model.newBoolVar("x");
final BoolVar y = model.newBoolVar("y");
model.addBoolOr(new Literal[] {x, y.not()});
// Creates a solver and solves the model.

View File

@@ -28,6 +28,52 @@ public final class LinearExprTest {
Loader.loadNativeLibraries();
}
@Test
public void testLinearExpr_add() {
final CpModel model = new CpModel();
assertNotNull(model);
final Domain domain = new Domain(0, 10);
final IntVar y = model.newIntVarFromDomain(domain, "y");
final LinearExpr expr = LinearExpr.newBuilder().add(y).build();
assertNotNull(expr);
assertEquals(1, expr.numElements());
assertEquals(y, expr.getVariable(0));
assertEquals(1, expr.getCoefficient(0));
assertEquals(0, expr.getOffset());
}
@Test
public void testLinearExpr_add_literal() {
final CpModel model = new CpModel();
assertNotNull(model);
final BoolVar y = model.newBoolVar("y");
final LinearExpr expr = LinearExpr.newBuilder().add(y).build();
assertNotNull(expr);
assertEquals(1, expr.numElements());
assertEquals(y, expr.getVariable(0));
assertEquals(1, expr.getCoefficient(0));
assertEquals(0, expr.getOffset());
}
@Test
public void testLinearExpr_add_negated_literal() {
final CpModel model = new CpModel();
assertNotNull(model);
final BoolVar y = model.newBoolVar("y");
final LinearExpr expr = LinearExpr.newBuilder().add(y.not()).build();
assertNotNull(expr);
assertEquals(1, expr.numElements());
assertEquals(y, expr.getVariable(0));
assertEquals(-1, expr.getCoefficient(0));
assertEquals(1, expr.getOffset());
}
@Test
public void testLinearExpr_term() {
final CpModel model = new CpModel();

View File

@@ -357,8 +357,9 @@ $(JAVA_ORTOOLS_JAR): \
$(SRC_DIR)/ortools/java/com/google/ortools/constraintsolver/LongTernaryPredicate.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/AffineExpression.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/AutomatonConstraint.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/BoolVar.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/CircuitConstraint.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/Constant.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/ConstantExpression.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/Constraint.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/CpModel.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/CpSolver.java \
@@ -374,9 +375,9 @@ $(JAVA_ORTOOLS_JAR): \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/NoOverlap2dConstraint.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/NotBooleanVariable.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/ReservoirConstraint.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/ScalProd.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/SumOfVariables.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/SumExpression.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/TableConstraint.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/WeightedSumExpression.java \
| $(CLASS_DIR)/com/google/ortools
"$(JAVAC_BIN)" -encoding UTF-8 -d $(CLASS_DIR) \
-cp $(LIB_DIR)$Sprotobuf.jar \
@@ -505,8 +506,9 @@ $(TEMP_JAVA_DIR)/$(JAVA_ORTOOLS_PROJECT)/timestamp: \
$(SRC_DIR)/ortools/java/com/google/ortools/constraintsolver/LongTernaryPredicate.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/AffineExpression.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/AutomatonConstraint.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/BoolVar.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/CircuitConstraint.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/Constant.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/ConstantExpression.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/Constraint.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/CpModel.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/CpSolver.java \
@@ -522,9 +524,9 @@ $(TEMP_JAVA_DIR)/$(JAVA_ORTOOLS_PROJECT)/timestamp: \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/NoOverlap2dConstraint.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/NotBooleanVariable.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/ReservoirConstraint.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/ScalProd.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/SumOfVariables.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/SumExpression.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/TableConstraint.java \
$(SRC_DIR)/ortools/java/com/google/ortools/sat/WeightedSumExpression.java \
$(TEMP_JAVA_DIR)/$(JAVA_ORTOOLS_PROJECT)/pom.xml
$(MKDIR_P) $(JAVA_PATH)$Sjava
ifeq ($(SYSTEM),unix)