constraint logic programming

29
Constraint Logic Programming Ryan Kinworthy

Upload: igor-jacobson

Post on 30-Dec-2015

95 views

Category:

Documents


0 download

DESCRIPTION

Constraint Logic Programming. Ryan Kinworthy. Overview. Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming Summary. Introduction. Up till now… Used consistency enforcing combined with backtracking search to solve CSP’s This approach is: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Constraint Logic Programming

Constraint Logic Programming

Ryan Kinworthy

Page 2: Constraint Logic Programming

Overview

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

Page 3: Constraint Logic Programming

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

Page 4: Constraint Logic Programming

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

Page 5: Constraint Logic Programming

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

Page 6: Constraint Logic Programming

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

Page 7: Constraint Logic Programming

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)

Page 8: Constraint Logic Programming

LP Syntax

Clauses Form

H :- B, where B = B1,… , Bm and both B and H are positive literals.

Each literal involves an n-ary predicate p, and has the form p(t1, … , tn) where the ti’s are terms

A term can be: A variable x A constant a The application of an n-ary function to n terms f(t1, … , tn)

Page 9: Constraint Logic Programming

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

SH is the set of variables appearing in H

SB is the set of variables appearing in B but not H

Page 10: Constraint Logic Programming

Interpreting LP clauses (cont.) Interpreting H :- B

This clause can be interpreted as the formula For all values of the variables in SH there exist values for

the variables in SB 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

Page 11: Constraint Logic Programming

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

Page 12: Constraint Logic Programming

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

Page 13: Constraint Logic Programming

How Are LP’s Solved?

The goal Given a goal G = A1,….,An,

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

Page 14: Constraint Logic Programming

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

Page 15: Constraint Logic Programming

Derivation

Def: A sequence of resolution steps

Refutation A derivation that ends with success

Visual representation A tree

Page 16: Constraint Logic Programming

Derivation Tree Example

Example 15.1.10

Page 17: Constraint Logic Programming

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

Page 18: Constraint Logic Programming

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

Page 19: Constraint Logic Programming

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

Page 20: Constraint Logic Programming

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)

Page 21: Constraint Logic Programming

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

Page 22: Constraint Logic Programming

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

Page 23: Constraint Logic Programming

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

Page 24: Constraint Logic Programming

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}

Page 25: Constraint Logic Programming

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

Page 26: Constraint Logic Programming

CLP Example

Example 15.3.4 Page 427

Page 27: Constraint Logic Programming

Other CLP Issues

Finite domain constraints Compact representation of constraints Domain ordering Bounds-consistency Global constraints

Page 28: Constraint Logic Programming

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

Page 29: Constraint Logic Programming

Questions?

??

Have a great summer!