Constraint Logic Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Constraint Logic Programming

Description:

Title: Logic Programming Author: T. K. Prasad Last modified by: test Created Date: 9/30/1996 6:28:10 PM Document presentation format: On-screen Show (4:3) – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 21
Provided by: tk4
Learn more at: http://cecs.wright.edu
Category:

less

Transcript and Presenter's Notes

Title: Constraint Logic Programming


1
Constraint Logic Programming
  • t.k.prasad_at_wright.edu
  • http//www.knoesis.org/tkprasad/

2
Ordinary Logic Programming
  • ?- p(a,X) p(Y,b).
  • Ya Xb
  • ?- p(X,X) p(a,b).
  • fails
  • Unification solves equality constraints (under
    unique name hypothesis)

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

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

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

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)

7
Introducing the Theory of Arithmetic
  • fib(0,1).
  • fib(1,1).
  • fib(N, X1 X2) - N gt 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 lt X, X lt 25.
  • N 6. X 13.
  • N 7. X 21.

8
Solving Simultaneous Equations
  • ?- X Y 12, 2X 4Y 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

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

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

11
CLP Linear Programming
  • light_meal(A,M,D) - appetizer(A,I),
  • main_course(M,J),
    dessert(D,K),
  • I gt 0, J gt 0, K gt 0,
  • I J K lt 12.
  • appetizer(soup,1).
  • appetizer(nachos,6).
  • main_course(sphagetti,3).
  • main_course(alfredo,7).
  • dessert(fruit,2).
  • dessert(ice_cream,6).

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

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

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

15
(contd)
  • 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

16
(contd)
  • 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

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

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

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,225180 G).
  • G 19640

20
CLP Limitations
  • ?- (Cows Pigs Sheeps) 100,
  • (10Cows 3Pigs Sheeps/2) 100,
  • Cows gt 1, Pigs gt 1, Sheeps gt 1.
  • Pigs -1.35714Sheeps 128.571
  • Cows 0.357143Sheeps - 28.5714
  • Sheeps lt 94
  • Lots of constraints
  • CLP(R) is unable to determine the unique
    solution
  • Cows 5, Pigs 1, Sheeps 94
Write a Comment
User Comments (0)
About PowerShow.com