Presentation is loading. Please wait.

Presentation is loading. Please wait.

Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.

Similar presentations


Presentation on theme: "Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming."— Presentation transcript:

1 Constraint Logic Programming Ryan Kinworthy

2 Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming Summary

3 Introduction Up till now…  Used consistency enforcing combined with backtracking search to solve CSP’s  This approach is: Effective, however… It makes assumptions which don’t always hold:  All constraints are available at the beginning of the search process  Constraints are represented extensionally as sets of allowed tuples

4 Introduction Now we need…  Incremental construction of the constraint problem  Representation of constraints via a language rather than tuples Solution  Embed constraints in a high-level language This allows for a more flexible and practical environment Constraints can now be incrementally formulated

5 Introduction  Some programming environments are more suitable than others Last semester  Did you try coding a CSP assignment in Java or C++?  How about LISP? Object-Oriented languages are poor vehicles for CSPs Logic programming makes a great environment

6 Introduction Logic programming is suitable  Several properties of LP make this appealing Constraints can be seen as relations or predicates Conjunctions of relations are logical ands Backtracking search is the means to solve them

7 Logic Programming What is it?  Programs are made up of Logical implications between collections of predicates Rather than statements or functions What does it look like?  Logic programs are made up of clauses These clauses relate the truth value of a literal (head of clause) to that of a collection of other literals (body of clause)

8 LP Syntax Clauses  Form H :- B, where B = B 1,…, B m and both B and H are positive literals. Each literal involves an n-ary predicate p, and has the form p(t 1, …, t n ) where the t i ’s are terms A term can be:  A variable x  A constant a  The application of an n-ary function to n terms f(t 1, …, t n )

9 Interpreting LP clauses Body  Interpreted as a logical conjunction of literals  Symbol :- is an implication from B to H If B is true, then H must also be true Note: since B is a conjunction of literals, if B is true then all literals in B must be true  Sets S H is the set of variables appearing in H S B is the set of variables appearing in B but not H

10 Interpreting LP clauses (cont.) Interpreting H :- B  This clause can be interpreted as the formula For all values of the variables in S H there exist values for the variables in S B such that B implies H.  Example 15.1.2 and 15.1.3 Page 415. Clauses with no body  Written as H… are called facts  H is true for any values of its variables

11 Logic Programs What is a logic program?  A collection of clauses  Example 15.1.5 Page 416 What is the aim of LP?  To decide the truth value of the goal statement Written as :- G G is any collection of positive literals Truth value of G is seeing if there are values for all variables in G to make them all true, given the clauses in the logic program Example 15.1.6 Find answer to goal by recursively unifying the current goal with the head of each clause

12 LP Terminology Unification of two literals  Find an assignment of terms which makes them equal Substitution  An assignment of terms to variables A   A new literal created from the application of a substitution  to the variables of literal A mgu  Most general unifier This is desired since the most general unifier subsumes all the others. So the mgu is the substitution that instantiates the variables as little as possible while still making the two literals equal Example 15.1.8

13 How Are LP’s Solved? The goal  Given a goal G = A 1,….,A n, We want to prove values can be found for all A’s which make it true in the given logic program How is this done?  Repeatedly transform the goal with resolution steps until we have the empty goal (success) or we cannot continue (failure) or we have an infinite loop

14 Resolution Each step:  Unification between Literal A (G = A,R), which is part of the goal and The head H (H :- B) of a clause  If A and H unify with mgu  Replace the goal G with the new goal (B,R)   In other words Replace A with the body of the clause and apply  to the whole new goal  Rewriting rule Page 417

15 Derivation Def:  A sequence of resolution steps Refutation  A derivation that ends with success Visual representation  A tree

16 Derivation Tree Example Example 15.1.10

17 LP as a CSP Language LP is a good fit for CSP  Derivation tree DFS maps well to BT search in CSPs A CSP is  A set of relations which  Can be modeled by a set of facts where the predicate name corresponds to the constraint name So satisfying all constraints is described by  A clause whose body contains all constraint predicates  H in this clause contains all variables of the constraints  Giving a goal which matches H means asking for a single solution to the CSP Example 15.2.1

18 LP Can Model Any CSP We can find either one or all solutions  Also find complete solutions given partial ones LP can do more  Supports recursion, functions and variables  Example 15.2.2

19 Recap Of CSP Modeling in LP Using LP to model CSPs is simple  However LP imposes restrictions on the solution algorithm which may result in slow run times No propagation capability LP’s solution engine is equivalent to DFS with chronological BT Obviously room for improvement  CLP languages greatly improve on this strategy

20 Constraint Logic Programming How are constraints added to LP?  Consider one specific constraint type (e.g. linear equations)  In addition to LP resolution engine Add a constraint solver which checks consistency of constraints of the specified type What are the advantages over LP?  Unification is generalized to constraints  Expressing constraints in a language is more general  The addition of a constraint solver allows combinations of BT search and propagation (more efficient)

21 What do we need? A constraint language  To specify the kinds of constraints we’ll use A consistency checker  To solve the CSP A variable elimination procedure  To solve the CSP

22 CLP Syntax Clause  Similar to an LP clause except that it’s body may contain constraints of the considered language  Example 15.3.1 Resolution  In addition to the LP def. of resolution… We also must check the consistency of current set of constraints with the constraints in the body of the clause We need two solvers  Unification  Constraints

23 CLP Resolution Context  In LP, a computation state consists of a goal and a substitution  In CLP, such a state consists of a goal and a set of constraints, called the constraint store  A CLP computation succeeds if you can get from the initial state  G, true  to the goal  G’,S  Where G’ is the empty goal and S is satisfiable

24 CLP Resolution Def. 15.3.2  Given a state of the form  G,S , where G is the current goal and S is the constraint store, assume G = A, R (we want to rewrite A) Resolution steps  If A is a constraint, add A to S Then the new state is  R, prop(S  A)  where prop(C) is the result of some constraint propagation algorithm to the constraint store C  If A is instead a literal, and there is a clause H :- B with the same head-predicate as A, add the constraint A = H to the store and replace A with B Then the new goal is  (B,R), S  {A = H} 

25 CLP Derivation Trees Similar to LP trees but  Each node represents The current goal and The current constraint store  Detection of failing computations Achieved by constantly checking the consistency of the constraint store (each node accumulates the constraints of previous nodes) This is what the prop(C) function does Continue DFS traversal if successful, else BT Note: we can also use constraint propagation techniques, such as AC

26 CLP Example Example 15.3.4  Page 427

27 Other CLP Issues Finite domain constraints Compact representation of constraints Domain ordering Bounds-consistency Global constraints

28 Summary LP is a great platform for CSP programming It’s natural extention is CLP, which is cleaner and more compact and more efficient Anyone doing serious CSP coding should know this paradigm of languages

29 Questions? ?? Have a great summer!


Download ppt "Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming."

Similar presentations


Ads by Google