[CP-SAT] change element and table protos, API to support affine expressions

This commit is contained in:
Laurent Perron
2024-10-23 05:54:30 +02:00
parent c7b05c942c
commit 488b43c37c
27 changed files with 788 additions and 389 deletions

View File

@@ -39,6 +39,7 @@ public final class CpModelTest {
@Test
public void testCpModelNewIntVar() throws Exception {
System.out.println("testCpModelNewIntVar");
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newIntVar(0, 10, "x");
@@ -64,6 +65,7 @@ public final class CpModelTest {
@Test
public void testCpModelNewIntervalVar() throws Exception {
System.out.println("testCpModelNewIntervalVar");
final CpModel model = new CpModel();
assertNotNull(model);
final int horizon = 100;
@@ -91,6 +93,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddBoolOr() throws Exception {
System.out.println("testCpModelAddBoolOr");
final CpModel model = new CpModel();
assertNotNull(model);
final BoolVar x = model.newBoolVar("x");
@@ -105,6 +108,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddAtLeastOne() throws Exception {
System.out.println("testCpModelAddAtLeastOne");
final CpModel model = new CpModel();
assertNotNull(model);
final BoolVar x = model.newBoolVar("x");
@@ -119,6 +123,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddAtMostOne() throws Exception {
System.out.println("testCpModelAddAtMostOne");
final CpModel model = new CpModel();
assertNotNull(model);
final BoolVar x = model.newBoolVar("x");
@@ -133,6 +138,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddExactlyOne() throws Exception {
System.out.println("testCpModelAddExactlyOne");
final CpModel model = new CpModel();
assertNotNull(model);
final BoolVar x = model.newBoolVar("x");
@@ -147,6 +153,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddBoolAnd() throws Exception {
System.out.println("testCpModelAddBoolAnd");
final CpModel model = new CpModel();
assertNotNull(model);
final BoolVar x = model.newBoolVar("x");
@@ -161,6 +168,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddBoolXor() throws Exception {
System.out.println("testCpModelAddBoolXor");
final CpModel model = new CpModel();
assertNotNull(model);
final BoolVar x = model.newBoolVar("x");
@@ -175,6 +183,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddImplication() throws Exception {
System.out.println("testCpModelAddImplication");
final CpModel model = new CpModel();
assertNotNull(model);
final BoolVar x = model.newBoolVar("x");
@@ -188,6 +197,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddLinear() throws Exception {
System.out.println("testCpModelAddLinear");
final CpModel model = new CpModel();
assertNotNull(model);
final BoolVar x = model.newBoolVar("x");
@@ -216,6 +226,7 @@ public final class CpModelTest {
@Test
public void testLinearExprAddEqualityLiteral() {
System.out.println("testLinearExprAddEqualityLiteral");
final CpModel model = new CpModel();
assertNotNull(model);
final Literal x = model.newBoolVar("x");
@@ -226,6 +237,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddElement() throws Exception {
System.out.println("testCpModelAddElement");
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newIntVar(0, 10, "x");
@@ -254,6 +266,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddLinearArgumentElement() throws Exception {
System.out.println("testCpModelAddLinearArgumentElement");
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newIntVar(0, 10, "x");
@@ -282,6 +295,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddExprElement() throws Exception {
System.out.println("testCpModelExceptionVisibility");
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newIntVar(0, 10, "x");
@@ -315,6 +329,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddConstantElement() throws Exception {
System.out.println("testCpModelAddConstantElement");
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar z = model.newIntVar(0, 10, "x");
@@ -345,6 +360,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddMinEquality() throws Exception {
System.out.println("testCpModelAddMinEquality");
final CpModel model = new CpModel();
assertNotNull(model);
final BoolVar x = model.newBoolVar("x");
@@ -369,6 +385,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddMaxEquality() throws Exception {
System.out.println("testCpModelAddMaxEquality");
final CpModel model = new CpModel();
assertNotNull(model);
final BoolVar x = model.newBoolVar("x");
@@ -393,6 +410,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddMinExprEquality() throws Exception {
System.out.println("testCpModelAddMinExprEquality");
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
@@ -418,6 +436,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddAbsEquality() throws Exception {
System.out.println("testCpModelAddAbsEquality");
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newBoolVar("x");
@@ -444,6 +463,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddCircuit() throws Exception {
System.out.println("testCpModelAddCircuit");
final CpModel model = new CpModel();
assertNotNull(model);
@@ -464,6 +484,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddMultipleCircuit() throws Exception {
System.out.println("testCpModelAddMultipleCircuit");
final CpModel model = new CpModel();
assertNotNull(model);
@@ -484,6 +505,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddAutomaton() throws Exception {
System.out.println("testCpModelAddAutomaton");
final CpModel model = new CpModel();
assertNotNull(model);
@@ -506,11 +528,13 @@ public final class CpModelTest {
assertThat(model.model().getConstraints(0).getAutomaton().getTransitionLabelCount())
.isEqualTo(3);
assertThat(model.model().getConstraints(0).getAutomaton().getStartingState()).isEqualTo(0);
assertThat(model.model().getConstraints(0).getAutomaton().getFinalStatesCount()).isEqualTo(2);
}
@Test
public void testCpModelAddNoOverlap() throws Exception {
System.out.println("testCpModelAddNoOverlap");
final CpModel model = new CpModel();
assertNotNull(model);
final int horizon = 100;
@@ -533,6 +557,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddCumulative() throws Exception {
System.out.println("testCpModelAddCumulative");
final CpModel model = new CpModel();
assertNotNull(model);
final int horizon = 100;
@@ -568,6 +593,7 @@ public final class CpModelTest {
@Test
public void testCpModelAddNoOverlap2D() throws Exception {
System.out.println("testCpModelAddNoOverlap2D");
final CpModel model = new CpModel();
assertNotNull(model);
final int horizon = 100;
@@ -587,12 +613,15 @@ public final class CpModelTest {
assertThat(model.model().getConstraints(0).hasInterval()).isTrue();
assertThat(model.model().getConstraints(1).hasInterval()).isTrue();
assertThat(model.model().getConstraints(2).hasNoOverlap2D()).isTrue();
assertThat(model.model().getConstraints(2).getNoOverlap2D().getXIntervalsCount()).isEqualTo(2);
assertThat(model.model().getConstraints(2).getNoOverlap2D().getYIntervalsCount()).isEqualTo(2);
}
@Test
public void testCpModelAddDecisionStrategy() throws Exception {
System.out.println("testCpModelAddDecisionStrategy");
final CpModel model = new CpModel();
assertNotNull(model);
@@ -608,18 +637,21 @@ public final class CpModelTest {
assertThat(model.model().getSearchStrategy(0).getExprsCount()).isEqualTo(3);
assertThat(model.model().getSearchStrategy(0).getExprs(0).getVarsCount()).isEqualTo(1);
assertThat(model.model().getSearchStrategy(0).getExprs(0).getCoeffsCount()).isEqualTo(1);
assertThat(model.model().getSearchStrategy(0).getExprs(0).getVars(0)).isEqualTo(x1.getIndex());
assertThat(model.model().getSearchStrategy(0).getExprs(0).getCoeffs(0)).isEqualTo(1);
assertThat(model.model().getSearchStrategy(0).getExprs(0).getOffset()).isEqualTo(0);
assertThat(model.model().getSearchStrategy(0).getExprs(1).getVarsCount()).isEqualTo(1);
assertThat(model.model().getSearchStrategy(0).getExprs(1).getCoeffsCount()).isEqualTo(1);
assertThat(model.model().getSearchStrategy(0).getExprs(1).getVars(0)).isEqualTo(x2.getIndex());
assertThat(model.model().getSearchStrategy(0).getExprs(1).getCoeffs(0)).isEqualTo(-1);
assertThat(model.model().getSearchStrategy(0).getExprs(1).getOffset()).isEqualTo(1);
assertThat(model.model().getSearchStrategy(0).getExprs(2).getVarsCount()).isEqualTo(1);
assertThat(model.model().getSearchStrategy(0).getExprs(2).getCoeffsCount()).isEqualTo(1);
assertThat(model.model().getSearchStrategy(0).getExprs(2).getVars(0)).isEqualTo(x3.getIndex());
assertThat(model.model().getSearchStrategy(0).getExprs(2).getCoeffs(0)).isEqualTo(1);
assertThat(model.model().getSearchStrategy(0).getExprs(2).getOffset()).isEqualTo(0);
@@ -627,6 +659,7 @@ public final class CpModelTest {
@Test
public void testCpModelModelStats() throws Exception {
System.out.println("testCpModelModelStats");
final CpModel model = new CpModel();
assertNotNull(model);
final BoolVar x = model.newBoolVar("x");
@@ -639,6 +672,7 @@ public final class CpModelTest {
@Test
public void testCpModelValidateOk() throws Exception {
System.out.println("testCpModelValidateOk");
final CpModel model = new CpModel();
assertNotNull(model);
final BoolVar x = model.newBoolVar("x");
@@ -651,6 +685,7 @@ public final class CpModelTest {
@Test
public void testCpModelValidateNotOk() throws Exception {
System.out.println("testCpModelValidateNotOk");
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newIntVar(0, 9223372036854775807L, "x");
@@ -664,6 +699,7 @@ public final class CpModelTest {
@Test
public void testCpModelExceptionVisibility() throws Exception {
System.out.println("testCpModelExceptionVisibility");
CpModel.MismatchedArrayLengths ex1 = new CpModel.MismatchedArrayLengths("test1", "ar1", "ar2");
CpModel.WrongLength ex2 = new CpModel.WrongLength("test2", "ar");
assertThat(ex1).hasMessageThat().isEqualTo("test1: ar1 and ar2 have mismatched lengths");
@@ -672,6 +708,7 @@ public final class CpModelTest {
@Test
public void testCpModelClearConstraint() throws Exception {
System.out.println("testCpModelClearConstraint");
final CpModel model = new CpModel();
assertNotNull(model);
final IntVar x = model.newIntVar(0, 92, "x");
@@ -707,6 +744,7 @@ public final class CpModelTest {
@Test
public void testCpModelMinimize() throws Exception {
System.out.println("testCpModelMinimize");
final CpModel model = new CpModel();
assertNotNull(model);
@@ -727,10 +765,12 @@ public final class CpModelTest {
assertThat(model.getBuilder().hasFloatingPointObjective()).isFalse();
model.minimize(DoubleLinearExpr.weightedSum(new IntVar[] {x1, x2}, new double[] {2.5, 3.5}));
assertThat(model.getBuilder().getFloatingPointObjectiveBuilder().getVarsCount()).isEqualTo(2);
assertThat(model.getBuilder().hasObjective()).isFalse();
model.minimize(DoubleLinearExpr.affine(x3, 1.4, 1.2));
assertThat(model.getBuilder().getFloatingPointObjectiveBuilder().getVarsCount()).isEqualTo(1);
assertThat(model.getBuilder().hasObjective()).isFalse();
@@ -743,10 +783,12 @@ public final class CpModelTest {
assertThat(model.getBuilder().hasFloatingPointObjective()).isFalse();
model.maximize(DoubleLinearExpr.weightedSum(new IntVar[] {x1, x2}, new double[] {2.5, 3.5}));
assertThat(model.getBuilder().getFloatingPointObjectiveBuilder().getVarsCount()).isEqualTo(2);
assertThat(model.getBuilder().hasObjective()).isFalse();
model.maximize(DoubleLinearExpr.affine(x3, 1.4, 1.2));
assertThat(model.getBuilder().getFloatingPointObjectiveBuilder().getVarsCount()).isEqualTo(1);
assertThat(model.getBuilder().hasObjective()).isFalse();
}
@@ -834,7 +876,7 @@ public final class CpModelTest {
@Test
public void testCrashEquality() {
System.out.println("testCrashInSolveWithAllowedAssignment");
System.out.println("testCrashEquality");
final CpModel model = new CpModel();
final IntVar[] entities = new IntVar[20];
@@ -877,7 +919,9 @@ public final class CpModelTest {
}
final CpSolver solver = new CpSolver();
solver.getParameters().setLogToStdout(true);
CpSolverStatus unused = solver.solve(model);
System.out.println(unused);
}
@Test