Download presentation

Presentation is loading. Please wait.

Published byMagdalena Charton Modified over 2 years ago

1
C HOCO A Constraint Programming System Suzette Person CSCE 821 Spring 2008 1

2
C HOCO : A Constraint Programming System S. Person Outline Background & introduction Domain types Constraints and Constraint Propagation Search & Branching How we use C HOCO in.. – Java Pathfinder, a static analysis tool for Java programs 2

3
C HOCO : A Constraint Programming System S. Person 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) 3

4
C HOCO : A Constraint Programming System S. Person Background Developed to support CP research in combinatorial optimization – ‘Easy’ to use – Extensible – Modular – ‘Optimized’ Library vs. autonomous system 4

5
C HOCO : A Constraint Programming System S. Person Implementation Highlights Developed in C LAIRE (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 5

6
C HOCO : A Constraint Programming System S. Person Outline Background & introduction Domain types Constraints and Constraint Propagation Search & Branching How we use C HOCO in.. – Java Pathfinder, a static analysis tool for Java programs 6

7
C HOCO : A Constraint Programming System S. Person Domain Types Supports three types of variable domains 1.Integer: IntDomainVar 2.Real: RealVar 3.Set: SetVar 7

8
C HOCO : A Constraint Programming System S. Person 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) 8

9
C HOCO : A Constraint Programming System S. Person Domain Types 2.RealVar (support for continuous domains) – Limited support currently available in C HOCO (enough to support “toy” problems) – Ex. CycloHexane problem 9 y 2 * (1 + z 2 ) + z * (z - 24 * y) = -13 x 2 * (1 + y 2 ) + y * (y - 24 * x) = -13 z 2 * (1 + x 2 ) + 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);

10
C HOCO : A Constraint Programming System S. Person 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 2 n values - every possible subset of {1..n} C HOCO applies a kind of bounds consistency to this type of domain 10

11
C HOCO : A Constraint Programming System S. Person Outline Background & introduction Domain types Constraints and Constraint Propagation Search & Branching How we use C HOCO in.. – Java Pathfinder, a static analysis tool for Java programs 11

12
C HOCO : A Constraint Programming System S. Person Constraints 1.Constraints on integer variables 2.Constraints on real variables 3.Constraints on set variables 4.User-defined constraints 12

13
C HOCO : A Constraint Programming System S. Person 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 13

14
C HOCO : A Constraint Programming System S. Person 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, … 14

15
C HOCO : A Constraint Programming System S. Person Integer Vars > Basic Constraints 15 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 var1var2 var3 var4 var5exp

16
C HOCO : A Constraint Programming System S. Person 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 16

17
C HOCO : A Constraint Programming System S. Person 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 17

18
C HOCO : A Constraint Programming System S. Person 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 18

19
C HOCO : A Constraint Programming System S. Person 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 19

20
C HOCO : A Constraint Programming System S. Person Integer Vars > Advanced Constraints Global – allDiff – globalCardinality – occurrence – cumulative – nth – lex – atMostValue – regular 20

21
C HOCO : A Constraint Programming System S. Person 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 21 c1:var1 < var2 c2: var1 <= var3 c3: var5exp<>var3 c4:gc(vars,low,high) Satisfying? var1 = 5 var2 = 8 var3 = 5 var4 = 5 vars=[var1,var2,var4] low[4]=1 high[4]=2 [1..10] [2..5] [5..10] [1..10] c1 c2 [-4..8] c3 var1var2 var3 var4 var5exp (var4 – var1)

22
C HOCO : A Constraint Programming System S. Person 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) 22 c1:var1 < var2 c2: var1 <= var3 c3: var5exp<>var3 c4: occur(vars, 5, var3) Satisfying? var1 = 5 var2 = 8 var3 = 2 var4 = 5 vars=[var1,var2,var4] [1..10] [2..5] [5..10] [1..10] c1 c2 [-4..8] c3 var1var2 var3 var4 var5exp (var4 – var1)

23
C HOCO : A Constraint Programming System S. Person 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). 23

24
C HOCO : A Constraint Programming System S. Person Integer Vars > Advanced Constraints > cumulative 24 t1 t2 t3 t4 t5 time cost/consumption In this example: each task has consumption/height = 1 unit C = capacity (cumulative)

25
C HOCO : A Constraint Programming System S. Person 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 http://www.emn.fr/x- info/sdemasse/gccat/sec4.105.html#uid11321 – lex: enforces a strict lexicographic ordering on two vectors of integer values 25

26
C HOCO : A Constraint Programming System S. Person Integer Vars > Advanced Constraints atMostValue: enforces the number of different values assigned to variables to be at most n 26 c1:var1 < var2 c2: var1 <= var3 c3: var5exp<>var3 c4:amv(vars, 2) Satisfying? var1 = 5 var2 = 8 var3 = 5 var4 = 5 vars=[var1,var2,var4] [1..10] [2..5] [5..10] [1..10] c1 c2 [-4..8] c3 var1var2 var3 var4 var5exp (var4 – var1)

27
C HOCO : A Constraint Programming System S. Person Integer Vars > Advanced Constraints regular: enforces a sequence of variables to be a word recognized by a given finite deterministic automaton (DFA) 27 2 3 0 1 1 1 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

28
C HOCO : A Constraint Programming System S. Person 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 t = new LinkedList (); 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 fs = new LinkedList (); 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 28

29
C HOCO : A Constraint Programming System S. Person 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 29

30
C HOCO : A Constraint Programming System S. Person Integer Vars > Advanced Constraints > regular AllEqual constraint using the regular constraint 30 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 tuples = new LinkedList (); 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));

31
C HOCO : A Constraint Programming System S. Person Integer Vars > Boolean Composition of Constraints 31 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 var1var2 var3 var4var5exp And Or Implies If Only If If Then

32
C HOCO : A Constraint Programming System S. Person 2. Constraints on Set Variables member notMember setDisjoint eqCard … 32

33
C HOCO : A Constraint Programming System S. Person 2a. Mixed Constraints (Set and Integer) member notMember setDisjoint eqCard … 33 (Provides supports for integer variables as set values)

34
C HOCO : A Constraint Programming System S. Person 3. Constraints on Real Variables 34 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); y 2 * (1 + z 2 ) + z * (z - 24 * y) = -13 x 2 * (1 + y 2 ) + y * (y - 24 * x) = -13 z 2 * (1 + x 2 ) + x * (x - 24 * z) = -13

35
C HOCO : A Constraint Programming System S. Person 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) 35

36
C HOCO : A Constraint Programming System S. Person Outline Background & introduction Domain types Constraints and Constraint Propagation Search & Branching How we use C HOCO in.. – Java Pathfinder, a static analysis tool for Java programs 36

37
C HOCO : A Constraint Programming System S. Person Constraint Propagation C HOCO 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 37

38
C HOCO : A Constraint Programming System S. Person 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 38

39
C HOCO : A Constraint Programming System S. Person 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.) C HOCO policy – INCINF & DECSUP: BFS (common FIFO queue) – REMOVAL events: FIFO queue – INSTANTIATE events: DFS (highest priority) 39

40
C HOCO : A Constraint Programming System S. Person Outline Background & introduction Domain types Constraints and constraint propagation Search & branching How we use C HOCO in.. – Java Pathfinder, a static analysis tool for Java programs 40

41
C HOCO : A Constraint Programming System S. Person 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 41

42
C HOCO : A Constraint Programming System S. Person Limiting the Search Bound the search based on – Time – Number of nodes visited – User-defined criterion (e.g., depth bound) 42

43
C HOCO : A Constraint Programming System S. Person Branching 1.Variable/value ordering 2.User-defined branching 3.Branching by posting constraints 4.Limited Discrepancy Search 5.Limited-Depth First Search 43

44
C HOCO : A Constraint Programming System S. Person 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 44

45
C HOCO : A Constraint Programming System S. Person 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 45

46
C HOCO : A Constraint Programming System S. Person 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 x middle point and x< middle point 46

47
C HOCO : A Constraint Programming System S. Person 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 47

48
C HOCO : A Constraint Programming System S. Person Outline Background & introduction Domain types Constraints and Constraint Propagation Search & Branching How we use C HOCO in.. – Java Pathfinder, a static analysis tool for Java programs 48

49
C HOCO : A Constraint Programming System S. Person My experience with C HOCO 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 49

50
C HOCO : A Constraint Programming System S. Person Terminology Static analysis Symbolic execution Path condition Test-case generation 50

51
C HOCO : A Constraint Programming System S. Person Model Analysis 51 package 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

52
C HOCO : A Constraint Programming System S. Person [PC:X>Y Y>X]END [PC:X>Y Y≤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 falsetrue path condition Code that swaps 2 integersSymbolic Execution Tree 52

53
C HOCO : A Constraint Programming System S. Person My experience with C HOCO 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… 53

54
C HOCO : A Constraint Programming System S. Person References C HOCO : 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 http://choco-solver.net Global Constraint Catalog http://www.emn.fr/x-info/sdemasse/gccat/ 54

55
C HOCO : A Constraint Programming System S. Person 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 55

56
C HOCO A Constraint Programming System Suzette Person CSCE 821 Spring 2008 56

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google