1 / 12

Constraint Logic Programming (Ch.14)

Constraint Logic Programming (Ch.14). Constraint: a requirement for, or limitation on, a variable Constraint satisfaction: finding combinations of variables that satisfy a set of constraints C onstraint programming: algorithms that implement constraint satisfaction

Download Presentation

Constraint Logic Programming (Ch.14)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Constraint Logic Programming (Ch.14) COSC 2P93 Prolog: CLP • Constraint: a requirement for, or limitation on, a variable • Constraint satisfaction: finding combinations of variables that satisfy a set of constraints • Constraint programming: algorithms that implement constraint satisfaction • Constraint Logic Programming (CLP): merging CP with LP • A constraint satisfaction problem: • set of variables • domains for the variables (float, integer, ...) • constraints the variables have to satisfy • Problem: find an assignment to the variables that satisfies constraints • Constraint solver: an algorithm which, given the above, tries to efficiently find a solution • CLP is implemented by adding a constraint solver to Prolog

  2. CLP COSC 2P93 Prolog: CLP • Types of CLP: • clpr:variables take values over reals (decimal points) • clpq: rationals(fractions) • clpfd: integers, finite domains (sets) and others • clpb: booleans • Good for solving problems involving: • scheduling • linear equations and optimization • logistics • combinatorial problems (puzzles) • To run with SICStus: ?- use_module(library(clpq)).

  3. Example 1: arithmetic COSC 2P93 Prolog: CLP Standard Prolog... ?- 1+X = 5. % unification no ?- 1+X is 5. % Prolog’s “is” no CLPQ... ?- use_module(library(clpq)). % rationals ?- {1+X = 5}. % CLP: enclose constraint in { }’s X = 4

  4. Example 2: Arith COSC 2P93 Prolog: CLP Standard Prolog... convert(Cent, Fahren) :- % v1: Faren to Cent only Cent is (Fahren - 32)*5/9. convert2(Cent, Fahren) :-% v2: F to C, or C to F, but must use ‘var’ var(Cent), Cent is (Fahren - 32)*5/9. convert2(Cent, Fahren) :- var(Fahren), Fahren is 32 + (Cent * 9/5).

  5. Ex 2 (cont) COSC 2P93 Prolog: CLP convert3(Cent, Fahren) :- % v3: uses clpr (reals) {Cent = (Fahren - 32)*5/9}. ?- convert3(35, F). F = 95 ?- convert3(C, 95). C = 35 ?- convert3(C, F). ?- convert3(C,F). % using rationals {F = 32.0 + 1.8*C} {F=32+9/5*C}

  6. Example: integers and Fibonacci COSC 2P93 Prolog: CLP % Regular Prolog... fib(0, 1). fib(1, 1). fib(N, F) :- N > 1, % *** N1 is N-1, fib(N1, F1), N2 is N-2, fib(N2, F2), F is F1+F2. ?- fib(6, F). F = 13 ?- fib(N, 13). % program assumes N has value... see *** ERROR

  7. Example: Integers and Fibonacci COSC 2P93 Prolog: CLP % CLP... fib(N, F) :- {N=0, F=1}. fib(N, F) :- {N=1, F=1}. fib(N, F) :- { N>1, F=F1+F2, N1 = N-1, N2 = N-2 }, fib(N1, F1), fib(N2, F2). ?- fib(N, 13). N = 6 ?- fib(N, 4). INF LOOP -> stack overflow (no fibonacci number 4)

  8. Fibonacci % CLP... fib(N, F) :- {N=0, F=1}. fib(N, F) :- {N=1, F=1}. fib(N, F) :- {N>1, F=F1+F2, N1 = N-1, N2 = N-2, F1 >= N1, % new constraints F2 >= N2}, fib(N1, F1), fib(N2, F2). COSC 2P93 Prolog: CLP • Infinite loop happens because constraint solver doesn’t know that there is no fib number = 4; keeps recursing, finding larger and larger possibilities • Solution: add more constraints • Fib(N) >= N • then it won’t search for any N > F

  9. Example: money puzzle! COSC 2P93 Prolog: CLP • Module clpfd (finite domains) uses its own operators, utilities • Note: no { }’s like clpq, clpr #= is constraint ‘=‘ (equality) all_different/1: indicates all variables in argument must differ domain/3: indicates integer domain of variables labeling/2: does the actual inference (backtracking, etc.)

  10. Money puzzle COSC 2P93 Prolog: CLP ?- use_module(library(clpfd)). % step 1: declare domains of the variables % step 2: post the problem constraints % step 3: money([S,E,N,D,M,O,R,Y], Type) :- domain([S,E,N,D,M,O,R,Y], 0, 9), % step 1 S#>0, M#>0, all_different([S,E,N,D,M,O,R,Y]), % step 2a sum(S,E,N,D,M,O,R,Y), % step 2b labeling(Type, [S,E,N,D,M,O,R,Y]). % step 3 sum(S,E,N,D,M,O,R,Y) :- 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E #= 10000*M + 1000*O + 100*N + 10*E + Y.

  11. More clpfd features COSC 2P93 Prolog: CLP Expr1 #= Expr2 : Expr1 equals Expr Expr1 #> Expr2 : Expr1 is strictly larger than Expr2 P #\/ Q : True iff either P or Q ?- X #> 3. X in 4..sup. ?- X #\= 20. X in inf..19\/21..sup. ?- X*X #= 144. X in -12\/12. ... And many more!

  12. Conclusion COSC 2P93 Prolog: CLP • CLP is an advanced and powerful extension of logic programming • gives Prolog mathematical intelligence about arithmetic, sets, numbers • Prolog’s search still used; constraints help guide search, and are used to determine values and ranges • Many nontrivial problems are solvable with CLP, which would be difficult for plain Prolog.

More Related