310 likes | 324 Views
Learn about various constraint systems and optimization techniques in constraint programming to solve computational problems efficiently.
E N D
Introduction to Constraint Programming Kris Kuchcinski kku@ida.liu.se
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
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
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
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.
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
Logic programming Constraint satisfaction/solving Optimisation The programming paradigm CLP
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 !!!
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
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
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
I5 I2 I3 Is I8 I9 I4 I1 V I6 I7 Analysis of DC circuitCLP(R) 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 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
Analysis of DC circuitCLP(R) (cont’d) dc([Is, I1, I2, I3, I4, I5, I6, I7, I8]) :- 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.
Analysis of DC circuitCLP(R) (cont’d) V = 10, R1 = 1 I8 = 0.259209 I7 = 0.828299 I6 = 0.296239 I5 = 0.25726 I4 = 0.53206 I3 = -0.31183 I2 = 0.56909 I1 = 10 IS = 10.8283 V= 10, R1=3 I8 = 0.086403 I7 = 0.2761 I6 = 0.098746 I5 = 0.085753 I4 = 0.177353 I3 = -0.103943 I2 = 0.189697 I1 = 3.33333 IS = 3.60943 V = 10, R1=? I7 = 3.19549*I8 I6 = 1.14286*I8 I5 = 0.992481*I8 I4 = 2.05263*I8 I3 = -1.20301*I8 I2 = 2.19549*I8 I1 = 38.5789*I8 IS = 41.7744*I8 10 = I1 * R1 *** Maybe
Analysis of DC circuitCLP(I) dc([Is,I1,I2,I3,I4,I5,I6,I7,I8]) :- Is>= -100, Is<=100, I1>= -100, I1<=100, I2>= -100, I2<=100, I3>= -100, I3<=100, I4>= -100, I4<=100, I5>= -100, I5<=100, I6>= -100, I6<=100, I7>= -100, I7<=100, I8>= -100, I8<=100, I9>= -100, I9<=100, 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, R>=1, R<=3, R*I1=:=10, 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.
Analysis of DC circuitCLP(I) (cont’d) ?- dc([Is,I1,I2,I3,I4,I5,I6,I7,I8]). Is in (3.6094328590907638,10.8282985772764136) I1 in (3.3333333333333330,10.0000000000000000] I2 in (0.1896966153445871,0.5690898460339618) I3 in (-0.3118300526213467,-0.1039433508737487) I4 in (0.1773533424283491,0.5320600272851572) I5 in (0.0857532644708472,0.2572597934126064) I6 in (0.0987461833300698,0.2962385499902706) I7 in (0.2760995257584099,0.8282985772754369) I8 in (0.0864029104138029,0.2592087312414950)
Digital Circuits- full adderCLP(B) adder(_, A, B, C, SUM, CARRY) :- sat(SUM =:= card([1,3], [A, B, C])), sat(CARRY =:= card([2,3], [A, B, C])).
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).
Digital Circuits- full adderCLP(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 Y
Digital Circuits- full adderCLP(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).
Digital Circuits- full adderCLP(B) (cont’d) | ?- full_adder(_, a, b, c, SUM, CARRY). sat(SUM=:=a#b#c), sat(CARRY=:=b*a#c*a#c*b).
3 x u dx 3 y u dx x dx 3 x u dx 3 y u dx x dx + + * * * * * * y dx a a x1 x1 + < < * * * * u u dx c c y1 - - * * y - - + u1 u1 y1 scheduled data-flow graph High-level synthesisCLP(FD) data-flow graph
High-level synthesisCLP(FD) (cont’d) Op2 Op1 T1 + D1 #=< T3, T2 + D2 #=< T3, T3 + D3 #=< T4, Op3 (T1 + D1 #=< T2 T2 + D2 #=< T1 R1 #\= R2). Op4 T4 :: 0..50, D4 :: 3..4, R4 :: 0..1
High-level synthesisCLP(FD) (cont’d) Scheduled design
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
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)
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/