1-AI Search
1-AI Search
Search
Search
• We have some actions that can change the state of the
world
– Change resulting from an action perfectly predictable
• Try to come up with a sequence of actions that will lead us
to a goal state
– May want to minimize number of actions
– More generally, may want to minimize total cost of actions
• Do not need to execute actions in real life while searching
for solution!
– Everything perfectly predictable anyway
A simple example:
traveling on a graph
C 2
B 9
2
3 F goal state
start state A E
D 4
3 4
Searching for a solution
C 2
B 9
2
3 F goal state
start state A
D
3
Search tree
state = A,
cost = 0
state = B, state = D,
cost = 3 cost = 3
state = C, state = F,
cost = 5 cost = 12
goal state!
state = A,
cost = 7
search tree nodes and states are not the same thing!
Full search tree
state = A,
cost = 0
state = B, state = D,
cost = 3 cost = 3
goal state!
state = B, state = D,
.. cost = 10 .. cost = 10
. .
Changing the goal:
want to visit all vertices on the graph
C 2
B 9
2
3 F
A E
D 4
3 4
state = F, {A, B}
state = C, {A, B} state = E, {A, D}
cost = 12
cost = 5 cost = 7
state = A, {B, C}
state = F, {A, D, E}
cost = 7
cost = 11
state = D, {A, B, C}
state = B, {A, C} What would happen if the
.. cost = 10 .. cost = 10
goal were to visit every
. . location twice?
Key concepts in search
• Set of states that we can be in
– Including an initial state…
– … and goal states (equivalently, a goal test)
• For every state, a set of actions that we can take
– Each action results in a new state
– Typically defined by successor function
• Given a state, produces all states that can be reached from it
• Cost function that determines the cost of each action (or path
= sequence of actions)
• Solution: path from initial state to a goal state
– Optimal solution: solution with minimal cost
8-puzzle
1 2 1 2 3
4 5 3 4 5 6
7 8 6 7 8
goal state
8-puzzle
1 2
4 5 3
7 8 6
1 2 1 2 1 5 2
4 5 3 4 5 3 4 3
7 8 6 7 8 6 7 8 6
.. ..
. .
Generic search algorithm
• Fringe = set of nodes generated but not expanded
= nodes we know we still have to explore
• fringe := {node corresponding to initial state}
• loop:
– if fringe empty, declare failure
– choose and remove a node v from fringe
– check if v’s state s is a goal state; if so, declare success
– if not, expand v, insert resulting nodes into fringe
• Key question in search: Which of the generated nodes
do we expand next?
Uninformed search
• Uninformed search: given a state, we only know
whether it is a goal state or not
• Cannot say one nongoal state looks better than
another nongoal state
• Can only traverse state space blindly in hope of
somehow hitting a goal state at some point
– Also called blind search
– Blind does not imply unsystematic!
Breadth-first search
Properties of breadth-first search
• Nodes are expanded in the same order in which they are
generated
– Fringe can be maintained as a First-In-First-Out (FIFO) queue
• BFS is complete: if a solution exists, one will be found
• BFS finds a shallowest solution
– Not necessarily an optimal solution
• If every node has b successors (the branching factor), first
solution is at depth d, then fringe size will be at least bd at
some point
– This much space (and time) required ☹
Depth-first search
Implementing depth-first search
• Fringe can be maintained as a Last-In-First-Out (LIFO)
queue (aka. a stack)
• Also easy to implement recursively:
• DFS(node)
– If goal(node) return solution(node);
– For each successor of node
• Return DFS(successor) unless it is failure;
– Return failure;
Properties of depth-first search
• Not complete (might cycle through nongoal states)
• If solution found, generally not optimal/shallowest
• If every node has b successors (the branching
factor), and we search to at most depth m, fringe is
at most bm
– Much better space requirement ☺
– Actually, generally don’t even need to store all of fringe
• Time: still need to look at every node
– bm + bm-1 + … + 1 (for b>1, O(bm))
– Inevitable for uninformed search methods…
Combining good properties of BFS and DFS
• Limited depth DFS: just like DFS, except never go deeper
than some depth d
• Iterative deepening DFS:
– Call limited depth DFS with depth 0;
– If unsuccessful, call with depth 1;
– If unsuccessful, call with depth 2;
– Etc.
• Complete, finds shallowest solution
• Space requirements of DFS
• May seem wasteful timewise because replicating effort
– Really not that wasteful because almost all effort at deepest level
– db + (d-1)b2 + (d-2)b3 + ... + 1bd is O(bd) for b > 1
Let’s start thinking about cost
• BFS finds shallowest solution because always
works on shallowest nodes first
• Similar idea: always work on the lowest-cost node
first (uniform-cost search)
• If it finds a solution, it will be an optimal one
• Will often pursue lots of short steps first
• If optimal cost is C, and cost increases by at least L
each step, we can go to depth C/L
• Similar memory problems as BFS
– Iterative lengthening DFS does DFS up to increasing
costs
Uniform cost search
state = A,
cost = 0
state = B, state = D,
cost = 3 cost = 3
state = A,
state = F,
cost = 7
cost = 11
goal state!
state = B, state = D,
cost = 10 cost = 10
Searching backwards from the goal
• Sometimes can search backwards from the goal
– Maze puzzles
– Eights puzzle
– Reaching location F
– What about the goal of “having visited all locations”?
• Need to be able to compute predecessors instead
of successors
• What’s the point?
Predecessor branching factor can be smaller than successor branching factor
• Stacking blocks:
– only action is to add something to the stack
A
B
C
In hand: A, B, C In hand: nothing
state = B, state = D,
cost = 3, h = 8 cost = 3, h = 6
state = E,
cost = 7, h = 3
state = F,
cost = 11, h = 0
• Rapidly finds the optimal solution! goal state!
• Does it always?
A bad example for greedy
B
7 6
F goal state
start state A D E
4
3 4
F goal state
start state A D E
4
3 4
• Say: h(A) = 9, h(B) = 5, h(D) = 6, h(E) = 3, h(F) = 0
state = B, state = D,
g = 3, h = 8 g = 3, h = 6
state = E,
g = 7, h = 3
state = F,
g = 11, h = 0 goal state!
Admissibility
• A heuristic is admissible if it never overestimates
the distance to the goal
– If n is the optimal solution from n’, then g(n) ≥ g(n’) + h(n’)
• Straight-line distance is admissible: can’t hope for
anything better than a straight road to the goal
• Admissible heuristic means that A* is always
optimistic
Optimality of A*
• If the heuristic is admissible, A* is optimal (in the
sense that it will never return a suboptimal solution)
• Proof:
– Suppose a suboptimal solution node n with solution value
C > C* is about to be expanded (where C* is optimal)
– Let n* be an optimal solution node (perhaps not yet
discovered)
– There must be some node n’ that is currently in the fringe
and on the path to n*
– We have g(n) = C > C* = g(n*) ≥ g(n’) + h(n’)
– But then, n’ should be expanded first (contradiction)
A* is not complete (in contrived examples)
B
F
goal state
start state A C D E …