Cs774 (Prasad)L12CLP1 Constraint Logic Programming [email protected]

20
cs774 (Prasad) L12CLP 1 Constraint Logic Programming [email protected] http://www.knoesis.org/ tkprasad/

Transcript of Cs774 (Prasad)L12CLP1 Constraint Logic Programming [email protected]

Page 1: Cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu

cs774 (Prasad) L12CLP 1

Constraint Logic Programming

[email protected]

http://www.knoesis.org/tkprasad/

Page 2: Cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu

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) L12CLP 2

Page 3: Cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu

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) L12CLP 3

Page 4: Cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu

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) L12CLP 4

Page 5: Cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu

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) L12CLP 5

Page 6: Cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu

CLP : Motivationfib(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) L12CLP 6

Page 7: Cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu

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) L12CLP 7

Page 8: Cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu

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))

Infinite solution: *Set of constraints**Set of constraints*

cs774 (Prasad) L12CLP 8

Page 9: Cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu

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) L12CLP 9

Page 10: Cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu

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) L12CLP 10

Page 11: Cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu

CLP : Linear Programminglight_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) L12CLP 11

Page 12: Cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu

?- 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) L12CLP 12

Page 13: Cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu

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) L12CLP 13

Page 14: Cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu

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) L12CLP 14

Page 15: Cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu

(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.731cs774 (Prasad) L12CLP 15

Page 16: Cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu

(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) L12CLP 16

Page 17: Cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu

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) L12CLP 17

Page 18: Cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu

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) L12CLP 18

Page 19: Cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu

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) L12CLP 19

Page 20: Cs774 (Prasad)L12CLP1 Constraint Logic Programming t.k.prasad@wright.edu

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) L12CLP 20