예제 #1
0
 public String toString(Solver solver) {
   Variable[] vars = solver.getVars();
   StringBuilder st = new StringBuilder("Solution: ");
   for (int i = 0; i < vars.length; i++) {
     if ((vars[i].getTypeAndKind() & Variable.TYPE) != Variable.CSTE) {
       int kind = vars[i].getTypeAndKind() & Variable.KIND;
       switch (kind) {
         case Variable.INT:
         case Variable.BOOL:
           IntVar v = (IntVar) vars[i];
           st.append(v.getName()).append("=").append(intmap.get(v.getId())).append(", ");
           break;
         case Variable.REAL:
           RealVar r = (RealVar) vars[i];
           double[] bounds = realmap.get(r.getId());
           st.append(r.getName())
               .append("=[")
               .append(bounds[0])
               .append(",")
               .append(bounds[1])
               .append("], ");
           break;
         case Variable.SET:
           SetVar s = (SetVar) vars[i];
           st.append(s.getName())
               .append("=")
               .append(Arrays.toString(setmap.get(s.getId())))
               .append(", ");
           break;
       }
     }
   }
   return st.toString();
 }
예제 #2
0
 public PropSetEqual(SetVar s1, SetVar s2) {
   super(new SetVar[] {s1, s2}, PropagatorPriority.LINEAR, true);
   this.s1 = s1;
   this.s1D = s1.monitorDelta(this);
   this.s2 = s2;
   this.s2D = s2.monitorDelta(this);
 }
예제 #3
0
 /**
  * Set all variables to their respective value in the solution Throws an exception is this empties
  * a domain (i.e. this domain does not contain the solution value)
  *
  * <p>BEWARE: A restart might be required so that domains contain the solution values
  */
 public void restore(Solver solver) throws ContradictionException {
   if (empty) {
     throw new UnsupportedOperationException("Empty solution. No solution found");
   }
   Variable[] vars = solver.getVars();
   for (int i = 0; i < vars.length; i++) {
     if ((vars[i].getTypeAndKind() & Variable.TYPE) != Variable.CSTE) {
       int kind = vars[i].getTypeAndKind() & Variable.KIND;
       switch (kind) {
         case Variable.INT:
         case Variable.BOOL:
           IntVar v = (IntVar) vars[i];
           int value = intmap.get(v.getId());
           if (value != NO_ENTRY) {
             v.instantiateTo(value, this);
           } // otherwise, this is not a decision variable
           break;
         case Variable.REAL:
           RealVar r = (RealVar) vars[i];
           double[] bounds = realmap.get(r.getId());
           if (bounds != null) {
             r.updateBounds(bounds[0], bounds[1], this);
           } // otherwise, this is not a decision variable
           break;
         case Variable.SET:
           SetVar s = (SetVar) vars[i];
           int[] values = setmap.get(s.getId());
           if (values != null) {
             s.instantiateTo(values, Cause.Null);
           } // otherwise, this is not a decision variable
           break;
       }
     }
   }
 }
예제 #4
0
 /**
  * Get the value of variable s in this solution
  *
  * @param s SetVar
  * @return the value of variable s in this solution, or null if the variable is not instantiated
  *     in the solution
  */
 public int[] getSetVal(SetVar s) {
   if (empty) {
     throw new UnsupportedOperationException("Empty solution. No solution found");
   }
   if (setmap.containsKey(s.getId())) {
     return setmap.get(s.getId());
   } else if ((s.getTypeAndKind() & Variable.TYPE) == Variable.CSTE) {
     return s.getValues();
   } else {
     return null;
   }
 }
예제 #5
0
 @Override
 public SetVar getVariable(SetVar[] variables) {
   int small_idx = -1;
   int delta = 0;
   for (int idx = 0; idx < variables.length; idx++) {
     SetVar variable = variables[idx];
     int d = variable.getEnvelopeSize() - variable.getKernelSize();
     if (d > delta) {
       delta = d;
       small_idx = idx;
     }
   }
   return small_idx > -1 ? variables[small_idx] : null;
 }
예제 #6
0
 @Override
 public void propagate(int idxVarInProp, int mask) throws ContradictionException {
   if (isS1Var(idxVarInProp)) {
     s1D.freeze();
     s1D.forEach(s1Env -> s2.remove(s1Env, this), SetEventType.REMOVE_FROM_ENVELOPE);
     s1D.forEach(s1Ker -> s2.force(s1Ker, this), SetEventType.ADD_TO_KER);
     s1D.unfreeze();
   } else {
     assert isS2Var(idxVarInProp);
     s2D.freeze();
     s2D.forEach(s2Env -> s1.remove(s2Env, this), SetEventType.REMOVE_FROM_ENVELOPE);
     s2D.forEach(s2Ker -> s1.force(s2Ker, this), SetEventType.ADD_TO_KER);
     s2D.unfreeze();
   }
 }
예제 #7
0
 /**
  * Records the current solution of the solver clears all previous recordings
  *
  * @param solver a solver
  */
 public void record(Solver solver) {
   if (empty) {
     Variable[] _dvars = solver.getStrategy().getVariables();
     for (int i = 0; i < _dvars.length; i++) {
       dvars.add(_dvars[i].getId());
     }
     empty = false;
   }
   boolean warn = false;
   intmap.clear();
   realmap.clear();
   setmap.clear();
   Variable[] vars = solver.getVars();
   for (int i = 0; i < vars.length; i++) {
     if ((vars[i].getTypeAndKind() & Variable.TYPE) != Variable.CSTE) {
       int kind = vars[i].getTypeAndKind() & Variable.KIND;
       if (!vars[i].isInstantiated()) {
         if (dvars.contains(vars[i].getId())) {
           throw new SolverException(vars[i] + " is not instantiated when recording a solution.");
         } else {
           warn = true;
         }
       } else {
         switch (kind) {
           case Variable.INT:
           case Variable.BOOL:
             IntVar v = (IntVar) vars[i];
             intmap.put(v.getId(), v.getValue());
             break;
           case Variable.REAL:
             RealVar r = (RealVar) vars[i];
             realmap.put(r.getId(), new double[] {r.getLB(), r.getUB()});
             break;
           case Variable.SET:
             SetVar s = (SetVar) vars[i];
             setmap.put(s.getId(), s.getValues());
             break;
         }
       }
     }
   }
   if (warn && solver.getSettings().warnUser()) {
     Chatterbox.err.printf(
         "Some non decision variables are not instantiated in the current solution.");
   }
 }
예제 #8
0
 @Override
 public ESat isEntailed() {
   boolean possibleTrue =
       PropUtil.isKerSubsetEnv(result, consequent) && PropUtil.isKerSubsetEnv(consequent, result);
   boolean possibleFalse =
       PropUtil.isKerSubsetEnv(result, alternative)
           && PropUtil.isKerSubsetEnv(alternative, result);
   if (!possibleTrue && !possibleFalse) {
     return ESat.FALSE;
   }
   if (antecedent.isInstantiated()) {
     if (antecedent.getValue() == 1) {
       if (!possibleTrue) {
         return ESat.FALSE;
       }
       return result.isInstantiated() && consequent.isInstantiated() ? ESat.TRUE : ESat.UNDEFINED;
     } else {
       if (!possibleFalse) {
         return ESat.FALSE;
       }
       return result.isInstantiated() && alternative.isInstantiated() ? ESat.TRUE : ESat.UNDEFINED;
     }
   }
   if (possibleTrue
       && possibleFalse
       && result.isInstantiated()
       && consequent.isInstantiated()
       && alternative.isInstantiated()) {
     return ESat.TRUE;
   }
   return ESat.UNDEFINED;
 }
예제 #9
0
 @Override
 public void propagate(int evtmask) throws ContradictionException {
   if (!antecedent.isInstantiated()) {
     if (!PropUtil.isKerSubsetEnv(result, consequent)
         || !PropUtil.isKerSubsetEnv(consequent, result)) {
       antecedent.instantiateTo(0, this);
     } else if (!PropUtil.isKerSubsetEnv(result, alternative)
         || !PropUtil.isKerSubsetEnv(alternative, result)) {
       antecedent.instantiateTo(1, this);
     } else {
       ISetIterator iter = result.getUB().iterator();
       while (iter.hasNext()) {
         int i = iter.nextInt();
         if (!consequent.getUB().contains(i) && !alternative.getUB().contains(i)) {
           result.remove(i, this);
         } else if (consequent.getLB().contains(i) && alternative.getLB().contains(i)) {
           result.force(i, this);
         }
       }
     }
   }
   if (antecedent.isInstantiated()) {
     if (antecedent.getValue() == 1) {
       PropUtil.envSubsetEnv(result, consequent, this);
       PropUtil.envSubsetEnv(consequent, result, this);
       PropUtil.kerSubsetKer(result, consequent, this);
       PropUtil.kerSubsetKer(consequent, result, this);
     } else {
       PropUtil.envSubsetEnv(result, alternative, this);
       PropUtil.envSubsetEnv(alternative, result, this);
       PropUtil.kerSubsetKer(result, alternative, this);
       PropUtil.kerSubsetKer(alternative, result, this);
     }
   }
 }
예제 #10
0
 private int checkSolutions(Model model, SetVar set, int value) {
   int nbSol = 0;
   while (model.getSolver().solve()) {
     nbSol++;
     assertTrue(set.getValue().contains(value));
   }
   assertTrue(nbSol > 0);
   return nbSol;
 }
예제 #11
0
 @Override
 public ESat isEntailed() {
   if (set.getEnvelopeSize() == 0) {
     if (notEmpty) {
       return ESat.FALSE;
     } else {
       return ESat.TRUE;
     }
   }
   int lb = min.getLB();
   int ub = min.getUB();
   for (int j = set.getKernelFirst(); j != SetVar.END; j = set.getKernelNext()) {
     if (get(j) < lb) {
       return ESat.FALSE;
     }
   }
   int minVal = Integer.MAX_VALUE;
   for (int j = set.getEnvelopeFirst(); j != SetVar.END; j = set.getEnvelopeNext()) {
     if (minVal > get(j)) {
       minVal = get(j);
     }
   }
   if (minVal > ub && (notEmpty || set.getKernelSize() > 0)) {
     return ESat.FALSE;
   }
   if (isCompletelyInstantiated()) {
     return ESat.TRUE;
   }
   return ESat.UNDEFINED;
 }
예제 #12
0
 @Override
 public void propagate(int evtmask) throws ContradictionException {
   for (int j = set.getKernelFirst(); j != SetVar.END; j = set.getKernelNext()) {
     min.updateUpperBound(get(j), this);
   }
   int minVal = Integer.MAX_VALUE;
   int lb = min.getLB();
   for (int j = set.getEnvelopeFirst(); j != SetVar.END; j = set.getEnvelopeNext()) {
     int k = get(j);
     if (k < lb) {
       set.removeFromEnvelope(j, this);
     } else {
       if (minVal > k) {
         minVal = k;
       }
     }
   }
   if (notEmpty || set.getKernelSize() > 0) {
     min.updateLowerBound(minVal, this);
   }
 }
예제 #13
0
  @Override
  public ESat isEntailed() {
    if (!PropUtil.isKerSubsetEnv(s1, s2) || !PropUtil.isKerSubsetEnv(s2, s1)) {
      return ESat.FALSE;
    }
    if (s1.isInstantiated() && s2.isInstantiated()) {
      return ESat.TRUE;
    }
    int setIntersection = 0;
    ISetIterator iter = s1.getUB().iterator();
    while (iter.hasNext()) {
      int i = iter.nextInt();
      if (s2.getUB().contains(i)) {
        setIntersection++;
      }
    }
    if (setIntersection < s1.getCard().getLB() || setIntersection < s2.getCard().getLB()) {
      return ESat.FALSE;
    }

    return ESat.UNDEFINED;
  }
예제 #14
0
 @Solution
 public Constraint setup(BoolVar[] bools, SetVar set) {
   return set.getModel().setBoolsChanneling(bools, set, 0);
 }
예제 #15
0
 @Override
 public double evaluate(SetVar variable) {
   return -variable.getEnvelopeSize() - variable.getKernelSize();
 }