コード例 #1
0
 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]));
   }
 }
コード例 #2
0
  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");
  }
コード例 #3
0
  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);
    }
  }
コード例 #4
0
    static void checkResults(IntSetVarArray array) {
      int size = array.size();
      for (int i = 0; i < size; i++) {
        IntSetVar var = array.get(i);
        Set checkSet = var.requiredSet();
        assertTrue(checkSet.size() >= req[i][iNbMembers]);
        assertTrue(intersectionCardinality(checkSet, Stewards.possibleSet()) >= req[i][iStewards]);
        assertTrue(
            intersectionCardinality(checkSet, Hostesses.possibleSet()) >= req[i][iHostesses]);
        assertTrue(intersectionCardinality(checkSet, French.possibleSet()) >= req[i][iFrench]);
        assertTrue(intersectionCardinality(checkSet, German.possibleSet()) >= req[i][iGerman]);
        assertTrue(intersectionCardinality(checkSet, Spanish.possibleSet()) >= req[i][iSpanish]);
      }
      for (int i = 0; i < size - 2; i++) {
        assertTrue(
            intersectionCardinality(array.get(i).requiredSet(), array.get(i + 1).requiredSet())
                == 0);
      }

      assertTrue(
          intersectionCardinality(
                  array.get(size - 2).requiredSet(), array.get(size - 1).requiredSet())
              == 0);
    }
コード例 #5
0
    static void test() {
      IntSetVarArray memberSets = new IntSetVarArray(C, 5);

      memberSets.set(Stewards, 0);
      memberSets.set(Hostesses, 1);
      memberSets.set(French, 2);
      memberSets.set(German, 3);
      memberSets.set(Spanish, 4);

      try {
        for (int i = 0; i < crews.size() - 1; i++) {
          crews.get(i).nullIntersectWith(crews.get(i + 1)).execute();
          if ((i + 2) < 10) {
            crews.get(i).nullIntersectWith(crews.get(i + 2)).execute();
          }
        }

        for (int i = 0; i < crews.size(); i++) {
          postTeamConstraints(
              crews.get(i),
              req[i][iNbMembers],
              new int[] {
                req[i][iStewards],
                req[i][iHostesses],
                req[i][iFrench],
                req[i][iGerman],
                req[i][iSpanish]
              },
              memberSets);
        }

        assertTrue(
            "Solution for crews task wasn't found", C.execute(new GoalIntSetGenerate(crews)));
        checkResults(crews);

      } catch (Failure f) {
        fail("test failed due to Failure exception's being thrown");
      }
    }
コード例 #6
0
 static void printSetArray(IntSetVarArray array, boolean requiredOnly) {
   for (int i = 0; i < array.size(); i++) {
     printSet(array.get(i), requiredOnly);
     System.out.println("");
   }
 }
コード例 #7
0
 static {
   C.showInternalNames(true);
   for (int i = 0; i < crews.size(); i++) {
     crews.set(C.addIntSetVar(Staff, "flight#" + i), i);
   }
 };