Exemple #1
0
 @Override
 public void impose(Store store) {
   x.putModelConstraint(this, getConsistencyPruningEvent(x));
   p.putModelConstraint(this, getConsistencyPruningEvent(p));
   store.addChanged(this);
   store.countConstraint();
 }
Exemple #2
0
  // registers the constraint in the constraint store
  @Override
  public void impose(Store store) {

    result.putModelConstraint(this, getConsistencyPruningEvent(result));

    for (Var V : list) V.putModelConstraint(this, getConsistencyPruningEvent(V));

    store.addChanged(this);
    store.countConstraint();
  }
  private void notifyHard(MultiSet<String> exploredSet) {
    // exclude all constraints that are in exploredSet from the set of
    // constraints
    // first impose all

    int targetWeight = getWeight(exploredSet);
    if (upperCostBound >= 0 && targetWeight >= upperCostBound) {
      if (debug) {
        System.out.println(
            "Infeasible: Target upper bound "
                + targetWeight
                + " but upper bound for problem is "
                + upperCostBound);
      }
      enumerator.foundSolutionOrInfeasibility();
      wasSolved = false;
      return;
    }

    // System.out.println("Trying ... "+exploredSet + " -> weight: "+targetWeight);
    for (org.jacop.constraints.Constraint c : idToConstraintMap.values()) {
      if (!exploredSet.contains(c.id)) c.impose(store);
    }

    // now exclude the perhaps unnecessary ones
    for (String s : exploredSet) {
      org.jacop.constraints.Constraint c = idToConstraintMap.get(s);
      c.removeConstraint();
    }

    // now do some actual solving

    store.setLevel(store.level + 1);
    boolean result = search.labeling(store, select);

    if (result) {
      wasSolved = true;
      objectiveValue = targetWeight;
      enumerator.foundSolutionOrInfeasibility();

      if (debug) {
        System.out.println("Found a best solution with cost " + targetWeight);
        System.out.println(store);
      }
    }

    store.removeLevel(store.level);
    store.setLevel(store.level - 1);
  }
  @Override
  public void imposeDecomposition(Store store) {

    if (constraints == null) decompose(store);

    for (Constraint c : constraints) store.impose(c, queueIndex);
  }
Exemple #5
0
  @Override
  public void consistency(Store store) {

    do {

      // domain consistency
      int xMin;
      if (Math.abs(p.min()) < (double) IntDomain.MaxInt)
        xMin = (int) (Math.round(Math.ceil(p.min())));
      else xMin = IntDomain.MinInt;

      int xMax;
      if (Math.abs(p.max()) < (double) IntDomain.MaxInt)
        xMax = (int) (Math.round(Math.floor(p.max())));
      else xMax = IntDomain.MaxInt;

      if (xMin > xMax) {
        int t = xMax;
        xMax = xMin;
        xMin = t;
      }

      x.domain.in(store.level, x, xMin, xMax);

      store.propagationHasOccurred = false;

      p.domain.in(store.level, p, x.min(), x.max());

    } while (store.propagationHasOccurred);
  }
Exemple #6
0
  @Override
  public void notConsistency(Store store) {

    do {

      store.propagationHasOccurred = false;

      int x0 = 0, index_01 = 0;

      for (int i = 0; i < list.length; i++) {
        if (list[i].min() == 1) {
          result.domain.in(store.level, result, 0, 0);
          return;
        } else if (list[i].max() == 0) x0++;
        else index_01 = i;
      }

      // for case >, then the in() will fail as the constraint should.
      if (result.min() == 1 && x0 < list.length)
        for (int i = 0; i < list.length; i++) list[i].domain.in(store.level, list[i], 0, 0);

      if (result.max() == 0 && x0 >= list.length - 1)
        list[index_01].domain.in(store.level, list[index_01], 1, 1);

    } while (store.propagationHasOccurred);
  }
Exemple #7
0
  /**
   * It specifies simple search method based on input order and lexigraphical ordering of values.
   *
   * @return true if there is a solution, false otherwise.
   */
  public boolean search() {

    long T1, T2;
    T1 = System.currentTimeMillis();

    SelectChoicePoint<SetVar> select =
        new SimpleSelect<SetVar>(vars.toArray(new SetVar[1]), null, new IndomainSetMin<SetVar>());

    search = new DepthFirstSearch<SetVar>();

    boolean result = search.labeling(store, select);

    if (result) store.print();

    T2 = System.currentTimeMillis();

    System.out.println("\n\t*** Execution time = " + (T2 - T1) + " ms");

    System.out.println();
    System.out.print(search.getNodes() + "\t");
    System.out.print(search.getDecisions() + "\t");
    System.out.print(search.getWrongDecisions() + "\t");
    System.out.print(search.getBacktracks() + "\t");
    System.out.print(search.getMaximumDepth() + "\t");

    return result;
  }
Exemple #8
0
  /**
   * It conducts master-slave search. Both of them use input order variable ordering.
   *
   * @param masterVars it specifies the search variables used in master search.
   * @param slaveVars it specifies the search variables used in slave search.
   * @return true if the solution exists, false otherwise.
   */
  public boolean searchMasterSlave(ArrayList<Var> masterVars, ArrayList<Var> slaveVars) {

    long T1 = System.currentTimeMillis();

    boolean result = false;

    Search<SetVar> labelSlave = new DepthFirstSearch<SetVar>();
    SelectChoicePoint<SetVar> selectSlave =
        new SimpleSelect<SetVar>(
            slaveVars.toArray(new SetVar[0]), null, new IndomainSetMin<SetVar>());
    labelSlave.setSelectChoicePoint(selectSlave);

    Search<SetVar> labelMaster = new DepthFirstSearch<SetVar>();
    SelectChoicePoint<SetVar> selectMaster =
        new SimpleSelect<SetVar>(
            masterVars.toArray(new SetVar[0]), null, new IndomainSetMin<SetVar>());

    labelMaster.addChildSearch(labelSlave);

    search = labelMaster;

    result = labelMaster.labeling(store, selectMaster);

    if (result) System.out.println("Solution found");

    if (result) store.print();

    long T2 = System.currentTimeMillis();

    System.out.println("\n\t*** Execution time = " + (T2 - T1) + " ms");

    return result;
  }
Exemple #9
0
  @Override
  public void impose(Store store) {

    this.store = store;

    int level = store.level;

    int pos = 0;
    positionMapping = new HashMap<IntVar, Integer>();

    for (IntVar v : listAlldiff) {
      positionMapping.put(v, pos++);
      v.putModelConstraint(this, getConsistencyPruningEvent(v));
      queueVariable(level, v);
    }
    grounded = new TimeStamp<Integer>(store, 0);

    store.addChanged(this);
    store.countConstraint();
  }
Exemple #10
0
  @Override
  public void notConsistency(Store store) {

    do {

      store.propagationHasOccurred = false;

      if (r.singleton()) p.domain.inComplement(store.level, p, r.min() - c);

      if (p.singleton()) r.domain.inComplement(store.level, r, p.min() + c);

    } while (store.propagationHasOccurred);
  }
Exemple #11
0
  @Override
  public void consistency(Store store) {

    do {
      store.propagationHasOccurred = false;

      FloatIntervalDomain pDom = FloatDomain.subBounds(r.min(), r.max(), c, c);
      p.domain.in(store.level, p, pDom.min(), pDom.max());

      FloatIntervalDomain rDom = FloatDomain.addBounds(p.min(), p.max(), c, c);
      r.domain.in(store.level, r, rDom.min(), rDom.max());

    } while (store.propagationHasOccurred);
  }
Exemple #12
0
  /**
   * It specifies simple search method based on input order and lexigraphical ordering of values. It
   * optimizes the solution by minimizing the cost function.
   *
   * @return true if there is a solution, false otherwise.
   */
  public boolean searchOptimal() {

    long T1, T2;
    T1 = System.currentTimeMillis();

    SelectChoicePoint<SetVar> select =
        new SimpleSelect<SetVar>(vars.toArray(new SetVar[1]), null, new IndomainSetMin<SetVar>());

    search = new DepthFirstSearch<SetVar>();

    boolean result = search.labeling(store, select, cost);

    if (result) store.print();

    T2 = System.currentTimeMillis();

    System.out.println("\n\t*** Execution time = " + (T2 - T1) + " ms");

    return result;
  }
Exemple #13
0
 DisjointCondVar(Store S) {
   DisjointCondVarValue val = new DisjointCondVarValue();
   value = val;
   index = S.putMutableVar(this);
   store = S;
 }
  void generateVariables(SimpleNode node, Tables table, Store store) {

    dictionary = table;
    annotations = new HashSet<String>();
    boolean var_introduced = false, output_var = false;
    OutputArrayAnnotation outArrayAnn = null;

    int type = getType(node);

    int initChild = getAnnotations(node, 1);

    // 	node.dump("");
    //    	System.out.println("*** Type = " + type + " init index = " + initChild);
    //    	System.out.println("*** Annotations: " + annotations);

    if (annotations.contains("var_is_introduced")) var_introduced = true;
    if (annotations.contains("output_var")) output_var = true;

    // 	    System.out.println("IS INTRODUCED");

    String ident;
    IntVar varInt;
    SetVar varSet;
    BooleanVar boolVar;
    IntDomain setValue;
    int initVal;
    IntVar initVar;
    switch (type) {
      case 0: // int
        ident = ((ASTVarDeclItem) node).getIdent();
        varInt = new IntVar(store, ident, IntDomain.MinInt, IntDomain.MaxInt);
        table.addVariable(ident, varInt);
        if (initChild < ((ASTVarDeclItem) node).jjtGetNumChildren()) {

          if (constant_int(node, initChild)) {
            initVal = getScalarFlatExpr(node, initChild);
            XeqC c = new XeqC(varInt, initVal);
            store.impose(c);
            // 		    System.out.println(c);
          } else {
            initVar = getScalarFlatExpr_var(store, node, initChild);
            XeqY c = new XeqY(varInt, initVar);
            store.impose(c);
            // 		    System.out.println(c);
          }
        }
        if (!var_introduced) table.addSearchVar(varInt);
        if (output_var) table.addOutVar(varInt);
        break;
      case 1: // int interval
        ident = ((ASTVarDeclItem) node).getIdent();
        // varInt = new IntVar(store, ident, lowInterval, highInterval); // more efficient but
        // SmallDenseDomain does not work with AmongVar
        varInt = new IntVar(store, ident, new IntervalDomain(lowInterval, highInterval));
        table.addVariable(ident, varInt);
        if (initChild < ((ASTVarDeclItem) node).jjtGetNumChildren()) {

          if (constant_int(node, initChild)) {
            initVal = getScalarFlatExpr(node, initChild);
            XeqC c = new XeqC(varInt, initVal);
            store.impose(c);
            // 		    System.out.println(c);
          } else {
            initVar = getScalarFlatExpr_var(store, node, initChild);
            XeqY c = new XeqY(varInt, initVar);
            store.impose(c);
            //  		    System.out.println(c);
          }
        }
        if (!var_introduced) table.addSearchVar(varInt);
        if (output_var) table.addOutVar(varInt);
        break;
      case 2: // int list
        ident = ((ASTVarDeclItem) node).getIdent();
        varInt = new IntVar(store, ident);
        for (Integer e : intList) ((IntVar) varInt).addDom(e.intValue(), e.intValue());
        table.addVariable(ident, varInt);
        if (initChild < ((ASTVarDeclItem) node).jjtGetNumChildren()) {

          if (constant_int(node, initChild)) {
            initVal = getScalarFlatExpr(node, initChild);
            XeqC c = new XeqC(varInt, initVal);
            store.impose(c);
            // 		    System.out.println(c);
          } else {
            initVar = getScalarFlatExpr_var(store, node, initChild);
            XeqY c = new XeqY(varInt, initVar);
            store.impose(c);
            // 		    System.out.println(c);
          }

          // 		initVal = getScalarFlatExpr(node, initChild);
          // // 		XeqY c = new XeqY(var, new Variable(store, initVal, initVal));
          // 		XeqC c = new XeqC((IntVar)varInt, initVal);
          // 		store.impose(c);
          // 		// 		System.out.println(c);
        }
        if (!var_introduced) table.addSearchVar(varInt);
        if (output_var) table.addOutVar(varInt);
        break;
      case 3: // bool
        ident = ((ASTVarDeclItem) node).getIdent();
        boolVar = new BooleanVar(store, ident);
        table.addVariable(ident, boolVar);
        if (initChild < ((ASTVarDeclItem) node).jjtGetNumChildren()) {

          if (constant_int(node, initChild)) {
            initVal = getScalarFlatExpr(node, initChild);
            XeqC c = new XeqC(boolVar, initVal);
            store.impose(c);
            // 		    System.out.println(c);
          } else {
            initVar = getScalarFlatExpr_var(store, node, initChild);
            XeqY c = new XeqY(boolVar, initVar);
            store.impose(c);
            // 		    System.out.println(c);
          }

          // 		initVal = getScalarFlatExpr(node, initChild);
          // // 		XeqY c = new XeqY(boolVar, new Variable(store, initVal, initVal));
          // 		XeqC c = new XeqC(boolVar, initVal);
          // 		store.impose(c);
          // 		// 		System.out.println(c);
        }
        if (!var_introduced) table.addSearchVar(boolVar);
        if (output_var) table.addOutVar(boolVar);
        numberBooleanVariables++;
        break;
      case 4: // set int
        ident = ((ASTVarDeclItem) node).getIdent();
        varSet = new SetVar(store, ident, new BoundSetDomain(IntDomain.MinInt, IntDomain.MaxInt));
        table.addSetVariable(ident, (SetVar) varSet);
        if (initChild < ((ASTVarDeclItem) node).jjtGetNumChildren()) {

          if (constant_set(node, initChild)) {
            setValue = getSetLiteral(node, initChild);
            AeqS c = new AeqS((SetVar) varSet, setValue);
            store.impose(c);
            // 		    System.out.println(c);
          } else {
            Var initSetVar = getSetFlatExpr_var(store, node, initChild);
            AeqB c = new AeqB((SetVar) varSet, (SetVar) initSetVar);
            store.impose(c);
            // 		    System.out.println(c);
          }
        }
        if (!var_introduced) table.addSearchSetVar(varSet);
        if (output_var) table.addOutVar(varSet);
        break;
      case 5: // set interval
        ident = ((ASTVarDeclItem) node).getIdent();
        varSet = new SetVar(store, ident, new BoundSetDomain(lowInterval, highInterval));
        table.addSetVariable(ident, (SetVar) varSet);
        if (initChild < ((ASTVarDeclItem) node).jjtGetNumChildren()) {

          if (constant_set(node, initChild)) {
            setValue = getSetLiteral(node, initChild);
            AeqS c = new AeqS((SetVar) varSet, setValue);
            store.impose(c);
            // 		    System.out.println(c);
          } else {
            Var initSetVar = getSetFlatExpr_var(store, node, initChild);
            System.out.println("intSetVar = " + initSetVar);

            AeqB c = new AeqB((SetVar) varSet, (SetVar) initSetVar);
            store.impose(c);
            // 		    System.out.println(c);
          }
        }
        if (!var_introduced) table.addSearchSetVar(varSet);
        if (output_var) table.addOutVar(varSet);
        break;
      case 6: // set list
        ident = ((ASTVarDeclItem) node).getIdent();
        SetDomain dom = new BoundSetDomain();
        for (Integer e : intList) dom.addDom(e.intValue(), e.intValue());
        varSet = new SetVar(store, ident, dom);
        table.addSetVariable(ident, (SetVar) varSet);
        if (initChild < ((ASTVarDeclItem) node).jjtGetNumChildren()) {

          if (constant_set(node, initChild)) {
            setValue = getSetLiteral(node, initChild);
            AeqS c = new AeqS((SetVar) varSet, setValue);
            store.impose(c);
            // 		    System.out.println(c);
          } else {
            Var initSetVar = getSetFlatExpr_var(store, node, initChild);
            AeqB c = new AeqB((SetVar) varSet, (SetVar) initSetVar);
            store.impose(c);
            // 		    System.out.println(c);
          }
        }
        if (!var_introduced) table.addSearchSetVar(varSet);
        if (output_var) table.addOutVar(varSet);
        break;
      case 7: // bool set
        ident = ((ASTVarDeclItem) node).getIdent();
        varSet = new SetVar(store, ident, new BoundSetDomain(0, 1));
        table.addSetVariable(ident, (SetVar) varSet);
        if (initChild < ((ASTVarDeclItem) node).jjtGetNumChildren()) {

          if (constant_set(node, initChild)) {
            setValue = getSetLiteral(node, initChild);
            AeqS c = new AeqS((SetVar) varSet, setValue);
            store.impose(c);
            // 		    System.out.println(c);
          } else {
            Var initSetVar = getSetFlatExpr_var(store, node, initChild);
            AeqB c = new AeqB((SetVar) varSet, (SetVar) initSetVar);
            store.impose(c);
            // 		    System.out.println(c);
          }
        }
        if (!var_introduced) table.addSearchSetVar(varSet);
        if (output_var) table.addOutVar(varSet);
        break;
      default:
        System.err.println("Not supported type in parameter; compilation aborted.");
        System.exit(0);
    }
  }
Exemple #15
0
 DisjointCondVar(Store S, RectangleWithCondition[] R) {
   value = new DisjointCondVarValue(R);
   index = S.putMutableVar(this);
   store = S;
 }
Exemple #16
0
 DisjointCondVar(Store S, Vector<RectangleWithCondition> R) {
   value = new DisjointCondVarValue();
   value.setValue(R);
   index = S.putMutableVar(this);
   store = S;
 }
  private void notifyReified(MultiSet<String> exploredSet) {

    if (debug) {
      System.out.println("Checking ... " + exploredSet);
    }
    satisfiedConstraints = new ArrayList<PrimitiveConstraint>(constraintMap.size());
    violatedConstraints = new ArrayList<PrimitiveConstraint>(constraintMap.size());

    HashSet<String> violatedIds = new HashSet<String>(exploredSet.size() * 2);

    for (String violatedConstraint : exploredSet) {
      WrappedPrimitiveConstraint wpc = constraintMap.get(violatedConstraint);
      BooleanVar indicator = wpc.getViolationIndicator();

      // violation indicator must be true!
      PrimitiveConstraint nextIndicatorForce = new XeqY(indicator, constantOne);
      nextIndicatorForce.impose(store);
      // try constant
      PrimitiveConstraint nextIndicatorConst = new XeqC(indicator, 1);
      store.impose(nextIndicatorConst);

      violatedIds.add(violatedConstraint);

      violatedConstraints.add(nextIndicatorForce);
      violatedConstraints.add(nextIndicatorConst);
    }

    for (Entry<String, WrappedPrimitiveConstraint> remainingEntry : constraintMap.entrySet()) {
      if (!violatedIds.contains(remainingEntry.getKey())) {
        BooleanVar indicator = remainingEntry.getValue().getViolationIndicator();

        PrimitiveConstraint nextIndicatorForce =
            new XeqY(indicator, constantZero); // no penalty for those
        nextIndicatorForce.impose(store);
        // try constant
        PrimitiveConstraint nextForce = new XeqC(indicator, 0);
        nextForce.impose(store);

        satisfiedConstraints.add(nextIndicatorForce);
        satisfiedConstraints.add(nextForce);
      }
    }

    // now do some actual solving

    store.setLevel(store.level + 1);

    boolean result = search.labeling(store, select);

    if (result) {
      objectiveValue = objective.value();
      wasSolved = true;
      enumerator.foundSolutionOrInfeasibility();
      if (debug) {
        System.out.println("Found a best solution with cost: " + objective.value());
        System.out.println(store);
      }
    }

    store.removeLevel(store.level);
    store.setLevel(store.level - 1);

    // now cleanup
    for (PrimitiveConstraint pc : satisfiedConstraints) {
      pc.removeConstraint();
    }

    for (PrimitiveConstraint pc : violatedConstraints) {
      pc.removeConstraint();
    }
  }