03 Search
03 Search
03 Search
Artificial Intelligence
Solving problems
by searching
AIMA Chapter 3
What are
Uninformed Informed
search State space Tree search
search search
problems?
What are search problems?
• We will consider the problem of designing goal-based agents in
known, fully observable, and deterministic environments.
• Example environment:
Start
Exit
Remember: Goal-based Agent
• The agent has the task to reach a defined goal state.
• The agent needs to move towards the goal. It can use search algorithms to
plan actions that lead to the goal.
• The performance measure is typically the cost to reach the goal.
Agent’s
Maze
location
Result of
moving
Exit
location
z Goal
state
Discretization grid
Notes:
• The state space is typically too large to be enumerated or it is continuous.
Therefore, the problem is defined by initial state, actions and the transition
model and not the set of all possible states.
• The optimal solution is the sequence of actions (or equivalently a sequence
of states) that gives the lowest path cost for reaching the goal.
Transition function and available actions
We use the notation: 𝑓: 𝑆 × 𝐴 → 𝑆 Initial state Actions: {N, E, S, W}
or 𝑠 ′ = 𝑟𝑒𝑠𝑢𝑙𝑡(𝑎, 𝑠) Transitions
1
2 g i
3
• As a table 𝑠 𝑎 𝑠′
4 a
5
1 S 2
2 N 1
2 S 3
… … …
4 E a
4 S 5
z Goal
4 N 3 state
Discretization grid
… … …
Distance in miles
Example: Vacuum world
State Space
Goal states
Construct a
search tree
for the state
space graph!
Goal states
Cycles vs. redundant paths
Cycles
Return to the same state. The search tree will create a new node!
Initial state
Redundant paths
Goal states Multiple paths to get to the same state
Initial state
Path 1 Path 2
Goal states
Search tree
Root node =
• Superimpose a “what if” tree of possible Initial state
actions and outcomes (states) on the state
space graph. a
• The Root node represents the initial stare. Edge = Action
• An action child node is reached by an edge Child node
representing an action. The corresponding b c
state is defined by the transition model. Redundant
• Trees have no cycles or redundant paths.
Cycles in the search space need to be
broken. Removing redundant paths d e
path
…
improves search efficiency. Cycle
• A path through the tree corresponds to a
sequence of actions (states).
• A solution is a path ending in a node
b … …
Solution path
representing a goal state.
• Nodes vs. states: Each node represents a f Node representing
state of the environment. It contains the a Goal state
data structure that creates the search tree.
Differences between typical Tree search and
AI search
Typical tree search AI tree/graph search
• Assumes a given tree that fits • The search space is too large to fit
in memory. into memory.
a. Builds parts of the tree from initial
state and transition function
representing the graph.
b. Memory management is very
important.
Transition model
Tree search example
1. Expand Arad
Frontier
Transition model
Tree search example
Frontier
2. Expand Sibiu
Transition model
Example of a
redundant path
We could have
also expanded
Timisoara or
Zerind!
Search strategies
• Worst case time and space complexity are measured in terms of the size
of the state space n (= number of nodes in the search tree).
Metrics used if the state space is only implicitly defined by initial state,
actions and a transition function are:
• d: depth of the optimal solution (= number of actions needed)
• m: the number of actions in any path (may be infinite with loops)
• b: maximum branching factor of the search tree (number of successor nodes for a
parent)
State Space for Search
State Space
• Number of different states the agent and State representation
environment can be in.
• Reachable states are defined by the initial state and
the transition model. Not all states may be reachable 𝑥1
from the initial state. 𝑥2
…
• Search tree spans the state space. Note that a single
state can be represented by several search tree
nodes if we have redundant paths.
• State space size is an indication of problem size.
2×2=4
3×2×1=6
Factorial: 𝑛! = 𝑛 × 𝑛 − 1 × ⋯ × 2 × 1
#Python
import math
print (math.factorial(23))
#Python
import scipy.special
𝑛 = number of squares
Dirt
• Order of A and B does not matter!
• Repetition: Dirt can be in both
places
• There are 2 options (clean/dirty)
→ 2𝑛
Robot
• Can be in 1 out of n places.
→𝑛
Total: 𝑛2𝑛
We call the number of squares (“slots”) 𝑛 here!
Examples: What is the state space size?
Often a rough upper limit is sufficient to determine how hard the search problem is.
Breadth-first search
Uniform-cost search
Depth-first search
Iterative deepening search
Breadth-first search (BFS)
Data Structures
• Frontier data structure: holds references to the green nodes (green) and is
implemented as a FIFO queue.
• Reached data structure: holds references to all visited nodes (gray and green)
and is used to prevent visiting nodes more than once (cycle checking).
• Builds a tree with links from parent to child.
Implementation: BFS
30
Implementation: Expanding the search tree
• Optimal?
Yes – if cost is the same per step (action). Otherwise: Use uniform-cost search.
• Time?
Sum of the number of nodes created in at each level in a b-ary tree of depth d:
1 + 𝑏 + 𝑏 2 + ⋯ + 𝑏 𝑑 = 𝑂(𝑏𝑑 )
• Space?
Stored nodes: 𝑂(𝑏 𝑑 )
Note:
• The large space complexity is usually a bigger problem than time!
d: depth of the optimal solution
m: max. depth of tree
Breadth-first search b: maximum branching factor
• Time and Space: 𝑂 𝑏 𝑑 - all paths to the depth of the goal are expanded
expanded
d=2
b=2
m=4
B C Goal
D E F G
C Goal
Uniform-cost search
(= Dijkstra’s shortest path algorithm)
• Expansion rule: Expand node in the frontier with the least path cost from the initial state.
• Implementation: best-first search where the frontier is a priority queue ordered by lower 𝑓(𝑛) =
path cost (cost of all actions starting from the initial state).
• Breadth-first search is a special case when all step costs being equal, i.e., each action costs the
same!
• Time?
Number of nodes with path cost ≤ cost of optimal solution (C*) is O(b1+C*/ ε).
This can be greater than O(bd): the search can explore long paths consisting of small steps before exploring
shorter paths consisting of larger steps
• Space?
O(b1+C*/ ε)
• Time?
Could be the time to reach a solution at maximum depth m in the last path: 𝑂 𝑏𝑚
Terrible if 𝑚 ≫ 𝑑, but if there are many shallow solutions, it can be much faster than
BFS.
• Space?
𝑂 𝑏𝑚 linear in max. tree depth (only if no reached data structure is used!)
d: depth of the optimal solution
m: max. depth of tree
Depth-first search b: maximum branching factor
A
b=2 d=2
B C Goal
m=4
D E
Note: The order in which we add new nodes to the frontier can change what goal we find!
Iterative deepening search (IDS)
Can we
a) get DFS’s good memory footprint,
b) avoid infinite cycles, and
c) preserve BFS’s optimality guaranty?
• Optimal?
Yes, if step cost = 1
• Time?
Consists of rebuilding trees up to d times
𝑑 𝑏1 + (𝑑 − 1)𝑏2 + … + 1𝑏 𝑑 = 𝑂(𝑏𝑑 ) Slower than BFS, but the same complexity!
• Space?
O(bd) linear space. Even less than DFS since m<=d. Cycles need to be handled by the
depth-limited DFS implementation.
Note: IDS produces the same result as BFS but trades better space complexity for
worse run time.
This makes IDS/DFS into the
workhorse of AI.
Informed Search
Informed search
• The agent can use additional information in the form of “hints” about how
promising different states/nodes are to lead to the goal. These hints are
derived from
• information the agent has (e.g., a map) or
• percepts coming from a sensor.
• The agent uses a heuristic function 𝒉(𝒏) to rank nodes in the frontier and
select the most promising state in the frontier for expansion using a best-
first search strategy.
• Algorithms:
• Greedy best-first search
• A* search
Heuristic function
• Heuristic function ℎ(𝑛) estimates the cost of reaching a node representing
the goal state from the current node 𝑛.
• Examples:
Euclidean distance Manhattan distance
Start state Start state
h(n)
Greedy best-first search example
Expansion rule: Expand the
node that has the lowest value
of the heuristic function h(n) h(n)=
Greedy best-first search example
Greedy best-first search example
Greedy best-first search example
Total:
140 + 99 + 211 = 450 miles
Properties of greedy best-first search
• Complete?
Yes – Best-first search if complete in finite spaces.
• Optimal?
No
Total:
140 + 99 + 211 = 450 miles
• Complete?
Yes – Best-first search if complete in finite spaces.
• Optimal?
No d: depth of the optimal solution
m: max. depth of tree
b: maximum branching factor
• Time?
Worst case: O(bm) like DFS
Best case: O(bm) – If ℎ(𝑛) is 100% accurate
• Space?
Same as time complexity.
How can we fix the optimality problem with
greedy best-first search?
• Idea: Take the cost of the path to 𝑛 called 𝑔(𝑛) into account to avoid
expanding paths that are already very expensive.
• The evaluation function 𝑓(𝑛) is the estimated total cost of the path
through node 𝑛 to the goal:
𝑓(𝑛) = 𝑔(𝑛) + ℎ(𝑛)
𝑔(𝑛): cost so far to reach n (path cost)
ℎ(𝑛): estimated cost from n to goal (heuristic)
• The agent in the example above will stop at n with 𝑓(𝑛) = 3 and chose the
path up with a better 𝑓(𝑛’) = 2
ℎ(𝑛)
A* search example
𝑓 𝑛 = 𝑔 𝑛 + ℎ(𝑛)
ℎ(𝑛)
A* search example
𝑓 𝑛 = 𝑔 𝑛 + ℎ(𝑛)
ℎ(𝑛)
A* search example
𝑓 𝑛 = 𝑔 𝑛 + ℎ(𝑛)
ℎ(𝑛)
A* search example
𝑓 𝑛 = 𝑔 𝑛 + ℎ(𝑛)
ℎ(𝑛)
A* search example
𝑓 𝑛 = 𝑔 𝑛 + ℎ(𝑛)
ℎ(𝑛)
BFS vs. A* search
BFS
A*
Source: Wikipedia
Implementation of A* Search
n* (goal)
Any unexplored node 𝑛 has:
𝐶 ∗ = 𝑓(𝑛∗ ) = 𝑔(𝑛∗ ) + 0 n
𝑓 𝑛 ≥ 𝑓(𝑛∗ )
n’ (other goal)
𝑔 𝑛′ ≥ 𝑓 𝑛 ⟺ 𝑔 𝑛′ ≥ 𝐶 ∗
A* is optimally efficient
a. No other tree-based search algorithm that uses the same heuristic can
expand fewer nodes and still be guaranteed to find the optimal solution.
b. Any algorithm that does not expand all nodes with 𝑓(𝑛) < 𝐶 ∗ (the lowest
cost of going to a goal node) cannot be optimal. It risks missing the optimal
solution.
Properties of A*
• Complete?
Yes
• Optimal?
Yes
• Time?
Number of nodes for which 𝑓(𝑛) ≤ 𝐶 ∗ (exponential)
• Space?
Same as time complexity.
Designing heuristic functions
Heuristics for the 8-puzzle
ℎ1(𝑛) = number of misplaced tiles
ℎ2(𝑛) = total Manhattan distance (number of squares from desired
location of each tile)
ℎ1(𝑠𝑡𝑎𝑟𝑡) = 8
ℎ2(𝑠𝑡𝑎𝑟𝑡) = 3 + 1 + 2 + 2 + 2 + 3 + 3 + 2 = 18
1 needs to move 3
Are ℎ1 and ℎ2 admissible? positions
Heuristics from relaxed problems
• A problem with fewer restrictions on the actions is called a relaxed
problem.
• The cost of an optimal solution to a relaxed problem is an admissible
heuristic for the original problem. I.e., the true cost is never smaller.
• ℎ1: If the rules of the 8-puzzle are relaxed so that a tile can move
anywhere, then ℎ1(𝑛) gives the shortest solution.
• ℎ2: If the rules are relaxed so that a tile can move to any adjacent square,
then ℎ2(𝑛) gives the shortest solution.
ℎ1(𝑠𝑡𝑎𝑟𝑡) = 8
ℎ2(𝑠𝑡𝑎𝑟𝑡)
= 3+1+2+2+2+3+3+2
= 18
Heuristics from relaxed problems
What relaxations are used in these two cases?
*
* * *
* * * *
Dominance: What heuristic is better?
• That is, always pick for each node the heuristic that
is closest to the real cost to the goal ℎ∗ (𝑛).
Satisficing Search: Weighted A* search
f 𝑛 = 𝑔 𝑛 + 𝑊 × ℎ(𝑛)
If all step
IDS Yes 𝑂(𝑏𝑑 ) 𝑂(𝑏𝑑)
costs are equal
Uniform-cost
Yes Yes Number of nodes with 𝑔(𝑛) ≤ 𝐶 ∗
Search
If all step
IDS Yes 𝑂(𝑏𝑑 ) 𝑂(𝑏𝑑)
costs are equal
• Tree search can be used for planning actions for goal-based agents in
known, fully observable and deterministic environments.
• Issues are:
• The large search space typically does not fit into memory. We use a
description using a compact transition model.
• The search tree is built on the fly, and we have to deal with cycles and
redundant paths.