L03 Problem Solving As Search I
L03 Problem Solving As Search I
Semester I, 2022-23
1
This Class
• Reflex Agents
• Problem Solving as search
• Uninformed Search
• Reference Material
• AIMA Ch. 3
2
Acknowledgement
These slides are intended for teaching purposes only. Some material
has been used/adapted from web sources and from slides by Doina
Precup, Dorsa Sadigh, Percy Liang, Mausam, Dan Klein, Nicholas Roy
and others.
3
Last time: Agent View of AI
• An agent is anything that can be viewed
as perceiving its environment through
sensors and acting upon that Sensors
environment through actuators.
Environment
Percepts
• Examples ?
Agent
• Alexa
Actuators
• Robotic system Actions
• Refinery controller
• Question answering system
• Crossword puzzle solver
• ……
4
Simple Reflex Agents
• A Reflex Agent
• Selects action based on the current percept.
• Directly map states to actions.
• Operate using condition-action rules.
• If (condition) then (action)
• Example:
• An autonomous car that is avoiding obstacles.
• If (car-in-front-is-braking) then (initiate-braking)
• Problem: no notion of goals
• The autonomous car cannot take actions that will
lead to an intended destination.
5
From Reflex to Problem Solving Agents
• Reflex agents
• Directly map states to actions.
• No notion of goals. Do not consider
action consequences.
6
Example – Route Finding
• Problem:
• Find a solution i.e., a sequence of
actions (road traversals) that can
take the agent to the destination
in minimum time.
• Search
• Process of looking for a sequence
of actions that reaches the goal
• Note: as we will see search problems
are more general than path finding.
7
Search Problem Formulation
Many problems in AI can be modeled as search problems.
8
Formulating a Search Problem
9
Example – Route Finding
• State space:
• All the cities on the map.
• Actions:
• Traversing a road: Going to an adjacent city.
• Cost:
• Distance along the road
• Start state:
• Arad
• Goal test:
• Is state == Bucharest?
10
Modeling Assumptions
• Environment is observable
• The agent always knows it current state.
• Discrete states and actions
• Finite number of cities.
• At any given state there are a finite number of actions.
• Known and Deterministic action outcomes
• The agent knows which sates are reached by each action.
• Action has exactly one outcome when applied to a state.
11
Example – The Eight Puzzle
13
Example – Block Manipulation
Actions: Put X on Y B
Put B on C
C C
A B A
Put C on A
Put C on A
A B C
A
Put A on C
Put C on B C C
A B B
14
State Space Graphs
• A representation of a search problem
• Nodes are (abstracted) world configurations
• Arcs represent successors (action results)
• The goal test is a set of goal nodes (maybe only one)
• Each state occurs only once
• The full graph is usually too large.
• The graph is built and explored implicitly by
applying actions on states.
15
Searching for a solution
16
Search Trees
Current state
a G d e p
b c q
b c e h r
e
d f a a h r p q f
S h
p q f q c G
p q r
q c G a
18
Tree Search
19
Infrastructure for Search Algorithms
20
Search Tree
• The process of expansion while
constructing the search tree.
• Note that Arad appears again.
21
The Eight Puzzle – State Space (Fragment)
22
Examples – Assembly Planning
Possible assemblies
23
Revisiting States
• What if we revisit a state that was already
expanded? Redundancy.
• Maintain an explored set (or closed list) to store
every expanded node
• Worst-case time and space requirements are O(|S|)
where |S| is the number of states.
24
Graph Search
25
Notion of a Frontier
• How to manage generated nodes?
• Need a data structure for managing nodes as
they are generated.
• Queue (characterized by the order win which Progression
they store the inserted nodes).
• Frontier of search
• Separates the explored and unexplored
nodes.
• Also called open list
• Search Strategy
• Search algorithms vary in their “strategy” to
decide which nodes to explore?
• We will see examples soon.
26
Measuring problem-solving performance
27
Properties of Search Algorithms
• Completeness
• Is the search algorithm guaranteed to find a solution when there is one?
• Should not happen that there is a solution but the algorithm does not find it
(e.g., infinite loop in a part of the state space)
• Optimality
• Is the plan returned by the search algorithm the optimal ?
• Time Complexity
• The number of nodes generated during search.
• Space Complexity
• The maximum number of nodes stored in memory during search.
28
Search Algorithms
• The strategy for exploration of nodes leads to a variety of search
algorithms
• Uninformed Search
• Only use information about the state in the problem definition.
• Generate successors and distinguish goal states from no-goal states.
• Informed Search
• Use problem-specific knowledge beyond the problem definition
• Heuristics for more efficient search
29
Breadth-First Search (BFS)
a G
Strategy: expand a shallowest unexplored node b c
first.
e
d f
All the successors of a node are expanded,
S h
then their successors and so on.
p q r
Implementation: Frontier is a FIFO queue
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
30
a
Breadth First Search (BFS) Properties
• Expansion Strategy
• Expands the shallowest unexplored node in 1 node
the frontier of the search tree. b
… b nodes
• Time Complexity d is the
depth of b2 nodes
• Search takes time O(bd) the
shallowest bd nodes
• Space Complexity goal
• Frontier nodes O(bd)
• Explored nodes O(bd-1) bm nodes
• Memory requirement is a problem.
31
Breadth First Search (BFS) Properties
Time and memory requirements for BFS. Branching factor b = 10. 1 million nodes per second
and 100 bytes per node.
32
Breadth First Search (BFS) Properties
• Is it complete?
• Yes. 1 node
b
• The shallowest goal is at a finite depth, d … b nodes
• If the branching factor, b, is finite then BFS d is the
depth of b2 nodes
will find it.
the
shallowest bd nodes
• Is it optimal? goal
• Yes. If the path cost is a non-decreasing
function of depth.
• For example, if all edge costs are equal. bm nodes
33
Depth-First Search (DFS)
a G
b c
e
d f
S h
p q r
34
Depth-First Search (DFS)
a G
Strategy: expand a deepest node first b c
Implementation: Frontier is a LIFO e
d f
stack
S h
p q r
d e p
b c e h r q
a a h r p q f
p q f q c G
q c G a
35
a
Depth First Search (DFS) Properties
• Expansion Strategy
• Expands the deepest unexplored node in the
1 node
frontier of the search tree b
… b nodes
• Time Complexity
• Worst case: processes the whole tree. b2 nodes
• If m is finite, takes time O(bm) m is max
depth
• Space Complexity
• Frontier stores:
• Single path from the root to the leaf node.
• Sibling nodes on the path that are unexplored. bm nodes
• Memory requirement is low O(bm)
• Is it complete?
• Yes, if m is finite. Eventually, finds the path.
• Is it optimal? Note: Variant of graph search where the goal test
is applied at node generation time. Space saving.
• No, it finds the “leftmost” solution, regardless 36
of depth or cost
Reducing DFS memory requirements
• Backtracking search
• Only one successor is generated at a time rather than all successors
• Each partially expanded node remembers which successor to generate next.
• Memory saving by modifying the current state description directly rather than
copying.
37
Depth-Limited Search
• Problem
• Depth First Search fails when the maximum goal depth is not known ahead of
time for a domain
• Solution
• Depth Limited Search
• Restrict the depth of search (supply a depth limit, l)
• Search depth-first, but terminate a path either if a goal state is found or if the maximum
allowed depth is reached.
• Equivalently, nodes at l have no successors.
38
Depth Limited Search (DLS) Properties
d shallowest b
• Termination goal depth l depth limit
• Always terminates.
• Time Complexity
• Worst case: processes the whole tree till l.
• Time O(bl)
• Space Complexity Not complete
• Frontier is managed like Depth First Search.
• Memory O(bl). d shallowest b
goal depth l depth limit
• Is it complete?
• Not complete when goal depth is greater than
the limit (d>l)
• Is it optimal?
• Not optimal when the limit is greater than the
goal depth (l > d) Not optimal
39
Iterative Deepening Search
• Combine DFS’s space advantage with BFS’s …
b
shallow-solution advantages
• Run a DLS with depth limit 1. If no solution…
• Run a DLS with depth limit 2. If no solution…
• Run a DLS with depth limit 3. …..
40
Iterative Deepening: Example
41
Adapted from Prof. Mausam’s slide
Iterative Deepening: Example
42
Adapted from Prof. Mausam’s slide
Iterative Deepening: Example
43
Adapted from Prof. Mausam’s slide
Iterative Deepening: Properties
• Is it wasteful to generate nodes
again and again?
• Not really!
• The lowest level contributes the
maximum. Overhead is not
significant in practice. No. of times generated.
44
Adapted from Prof. Mausam’s slide
Iterative Deepening Properties
• Time Complexity
• Time O(bd) L=0
L=1
• Space Complexity
• Memory O(bd) L=2
• Linear memory requirement like DFS
• Is it complete?
L=3
• Yes. Complete like BFS
• Is it optimal?
• Yes. Optimal like BFS (if costs are non-
decreasing function of path length)
• Relevance
• Preferred method for large state spaces where
maximum depth of a solution is unknown
45
Two small search trees instead of one large?
Bi-directional Search
• Run one search forward from
the initial state.
• Run another search backward
from the goal.
• Stop when the two searches
meet in the middle.
46
Bi-directional Search
• Space and time complexity
• O(bd/2)
• bd/2 + bd/2 is smaller than bd
• 108+108 =2.108 << 1016
• Needs an efficiently computable
Predecessor() function
• Difficult: e.g., predecessors of
checkmate in chess?
• What if there are several goal
states?
• Create a new dummy goal state
whose predecessors are the actual
goal states.
47
Guiding search by costs instead of depth
• Till now, the cost on the edges GOAL
was not considered 2 a
2
• Worked with solution depths.
b c
3
• Solution was found in terms of 2
1 8
number of actions.
2 e
• Did not find the least-cost path.
3 d
• BFS, DFS …. f
9 8 2
• Next START h
• Cost-sensitive search 1 4 2
p 4 r
15
q
48
Uniform Cost Search (UCS)
Intuition, the low-cost plans
2 a G
Strategy: expand a “cheapest” cost should be pursued first.
b c
node first: 1 8 2
2 e
3 d f
Frontier is a Priority Queue (Priority: 9 2
S h 8 1
cumulative cost so far)
1 p q r
15
S 0
d 3 e 9 p 1
b 4 c e 5 h 17 r 11 q 16
11
Cost contours a 6 a h 13 r 7 p q f
during search
p q f 8 q c G
q 11 c G 10 a
49
a
Uniform Cost Search (UCS)
50
Uniform Cost Search (UCS) Properties
• What nodes does UCS expand?
• Guided by costs and not the depth.
• If that solution costs C* and action cost at least e , then the b c£1
“effective depth” is roughly C*/e …
• Takes time O(bC*/e) (exponential in effective depth) c£2
C*/e “tiers”
c£3
• How much space does the frontier take?
• O(bC*/e)
• Is it complete?
• Yes. Assuming best solution has a finite cost and minimum arc
cost is positive, yes!
• Is it optimal?
• Yes. (Proof via contradiction)
51
What if we bound the frontier size?
Beam Search
• Keep a maximum size of the frontier.
• Only keep the k best candidates for expansion, discard the rest.
• Advantage:
• More space efficient
• Disadvantage
• May throw away a node that is on the solution path
• Complete? No.
• Optimal? No.
• Very popular in practice
52
Summary Table
53
Repeated States
Towers of Hanoi problem
If we did not check for duplicate states, then the tree size is exponential in the number of states. If we
do check for repeated states, then our tree is much smaller (linear).
Handling Repeated States – Remember all the
visited nodes
• Never generate states that have
already been generated before.
• Maintain an explored list (Graph search)
• Optimal approach
• Memory inefficient, why?
• Exponential number of nodes in the tree
• E.g., 8-puzzle problem, we have 9! = 362,880
states.
• Duplicate checking of states also adds time.
Handling Repeated States – Use efficient data
structures
• Use efficient data structures to
keep the explored nodes.
• Hash Tables
• Insertion and look up in constant
time.
• Duplicate checking
• Canonical form, sorted list or other
efficient methods.
Handling Repeated States – Check for some of
the cases
• Never return to the state you have just come from
• Prevent the node expansion function from generating any node successor
that is the same state as the node’s parent.
• Never create search paths with cycles in them
• The node expansion function must be prevented from generating any node
successor that is the same state as any of the node’s ancestors
• Practical techniques but sub-optimal
Uniformed Search Issues
• Uninformed search explores options in … c£1
c£2
every “direction”
c£3
• For example, UCS explores increasing cost
contours
Start Goal
61
Summary
• Algorithms that are given no information about the problem other
than its definition.
• No additional information about the state beyond that provided in the
problem definition.
• Generate successors and distinguish goal from non-goal states.
• Hence, all we can do is move systematically between states until we stumble
on a goal
• Search methods are distinguished by the order in which the nodes are
expanded.
• Next time: Informed (heuristic) search uses a guess on how close to
the goal a state might be.
62