Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
ortools-backend: add all_different aggregate constraint
Browse files Browse the repository at this point in the history
Signed-off-by: Lalith Suresh <[email protected]>
  • Loading branch information
lalithsuresh committed Oct 30, 2020
1 parent f7fb6a6 commit 6b8988a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
6 changes: 6 additions & 0 deletions dcm/src/main/java/com/vmware/dcm/backend/ortools/Ops.java
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,12 @@ public <T> boolean allEqual(final List<T> array) {
return true;
}


public <T> void allDifferent(final List<IntVar> array) {
final IntVar[] intVars = array.toArray(new IntVar[0]);
model.addAllDifferent(intVars);
}

public IntVar toConst(final boolean expr) {
return expr ? trueVar : falseVar;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1316,10 +1316,12 @@ protected String visitMonoidFunction(final MonoidFunction node, final Translatio
case ALL_EQUAL:
function = "allEqual";
break;
case ALL_DIFFERENT:
context.currentScope().addBody(statement("o.allDifferent($L)", listOfProcessedItem));
return apply("model.newConstant(1)", context);
case INCREASING:
context.currentScope().addBody(statement("o.increasing($L)", listOfProcessedItem));
return apply("model.newConstant(1)", context);
case ALL_DIFFERENT:
default:
throw new UnsupportedOperationException("Unsupported aggregate function " + node.getFunction());
}
Expand Down
30 changes: 30 additions & 0 deletions dcm/src/test/java/com/vmware/dcm/ModelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Result;
import org.jooq.impl.DSL;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -22,6 +23,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.jooq.impl.DSL.using;
Expand Down Expand Up @@ -109,6 +111,34 @@ public void nullTest(final SolverConfig solver) {
assertEquals(2, fetch.get(3).get("CONTROLLABLE__C3"));
}

@Test
public void allDifferentTest() {
// Create an in-memory database and get a JOOQ connection to it
final DSLContext conn = DSL.using("jdbc:h2:mem:");

// All different
conn.execute("create table t1(id integer, controllable__var integer)");

final String all_different = "create view constraint_all_different as " +
"select * from t1 check all_different(controllable__var) = true";

final String domain = "create view constraint_domain as " +
"select * from t1 check controllable__var <= 10 and controllable__var >= 1";

conn.execute("insert into t1 values (1, null)");
conn.execute("insert into t1 values (2, null)");
conn.execute("insert into t1 values (3, null)");

// Create a DCM model using the database connection and the above constraint
final Model model = Model.build(conn, List.of(all_different, domain));
model.updateData();

final Result<? extends Record> results = model.solve("T1");
final Set<Integer> controllableVars = results.stream().map(e -> e.get("CONTROLLABLE__VAR", Integer.class))
.collect(Collectors.toSet());
assertEquals(3, controllableVars.size());
}

@ParameterizedTest
@MethodSource("solvers")
public void solveModelWithUpdateTest(final SolverConfig solver) {
Expand Down

0 comments on commit 6b8988a

Please sign in to comment.