02 Search
02 Search
02 Search
1
Motivation
Google itinerary
8-puzzle
2
Today
State Space Representation Agent Perception
State Space Search Robotics
Uninformed search
Breadth-first and Depth-first Reasoning
Depth-limited Search Search
Iterative Deepening Learning
Uniform Cost
Informed search KnowledgeConstraint
Planning rep. satisfaction
Hill climbing
Best-First
(Designing Heuristics)
A* Natural
... Expert
language
Summary Systems
3
Example: 8-Puzzle
State: Any arrangement of 8 numbered tiles and an empty tile on a 3x3 board
8 2 1 2 3
3 4 7 4 5 6
5 1 6 7 8
1 2 3 1 2 3
4 5 6 8 4 …
7 8 7 6 5
4
(n2-1)-puzzle
....
8 2 1 2 3 4
3 4 7 5 6 7 8
5 1 6 9 10 11 12
13 14 15
8-puzzle
15-puzzle
5
15-Puzzle
Invented in 1874 by Noyes Palmer Chapman
… but Sam Loyd claimed he invented it!
6
15-Puzzle
Sam Loyd even offered $1,000 of his own
money to the first person who would solve the
following problem:
1 2 3 4 1 2 3 4
5 6 7 8 ? 5 6 7 8
9 10 11 12 9 10 11 12
13 14 15 13 15 14
7
But no one ever won the prize…
8
State Space
Many AI problems, can be expressed in terms of going
from an initial state to a goal state
Ex: to solve a puzzle, to drive from home to Concordia…
9
State Space
Problem is represented by:
1. Initial State
starting state
ex. unsolved puzzle, being at home
2. Set of operators
actions responsible for transition between states
3. Goal test function
Applied to a state to determine if it is a goal state
ex. solved puzzle, being at Concordia
4. Path cost function
Assigns a cost to a path to tell if a path is preferable to
another
Search space: the set of all states that can be reached
from the initial state by any sequence of action
Search algorithm: how the search space is visited
10
Example: The 8-puzzle
8 2 1 2 3
3 4 7 4 5 6
5 1 6 7 8
Initial state Goal state
Set of operators:
blank moves up, blank moves down, blank moves left, blank moves right
8 2 7
3 4
5 1 6
8 2 8 2 7 8 2 7
3 4 7 3 4 6 3 4
5 1 6 5 1 5 1 6
Each state is
represented by a
distinct node
An arc (or edge)
connects a node s
to a node s’ if
s’ ∈ SUCCESSOR(s)
The state graph may
contain more than one
connected component
13
Just to make sure we’re clear…
5 8 1 2 3
4 2 1 4 5 6
7 3 6 7 8
Initial state Goal state 14
State Space as a Search Tree
15
State Space for the 8-puzzle
Right Up Left
Down
17
Today
State Space Representation
State Space Search
Uninformed search
Breadth-first and Depth-first
Depth-limited Search
Iterative Deepening
Uniform Cost
Informed search
Hill climbing
Best-First
(Designing Heuristics)
A*
Summary
18
Uninformed VS Informed Search
Uninformed search
We systematically explore the alternatives
aka: systematic/exhaustive/blind/brute force search
Breadth-first
Depth-first
Uniform-cost
Depth-limited search
Iterative deepening search
Bidirectional search
…
19
Today
State Space Representation
State Space Search
Uninformed search
Breadth-first and Depth-first
Depth-limited Search
Iterative Deepening
Uniform Cost
Informed search
Hill climbing
Best-First
(Designing Heuristics)
A*
Summary
20
Breadth-first vs Depth-first Search
ex:
Closed = [A, B, C, D, E]
Open = [F, G, H, I, J, K, L]
STATE PARENT-NODE
8 2 REPRESENTATION
3 4 7
5 1 6
BOOKKEEPING
Action Right
CHILDREN
Depth 5
... Path-Cost 5
23
Generic Search Algorithm
1. Initialize the open list with the initial node so (top node)
2. Initialize the closed list to empty
3. Repeat
a) If the open list is empty, then exit with failure.
b) Else, take the first node s from the open list.
c) If s is a goal state, exit with success. Extract the solution path
from s to so
d) Else, insert s in the closed list (s has been visited /expanded)
e) Insert the successors of s in the open list in a certain order if
they are not already in the closed and/or open lists (to avoid
cycles)
Notes:
The order of the nodes in the open list depends on the search
strategy
24
DFS and BFS
DFS and BFS differ only in the way they order nodes in the
open list:
25
Breadth-First Search
Search graph at
iteration 6 of
depth-first
search
States on open
and closed are
highlighted
required: Bn
Depth-first:
Not optimal (no guarantee to find the shortest path)
But:
Requires less memory
34
Today
State Space Representation
State Space Search
Uninformed search
Breadth-first and Depth-first
Depth-limited Search
Iterative Deepening
Uniform Cost
Informed search
Hill climbing
Best-First
(Designing Heuristics)
A*
Summary
35
Depth-Limited Search
36
Today
State Space Representation
State Space Search
Uninformed search
Breath-first and Depth-first
Depth-limited Search
Iterative Deepening
Uniform Cost
Informed search
Hill climbing
Best-First
(Designing Heuristics)
A*
Summary
37
Iterative Deepening
Compromise between BFS and DFS:
use depth-first search, but
38
Iterative Deepening: Example
44
Today
State Space Representation
State Space Search
Uninformed search
Breath-first and Depth-first
Depth-limited Search
Iterative Deepening
Uniform Cost
Informed search
Hill climbing
Best-First
(Designing Heuristics)
A*
Summary
45
Informed Search (aka heuristic search)
Most of the time, it is not feasible to do an exhaustive search,
search space is too large
e.g. state space of all possible moves in chess = 10120
1075 = nb of molecules in the universe
1026 = nb of nanoseconds since the “big bang”
Idea:
choose "best" next node to expand
according to a selection function (i.e. a heuristic function h(n))
46
Heuristic - Heureka!
Heuristic:
a rule of thumb, a good bet
but has no guarantee to be correct whatsoever!
Heuristic search:
A technique that improves the efficiency of search,
possibly sacrificing on completeness
Focus on paths that seem most promising according to
some function
Need an evaluation function (heuristic function) to
score a node in the search tree
Heuristic function h(n) = an approximation of the
lowest cost from node n to the goal
47
g(n)
4 3
2
6 3
2 1
48
h(n)
3 6
49
Today
State Space Representation
State Space Search
Uninformed search
Breath-first and Depth-first
Depth-limited Search
Iterative Deepening
Informed search
Hill Climbing
Best-First
(Designing Heuristics)
A*
Summary
50
Example: Hill Climbing with Blocks World
Operators:
pickup&putOnTable(Block)
pickup&stack(Block1,Block2)
Heuristic:
0pt if a block is sitting where it is supposed to sit
+1pt if a block is NOT sitting where it is supposed to sit
pickup&putOnTable(A)
h(n) = 1
pickup&stack(A,H) pickup&putOnTable(H)
pickup&stack(H,A)
53
Steepest Ascent Hill Climbing
currentNode = startNode;
loop do
L = CHILDREN(currentNode);
nextEval = INFINITY;
nextNode = NULL;
for all c in L
if (HEURISTIC-VALUE(c) < nextEval) // lower h is better
nextNode = c;
nextEval = HEURISTIC-VALUE(c);
currentNode = nextNode;
pickup&stack(A,H) pickup&putOnTable(H)
pickup&stack(H,A)
Don’t be confused…
a lower h(n) is better…
56
Problems with Hill Climbing
Plateau
a flat area of the search space in which the next states
have the same value.
it is not possible to determine the best direction in which
to move by making local comparisons.
keep going even if the best successor has the same value as
current node
works well on a "shoulder"
but could lead to infinite loop
on a plateau
source: Rich & Knight, Artificial Intelligence, McGraw-Hill College 1991. & Russel & Norvig (2003)
58
Today
State Space Representation
State Space Search
Uninformed search
Breadth-first and Depth-first
Depth-limited Search
Iterative Deepening
Informed search
Hill Climbing
Best-First
(Designing Heuristics)
A*
Summary
59
Best-First Search
problem with hill-climbing:
one move is selected and all others are forgotten.
solution to hill-climbing:
use "open" as a priority queue
this is called best-first search
Best-first search:
Insert nodes in open list so that the nodes are sorted in
ascending h(n)
Always choose the next node to visit to be the one with the
best h(n) -- regardless of where it is in the search space
60
Best-First: Example
It is an exhaustive search …
will eventually try all possible paths
62
Best-First Search: Example
1. open = [A-null-5] closed = []
2. open = [B-A-4 C-A-4 D-A-6] (arbitrary choice) closed [A]
3. open = [C-A-4 E-B-5 F-B-5 D-A-6] closed = [B A]
4. → Worksheet #1 (“Best-First Search”)
65
Designing Heuristics
66
Example: 8-Puzzle – Heuristic 1
h1: Simplest heuristic
Hamming distance : count
5 8 1 2 3
4 2 1 4 5 6
7 3 6 7 8
STATE n Goal state
h1(n) = 6
does not consider the
inversions
See next slide…
5 8 5 8 4 2 1 7 3 6
4 2 1
7 3 6
STATE n
For each numbered tile, count how many tiles on its right
should be on its left in the goal state.
1 2 3
h3(n) = n5 + n8 + n4 + n2 + n1 + n7 + n3 + n6 4 5 6
=4 +6 +3 +1 +0 +2 +0 +0 7 8
= 16
Goal state
70
Heuristics for the 8-Puzzle
5 8 1 2 3
4 2 1 4 5 6
7 3 6 7 8
STATE n Goal state
h1(n) = misplaced numbered tiles
=6
71
g(n), h(n) and f(n)
Evaluation function f(n) = g(n) + h(n) for node n:
g(n) current cost from start to node n
h(n) estimate of the lowest cost from n to goal
f(n) estimate of the lowest cost of the solution
path (from start to goal passing through n)
72
Evaluating Heuristics
1. Admissibility:
“optimistic”
never overestimates the actual cost of reaching the goal
guarantees to find the lowest cost solution path to the goal (if it
exists)
2. Monotonicity:
“local admissibility”
guarantees to find the lowest cost path to each state n encountered
in the search
3. Informedness:
measure for the “quality” of a heuristic
the more informed, the better
73
Admissibility
A heuristic is admissible if it never overestimates the
cost of reaching the goal
i.e.:
h(n) ≤ h*(n) for all n
74
Example: 8-Puzzle
5 8 1 2 3
4 2 1 4 5 6
7 3 6 7 8
n goal
75
Monotonicity (aka consistent)
An admissible heuristics may temporarily reach non-goal states
along a suboptimal path
A heuristic is monotonic if it always finds the optimal path to
each state the 1st time it is encountered !
guarantees to find the lowest cost path to each state n
encountered in the search
76
Informedness
Intuition: number of misplaced tiles is less informed than
Manhattan distance
For two admissible heuristics h1 and h2
if h1(n) ≤ h2(n), for all states n
then h2 is more informed than h1
h1(n) ≤ h2(n) ≤ h*(n)
More informed heuristics search smaller space to find the
solution path
77
Today
State Space Representation
State Space Search
Uninformed search
Breadth-first and Depth-first
Depth-limited Search
Iterative Deepening
Informed search
Hill Climbing
Best-First
(Designing Heuristics)
A*
Summary
78
Algorithm A
Heuristics might be wrong:
so search could continue down a wrong path
Solution:
Maintain depth/cost count, i.e., give preference to
shorter/least expensive paths
Modified evaluation function f:
f(n) = g(n) + h(n)
f(n) estimate of total cost along path through n
g(n) actual cost of path from start to node n
h(n) estimate of cost to reach goal from node n
79
Algorithm A on the 8-puzzle
85
Today
State Space Representation
State Space Search
Uninformed search
Breath-first and Depth-first
Depth-limited Search
Iterative Deepening
Informed search
Hill climbing
Best-First
(Designing Heuristics)
A*
Summary
86
Summary
Search Uses h(n)? Open is a…
Breadth-first No Queue
Depth-first No Stack
Depth-limited No Stack
Iterative Deepening No Stack
Uniform Cost No Priority queue sorted by g(n)
Hill Climbing Yes none
Best-First Yes Priority queue sorted by h(n)
Algorithm A Yes Priority queue sorted by f(n)
- no constraints on h(n) f(n) = g(n) + h(n)
Algorithm A* Yes Priority queue sorted by f(n)
- same as A, but h(n) must be f(n) = g(n) + h(n)
admissible
- guarantees to find the lowest
cost solution path 87
Today
State Space Representation
State Space Search
Uninformed search
Breadth-first and Depth-first
Depth-limited Search
Iterative Deepening
Uniform Cost
Informed search
Hill climbing
Best-First
A*
Summary
88