2.3 Constraints

In JaCoP, there are three major types of constraints:

Primitive constraints and global constraints can be imposed using impose method, while decomposable constraints are imposed using imposeDecomposition method. An example that imposes a primitive constraint XneqY is defined below. Again, in order to impose a constraint a store object must be available.

   store.impose( new XeqY(x1, x2));

Alternatively, one can define first a constraint and then impose it, as shown below.

   PrimitiveConstraint c = new XeqY(x1, x2); 
   c.impose(store);

Both methods are equivalent.

The methods impose(constraint) and constraint.impose(store) often create additional data structures within the constraint store as well as constraint itself. Do note that constraint imposition does not involve checking if the constraint is consistent. Both methods of constraint imposition does not check whether the store remains consistent. If checking consistency is needed, the method imposeWithConsistency(constraint) should be used instead. This method throws FailException if the store is inconsistent. Note, that similar functionality can be achieved by calling the procedure store.consistency() explicitly (see section 2.4).

Constraints can have another constraints as their arguments. For example, reified constraints of the form X = Y B can be defined in JaCoP as follows.

   IntVar x = new IntVar(store, "X", 1, 100); 
   IntVar y = new IntVar(store, "Y", 1, 100); 
   IntVar b = new IntVar(store, "B", 0, 1); 
   store.impose( new Refified( new XeqY(x, y), b));

In a similar way disjunctive constraints can be imposed. For example, the disjunction of three constraints can be defined as follows.

   PrimitiveConstraint[] c = {c1, c2, c3}; 
   store.impose( new Or(c) );

or

   ArrayList<PrimitiveConstraint> c = 
                          new ArrayList<PrimitiveConstraint>(); 
   c.add(c1); c.add(c2); c.add(c3); 
   store.impose( new Or(c) );

Note, that disjunction and other similar constraints accept only primitive constraints as parameters.