Cream: Class Library for Constraint Programming in Java


FAQ of Cream

  1. How disjunctive constraints can be expressed in Cream?

    Cream has no disjunctive constraints, such as (x > 0) or (y > 0). However, you can encode it in Cream as follows. Or, you can simply use x.max(y).gt(0) in this case.

            Network net = new Network();
            IntVariable x = new IntVariable(net, -1, 1, "x");
            IntVariable y = new IntVariable(net, -1, 1, "y");
            // x > 0 or y > 0
            IntVariable c1 = x.sign().max(0);
            IntVariable c2 = y.sign().max(0);
            // c1 or c2 --> c1 max c2
            c1.max(c2).gt(0);
      
  2. How conditional constraints can be expressed in Cream?

    Cream has no conditional constraints, such as if (x > 0) then (y > 0) else (z > 0). However, you can encode it in Cream as follows.

            Network net = new Network();
            IntVariable x = new IntVariable(net, 0, 1, "x");
            IntVariable y = new IntVariable(net, 0, 1, "y");
            IntVariable z = new IntVariable(net, 0, 1, "z");
            // if x > 0 then y > 0 else z > 0
            IntVariable c1 = x.sign().max(0);
            IntVariable c2 = y.sign().max(0);
            IntVariable c3 = z.sign().max(0);
            // if c1 then c2 else c3
            // --> (c1 and c2) or (not c1 and c3)
            // --> (c1 min c2) max (1-c1 min c3)
            c1.min(c2).max(c1.negate().add(1).min(c3)).gt(0);
      
  3. Can Cream handle soft constraints?

    Cream does not support soft constraints directly. However, you can encode them in some cases. Suppose you have a set of soft constraints x1<=0, x2<=0, ..., xn<=0. They can be expressed in Cream as follows (but, perhaps not efficient).

            IntVariable w1 = x1.sign().max(0);
            IntVariable w2 = x2.sign().max(0);
            ...
            IntVariable wn = x3.sign().max(0);
            IntVariable w = w1.add(w2)....add(wn);
            net.setObjective(w);
            int opt = Solver.MAXIMIZE | Solver.BETTER;
            Solver solver = new DefaultSolver(net, opt);
      
  4. Is it possible to add constraints dynamically?

    Yes, but you need to start a new solver after adding constraints. If you want to find a similar solution after adding constraints, please refer to neighborhoodSearchExample() in Examples.java.

  5. Is it possible to use non-contiguous values for domains?

    Yes. The following example declares an integer variable "x" with the domain {1,2,4,8}.

            IntDomain d = new IntDomain(1, 7).delete(3).delete(5, 7);
            IntVariable x = new IntVariable(net, d, "x");
      

Naoyuki Tamura