1

I am trying to model a SAT formula using Choco 4.0.1. I read the docs , I am trying to understand from the javadoc, but unfortunately I am failing so far. This is my first time working on these type of problems and also choco. So, I may be asking something very obvious.

I need to add a number of constraints to the model like (each var is a BoolVar):

x <-> (a and -b)

I am trying to use ifOnlyIf method in Model, but I don't know how to negate a variable, or use and. Could someone provide me (ideally) some sample code or any ideas on how to model these types of constraints?

begumgenc
  • 393
  • 1
  • 2
  • 12

1 Answers1

2

According to the Choco 4.0.1 online manual, it should be something like this:

SatFactory.addClauses(LogOp.ifOnlyIf(x, LogOp.and(a, LogOp.nor(b))), model);
// with static import of LogOp
SatFactory.addClauses(ifOnlyIf(x, and(a, nor(b))), model);

However, the manual seems to be outdated. Like suggested in the comment, I arrived at:

import static org.chocosolver.solver.constraints.nary.cnf.LogOp.and;
import static org.chocosolver.solver.constraints.nary.cnf.LogOp.ifOnlyIf;
import static org.chocosolver.solver.constraints.nary.cnf.LogOp.nor;

import org.chocosolver.solver.Model;
import org.chocosolver.solver.variables.BoolVar;

public class AkChocoSatDemo {

    public static void main(String[] args) {
        // 1. Create a Model
        Model model = new Model("my first problem");

        // 2. Create variables
        BoolVar x = model.boolVar("X");
        BoolVar a = model.boolVar("A");
        BoolVar b = model.boolVar("B");

        // 3. Post constraints
        // LogOp omitted due to import static ...LogOp.*
        model.addClauses(ifOnlyIf(x, and(a, nor(b))));

        // 4. Solve the problem
        model.getSolver().solve();

        // 5. Print the solution
        System.out.println(x); // X = 1
        System.out.println(a); // A = 1
        System.out.println(b); // B = 0
    }
}

I have used nor() with single parameter as not() to negate an input.

Axel Kemper
  • 10,544
  • 2
  • 31
  • 54
  • Thanks for the reply but the problem is, when I use SatFactory as in the sample, I can not reach the methods. I was wondering if I am doing something wrong. I would appreciate if you let me know if you try and it works. – begumgenc Jan 09 '17 at 19:21
  • Actually, I modified it a little bit and it seems to be working. model.addClauses(LogOp.ifOnlyIf(x, LogOp.and(a, LogOp.nor(b)))); – begumgenc Jan 09 '17 at 19:35