static void postTeamConstraints(IntSetVar E, int n, int[] nbMembers, IntSetVarArray memberSets) throws Failure { C.postConstraint(E.cardinality().eq(n)); for (int i = 0; i < nbMembers.length; i++) { C.postConstraint(E.intersectionWith(memberSets.get(i)).cardinality().ge(nbMembers[i])); } }
public void testIntersection() { int[] array1 = {1, 2, 3, 4, 5, 6, 7}; int[] array2 = {1, 2, 7, 9, 4}; Constrainer C = new Constrainer("test"); IntSetVar var2 = C.addIntSetVar(array1); IntSetVar var1 = C.addIntSetVar(array2); IntSetVar var3 = var2.intersectionWith(var1); try { C.postConstraint(var3.cardinality().eq(2)); IntSetVarArray array = new IntSetVarArray(C, 2); array.set(var1, 0); array.set(var2, 1); assertTrue(C.execute(new GoalIntSetGenerate(array))); assertTrue(intersectionCardinality(var2.requiredSet(), var1.requiredSet()) == 2); } catch (Failure f) { fail("test failed: " + f); } var1 = C.addIntSetVar(new int[] {1, 2, 3, 4, 5}); var2 = C.addIntSetVar(new int[] {4, 5, 6, 7, 8}); var3 = var2.intersectionWith(var1); try { IntSetVarArray array = new IntSetVarArray(C, 2); array.set(var1, 0); array.set(var2, 1); assertTrue( C.execute(new GoalFastMinimize(new GoalIntSetGenerate(array), var3.cardinality().neg()))); assertTrue(var1.value().contains(new Integer(4))); assertTrue(var1.value().contains(new Integer(5))); assertTrue(var2.value().contains(new Integer(4))); assertTrue(var2.value().contains(new Integer(5))); } catch (Failure f) { fail("test failed: " + f); } }
public void testEventPropagation() { class IntSetVarValueObserver extends Observer { int counter = 0; @Override public Object master() { return this; } @Override public int subscriberMask() { return IntSetEventConstants.VALUE; } @Override public void update(Subject var, EventOfInterest ev) throws Failure { // IntSetVar.IntSetEvent e = (IntSetVar.IntSetEvent)ev; System.out.println("value#" + counter + " obtained: " + ((IntSetVar) var).value()); counter++; } } IntSetVar var1 = C.addIntSetVar(new int[] {1, 2, 3, 4, 5, 6, 7}); var1.attachObserver(new IntSetVarValueObserver()); IntSetVar var2 = C.addIntSetVar(new int[] {5, 6, 7, 8, 9, 10, 11}); IntSetVar var3 = C.addIntSetVar(new int[] {28, 78, 23, 1, 4, 7}); IntSetVarArray array = new IntSetVarArray(C, 1); array.set(var1, 0); Goal generate = new GoalIntSetGenerate(array); IntExp intersectionWithVar2 = var1.intersectionWith(var2).cardinality(); IntExp intersectionWithVar3 = var1.intersectionWith(var3).cardinality(); Goal alt1 = new GoalMinimize(generate, intersectionWithVar2.neg()); Goal alt2 = new GoalMinimize(generate, intersectionWithVar3.neg()); alt1 = new GoalAnd(alt1, new GoalFail(C)); Goal main = new GoalOr(alt1, alt2); C.execute(main); System.out.println("Succeeded"); }