38
1 CMSC 471 CMSC 471 Fall 2009 Fall 2009 Class #7 – Tuesday, September 22 Chapter 5: Constraints Prof. Marie desJardins

CMSC 471 Fall 2009

  • Upload
    becka

  • View
    60

  • Download
    0

Embed Size (px)

DESCRIPTION

CMSC 471 Fall 2009. Chapter 5: Constraints Prof. Marie desJardins. Class #7 – Tuesday, September 22. On Lisp. “Tell your students they'll learn to love it, I did.” Mike Pickens ’03 (CMSC 471 student, Fall 2002). Today’s class. - PowerPoint PPT Presentation

Citation preview

Page 1: CMSC 471 Fall 2009

1

CMSC 471CMSC 471Fall 2009Fall 2009

Class #7 – Tuesday, September 22

Chapter 5: Constraints

Prof. Marie desJardins

Page 2: CMSC 471 Fall 2009

2

On Lisp

• “Tell your students they'll learn to love it, I did.”– Mike Pickens ’03 (CMSC 471 student, Fall 2002)

Page 3: CMSC 471 Fall 2009

3

Today’s class

• Constraint Processing / Constraint Satisfaction Problem (CSP) paradigm

• Algorithms for CSPs– Backtracking (systematic search)– Constraint propagation (k-consistency)– Variable and value ordering heuristics– Intelligent backtracking

Page 4: CMSC 471 Fall 2009

4

Constraint Constraint SatisfactionSatisfaction

Russell & Norvig Ch. 5.2-5.3

Page 5: CMSC 471 Fall 2009

5

Overview

• Constraint satisfaction offers a powerful problem-solving paradigm– View a problem as a set of variables to which we have to assign

values that satisfy a number of problem-specific constraints.– Constraint programming, constraint satisfaction problems (CSPs),

constraint logic programming…

• Algorithms for CSPs– Backtracking (systematic search)– Constraint propagation (k-consistency)– Variable and value ordering heuristics– Backjumping and dependency-directed backtracking

Page 6: CMSC 471 Fall 2009

6

Informal definition of CSP• CSP = Constraint Satisfaction Problem• Given

(1) a finite set of variables(2) each with a domain of possible values (often finite)(3) a set of constraints that limit the values the variables

can take on• A solution is an assignment of a value to each variable such

that the constraints are all satisfied.• Tasks might be to decide if a solution exists, to find a

solution, to find all solutions, or to find the “best solution” according to some metric (objective function).

Page 7: CMSC 471 Fall 2009

7

Informal example: Map coloring• Color the following map using three colors

(red, green, blue) such that no two adjacent regions have the same color.

E

D A

C

B

Page 8: CMSC 471 Fall 2009

8

Map coloring II

• Variables: A, B, C, D, E all of domain RGB• Domains: RGB = {red, green, blue}• Constraints: AB, AC,A E, A D, B C, C D, D

E• One solution: A=red, B=green, C=blue, D=green, E=blue

ED A

CB

ED A

CB

=>

Page 9: CMSC 471 Fall 2009

9

Example: SATisfiability

• Given a set of propositions containing variables, find an assignment of the variables to {false,true} that satisfies them.

• For example, the clauses:– (A B C) ( A D)– (equivalent to (C A) (B D A)

are satisfied byA = falseB = trueC = falseD = false

Page 10: CMSC 471 Fall 2009

10

Real-world problems

• Scheduling• Temporal reasoning• Building design• Planning• Optimization/satisfaction• Vision

• Graph layout• Network management• Natural language

processing• Molecular biology /

genomics• VLSI design

Page 11: CMSC 471 Fall 2009

11

Formal definition of a constraint network (CN)

A constraint network (CN) consists of• a set of variables X = {x1, x2, … xn}

– each with an associated domain of values {d1, d2, … dn}. – the domains are typically finite

• a set of constraints {c1, c2 … cm} where – each constraint defines a predicate which is a relation over

a particular subset of X. – e.g., Ci involves variables {Xi1, Xi2, … Xik} and defines the

relation Ri Di1 x Di2 x … Dik

• Unary constraint: only involves one variable• Binary constraint: only involves two variables

Page 12: CMSC 471 Fall 2009

12

Formal definition of a CN (cont.)

• Instantiations– An instantiation of a subset of variables S is an

assignment of a value in its domain to each variable in S

– An instantiation is legal iff it does not violate any constraints.

• A solution is an instantiation of all of the variables in the network.

Page 13: CMSC 471 Fall 2009

13

Typical tasks for CSP

• Solutions:– Does a solution exist?– Find one solution– Find all solutions– Given a partial instantiation, do any of the above

• Transform the CN into an equivalent CN that is easier to solve.

Page 14: CMSC 471 Fall 2009

14

Binary CSP

• A binary CSP is a CSP in which all of the constraints are binary or unary.

• Any non-binary CSP can be converted into a binary CSP by introducing additional variables.

• A binary CSP can be represented as a constraint graph, which has a node for each variable and an arc between two nodes if and only there is a constraint involving the two variables.– Unary constraint appears as a self-referential arc

Page 15: CMSC 471 Fall 2009

15

Example: Crossword puzzle

1 2 3

4

5

Page 16: CMSC 471 Fall 2009

16

Running example: XWORD puzzle

• Variables and their domains– X1 is 1 across D1 is 5-letter words– X2 is 2 down D2 is 4-letter words– X3 is 3 down D3 is 3-letter words– X4 is 4 across D4 is 4-letter words– X5 is 5 across D5 is 2-letter words

• Constraints (implicit/intensional)– C12 is “the 3rd letter of X1 must equal the 1st letter of X2”– C13 is “the 5th letter of X1 must equal the 1st letter of X3”– C24 is …– C25 is …– C34 is ...

Page 17: CMSC 471 Fall 2009

17

1 2 3

4

5

Variables: X1X2X3X4

Domains:D1 = {astar, happy, hello, hoses}D2 = {live, load, peal, peel, save, talk}D3 = {ant, oak, old}D4 = {live, load, peal, peel, save, talk}

Constraints (explicit/extensional):C12 = {(astar, talk), (happy, peal), (happy, peel), (hello, live) …}C13 = ...

X1

X4

X2 X3

Page 18: CMSC 471 Fall 2009

18

Solving constraint problems

• Systematic search– Generate and test– Backtracking

• Constraint propagation (consistency)• Variable ordering heuristics• Value ordering heuristics• Backjumping and dependency-directed

backtracking

Page 19: CMSC 471 Fall 2009

19

Generate and test: XWORD

• Try each possible combination until you find one that works:– astar – live – ant – live – astar – live – ant – load – astar – live – ant – peal– …

• Doesn’t check constraints until all variables have been instantiated

• Very inefficient way to explore the space of possibilities (4*6*3*6 = 432 for this trivial problem, most illegal)

Page 20: CMSC 471 Fall 2009

20

Systematic search: Backtracking(a.k.a. depth-first search!)

• Consider the variables in some order• Pick an unassigned variable and give it a provisional

value such that it is consistent with all of the constraints• If no such assignment can be made, we’ve reached a

dead end and need to backtrack to the previous variable• Continue this process until a solution is found or we

backtrack to the initial variable and have exhausted all possible values

Page 21: CMSC 471 Fall 2009

21

Backtracking: XWORD1 2 3

4

5

X1=astar X1=happy

X2=load X2=talk

X2=liveX2=live

……

X3=ant X3=oak X3=old

a s t a run

alk

Page 22: CMSC 471 Fall 2009

22

Problems with backtracking

• Thrashing: keep repeating the same failed variable assignments– Consistency checking can help– Intelligent backtracking schemes can also help

• Inefficiency: can explore areas of the search space that aren’t likely to succeed– Variable ordering can help

Page 23: CMSC 471 Fall 2009

23

Consistency• Node consistency

– A node X is node-consistent if every value in the domain of X is consistent with X’s unary constraints

– A graph is node-consistent if all nodes are node-consistent• Arc consistency

– An arc (X, Y) is arc-consistent if, for every value x of X, there is a value y for Y that satisfies the constraint represented by the arc.

– A graph is arc-consistent if all arcs are arc-consistent.• To create arc consistency, we perform constraint

propagation: that is, we repeatedly reduce the domain of each variable to be consistent with its arcs

Page 24: CMSC 471 Fall 2009

24

Constraint propagation: XWORD example

1 2 3

4

5

X1 X2 X4

astarhappyhellohoses

liveloadpealpeelsavetalk

liveloadpealpeelsavetalk

….No more changes!

Page 25: CMSC 471 Fall 2009

25

A famous example:Labelling line drawings

• Waltz labelling algorithm – one of the earliest CSP applications– Convex interior lines are labelled as +– Concave interior lines are labeled as –– Boundary lines are labeled as

• There are 208 labellings (most of which are impossible)• Here are the 18 legal labellings:

Page 26: CMSC 471 Fall 2009

26

Labelling line drawings II

• Here are some illegal labelings:

+ + --

-

Page 27: CMSC 471 Fall 2009

27

Labelling line drawings (cont.)

• Waltz labelling algorithm: Propagate constraints repeatedly until a solution is found

A solution for one labelling problem

A labelling problem with no solution

Page 28: CMSC 471 Fall 2009

31

Tree-structured constraint graph• A constraint tree rooted at V1 satisfies the following property:

– There exists an ordering V1, …, Vn such that every node has zero or one parents (i.e., each node only has constraints with at most one “earlier” node in the ordering)

– Also known as an ordered constraint graph with width 1• If this constraint tree is also node- and arc-consistent (a.k.a. strongly 2-

consistent), then it can be solved without backtracking• (More generally, if the ordered graph is strongly k-consistent, and has

width w < k, then it can be solved without backtracking.)

V1V8 V4 V7

V6V10V9

V5V3V2

Page 29: CMSC 471 Fall 2009

32

Proof sketch for constraint trees

• Perform backtracking search in the order that satisfies the constraint tree condition

• Every node, when instantiated, is constrained only by at most one previous node

• Arc consistency tells us that there must be at least one legal instantiation in this case– (If there are no legal solutions, the arc consistency procedure will

collapse the graph – some node will have no legal instantiations)

• Keep doing this for all n nodes, and you have a legal solution – without backtracking!

Page 30: CMSC 471 Fall 2009

34

So what if we don’t have a tree?

• Answer #1: Try interleaving constraint propagation and backtracking

• Answer #2: Try using variable-ordering heuristics to improve search

• Answer #3: Try using value-ordering heuristics during variable instantiation

• Answer #4: See if iterative repair works better• Answer #5: Try using intelligent backtracking methods

Page 31: CMSC 471 Fall 2009

35

Interleaving constraint propagation and search

Generate and Test

No constraint propagation: assign all variable values, then test constraints

Simple Backtracking

Check constraints only for variables “up the tree”

Forward Checking

Check constraints for immediate neighbors “down the tree”

Partial Lookahead

Propagate constraints forward “down the tree”

Full Lookahead

Ensure complete arc consistency after each instantiation (AC-3)

Page 32: CMSC 471 Fall 2009

36

Variable ordering• Intuition: choose variables that are highly constrained early in

the search process; leave easy ones for later• Minimum width ordering (MWO): identify OCG with

minimum width• Maximum cardinality ordering: approximation of MWO

that’s cheaper to compute: order variables by decreasing cardinality (a.k.a. degree heuristic)

• Fail first principle (FFP): choose variable with the fewest values (a.k.a. minimum remaining values (MRV))– Static FFP: use domain size of variables– Dynamic FFP (search rearrangement method): At each point in the

search, select the variable with the fewest remaining values

Page 33: CMSC 471 Fall 2009

37

Variable ordering II

• Maximal stable set: find largest set of variables with no constraints between them and save these for last

• Cycle-cutset tree creation: Find a set of variables that, once instantiated, leave a tree of uninstantiated variables; solve these, then solve the tree without backtracking

• Tree decomposition: Construct a tree-structured set of connected subproblems

Page 34: CMSC 471 Fall 2009

38

Value ordering

• Intuition: Choose values that are the least constrained early on, leaving the most legal values in later variables

• Maximal options method (a.k.a. least-constraining-value heuristic): Choose the value that leaves the most legal values in uninstantiated variables

• Min-conflicts: Used in iterative repair search (see below)

Page 35: CMSC 471 Fall 2009

39

Iterative repair

• Start with an initial complete (but invalid) assignment• Hill climbing, simulated annealing• Min-conflicts: Select new values that minimally conflict

with the other variables– Use in conjunction with hill climbing or simulated annealing or…

• Local maxima strategies– Random restart– Random walk– Tabu search: don’t try recently attempted values

Page 36: CMSC 471 Fall 2009

40

Min-conflicts heuristic

• Iterative repair method1. Find some “reasonably good” initial solution

– E.g., in N-queens problem, use greedy search through rows, putting each queen where it conflicts with the smallest number of previously placed queens, breaking ties randomly

2. Find a variable in conflict (randomly)3. Select a new value that minimizes the number of constraint

violations– O(N) time and space

4. Repeat steps 2 and 3 until done• Performance depends on quality and informativeness of

initial assignment; inversely related to distance to solution

Page 37: CMSC 471 Fall 2009

42

Some challenges for constraint reasoning

• What if not all constraints can be satisfied?– Hard vs. soft constraints– Degree of constraint satisfaction– Cost of violating constraints

• What if constraints are of different forms?– Symbolic constraints– Numerical constraints [constraint solving]– Temporal constraints– Mixed constraints

Page 38: CMSC 471 Fall 2009

43

Some challenges for constraint reasoning II

• What if constraints are represented intensionally?– Cost of evaluating constraints (time, memory, resources)

• What if constraints, variables, and/or values change over time?– Dynamic constraint networks– Temporal constraint networks– Constraint repair

• What if you have multiple agents or systems involved in constraint satisfaction?– Distributed CSPs– Localization techniques