示例#1
0
 @Test(groups = "1s", timeOut = 60000)
 public void test11() {
   Model model = new Model();
   BoolVar a = model.boolVar("a");
   BoolVar b = model.boolVar("b");
   LogOp l = LogOp.or(LogOp.and(a, b.not()), LogOp.and(a.not(), b), LogOp.and(a.not(), b.not()));
   ILogical ll = LogicTreeToolBox.toCNF(l, model);
   Assert.assertEquals(ll.toString(), "(not(b) or not(a))");
 }
示例#2
0
 public void maxref(Solver solver, IntVar x, IntVar y, IntVar z) {
   BoolVar[] bs = VariableFactory.boolArray("b", 3, solver);
   LogicalConstraintFactory.ifThenElse(
       bs[0], IntConstraintFactory.arithm(z, "=", x), IntConstraintFactory.arithm(z, "!=", x));
   LogicalConstraintFactory.ifThenElse(
       bs[1], IntConstraintFactory.arithm(z, "=", y), IntConstraintFactory.arithm(z, "!=", y));
   LogicalConstraintFactory.ifThenElse(
       bs[2], IntConstraintFactory.arithm(x, ">=", y), IntConstraintFactory.arithm(x, "<", y));
   SatFactory.addClauses(LogOp.or(LogOp.and(bs[0], bs[2]), LogOp.and(bs[1], bs[2].not())), solver);
 }
示例#3
0
 /**
  * Create the logical complement of <code>l</code>.
  *
  * @param l operand
  * @return a new ILogical
  * @throws CloneNotSupportedException
  */
 private static ILogical negate(ILogical l) throws CloneNotSupportedException {
   if (l.isLit()) {
     return ((BoolVar) l).not();
   } else {
     LogOp n = (LogOp) l;
     LogOp na = n.clone();
     na.type = Type.flip(and().type);
     return na;
   }
 }
示例#4
0
  @Test(groups = "1s", timeOut = 60000)
  public void test3() {
    Model model = new Model();

    BoolVar a = model.boolVar("a");
    BoolVar b = model.boolVar("b");
    BoolVar c = model.boolVar("c");

    LogOp root = LogOp.or(LogOp.and(a, b), c);
    root = LogicTreeToolBox.developOr(root);
    Assert.assertEquals(root.toString(), "((a or c) and (b or c))");
  }
示例#5
0
  @Test(groups = "1s", timeOut = 60000)
  public void test4() {
    Model model = new Model();

    BoolVar a = model.boolVar("a").not();
    BoolVar b = model.boolVar("b");
    BoolVar c = model.boolVar("c");
    BoolVar d = model.boolVar("d");

    LogOp root = LogOp.nor(LogOp.or(LogOp.nand(a, b), c), d);

    LogicTreeToolBox.expandNot(root);

    Assert.assertEquals(root.toString(), "(((not(a) and b) and not(c)) and not(d))");
  }
示例#6
0
  @Test(groups = "1s", timeOut = 60000)
  public void test2() {
    Model model = new Model();

    BoolVar a = model.boolVar("a").not();
    BoolVar b = model.boolVar("b");
    BoolVar c = model.boolVar("c");
    BoolVar d = model.boolVar("d");

    LogOp root = LogOp.or(LogOp.or(LogOp.or(a, b), c), d);

    LogicTreeToolBox.merge(LogOp.Operator.OR, root);

    Assert.assertEquals(root.toString(), "(d or c or not(a) or b)");
  }
示例#7
0
  @Test(groups = "1s", timeOut = 60000)
  public void test1() {
    Model model = new Model();

    BoolVar a = model.boolVar("a");
    BoolVar b = model.boolVar("b");
    BoolVar c = model.boolVar("c");
    BoolVar d = model.boolVar("d");

    LogOp root = LogOp.nand(LogOp.nor(a, b), LogOp.or(c, d));

    ILogical l = LogicTreeToolBox.toCNF(root, model);

    Assert.assertEquals(l.toString(), "((a or b or not(c)) and (a or b or not(d)))");
  }
示例#8
0
 @Override
 public LogOp clone() throws CloneNotSupportedException {
   LogOp logOp = (LogOp) super.clone();
   logOp.type = this.type;
   logOp.operator = this.operator;
   logOp.children = new ILogical[this.children.length];
   for (int c = 0; c < children.length; c++) {
     if (children[c].isLit()) {
       logOp.children[c] = children[c];
     } else {
       logOp.children[c] = ((LogOp) children[c]).clone();
     }
   }
   return logOp;
 }
示例#9
0
  @Test(groups = "1s", timeOut = 60000)
  public void test8() {
    Model model = new Model();

    BoolVar a = model.boolVar("a");
    BoolVar na = a.not();
    BoolVar b = model.boolVar("b");
    BoolVar nb = b.not();
    BoolVar c = model.boolVar("c");
    BoolVar d = model.boolVar("d");

    LogOp root = LogOp.and(LogOp.or(a, b, na), LogOp.or(c, d), LogOp.or(b, nb));

    ILogical l = LogicTreeToolBox.toCNF(root, model);

    Assert.assertEquals(l.toString(), "(c or d)");
  }
示例#10
0
 @Test(groups = "1s", timeOut = 60000)
 public void test14() {
   Model model = new Model();
   BoolVar a = model.boolVar("a");
   BoolVar b = model.boolVar("b");
   LogOp l = LogOp.or(a, b, a.not(), a.not());
   ILogical ll = LogicTreeToolBox.toCNF(l, model);
   Assert.assertEquals(ll.toString(), "cste -- 1 = 1");
 }
示例#11
0
  @Test(groups = "1s", timeOut = 60000)
  public void test6() {
    Model model = new Model();

    BoolVar a = model.boolVar("a");
    BoolVar b = model.boolVar("b");

    LogOp root = LogOp.implies(a, b);

    ILogical l = LogicTreeToolBox.toCNF(root, model);

    Assert.assertEquals(l.toString(), "(b or not(a))");
  }
示例#12
0
  @Test(groups = "1s", timeOut = 60000)
  public void test15() {
    Model model = new Model();
    IntVar a = model.intVar("a", -1, 1, false);
    BoolVar b1 = model.boolVar("b1");
    BoolVar b2 = model.boolVar("b2");
    model.arithm(a, "=", 0).reifyWith(b1);
    model.arithm(a, ">", 0).reifyWith(b2);

    LogOp l =
        LogOp.or(LogOp.and(b1, b2.not()), LogOp.and(b1.not(), b2), LogOp.and(b1.not(), b2.not()));
    model.addClauses(l);
    model.getMinisat().getPropSat().initialize();
    try {
      model.getSolver().propagate();
      b1.instantiateTo(1, Cause.Null);
      model.getSolver().propagate();
    } catch (ContradictionException ex) {
      Assert.fail();
    }
    Assert.assertTrue(b1.isInstantiatedTo(1));
    Assert.assertTrue(b2.isInstantiatedTo(0));
  }
示例#13
0
  @Test(groups = "1s", timeOut = 60000)
  public void test7() {
    Model model = new Model();

    BoolVar a = model.boolVar("a");
    BoolVar b = model.boolVar("b");
    BoolVar c = model.boolVar("c");

    LogOp root = LogOp.ifThenElse(a, b, c);

    ILogical l = LogicTreeToolBox.toCNF(root, model);

    Assert.assertEquals(l.toString(), "((a or c) and (b or c) and (b or not(a)))");
  }
示例#14
0
  @Test(groups = "1s", timeOut = 60000)
  public void test9() {
    Model model = new Model();

    BoolVar a = model.boolVar("a");
    BoolVar na = a.not();
    BoolVar b = model.boolVar("b");
    BoolVar c = model.boolVar("c");
    BoolVar d = model.boolVar("d");

    LogOp root = LogOp.and(a, b, na, c, d);

    ILogical l = LogicTreeToolBox.toCNF(root, model);

    Assert.assertEquals(l.toString(), "cste -- 0 = 0");
  }