Example: N-Queens (1) Slightly Less Informal Standard search - - PDF document

example n queens 1
SMART_READER_LITE
LIVE PREVIEW

Example: N-Queens (1) Slightly Less Informal Standard search - - PDF document

Bookkeeping Constraint Satisfaction HW 2 questions? AI Class 7 Ch. 6.16.4 (skip 6.3.3) ) Please note: it must be due COB Blackboard time 11:59: XX is late! We will not let this slide again. Based on slides by: Marie


slide-1
SLIDE 1

1

Constraint Satisfaction

AI Class 7 — Ch. 6.1–6.4 (skip 6.3.3))

Cynthia Matuszek – CMSC 671

Based on slides by: Marie desJardin, Paula Matuszek, Luke Zettlemoyer, Dan Klein, Stuart Russell, Andrew Moore

Bookkeeping

  • HW 2 questions?
  • Please note: it must be due COB Blackboard time
  • 11:59:XX is late!
  • We will not let this slide again.
  • Need to upload .py files – see Nikhil if it’s not working

2

Today’s Class

  • Constraint Satisfaction Problems
  • A.K.A., Constraint Processing / CSP paradigm
  • Algorithms for CSPs
  • Search Terminology

3

Constraint (n): A relation … between the values of one or more mathematical variables (e.g., x>3 is a constraint on x). Constraint satisfaction assigns values to variables so that all constraints are true. – http://foldoc.org/constraint

Constraint Satisfaction

  • Con•straint /kənˈstrānt/, (noun):
  • Something that limits or restricts someone or something.1
  • Control that limits or restricts someone’s actions or behavior.1
  • A relation … between the values of one or more mathematical

variables (e.g., x>3 is a constraint on x).2

  • Assigns values to variables so that all constraints are true.2
  • General Idea
  • View a problem as a set of variables
  • To which we have to assign values
  • That satisfy a number of (problem-specific) constraints

4

[1] Merriam-Webster online. [2] The Free Online Computing Dictionary.

Overview

  • Constraint satisfaction: a problem-solving

paradigm

  • Constraint programming, constraint satisfaction

problems (CSPs), constraint logic programming…

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

5

Search Vocabulary

  • We’ve talked about caring about goals (end states) vs. paths
  • These correspond to…
  • Planning: finding sequences of actions
  • The path to the goal is the important thing
  • Paths have various costs, depths
  • Heuristics to guide, fringe to keep backups
  • Identification: assignments to variables representing

unknowns

  • The goal itself is important, not the path
  • CSPs are specialized for identification problems

6

slide-2
SLIDE 2

2

Slightly Less Informal Definition of CSP

7

  • CSP = Constraint Satisfaction

Problem

  • Given:
  • 1. A finite set of variables
  • 2. Each with a domain of possible values

they can take (often finite)

  • 3. A set of constraints that limit the

values the variables can take on

  • Solution: an assignment of a value

to each variable such that the constraints are all satisfied.

CSP Applications

  • Decide if a solution exists
  • Find some solution
  • Find all solutions
  • Find the “best solution”
  • According to some metric (objective function)
  • Does that mean “optimal”?

8

Informal Example: Map Coloring

  • Color a map, such that:
  • Using three colors (red, green, blue)
  • No two adjacent regions have the same color

9

E D A C B

Map Coloring II

  • Variables: A, B, C, D, E
  • Domains: RGB = {red, green, blue}
  • Constraints: A≠B, A≠C, A ≠ E, A ≠ D, B ≠ C, C ≠ D, D ≠ E
  • One solution: A=red, B=green, C=blue, D=green, E=blue

10

E D A C B E D A C B

Slightly Less Informal

  • Standard search problems:
  • State is a “black box”: arbitrary data structure
  • Goal test: any function over states
  • Successor function can be anything
  • Constraint satisfaction problems (CSPs):
  • A special subset of search problems
  • State is defined by variables Xi with values from a

domain D

  • Sometimes D depends on i
  • Goal test is a set of constraints specifying allowable

combinations of values for subsets of variables

Example: N-Queens (1)

  • Formulation 1:
  • Variables:
  • Domains:
  • Constraints:
slide-3
SLIDE 3

3

Example: N-Queens (2)

  • Formulation 2:
  • Variables:
  • Domains:
  • Constraints:

Implicit: Explicit:

  • or-

Special case!

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 by

A = false B = true C = false D = false

14

Real-World Problems

15

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

satisfaction

  • Vision
  • Graph layout
  • Network management
  • Natural language

processing

  • Molecular biology /

genomics

  • VLSI design

Formal Definition: 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

some subset of X.

  • E.g., ci involves variables {Xi1, Xi2, … Xik} and defines the

relation Ri ⊆ Di1 × Di2 × … Dik

16

  • Unary constraint: only involves one variable
  • e.g.: C can’t be green.
  • Binary constraint: only involves two variables
  • e.g.: E ≠ D

17

E D A C B

Constraint Restrictions

Formal Definition of a CN (cont.)

  • An instantiation is an assignment of a value

dx ∈ D to some subset of variables S.

  • Ex: Q2 = {2,3} ∧ Q3 = {1,1} instantiates Q2 and Q3
  • An instantiation is legal iff it does not violate

any constraints

  • A solution is an instantiation of all variables
  • A correct solution is a legal instantiation of all variables

18

slide-4
SLIDE 4

4

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

19

Binary CSP

  • Binary CSP: all constraints are binary or unary
  • Can convert a non-binary CSP à binary CSP by:
  • Introducing additional variables
  • Dual graph construction: one variable for each constraint; one

binary constraint for each pair of original constraints that share variables

  • Can represent a binary CSP as a constraint graph with:
  • A node for each variable
  • An arc between two nodes iff there is a constraint on the two

variables

  • Unary constraint appears as a self-referential arc

20

Example: Sudoku

  • Variables
  • vi,j is the value in the

j th cell of the i th row

  • Domains
  • Di,j = D = {1, 2, 3, 4}
  • Blocks:
  • B1 = {11, 12, 21, 22}, …, B4 = {33, 34, 43, 44}

21

3 1 1 4 3 4 1 2 4 v11 3 v13 1 v21 1 v23 4 3 4 1 2 v41 v42 4 v44

Running Example: Sudoku

  • Constraints (implicit/intensional)
  • CR : ∀i, ∪j vij = D

(every value appears in every row)

  • CC : ∀j, ∪i vij = D

(every value appears in every column)

  • CB : ∀k, ∪ (vij | ij ∈Bk) = D

(every value appears in every block)

  • Alternative representation: pairwise inequality constraints
  • IR : ∀i, j≠j’ : vij ≠ vij’

(no value appears twice in any row)

  • IC : ∀j, i≠i’ : vij ≠ vi’j

(no value appears twice in any column)

  • IB : ∀k, ij ∈ Bk, i’j’ ∈ Bk, ij ≠ i’j’ :vij ≠ vi’j‘

(no value appears twice in any block)

22

v11 3 v13 1 v21 1 v23 4 3 4 1 2 v41 v42 4 v44

Advantage of the second representation: all binary constraints!

Sudoku Constraint Network

23

v11 v44 v42 v41 v23 v21 v13 3 1 1 4 3 4 1 2 4 v11 3 v13 1 v21 1 v23 4 3 4 1 2 v41 v42 4 v44

Solving Constraint Problems

  • 1. Systematic search
  • Generate and test
  • Backtracking
  • 2. Constraint propagation (consistency)
  • 3. Variable ordering heuristics
  • 4. Value ordering heuristics
  • 5. Backjumping and dependency-directed

backtracking

24

slide-5
SLIDE 5

5

Generate and Test: Sudoku

  • Try every possible assignment of domain elements to

variables until you find one that works:

  • Doesn’t check constraints until all variables have been

instantiated

  • Very inefficient way to explore the space of possibilities (4^7

for this trivial Sudoku puzzle, most illegal)

25

1 3 1 1 1 1 1 4 3 4 1 2 1 1 4 1 1 3 1 1 1 1 1 4 3 4 1 2 1 1 4 2 1 3 1 1 1 1 1 4 3 4 1 2 1 1 4 3

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

26

Problems with Backtracking

  • Thrashing: keep repeating same

failed variable assignments

  • Consistency checking can help
  • Intelligent backtracking schemes can also help
  • Inefficiency: can spend time exploring areas
  • f search space that aren’t likely to succeed
  • Variable ordering can help
  • IF there’s a meaningful way to order them

28

v11 3 v13 1 v21 1 v23 4 3 4 1 2 v41 v42 4 v44

Consistency

  • An assignment of values to variables is said to be

consistent if no constraints are violated

  • There are multiple kinds of consistency
  • Once the whole graph is consistent, we have a

solution

29

Node and Arc Consistency

  • Node consistency: every value in node X’s domain

is consistent with X’s unary constraints

  • A graph is node-consistent if all nodes are node-consistent
  • South Australia can’t be green
  • SA = {red, green, blue}
  • Arc consistency: for every value x of X in

Arc(X,Y), ∃y for Y that satisfies the constraint represented by the arc

  • A graph is arc-consistent if all arcs are arc-consistent

30

Constraint Propagation

  • How do we find a set of consistent assignments?
  • We perform constraint propagation
  • That is, we repeatedly reduce the domain of each variable to be

consistent with its arcs

  • Constraints reduce # of legal values for a variable
  • Which may then reduce legal values of another variable
  • Then another, then another…
  • Key idea: local consistency
  • Enforce nearby constraints
  • Propagate

31

slide-6
SLIDE 6

6

Constraint Propagation: Sudoku

32

v11 3 v13 1 v21 1 v23 4 3 4 1 2 v41 v42 4 v44 v11 v44 v42 v41 v23 v21 v13 2,4 3 2 1,2 2,3 2 2

Arc consistency

4 3 2 1 3 2 2

Node consistency …and we didn’t even need to search!

Example: Map-Coloring

  • Variables:
  • Domain:
  • Constraints: adjacent regions

must have different colors

  • Ex:
  • Solutions are assignments satisfying all constraints, e.g.:

WA, NT, Q, NSW, V, SA, T D = {red, green, blue }

WA ≠ NT (WA, NT ) ∈ {(red, green), (red, blue), (green, blue), (green, red), (blue, red)}

{WA = red, NT = green, Q = red, NSW = green, V = red, SA = blue, T = green }

Constraint Graphs

  • Binary CSP: each constraint relates (at most) two variables
  • Binary constraint graph: nodes are variables, arcs show

constraints

  • General-purpose CSP algorithms use the graph structure to

speed up search. E.g., Tasmania is an independent subproblem!

Standard Search Formulation

  • Standard search formulation of CSPs (incremental)
  • Let’s start with a straightforward, dumb approach,

then fix it

  • States are defined by the values assigned so far
  • Initial state: the empty assignment, {}
  • Successor function: assign a value to an unassigned

variable

  • Goal test: the current assignment is complete and

satisfies all constraints

Search Methods

  • What does BFS do?
  • What does DFS do?

DFS & BFS: not good!

slide-7
SLIDE 7

7

Backtracking Search

  • Idea 1: Only consider a single variable at each point
  • Variable assignments are commutative, so fix ordering
  • I.e., [WA = red then NT = green] same as [NT = green then WA = red]
  • Only need to consider assignments to a single variable at each step
  • How many leaves are there now?
  • Idea 2: Only allow legal assignments at each point
  • I.e. consider only values which do not conflict previous assignments
  • Might have to do some computation to figure out whether a value is ok
  • “Incremental goal test”

Backtracking Search

  • Idea 1: Only consider a single variable at each point
  • Idea 2: Only allow legal assignments at each point
  • DFS for CSPs with these two improvements is called

backtracking search

  • We backtrack when there’s no legal assignment for the next

variable

  • Backtracking search is the basic uninformed algorithm

for CSPs

  • Can solve n-queens for n ≈ 25

Backtracking Search

  • What are the choice points?

Backtracking Example Backtracking Good Enough?

slide-8
SLIDE 8

8

Improving Backtracking

  • General-purpose ideas give huge gains in speed
  • Ordering:
  • Which variable should be assigned next?
  • In what order should its values be tried?
  • Filtering: Can we detect inevitable failure early?
  • Structure: Can we exploit the problem structure?

Forward Checking .

  • Idea: Keep track of remaining legal values for unassigned

variables (using immediate constraints); terminate when any variable has no legal values

Forward Checking

  • Propagates information from assigned to adjacent unassigned variables
  • But doesn’t detect more distant failures
  • NT and SA cannot both be blue!
  • Why didn’t we detect this yet?
  • Constraint propagation repeatedly enforces constraints locally – this is a

local maximum!

Forward Checking Are We Done? Arc Consistency

  • Simplest form of propagation makes each arc consistent
  • X → Y is consistent iff for every value x there is some allowed y
  • If X loses a value, neighbors of X need to be rechecked!
  • Arc consistency detects failure earlier than forward checking
  • What’s the downside of arc consistency?
  • Can be run as a preprocessor or after each assignment
slide-9
SLIDE 9

9

K-consistency

  • K-consistency generalizes the notion of arc consistency

to sets of more than two variables

  • A graph is K-consistent if, for legal values of any K-1

variables in the graph, and for any Kth variable Vk, there is a legal value for Vk

  • Strong K-consistency = J-consistency for all J≤K
  • Node consistency = strong 1-consistency
  • Arc consistency = strong 2-consistency
  • Path consistency = strong 3-consistency

55

Why Do We Care?

  • 1. A strongly N-consistent CSP with N

variables can be solved without backtracking

  • 2. For any CSP that is strongly K-consistent:
  • If we find an appropriate variable ordering (one

with “small enough” branching factor)

  • We can solve the CSP without backtracking

56

Ordered Constraint Graphs

  • Select a variable ordering, V1, …, Vn
  • Width of a node in this OCG is the number of arcs

leading to earlier variables:

  • w(Vi) = Count ( (Vi, Vk) | k < i)
  • Width of the OCG is the maximum width of any node:
  • w(G) = Max (w (Vi)), 1 ≤ i ≤ N
  • Width of an unordered CG is the minimum width of

all orderings of that graph (“best you can do”)

57

Tree-Structured Constraint Graph

  • A constraint tree rooted at V1 satisfies:
  • There exists an ordering V1, …, Vn such that every node has zero or
  • ne 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), 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.)

58

V1 V8 V4 V7 V6 V10 V9 V5 V3 V2

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

61

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

62

slide-10
SLIDE 10

10

Possible Variable Orderings

  • Intuition: choose variables that are highly constrained

early in the search process; leave easy ones for later. Some possibilities:

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

63

Possible Variable Orderings

  • 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

64

Minimum Width

  • Or “minimum remaining values” (MRV):
  • Choose the variable with the fewest remaining legal values
  • Why min rather than max?
  • Also called “most constrained variable”
  • “Fail-fast” ordering

Variable Orderings II

  • Maximal stable set: find largest set of variables

with no constraints between them, 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
  • f connected subproblems

66

Value Ordering

  • Intuition: Choose values that are the least constrained early
  • n, leaving the most legal values in later variables
  • 1. Maximal options method (a.k.a. least-constraining-value

heuristic): Choose the value that leaves the most legal values for not-yet-instantiated variables

  • 2. Min-conflicts: For iterative repair search (Coming up)
  • 3. Symmetry: Introduce symmetry-breaking constraints to

constrain search space to ‘useful’ solutions (don’t examine more than one symmetric/isomorphic solution)

67

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
  • r…
  • Local maxima strategies
  • Random restart
  • Random walk
  • Tabu search: don’t try recently attempted values

68

slide-11
SLIDE 11

11

Min-Conflicts Heuristic

  • Iterative repair method
  • 1. 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

69

Performance depends on quality and informativeness of initial assignment; inversely related to distance to solution

Challenges

  • 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

71

More Challenges

  • What if constraints are represented intensionally?
  • Cost of evaluating constraints (time, memory, resources)
  • What if constraints/variables/values change over time?
  • Dynamic constraint networks
  • Temporal constraint networks
  • Constraint repair
  • What if you have multiple agents or systems involved?
  • Distributed CSPs
  • Localization techniques

72

Questions? Thanks!