Download - Choco A Constraint Programming System
1
CHOCO A Constraint Programming System
Suzette PersonCSCE 821
Spring 2008
CHOCO: A Constraint Programming SystemS. Person
2
Outline
• Background & introduction• Domain types• Constraints and Constraint Propagation• Search & Branching• How we use CHOCO in..– Java Pathfinder, a static analysis tool for Java
programs
CHOCO: A Constraint Programming SystemS. Person
3
Background
• Open-source (BSD license)• Hosted on Sourceforge.net• Created in 1996 by François Laburthe and
Narenda Jussien• Associated with Ecole des Mines de Nantes
(Nantes, France), Bouygues (Paris, France) and Amadeus (Nice, France)
CHOCO: A Constraint Programming SystemS. Person
4
Background
• Developed to support CP research in combinatorial optimization– ‘Easy’ to use– Extensible– Modular– ‘Optimized’
• Library vs. autonomous system
CHOCO: A Constraint Programming SystemS. Person
5
Implementation Highlights
• Developed in CLAIRE (compile to Java or C++)• Encapsulate all information in a Problem
object to support definition of multiple problems in a single session
• Single threaded search, one per problem instance
• Use trailing (i.e. incremental) stack for backtracking
CHOCO: A Constraint Programming SystemS. Person
6
Outline
• Background & introduction• Domain types• Constraints and Constraint Propagation• Search & Branching• How we use CHOCO in..– Java Pathfinder, a static analysis tool for Java
programs
CHOCO: A Constraint Programming SystemS. Person
7
Domain Types
Supports three types of variable domains1. Integer: IntDomainVar 2. Real: RealVar 3. Set: SetVar
CHOCO: A Constraint Programming SystemS. Person
8
Domain Types
1. IntDomainVar (support for discrete domains)– EnumIntVar• Enumerated integer values• Used for small domains
– BoundIntVar • Intervals of integer values• Used for large domains• Represented by upper and lower bounds: useful for
box-consistency (propagation is performed only on bounds)
CHOCO: A Constraint Programming SystemS. Person
9
Domain Types
2. RealVar (support for continuous domains)– Limited support currently available in CHOCO
(enough to support “toy” problems)– Ex. CycloHexane problem
y2 * (1 + z2) + z * (z - 24 * y) = -13 x2 * (1 + y2) + y * (y - 24 * x) = -13 z2 * (1 + x2) + x * (x - 24 * z) = -13
RealVar x = pb.makeRealVar("x"); RealVar y = pb.makeRealVar("y", -1.0e8, 1.0e8); RealVar z = pb.makeRealVar("z", -1.0e8, 1.0e8);
CHOCO: A Constraint Programming SystemS. Person
10
Domain Types
3. SetVar– High level modeling tool– Supports representation of domains where each
value is a set• Example: a set variable on integer values [1..n] has 2n
values - every possible subset of {1..n}• CHOCO applies a kind of bounds consistency to this
type of domain
CHOCO: A Constraint Programming SystemS. Person
11
Outline
• Background & introduction• Domain types• Constraints and Constraint Propagation• Search & Branching• How we use CHOCO in..– Java Pathfinder, a static analysis tool for Java
programs
CHOCO: A Constraint Programming SystemS. Person
12
Constraints
1. Constraints on integer variables2. Constraints on real variables3. Constraints on set variables4. User-defined constraints
CHOCO: A Constraint Programming SystemS. Person
13
1. Constraints on integer variables
• Basic constraints• Channeling constraints• Arbitrary constraints (in extension)– Binary constraints; Nary constraints
• Advanced constraints– Global constraints
• Boolean composition of constraints
CHOCO: A Constraint Programming SystemS. Person
14
Integer Vars > Basic Constraints
1. Arithmetic– Equality (v1 == v2, v1 != v2); comparisons (v1 <= v2,
v1 < v2); difference; linear combinations; product of variables, …
2. Complex expressions– Over expressions: plus, minus, mult, … – Over variables: times, scalar, sum, …
3. More expressions– max, min, abs , …
CHOCO: A Constraint Programming SystemS. Person
15
Integer Vars > Basic Constraints
IntDomainVar var1 = prob.makeEnumIntVar(“var1”, 2, 5);IntDomainVar var2 = prob.makeEnumIntVar(“var2”, 1, 10);IntDomainVar var3 = prob.makeEnumIntVar(“var3”, 5, 10);IntDomainVar var4 = prob.makeEnumIntVar(“var4”, 1, 10);
Constraint c1 = prob.gt(var2,var1);Constraint c2 = prob.leq(var1,var3);
IntExp var5Exp = prob.minus(var4, var1);
Constraint c3 = prob.neq(var5Exp,var3);prob.post(c1); prob.post(c2); prob.post(c3);
[1..10]
[2..5]
[5..10]
[1..10]c1
c2[-4..8]
c3
var1 var2
var3
var4
var5exp
CHOCO: A Constraint Programming SystemS. Person
16
Integer Vars > Channeling Constraints
• Redundant models– A common technique for strengthening
propagation or to get more freedom in designing dedicated heuristics
• Channeling constraints– Used to ensure the integrity of different models– By propagating variable instantiations and domain
reductions between models
CHOCO: A Constraint Programming SystemS. Person
17
Integer Vars > Channeling Constraints
• Channeling constraints are not part of the original problem specification – only serve as a link between models
• Inverse channeling: ensures x[i]=j <=> y[j]=i (useful in matrix models)
• Boolean channeling: ensures x=j <=> bv=1 where bv is a boolean variable of domain {0,1} and x is an integer variable
CHOCO: A Constraint Programming SystemS. Person
18
Integer Vars > Arbitrary Constraints
• Binary and N-ary• Specified in extension – As supports or conflicts – Suitable for small domains – In tables, which may be shared by different
constraints (!) in order to reduce space• Specified in intension– As predicates, implies some run-time overhead for
calling the feasibility test
CHOCO: A Constraint Programming SystemS. Person
19
Integer Vars > Arbitrary Constraints > Extension
• Binary– Consistency checking available: AC3, AC4, AC2001
• Nary– Consistency checking available: FC (?) & GAC– GAC• Supports: GAC3rm by [Lecoutre & Hemery IJCAI 2007]• Conflicts: standard GAC
CHOCO: A Constraint Programming SystemS. Person
20
Integer Vars > Advanced Constraints
• Global– allDiff– globalCardinality– occurrence– cumulative– nth– lex– atMostValue– regular
CHOCO: A Constraint Programming SystemS. Person
21
Integer Vars > Advanced Constraints
• allDiff: ensures all pairs of variables have distinct values
• globalCardinality: ensures the number of occurrences of the value i is within the specified bounds
c1:var1 < var2c2: var1 <= var3c3: var5exp<>var3c4:gc(vars,low,high)
Satisfying?var1 = 5var2 = 8var3 = 5var4 = 5
vars=[var1,var2,var4]low[4]=1high[4]=2
[1..10]
[2..5]
[5..10]
[1..10]c1
c2[-4..8]
c3
var1 var2
var3
var4
var5exp(var4 – var1)
CHOCO: A Constraint Programming SystemS. Person
22
Integer Vars > Advanced Constraints
• occurrence: ensures that the specified variable is instantiated to the number of occurrences of the value in a list of variable values (a specialization of globalCardinality)
c1:var1 < var2c2: var1 <= var3c3: var5exp<>var3c4: occur(vars, 5, var3)
Satisfying?var1 = 5var2 = 8var3 = 2var4 = 5
vars=[var1,var2,var4]
[1..10]
[2..5]
[5..10]
[1..10]c1
c2[-4..8]
c3
var1 var2
var3
var4
var5exp(var4 – var1)
CHOCO: A Constraint Programming SystemS. Person
23
Integer Vars > Advanced Constraints
• cumulative: Given a set of tasks defined – by their starting dates, ending dates, durations
and consumptions/heights, – the cumulative ensures that at any time t, the sum
of the heights of the tasks which are executed at time t does not exceed a given limit C (the capacity of the resource).
CHOCO: A Constraint Programming SystemS. Person
24
Integer Vars > Advanced Constraints > cumulative
t1
t2t3t4
t5
time
cost
/con
sum
ption
In this example: each task has consumption/height = 1 unit
C = capacity (cumulative)
CHOCO: A Constraint Programming SystemS. Person
25
Integer Vars > Advanced Constraints
• nth: allows specification of a constraint where values are variables (also known as the element constraint)• http://www.emn.fr/x-info/sdemasse/gccat/sec4.105.html#
uid11321– lex: enforces a strict lexicographic ordering on two
vectors of integer values
CHOCO: A Constraint Programming SystemS. Person
26
Integer Vars > Advanced Constraints
• atMostValue: enforces the number of different values assigned to variables to be at most n
c1:var1 < var2c2: var1 <= var3c3: var5exp<>var3c4:amv(vars, 2)
Satisfying?var1 = 5var2 = 8var3 = 5var4 = 5
vars=[var1,var2,var4]
[1..10]
[2..5]
[5..10]
[1..10]c1
c2[-4..8]
c3
var1 var2
var3
var4
var5exp(var4 – var1)
CHOCO: A Constraint Programming SystemS. Person
27
Integer Vars > Advanced Constraints
• regular: enforces a sequence of variables to be a word recognized by a given finite deterministic automaton (DFA)
2
3
0
1 11
1
3
3
Accepted words : sequence of value 3 (of size 6) where subsequences of value 1 (if any exist) are exactly of size 3. For example : 331113.
3
CHOCO: A Constraint Programming SystemS. Person
28
Integer Vars > Advanced Constraints > regular
Problem pb = new Problem(); IntDomainVar[] vars = new IntDomainVar[6]; for (int i = 0; i < vars.length; i++) { vars[i] = pb.makeEnumIntVar("v" + i, 0, 5); } // Build the list of transitions of the DFA List<Transition> t = new LinkedList<Transition>(); t.add(new Transition(0, 1, 1)); t.add(new Transition(1, 1, 2)); t.add(new Transition(2, 1, 3)); t.add(new Transition(3, 3, 0));t.add(new Transition(0, 3, 0)); // Two final states: 0, 3 List<Integer> fs = new LinkedList<Integer>(); fs.add(0); fs.add(3); DFA auto = new DFA(t, fs, 6); // Build the DFA pb.post(pb.regular(vars , auto)); // post the constraint
CHOCO: A Constraint Programming SystemS. Person
29
Integer Vars > Advanced Constraints > regular
• Can be used as a GAC algorithm for a list of feasible or infeasible tuples– Can be more efficient than a standard GAC
algorithm if the DFA is compact
CHOCO: A Constraint Programming SystemS. Person
30
Integer Vars > Advanced Constraints > regular
• AllEqual constraint using the regular constraint
Problem pb = new Problem(); IntDomainVar v1 = pb.makeEnumIntVar("v1", 1, 4); IntDomainVar v2 = pb.makeEnumIntVar("v2", 1, 4); IntDomainVar v3 = pb.makeEnumIntVar("v3", 1, 4); //add some allowed tuples (here, the tuples define an allEqual constraint) List<int[]> tuples = new LinkedList<int[]>();tuples.add(new int[]{1, 1, 1}); tuples.add(new int[]{2, 2, 2}); tuples.add(new int[]{3, 3, 3}); tuples.add(new int[]{4, 4, 4}); // post the constraint pb.post(pb.regular(new IntDomainVar[]{v1, v2, v3},tuples));
CHOCO: A Constraint Programming SystemS. Person
31
Integer Vars > Boolean Composition of Constraints
IntDomainVar var1 = prob.makeEnumIntVar(“var1”, 2, 5);IntDomainVar var2 = prob.makeEnumIntVar(“var2”, 1, 10);IntDomainVar var3 = prob.makeEnumIntVar(“var3”, 5, 10);IntDomainVar var4 = prob.makeEnumIntVar(“var4”, 1, 10);
Constraint c1 = prob.gt(var2,var1);Constraint c2 = prob.leq(var1,var3);
IntExp var5Exp = prob.minus(var4, var1);
Constraint c3 = prob.neq(var5Exp,var3);Constraint c4 = prob.or(prob.lt(var2,var3),prob.gt(var5Exp,0));prob.post(c1); prob.post(c2); prob.post(c3); prob.post(c4);
[1..10]
[2..5]
[5..10]
[1..10]c1
c2[-4..8]
c3
var1 var2
var3 var4var5exp
• And• Or• Implies
• If Only If• If Then
CHOCO: A Constraint Programming SystemS. Person
32
2. Constraints on Set Variables
• member• notMember• setDisjoint• eqCard• …
CHOCO: A Constraint Programming SystemS. Person
33
2a. Mixed Constraints (Set and Integer)
• member• notMember• setDisjoint• eqCard• …
(Provides supports for integer variables as set values)
CHOCO: A Constraint Programming SystemS. Person
34
3. Constraints on Real Variables
RealVar x = pb.makeRealVar("x"); RealVar y = pb.makeRealVar("y", -1.0e8, 1.0e8); RealVar z = pb.makeRealVar("z", -1.0e8, 1.0e8);
RealExp exp1 = pb.plus(pb.mult(pb.power(y, 2), pb.plus(pb.cst(1.0), pb.power(z, 2))), pb.mult(z, pb.minus(z, pb.mult(pb.cst(24), y)))); …Equation eq1 = (Equation) pb.eq(exp1, pb.cst(-13)); eq1.addBoxedVar(y); eq1.addBoxedVar(z); …pb.post(eq1); pb.post(eq2); pb.post(eq3);
y2 * (1 + z2) + z * (z - 24 * y) = -13 x2 * (1 + y2) + y * (y - 24 * x) = -13 z2 * (1 + x2) + x * (x - 24 * z) = -13
CHOCO: A Constraint Programming SystemS. Person
35
4. User-Defined Constraints
• Useful when the basic propagation algorithms are not efficient enough to propagate the constraint
• Abstract classes are available to support management of – the constraint network and – constraint propagation (for Integer and Set
constraints)
CHOCO: A Constraint Programming SystemS. Person
36
Outline
• Background & introduction• Domain types• Constraints and Constraint Propagation• Search & Branching• How we use CHOCO in..– Java Pathfinder, a static analysis tool for Java
programs
CHOCO: A Constraint Programming SystemS. Person
37
Constraint Propagation
• CHOCO uses a reactive approach to propagate constraints– Events are generated on variables– Variables “wake up” their constraints– Which generates new events• Propagate immediately• Add event to a queue
CHOCO: A Constraint Programming SystemS. Person
38
Constraint Propagation
• Events on finite domains– INCINF: domain lower-bound on variable v is
increased from b to a.– DECSUP: domain upper-bound on variable v is
decreased from b to a.– INSTANTIATE: domain of v is reduced to {a}– REMOVAL: the value a is removed from the
domain of v
CHOCO: A Constraint Programming SystemS. Person
39
Constraint Propagation
• DFS vs. BFS processing of constraints– DFS: preemptive propagation of child events– BFS: propagation of events by generations
(propagate root event, then children, then grandchildren, etc.)
• CHOCO policy– INCINF & DECSUP: BFS (common FIFO queue)– REMOVAL events: FIFO queue– INSTANTIATE events: DFS (highest priority)
CHOCO: A Constraint Programming SystemS. Person
40
Outline
• Background & introduction• Domain types• Constraints and constraint propagation• Search & branching• How we use CHOCO in..– Java Pathfinder, a static analysis tool for Java
programs
CHOCO: A Constraint Programming SystemS. Person
41
Search• One (First) solution or all solutions• Optimization– Create a new variable corresponding to the cost of a solution
(‘cost variable’)
– Generate a constraint • representing the objective function and • relating the variables in the CSP to the ‘cost variable’• Value of ‘cost variable’ is computed from the partial/complete
assignment using the objective function• Values of ‘cost variable’ is then maximized/minimized
– Restart parameter restarts the search after a solution is found or backtracks from current solution
CHOCO: A Constraint Programming SystemS. Person
42
Limiting the Search
• Bound the search based on– Time– Number of nodes visited– User-defined criterion (e.g., depth bound)
CHOCO: A Constraint Programming SystemS. Person
43
Branching
1. Variable/value ordering2. User-defined branching3. Branching by posting constraints4. Limited Discrepancy Search5. Limited-Depth First Search
CHOCO: A Constraint Programming SystemS. Person
44
Branching > Over var/val
• Defaults– Variable ordering: Least Domain– Value ordering: lexicographical increasing order
• Also available– Most constrained (deg)– Domain over Degree (dom/deg)– Random
• User-defined heuristics
CHOCO: A Constraint Programming SystemS. Person
45
Branching > User-Defined Branching
• Beyond var/val ordering– User defines branching alternatives– Search proceeds down the first alternative, then
moves to the next one, etc. – Special case: dichotomic branching
• Dichotomic– Branches over a variable’s domain, splitting it in half– Left branch, updating upper bound– Right branch, updating lower bound
CHOCO: A Constraint Programming SystemS. Person
46
Branching > By Posting Constraints
• One way to do ‘user-defined’ branching is to explicitly split the domain of a given variable
• Another way is to add a new constraint whose effect to split the domain– Example: dichotomic branching• Divide in half• Add constraint for xmiddle point and x< middle point
CHOCO: A Constraint Programming SystemS. Person
47
Branching > Other
• Limited Discrepancy Search• Limited Depth First Search– Choose a given depth• Allow backtracking as long as depth of partial solution
is smaller than threshold– When depth limit is reached• Solver branches according to the heuristic• Without backtracking
CHOCO: A Constraint Programming SystemS. Person
48
Outline
• Background & introduction• Domain types• Constraints and Constraint Propagation• Search & Branching• How we use CHOCO in..– Java Pathfinder, a static analysis tool for Java
programs
CHOCO: A Constraint Programming SystemS. Person
49
My experience with CHOCO
• Use in Java Pathfinder (JPF)• To support software development– Test case generation– Error detection (e.g., check for unhandled
exceptions)• Check path conditions (specified as
conjunctions of constraints) during symbolic execution– Check for path feasibility
CHOCO: A Constraint Programming SystemS. Person
50
Terminology
• Static analysis• Symbolic execution• Path condition• Test-case generation
CHOCO: A Constraint Programming SystemS. Person
51
Model Analysispackage Bug;
/** * class AllocationVector: Used to manage allocation and freeing of blocks. * BUG DOCUMENTATION: There is a synchronization GAP between the methods * "getFreeBlockIndex" and "markAsAllocatedBlock", in which anything can be done. */public class AllocationVector { /** * Character vector which holds information about allocated and free blocks, in the following way: * if vector[i] == 'F' -> i-th block is free. * if vector[i] == 'A' -> i-th block is allocated. */ private char[] vector = null;
/** * Constructor: Constructs AllocationVector for 'size' blocks, when all blocks are free * @param size Size of AllocationVector. */ public AllocationVector(int size) { // Allocating vector of size 'size', when all blocks are assigned to free. vector = new char[size]; for (int i=0; i < size; i++) { vector[i] = 'F'; } }
/** * Returns index of free block, if such exists. * If no free block, then -1 is returned. * @return Index of free block if such exists, else -1. */ synchronized public int getFreeBlockIndex() { int i; int count; int startIndex; int interval; int searchDirection; double randomValue; int[] primeValues = { 1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 }; randomValue = Math.random();
// Choosing randomly start entry for search. startIndex = (int)Math.floor((vector.length - 1) * randomValue);
// Choosing randomly increment/decrement prime value. interval = primeValues[(int)Math.floor((primeValues.length - 1) * randomValue)];
// Choosing randomly search direction and starting the search from randomly // choosen start entry in that direction with the randomly choosen interval. if (randomValue > 0.5) { // Searching forward. for (i = startIndex,count = 0; (count < vector.length) && (vector[i] != 'F') ; i = i + interval, i %= vector.length, count++); } else { // Searching backward. for (i = startIndex,count = 0; (count < vector.length) && (vector[i] != 'F') ; count++) { i = i - interval; if (i < 0) { i = i + vector.length; } } } if (count == vector.length) { return -1; // Indicates "no free block". } else { return i; // Returns the index of the found free block. } }
/** * Marks i-th block as allocated. * @param i Index of block to allocate. * NOTE: If allocating already allocated block, then Exception is thrown. */ synchronized public void markAsAllocatedBlock(int i) throws Exception { if (vector[i] != 'A') { vector[i] = 'A'; // Allocates i-th block. } else { throw new Exception("Allocation"); } }
/** * Marks i-th block as free. * @param i Index of block to free. * NOTE: If freeing already free block, then Exception is thrown. */ synchronized public void markAsFreeBlock(int i) throws Exception { if (vector[i] != 'F') { vector[i] = 'F'; // Frees i-th block. } else { throw new Exception("Freeing"); } }}
extract
model
CHOCO: A Constraint Programming SystemS. Person
52
[PC:X>YY>X]END[PC:X>YY≤X]END
[PC:X>Y]Y>X ?
[PC:X>Y]x= X+Y[PC:X≤Y]END
[PC:true] X > Y ?
[PC:true]x = X,y = Y
Symbolic Execution
[PC:X>Y]y = X+Y–Y = X
[PC:X>Y]x = X+Y–X = Y
int x, y;
if (x > y) {
x = x + y;
y = x – y;
x = x – y;
if (x > y)
assert false;
}
false true
false true
path conditionCode that swaps 2 integers Symbolic Execution Tree
CHOCO: A Constraint Programming SystemS. Person
53
My experience with CHOCO
• Advantages– Java API (easy integration with JPF)
• Disadvantages– A few bugs
• Challenges of using Constraint Solver (any)– We typically work with huge (i.e., unbounded)
domains, huge numbers of variables, huge numbers of complex constraints…
CHOCO: A Constraint Programming SystemS. Person
54
References
• CHOCO: implementing a CP kernel. F. Laburthe, CP 00 Workshop on Techniques for Implementing Constraint Programming Systems (TRICS). Singapore, 2000
• User Guide & tutorial: http://choco-solver.net• Global Constraint Catalog
http://www.emn.fr/x-info/sdemasse/gccat/
CHOCO: A Constraint Programming SystemS. Person
55
A Bit of Trivia…
• One of the first software patents was for a register allocation heuristic.– Previous schemes were ad hoc and not entirely
effective– Register allocation was a limiting factor in the
development of optimizing compilers– In the late 1970’s Chaitin formulated the register
allocation problem as a CSP (using graph coloring)• Finding a register assignment with k registers is
equivalent to finding a k coloring of the constraint graph
56
CHOCO A Constraint Programming System
Suzette PersonCSCE 821
Spring 2008