Introduction to Constraint Programming Kris Kuchcinski kku@ida.liu.se 5/31/2016 1 Programming with Constraints “Constraint programming represents one of the closest approaches computer science has yet made to the Holy Grail of programming: the user states the problem, the computer solves it.” Eugene C. Freuder CONSTRAINTS, April 1997 5/31/2016 2 Programming with Constraints “Were you to ask me which programming paradigm is likely to gain most in commercial significance over the next 5 years I’d have to pick Constrained Logic Programming (CLP), even though it’s perhaps currently one of the least known and understood” Dick Pountain BYTE, February 1995 5/31/2016 3 Course organization Date 16 Feb 23 Feb 2 Mar Content Organization, motivation, limitations, etc. Introductory notions (Ch 1) Basic operations on constraints (Ch 2) (Satisfiability, entailment, projection, optimization) 9 Mar Finite domain constraints (Ch 3) (Incompleteness, consistency techniques). 16 Mar Logic programming (*) 23 Mar Constraint logic programming (Ch 4, 5) 30 Mar Advanced logic programming (Ch 6, 7) 5/31/2016 Lect KK JM JM JM UN UN UN 4 Course organization (cont’d) Date Content 13 Apr Exercises, batch 1 20 Apr Modeling with finite domain constraints I (Global constraints- diffn, cumulative, precedence, element and search (Ch 8) 27 Apr Modeling with finite domain constraints II (Global constraints and search (CHIP based) 4 May Reducing search Redundant, disjunctive and global constraintscycle, among, sequence) 11 May Exercises, batch 2 18 May Advanced algorithms (consistency, SA, TS) 25 May Exercises, batch 3 5/31/2016 Lect -KK KK UN -?? -5 Course organization (cont’d) Three obligatory course assignments with the following deadlines: 25 March 20 April 18 May Course credit points 4 Possibility for additional project and up to 6 credit points 5/31/2016 6 A simple definition Constraint programming is the study of computational systems based on constraints. The idea of constraint programming is to solve problems by exploring constraints which must be satisfied by the solution. 5/31/2016 7 Different constraint systems Real/rational constraints- CLP(R), CLP(Q) CLP(R), Sicstus, CHIP Finite domains constraints- CLP(FD) Sicstus, CHIP Boolean constraints- CLP(B) Sicstus, CHIP Interval constraints- CLP(I) CLP(BNR), Numerica 5/31/2016 8 The programming paradigm Logic programming Constraint satisfaction/solving CLP Optimisation 5/31/2016 9 Logic programming Logic for problem description Declarative description style Problem description separated from its solving Unification Lists and recursion Standard backtracking Constraint programming does not need to use LP !!! 5/31/2016 10 Constraint satisfaction/solving Set of variables and constraints which limit the values that can be assigned to the constraint variables Constraint propagation methods “Encapsulation” of specific knowledge from mathematics, geometry, graph theory and operational research 5/31/2016 11 Optimization Finding a solution which satisfies constraints and minimizes/maximizes objective function Different types combinatorial optimization of discrete (finite domain) variables linear optimization for continuous variables 5/31/2016 12 Examples DC circuit using real constraints Digital circuits synthesis and verification using binary constraints Allocation, scheduling and binding in high-level synthesis using finite domain constraints 5/31/2016 13 Analysis of DC circuit CLP(R) I5 I2 I3 Is I8 V I1 I9 I4 R1 I6 I7 5/31/2016 Is - I1 - I2 - I8 = 0, I1 + I7 - Is = 0, I2 + I3 - I5 = 0, I8 - I3 - I4 - I9 = 0, I4 + I6 - I7 = 0, I5 - I6 + I9 = 0, R1*I1 = V, 2*I2 - 3*I3 - 8*I8 = 0, 3*I3 + 5*I5 -9*I9 = 0, 6*I6 - 4*I4 +9*I9 =0, 4*I4 - I1 + 7*I7 +8*I8 = 0 14 Analysis of DC circuit CLP(R) dc([Is, I1, Is - I1 I1 + I7 I2 + I3 I8 - I3 I4 + I6 I5 - I6 + (cont’d) I2, I3, I4, I5, I6, I7, I8]) :I2 - I8 = 0, Is = 0, I5 = 0, I4 - I9 = 0, I7 = 0, I9 = 0, R1*I1 = V, 2*I2 3*I3 6*I6 4*I4 5/31/2016 + - 3*I3 5*I5 4*I4 I1 + - 8*I8 - 9*I9 + 9*I9 7*I7 + = 0, = 0, =0, 8*I8 = 0. 15 Analysis of DC circuit CLP(R) (cont’d) V = 10, R1 = 1 V= 10, R1=3 V = 10, R1=? I8 I7 I6 I5 I4 I3 I2 I1 IS I8 I7 I6 I5 I4 I3 I2 I1 IS I7 I6 I5 I4 I3 I2 I1 IS 10 = = = = = = = = = 0.259209 0.828299 0.296239 0.25726 0.53206 -0.31183 0.56909 10 10.8283 = = = = = = = = = 0.086403 0.2761 0.098746 0.085753 0.177353 -0.103943 0.189697 3.33333 3.60943 = = = = = = = = = 3.19549*I8 1.14286*I8 0.992481*I8 2.05263*I8 -1.20301*I8 2.19549*I8 38.5789*I8 41.7744*I8 I1 * R1 *** Maybe 5/31/2016 16 Analysis of DC circuit CLP(I) dc([Is,I1,I2,I3,I4,I5,I6,I7,I8]) Is>= -100, Is<=100, I1>= -100, I2>= -100, I2<=100, I3>= -100, I4>= -100, I4<=100, I5>= -100, I6>= -100, I6<=100, I7>= -100, I8>= -100, I8<=100, I9>= -100, :I1<=100, I3<=100, I5<=100, I7<=100, I9<=100, Is-I1-I2-I8=:=0, I2+I3-I5=:=0, I4+I6-I7=:=0, I1+I7-Is=:=0, I8-I3-I4-I9=:=0, I5-I6+I9=:=0, R>=1, R<=3, R*I1=:=10, 2*I2-3*I3-8*I8=:=0, 6*I6-4*I4+9*I9=:=0, 3*I3+5*I5-9*I9=:=0, 4*I4-I1+7*I7+8*I8=:=0. 5/31/2016 17 Analysis of DC circuit CLP(I) (cont’d) ?- dc([Is,I1,I2,I3,I4,I5,I6,I7,I8]). Is I1 I2 I3 I4 I5 I6 I7 I8 in in in in in in in in in 5/31/2016 (3.6094328590907638,10.8282985772764136) (3.3333333333333330,10.0000000000000000] (0.1896966153445871,0.5690898460339618) (-0.3118300526213467,-0.1039433508737487) (0.1773533424283491,0.5320600272851572) (0.0857532644708472,0.2572597934126064) (0.0987461833300698,0.2962385499902706) (0.2760995257584099,0.8282985772754369) (0.0864029104138029,0.2592087312414950) 18 Digital Circuits- full adder CLP(B) A 0 0 0 0 1 1 1 1 B 0 0 1 1 0 0 1 1 Cin 0 1 0 1 0 1 0 1 Sum Cout 0 0 1 0 1 0 0 1 1 0 0 1 0 1 1 1 adder(_, A, B, C, SUM, CARRY) :sat(SUM =:= card([1,3], [A, B, C])), sat(CARRY =:= card([2,3], [A, B, C])). 5/31/2016 19 Digital Circuits- full adder CLP(B) (cont’d) | ?- adder(_, a, b, c, SUM, CARRY). sat(SUM=:=a#b#c), sat(CARRY=:=b*a#c*a#c*b). 5/31/2016 20 Digital Circuits- full adder CLP(B) (cont’d) N-transistor model ntrans(_, B, X, Y) :sat((B * (X=:=Y)) + (B=:=0)). P-transistor model ptrans(_, B, X, Y) :sat((~B * (X=:=Y)) + (B=:=1)). X X B B Y 5/31/2016 Y 21 Digital Circuits- full adder CLP(B) (cont’d) full_adder(_, A, B, C, SUM, CARRY) :carry_part(_, A, B, C, NCA, CARRY), sum_part(_, A, B, C, NCA, SUM). sum_part(_, A, B, C, NCA, SUM) :ptrans(_, NCA, T1, 1), ptrans(_, C, 1, T5), ptrans(_, B, T1, T5), ptrans(_, A, T1, T2), ptrans(_, NCA, T5, T2), ptrans(_, T2, 1, SUM), ntrans(_, A, T2, T3), ntrans(_, NCA, T2, T6), ntrans(_, T2, SUM, 0), ntrans(_, B, T3, T6), ntrans(_, NCA, T3, 0), ntrans(_, C, T6, 0). carry_part(_, A, B, C, NCA, CA) :ptrans(_, A, T1, 1), ptrans(_, B, T1, 1), ptrans(_, A, T2, 1), ptrans(_, C, T1, NCA), ptrans(_, B, T2, NCA), ptrans(_, NCA, 1, CA), ntrans(_, C, NCA, T3), ntrans(_, B, NCA, T4), ntrans(_, NCA, CA, 0), ntrans(_, A, T3, 0), ntrans(_, B, T3, 0), ntrans(_, A, T4, 0). 5/31/2016 22 Digital Circuits- full adder CLP(B) (cont’d) | ?- full_adder(_, a, b, c, SUM, CARRY). sat(SUM=:=a#b#c), sat(CARRY=:=b*a#c*a#c*b). 5/31/2016 23 High-level synthesis CLP(FD) 3 x u dx 3 * * y u dx x * * * + * y dx dx x u dx 3 + c a x1 * u y1 dx * * < u - y u dx x a x1 + 3 dx - < * c * y * - + u1 u1 data-flow graph 5/31/2016 y1 scheduled data-flow graph 24 High-level synthesis CLP(FD) Op1 (cont’d) Op2 T1 + D1 #=< T3, T2 + D2 #=< T3, T3 + D3 #=< T4, Op4 Op3 T4 :: 0..50, D4 :: 3..4, R4 :: 0..1 5/31/2016 (T1 + D1 #=< T2 T2 + D2 #=< T1 R1 #\= R2). 25 High-level synthesis CLP(FD) (cont’d) Scheduled design 5/31/2016 26 Methods for constraint solving CLP(R) Gauss-Jordan elimination simplex CLP(I) interval narrowing, box consistency, Gauss-Seidel elimination, interval Newton method, CLP(B) for example, operations on BDD’s CLP(FD) arc, node and path consistency methods constraint propagation (forward checking, look-ahead), branch-and-bound 5/31/2016 27 Constraint programming limitations Many addressed problems in the area of FD constraints are NP-complete (combinatorial problems, such as scheduling, traveling salesman) search for (optimal) solution has exponential complexity in general case constraints and their propagation methods try to reduce the search space possible heuristic search methods Boolean satisfaction problem is NP-complete Linear programming has polynomial time solving algorithms (but in many practical cases it has too long execution times) 5/31/2016 28 An Example 5/31/2016 29 An Example (cont’d) 5/31/2016 30 Web resources Constraints archive http://www.cs.unh.edu/ccc/archive Guide to constraints programming http://kti.ms.mff.cuni.cz/~bartak/constraints Sicstus manual http://www.sics.se/isl/sicstus/sicstus_toc.html CHIP documentation file:/sw/chip-5.1/chip.htm Prolog systems at IDA http://www.ida.liu.se/labs/logpro/lpsw/ 5/31/2016 31