Presentation is loading. Please wait.

Presentation is loading. Please wait.

Cs774 (Prasad)L12CLP1 Constraint Logic Programming

Similar presentations


Presentation on theme: "Cs774 (Prasad)L12CLP1 Constraint Logic Programming"— Presentation transcript:

1 cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu http://www.knoesis.org/tkprasad/

2 Ordinary Logic Programming ?- p(a,X) = p(Y,b). Y=a X=b ?- p(X,X) = p(a,b). *fails* Unification solves equality constraints (under unique name hypothesis) cs774 (Prasad)L12CLP2

3 Constraint Logic Programming ?- X + 2 = 5. *fails in ordinary LP* In CLP, succeeds with: X = 3 Generalizes to constraint satisfaction problem, interpreting “+” as the arithmetic addition operator cs774 (Prasad)L12CLP3

4 Constraint Satisfaction Problem Given a set of variables, their types (domains of values and applicable operations), and constraints on them, determine assignment of values to variables so that the constraints are satisfied. Benefit: Embodiment of Declarative Programming Challenge: Designing tractable constraint languages cs774 (Prasad)L12CLP4

5 Applications Scheduling and Resource Management in production and transportation teachers and courses to classes rooms machines to jobs crews to planes Linear and Non-linear programming problems Mortgage Calculations cs774 (Prasad)L12CLP5

6 CLP : Motivation fib(0,1). fib(1,1). fib(N,X) :- N1 is N-1, N2 is N-2, fib(N1,X1), fib(N2,X2), X is X1 + X2. ?- fib(4,5). Succeeds. ?- fib(X,5). Fails. Treatment of arithmetic expression looks unnatural, as it does not smoothly integrate with the logic programming paradigm (e.g., invertibility destroyed) cs774 (Prasad)L12CLP6

7 Introducing the Theory of Arithmetic fib(0,1). fib(1,1). fib(N, X1 + X2) :- N > 1, fib(N - 1,X1), fib(N - 2,X2). Interpret arithmetic operators in the domain of reals –improves readability –improves invertibility ?- fib(N, 13). N = 6. ?- fib(N, X), 10 =< X, X =< 25. N = 6. X = 13. N = 7. X = 21. cs774 (Prasad)L12CLP7

8 Solving Simultaneous Equations ?- X + Y = 12, 2*X + 4*Y = 34. X = 7. Y = 5. Complex Multiplication zmul(c(R1,I1),c(R2,I2),c(R3,I3)) :- R3 = R1 * R2 - I1 * I2, I3 = R1 * I2 + R2 * I1. ?- zmul(c(2,2),Ans,c(0,16)) Unique solution: Ans = C(4,4) ?- zmul(A,B,c(0,16)) *Set of constraints* Infinite solution: *Set of constraints* cs774 (Prasad)L12CLP8

9 Prolog vs CLP(R) ?- 5 + 2 = X + Y. X = 5 Y = 2 ?- 5 + 2 = X + 3. *fail* Equality constraints over terms ?- 5 + 2 = X + Y. X = 7 - Y ?- 5 + 2 = X + 3. X = 4 General constraints over arithmetic expressions; Equality constraints over terms cs774 (Prasad)L12CLP9

10 Prolog vs CLP(R) Uninterpreted function symbols Unification algorithm Backtrack when terms fail to unify Arithmetic operators + uninterpreted term trees General constraint solvers – E.g., Simplex algorithm for linear constraints Backtrack when constraints violated cs774 (Prasad)L12CLP10

11 CLP : Linear Programming light_meal(A,M,D) :- appetizer(A,I), main_course(M,J), dessert(D,K), I >= 0, J >= 0, K >= 0, I + J + K <= 12. appetizer(soup,1). appetizer(nachos,6). main_course(sphagetti,3). main_course(alfredo,7). dessert(fruit,2). dessert(ice_cream,6). cs774 (Prasad)L12CLP11

12 ?- light_meal(App,Main,Dess). Dess = fruit Main = sphagetti App = soup *** Retry? y Dess = ice_cream Main = sphagetti App = soup *** Retry? y Dess = fruit Main = alfredo App = soup *** Retry? y Dess = fruit Main = sphagetti App = nachos cs774 (Prasad)L12CLP12

13 CLP : Mortgage Payment Calculator %mortgage(Principal,Time_Months, Interest_Rate,Monthly_Payment, Balance) mortgage(P,0,_,_,P). mortgage(P,1,_,_,B) :- B = P * (1 + (I / 1200)) - MP. mortgage(P, T, I, MP, B) :- mortgage( (P * (1 + I / 1200)) - MP, T – 1, I, MP, B). cs774 (Prasad)L12CLP13

14 CLP : Mortgage Payment Queries %mortgage(Principal,Time_Months, Interest_Rate,Monthly_Payment, Balance) %Customer: What will be the monthly payment? ?- mortgage(148000,180,8,M,0). M = 1414.37 %Lender: How much loan does one qualify for, given monthly payment limit? ?- mortgage(P,180,8,1200,0). P = 125569 cs774 (Prasad)L12CLP14

15 (cont’d) %mortgage(Principal,Time_Months, Interest_Rate,Monthly_Payment, Balance) %Customer/Lender: What is the remaining balance? ?- mortgage(148000,180,8.375,1124.91,B). B = 115088 %Customer: How long will it take to clear the debt? ?- mortgage(148000,L,8.5,1400,0). L = 195.731 cs774 (Prasad)L12CLP15

16 (cont’d) %mortgage(Principal,Time_Months, Interest_Rate,Monthly_Payment, Balance) %Customer: What is the contribution to the principal in the first month (in a 15 yr loan at 8 or a 30 yr loan at 8.375)? %Customer: Building equity. ?- mortgage(148000,1,8,1414,148000-CP). CP = 427 ?- mortgage(148000,1,8.375,1124,148000-CP). CP = 92 cs774 (Prasad)L12CLP16

17 Non-linear constraints : CLP(R) Fails Here %mortgage(Principal,Time_Months, Interest_Rate,Monthly_Payment, Balance) %Customer: What is the maximum interest rate for the given principal and monthly payment? %Customer: Affordability ?- mortgage(148000,180,I,1400,0). *lots of constraints output* cs774 (Prasad)L12CLP17

18 CLP : Annuity Calculator %annuity(Time_Months, Interest_Rate,Monthly_Payment, Initial_Principal, Total_Value) annuity(0,_,_,IP,IP). annuity(T, I, MP, IP, TV) :- annuity(T-1, I, MP, IP, V), TV = MP + (V * (1 + I / 1200)). cs774 (Prasad)L12CLP18

19 CLP : Annuity Queries %annuity(Time_Months, Interest_Rate,Monthly_Payment, Initial_Principal, Total_Value) %Customer: What will be the final investment value? ?- annuity(180,5,225,0,FV). FV = 60140 %Customer: What is the net gain? ?- annuity(180,5,225,0,225*180 + G). G = 19640 cs774 (Prasad)L12CLP19

20 CLP : Limitations ?- (Cows + Pigs + Sheeps) = 100, (10*Cows + 3*Pigs + Sheeps/2) = 100, Cows >= 1, Pigs >= 1, Sheeps >= 1. Pigs = -1.35714*Sheeps + 128.571 Cows = 0.357143*Sheeps - 28.5714 Sheeps <= 94 * Lots of constraints * CLP(R) is unable to determine the unique solution: Cows = 5, Pigs = 1, Sheeps = 94 cs774 (Prasad)L12CLP20


Download ppt "Cs774 (Prasad)L12CLP1 Constraint Logic Programming"

Similar presentations


Ads by Google