2024 Slide2 Uninform Search Update
2024 Slide2 Uninform Search Update
3
Example: Measuring problem!
9l
3l 5l
4
Example: Measuring problem!
a b c 9l
0 0 0 start 3l 5l
3 0 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
5
Example: Measuring problem!
a b c 9l
0 0 0 start 3l 5l
3 0 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
6
Example: Measuring problem!
a b c 9l
0 0 0 start 3l 5l
3 0 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
7
Example: Measuring problem!
a b c 9l
0 0 0 start 3l 5l
3 0 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
8
Example: Measuring problem!
a b c 9l
0 0 0 start 3l 5l
3 0 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
9
Example: Measuring problem!
a b c 9l
0 0 0 start 3l 5l
3 0 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
10
Example: Measuring problem!
a b c 9l
0 0 0 start 3l 5l
3 0 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
11
Example: Measuring problem!
a b c 9l
0 0 0 start 3l 5l
3 0 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
12
Example: Measuring problem!
a b c 9l
0 0 0 start 3l 5l
3 0 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
13
Example: Measuring problem!
a b c 9l
0 0 0 start 3l 5l
3 0 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
14
Example: Measuring problem!
• Another Solution:
a b c 9l
0 0 0 start 3l 5l
0 5 0
0 0 3 a b c
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
15
Example: Measuring problem!
• Another Solution:
a b c 9l
0 0 0 start 3l 5l
0 5 0
3 2 0 a b c
0 0 3
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
16
Example: Measuring problem!
• Another Solution:
a b c 9l
0 0 0 start 3l 5l
0 5 0
3 2 0 a b c
3 0 2
3 0 3
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
17
Example: Measuring problem!
• Another Solution:
a b c 9l
0 0 0 start 3l 5l
0 5 0
3 2 0 a b c
3 0 2
3 5 2
0 0 6
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
18
Example: Measuring problem!
• Another Solution:
a b c 9l
0 0 0 start 3l 5l
0 5 0
3 2 0 a b c
3 0 2
3 5 2
3 0 7 goal
3 0 6
0 3 6
3 3 6
1 5 6
0 5 7 goal
19
Which solution do we prefer?
• Solution 1: • Solution 2:
a b c a b c
0 0 0 start 0 0 0 start
3 0 0 0 5 0
0 0 3 3 2 0
3 0 3 3 0 2
0 0 6 3 5 2
3 0 6 3 0 7 goal
0 3 6
3 3 6
1 5 6
0 5 7 goal
20
Other Example:
21
Rational Decisions
Environment
Percepts
• General AI techniques for a variety of problem types
Agent
?
• Learning to recognize when and how a new problem can
be solved with an existing technique Actuators
Actions
Problem-solving: (Iteratively) Selecting the right agent
design
• The design of an AI agent for a complex task might require the integration of multiple
components and techniques to deal with different aspects of the problem
Search Problems
25
Search problems (Deterministic Planning)
Search problem components Start
state
• Start state
• Actions: available to the agent in each state
• Transition model
• the state resulting from doing action a in state s
• Goal state
• Path cost
• Assume that it is a sum of
nonnegative step costs (𝑐(𝑠, 𝑎, 𝑠’) ≥ 0)
Goal
• A solution is a sequence of actions (a plan) which transforms state
the start state to a goal state
• The optimal solution is the sequence of actions that gives
the lowest path cost for reaching the goal
Example: Romania
• On vacation in Romania; currently in Arad
• Flight leaves tomorrow from Bucharest
• Start state
• Arad
• Actions
• Go from one city to another
• Transition model
• If you go from city A to
city B, you end up in city B
• Goal state
• Bucharest
• Path cost
• Sum of edge costs (total
distance traveled)
Task Environment - PEAS
Performance measure
-1 per step; +10 food; +500 win; -500 die;
+200 hit scared ghost
Environment
Pacman dynamics (incl ghost behavior)
Actuators
North, South, East, West, (Stop)
Sensors
Entire state is visible
Environment Types – Quiz?
Pacman Taxi
Fully or partially observable
Single agent or multi-agent
Deterministic or stochastic
Static or dynamic
Discrete or continuous
State space
• The start state, actions, and transition model define the state space
of the problem
• The set of all states reachable from start state by any sequence of actions
• Determined by start state + actions + transition model
• Can be represented as a directed graph where the nodes are states and links between
nodes are actions
• What is the state space for the Romania problem?
What’s in a State Space?
32
State Space Sizes?
• World state:
• Agent positions: 120
• Food count: 30
• Ghost positions: 12
• Agent facing: NSEW
• How many
• World states?
120x(230)x(122)x4
• States for pathing?
120
• States for eat-all-dots?
120x(230)
State Space Graphs
Oradea
71
Neamt
Zerind 87
151
75
Iasi
Arad
140
92
Sibiu Fagaras
99
118
Vaslui
80
Rimnicu Vilcea
Timisoara
142
111 Pitesti 211
Lugoj 97
70 98
85 Hirsova
Mehadia 146 101 Urziceni
75 138 86
Bucharest
Drobeta 120
90
Craiova Eforie
Giurgiu
Example: 8-puzzle
36
Example: 8-puzzle
37
Example: 8-puzzle
• Given:
• Start state
• Actions
• Transition model
• Goal state
• Path cost
• How do we find the optimal solution?
• How about building the state space and then using Dijkstra’s shortest path
algorithm?
• Complexity of Dijkstra’s is O(E + V log V), where V is the size of the state space
• The state space may be huge!
Search: Basic idea
Consider this 4-state graph: How big is its search tree (from S)?
S
a
a b
S G
b G G a
b G a b G
∞
Important: Lots of repeated structure in the search tree!
Tree Search Algorithm Outline
43
Problem-Solving
• Problem solving:
• Goal formulation
• Problem formulation (states, operators)
• Search for solution
• Problem formulation:
• Initial (start) state
• Actions
• Goal test
• Path cost
44
Finding a solution
Solution: is ???
45
Finding a solution
46
Finding a solution
47
Example: Traveling from Arad To Bucharest
48
Tree search example
49
Tree search example
50
Tree search example
51
Tree search example
52
Implementation of search algorithms
53
Function TREE_SEARCH(problem) returns a solution, or failure
initialize the frontier as a specific work list (stack, queue, priority queue)
add initial state of problem to frontier
loop do
if the frontier is empty then
return failure
choose a node and remove it from the frontier
if the node contains a goal state then
return the corresponding solution
56
Evaluation of search strategies
Depth = 0
root
Depth = 1
N1 N2
Depth = 2 N3 N4 N5 N6
because a problem may be solvable in principle but may take too long to
solve in practice
59
Complexity: Tower of Hanoi
60
Complexity:
Tower of Hanoi
61
Complexity: Tower of Hanoi
62
Complexity: Tower of Hanoi
63
Complexity: Tower of Hanoi
64
Complexity: Tower of Hanoi
65
Complexity
66
Complexity of Algorithms
• T(n) is O(f(n)) means there exists n0, k such that for all n >n0 T(n) <= kf(n):
• N = input size
• T(n) = total number of step of the algorithm
• Independent of the implementation, compiler, …
• Asymtotic analysis: For large n, an O(n) algorithm is better than an O(n2) algorithm.
• O() abstract over constant factors:
• T(100n +1000) is better than T(n2 + 1) only for n > 110
• O() notation is a good compromise between precision and easy of analysis
67
Remember: Implementation of search algorithms
68
Types of search strategies
• Uninformed/blind search
• Can only generate successors, sum up what has happened so
far, and distinguish goals from non-goals
• Informed/heuristic search:
• Strategies that know whether one non-goal state is more
promising than another
• Adversarial Search
69
Uninformed/Blind search
strategies
70
Uninformed/Blind search strategies
• Breadth-first search)
• Depth-first search)
• Depth-limited search
• Iterative deepening depth-first search
• Uniform-cost Search
71
Breadth-first search - Idea
72
Breadth-first search
A D Move downwards,
level by level,
until goal is
B D A E reached.
C E E B B F
D F B F C E A C G
G C G F
G 73
Example: Traveling from Arad To Bucharest
74
Breadth-first search
75
Breadth-first search
76
Breadth-first search
77
BFS Graph Search for 8-Puzzle
78
Breadth-first search (BFS)
79
Properties of breadth-first search
• Completeness:
• Time complexity:
• Space complexity:
• Optimality:
• Search algorithms are commonly evaluated according to the following four criteria:
• Completeness: does it always find a solution if one exists?
• Time complexity: how long does it take as function of num. of nodes?
• Space complexity: how much memory does it require?
• Optimality: does it guarantee the least-cost solution?
80
Properties of breadth-first search
81
Time complexity of breadth-first search
• If a goal node is found on depth d of the tree, all nodes up till that
depth are created.
d
m
b G
• Thus: O(bd)
82
Space complexity of breadth-first
d
m
b G
83
Examples
• Assuming b=10, checking 1000 states takes 1 second, storing one state
takes 100 bytes
84
Depth First Search - Idea
85
Depth First Search - Idea
86
Depth First Search
C E
D F
87
Romania with step costs in km
88
Depth-first search
89
Depth-first search
90
Depth-first search
91
DFS for 8-Puzzle
92
DFS for 8-Puzzle
Goal found after 19 node expansions
and 32 goal checks
93
DFS vs. BFS
94
Depth-first search algorithm
95
Properties of depth-first search
Remember:
b = branching factor
m = max depth of search tree
96
Time complexity of depth-first: details
m
b
...
• A search tree can contain many nodes corresponding to the same state –
these states are called repeated states
• Search algorithms will waste a lot of time re-expanded the states we have
already explored
• To handle repeated states:
• Every time you expand a node, add that state to the explored set; do not put explored
states on the frontier again
• Every time you add a node to the frontier, check whether it already exists in the frontier
with a higher path cost, and if yes, replace that node with the new one
99
Exercise
100
Exercise (2)
101
Breadth-First Search
Strategy: expand a a G
shallowest node first b c
Implementation: e
d f
Frontier is a FIFO queue S h
p q r
d e p
Search
b c e h r q
Tiers
a a h r p q f
p q f q c G
q c G a
a
Depth-First Search
Strategy: a G
b c
expand a e
d f
deepest node S h
first p q r
Implementation S
: Frontier is a d e p
LIFO stack b c e h r q
a a h r p q f
p q f q c G
q c G a
a
Quiz: DFS vs BFS
105
Iterative Deepening
108
109
110
111
112
113
114
115
116
Iterative deepening search
Iterative deepening complexity
118
Uniform-cost search
• For each frontier node (queue), save the total cost of the path from the initial
state to that node
• Expand the frontier node with the lowest path cost
• Implementation: frontier is a priority queue ordered by path cost
• Equivalent to breadth-first if step costs all equal
• Equivalent to Dijkstra’s algorithm in general
Function UNIFORM-COST-SEARCH(problem) returns a solution, or failure
initialize the explored set to be empty
initialize the frontier as a priority queue using node path_cost as the priority
add initial state of problem to frontier with path_cost = 0
loop do
if the frontier is empty then
return failure
choose a node and remove it from the frontier
if the node contains a goal state then
return the corresponding solution
add the node state to the explored set
for each resulting child from node
if the child state is not already in the frontier or explored set then
add child to the frontier
else if the child is already in the frontier with higher path_cost then
replace that frontier node with child
Uniform-cost search example
Uniform-cost search example
• Expansion order:
(S,p,d,b,e,a,r,f,e,G)
Another example of uniform-cost search
Source: Wikipedia
Properties of uniform-cost search
• Complete?
Yes, if step cost is greater than some positive constant ε
(we don’t want infinite sequences of steps that have a
finite total cost)
• Optimal?
Yes!
Optimality of uniform-cost search
• Is it complete?
• Assuming C* is finite and > 0, yes!
• Is it optimal?
• Yes!
Comparing uninformed search strategies
128
References
• AIMA textbook
• Slides of CMU AI course
• Slides of UC Berkeley AI course
129