3 Cps170 More Search
3 Cps170 More Search
http://www.cs.duke.edu/courses/spring09/cps170/
More search:
When the path to the solution
doesn’t matter
Q
Q
Q
Q
Q
Q
Q
Q
Search formulation of the queens puzzle
• Successors: all valid ways of placing additional queen on
the board; goal: eight queens placed
Q
Q
How big is this tree?
Q
How many leaves?
What if they were rooks?
Q
Q Q Q Q Q
Q Q Q Q Q
Q Q Q Q Q
Q Q
Q Q Q Q Q
Q Q
Q Q Q Q Q
Search formulation of the queens puzzle
• Successors: all valid ways of placing a queen in the next
column; goal: eight queens placed
Q
Q Q
Q
Q Q
Constraint satisfaction problems (CSPs)
• Defined by:
– A set of variables x1, x2, …, xn
– A domain Di for each variable xi
– Constraints c1, c2, …, cm
• A constraint is specified by
– A subset (often, two) of the variables
– All the allowable joint assignments to those variables
• Goal: find a complete, consistent assignment
• Queens problem: (other examples in next slides)
– xi in {1, …, 8} indicates in which row in the ith column to
place a queen
– For example, constraint on x1 and x2: {(1,3), (1,4), (1,5),
(1,6), (1,7), (1,8), (2,4), (2,5), …, (3,1), (3,5), … …}
Meeting scheduling
• Meetings A, B, C, … need to be scheduled
on M, Tu, W, Th, F
• A and B cannot be scheduled on the same
day
• B needs to be scheduled at least two days
before C
• C cannot be scheduled on Th or F
• Etc.
• How do we model this as a CSP?
Graph coloring
• Fixed number of colors; no two adjacent
nodes can share a color
red A
blue B C green
D
red
Satisfiability
• Formula in conjunctive normal form:
(x1 OR x2 OR NOT(x4)) AND (NOT(x2) OR
NOT(x3)) AND …
– Label each variable xj as true or false so that
the formula becomes true
Constraint hypergraph:
X1 X2
each hyperedge
represents a constraint
X3 X4
Cryptarithmetic puzzles
TWO
TWO+
FOUR
E.g., setting F = 1, O = 4, R = 8, T = 7, W = 3,
U = 6 gives 734+734=1468
Cryptarithmetic puzzles…
TWO Trick: introduce auxiliary
variables X, Y
TWO+
O + O = 10X + R
FOUR
W + W + X = 10Y + U
T + T + Y = 10F + O
also need pairwise
constraints between
O R W U T F original variables if they
are supposed to be
different
A=0,B=?,C=? A=1,B=?,C=?
Can you prove that this never increases the number of leaves?
A generic recursive search algorithm
(assignment is a partial assignment)
• Search(assignment, constraints)
• If assignment is complete, return it
• Choose an unassigned variable x
• For every value v in x’s domain, if setting x to v in
assignment does not violate constraints:
– Set x to v in assignment
– result := Search(assignment, constraints)
– If result != failure return result
– Unassign x in assignment
• Return failure
Keeping track of remaining possible values
• For every variable, keep track of which values are still
possible
Q X X X Q X X Q
Q X X X Q X X Q
Q X X X Q X X Q
X X Q
Q X X X Q X X Q
X X X X Q Q
X X Q
Q X X X Q X X Q
Q X X Consider
X
cryptarithmetic
X Q X
X puzzle again…
Is the arc from the fifth to the eighth column consistent?
What about the arc from the eighth to the fifth?
An example where arc
consistency fails
A
(A, B) in {(0,0), (1,1)} (A, C) in {(0,1), (1,0)}
B C
(B, C) in {(0,0), (1,1)}
• A = B, B = C, C ≠ A – obviously inconsistent
– ~ Moebius band
X2 X3 X4
X5 X7 X6
• Dynamic program for solving this (linear in #variables):
– Starting from the leaves and going up, for each node x, compute all the
values for x such that the subtree rooted at x can be solved
• Equivalently: apply arc consistency from each parent to its children, starting
from the bottom
– If no domain becomes empty, once we reach the top, easy to fill in solution
Example: graph coloring with limited
set of colors per node
X1 {green, blue, red}
{green} X5 X6 X7
{green, blue} {green}
• Pure optimization
Local search: hill climbing
• Start with a complete state
• Move to successor with best (or at least better) objective value
– Successor: move one queen within its column
Q Q Q
Q Q Q
Q Q Q Q
Q Q Q no more
Q Q Q
improvements
Q Q Q
Q Q
Q Q Q
• Simulated annealing:
– Generate a random successor (possibly worse than
current state)
– Move to that successor with some probability that is
sharply decreasing in the badness of the state
– Also, over time, as the “temperature decreases,”
probability of bad moves goes down
Constraint optimization
• Like a CSP, but with an objective
– Example: no two queens can be in the same row or
column (hard constraint), minimize number of pairs of
queens attacking each other diagonally (objective)
(matter of definition; Q
that violated
constraints so far is Q cost = 0
the heuristic and
interior nodes have Q
no cost)
Q Q Q
Q Q Q
Q Q Q
Q Q Q
Depth first branch and bound will find a A* (=uniform cost here), IDA* Optimal solution is down here
suboptimal solution here first (no way to tell (=iterative lengthening here) will (cost 0)
at this point this is worse than right node) never explore this node
Linear programs: example
• We make reproductions of
two paintings maximize 3x + 2y
subject to
4x + 2y ≤ 16
x + 2y ≤ 8
4 optimal solution:
x+y≤5 x=3, y=2
x≥0 2
y≥0
0 2 4 6 8
Modified LP
maximize 3x + 2y
Optimal solution: x = 2.5,
subject to y = 2.5
4x + 2y ≤ 15 Solution value = 7.5 + 5 =
x + 2y ≤ 8 12.5
x+y≤5
x≥0 Half paintings?
y≥0
Integer (linear) program
maximize 3x + 2y
8
subject to
optimal IP
4x + 2y ≤ 15 6 solution: x=2,
y=3
(objective 12)
x + 2y ≤ 8 optimal LP
4
solution: x=2.5,
x+y≤5 y=2.5
(objective 12.5)
2
x ≥ 0, integer
y ≥ 0, integer
0 2 4 6 8
Mixed integer (linear) program
maximize 3x + 2y
8
subject to
optimal IP
4x + 2y ≤ 15 6 solution: x=2,
y=3
(objective 12)
x + 2y ≤ 8 optimal LP
4 solution: x=2.5,
x+y≤5 y=2.5
(objective 12.5)
2 optimal MIP
x≥0 solution: x=2.75,
y=2
y ≥ 0, integer (objective 12.25)
0 2 4 6 8
Solving linear/integer programs
• Linear programs can be solved efficiently
– Simplex, ellipsoid, interior point methods…
B C
D
• Let’s say xB,green is 1 if B is colored green, 0 otherwise
• Must have 0 ≤ xB,green ≤ 1, xB,green integer
– shorthand: xB,green in {0,1}
• Constraint that B and C can’t both be green: xB,green + xC,green ≤ 1
• Etc.
• Solving integer programs is at least as hard as graph coloring, hence
NP-hard (we have reduced graph coloring to IP)
Satisfiability as an integer program
(x1 OR x2 OR NOT(x4)) AND (NOT(x2) OR NOT(x3)) AND
…
becomes
for all xj, 0 ≤ xj ≤ 1, xj integer (shorthand: xj in {0,1})
x1 + x2 + (1-x4) ≥ 1
(1-x2) + (1-x3) ≥ 1
…