03_search
03_search
problems by
searching
Contents
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 performance measure is typically the cost to reach the goal.
• We will discuss a special type of goal-based agents called planning agents which
use search algorithms to plan a sequence of actions that lead to the goal.
Agent’s
Maze
location
Map of Search
the maze for a plan
Exit
location
…).
• The state space is the set of all possible states of
the environment and some states are marked as
goal 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. z Goal
state
Phases:
1) Search/Planning: the process of looking for the sequence of actions that reaches a
goal state. Requires that the agent knows what happens when it moves!
2) Execution: Once the agent begins executing the search solution in a deterministic,
known environment, it can ignore its percepts (open-loop system).
Definition of a Search problem
Initial state Actions: {N, E, S, W}
Transitions
1
• Initial state: state description g i
4
• Actions: set of possible actions
• Transition model: a function that
defines the new state resulting from a
performing an action in the current state
• Goal state: state description
• Path cost: the sum of step costs z Goal
state
Discretization grid
A state space
Discretization grid
Goal state
• Available actions in a state come from the transition
function. E.g.,
Note: Known and deterministic is a property of the transition function!
Example: Traveling in Romania
State space:
Cities
Successor function:
Roads: Go to adjacent city with
cost = distance
Start state:
Arad
Goal test:
Is state == Bucharest?
Solution?
Original Description
Distance in miles
Example: Vacuum world
State Space
Goal states
Goal states
Issue: Transition Model is Not a Tree!
It can have Redundant Paths
Cycles
Return to the same state. The search tree will create a new node!
Initial state
Path 1 Path 2
Goal states
Search Tree
• Superimpose a “what if” tree of possible actions Root node =
and outcomes (states) on the state space graph. Initial state
• The Root node represents the initial state. a
• An action child node is reached by an edge Edge = Action
representing an action. The corresponding state
is defined by the transition model. Child node Non-cycle
b c redundant
• Trees cannot have cycles (loops) or multiple
path
paths to the same state. These are called
redundant paths. Cycles in the search space
must be broken to prevent infinite loops.
Removing other redundant paths improves d e e
search efficiency.
… … …
Cycle
• A path through the tree corresponds to a
sequence of actions (states). b
• A solution is a path ending in a node
representing a goal state.
Solution path
• Nodes vs. states: Each tree node represents a
f Node representing
state of the system. If redundant path cannot be a Goal state
prevented then state can be represented by
multiple nodes.
Tree Search Algorithm Outline
Transition model
Tree Search Example
1. Expand Arad
Frontier
Transition model
Tree Search Example
Frontier
2. Expand Sibiu
Transition model
Example of
a cycle
• Frontier – the part of the
tree we have
constructed so far.
• Partial plans under
consideration
• We could have also
expanded Timisoara or
Zerind!
General Tree Search
Important ideas:
Frontier
Expansion
Exploration strategy
• 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).
The state
State Space Size Estimation consists of
variables called
• Even if the used algorithm represents the state space using fluents that
atomic states, we may know that internally they have a represent
factored representation that can be used to estimate the
problem size. conditions that
• The basic rule to calculate (estimate) the state space size for can change over
factored state representation with fluents (variables) is: time.
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 (redundant path checking).
• Builds a tree with links between parent and child.
Breadth-First Search
Strategy: expand a a G
shallowest node first b c
Implementation: Fringe e
d f
is a FIFO queue S h
p q r
d e p
Searc
b c e h r q
h
Tiers a a h r p q f
p q f q c G
q c G a
a
Implementation: BFS
40
Implementation: Expanding the
Search Tree
• AI tree search creates the search tree while searching.
• The EXPAND function tries all available actions in the current node
using the transition function (RESULTS). It returns a list of new nodes
for the frontier.
expanded
d=1
b=2
m=3
B C Goal
D E F G
E C Goal
• Optimal?
Yes – if cost is the same per step (action). Otherwise: Use uniform-cost search.
• Time?
Number of nodes created:
• Space?
Stored nodes:
Note:
• The large space complexity is usually a bigger problem than time!
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!
• Optimal?
Yes – nodes expanded in increasing order of path cost
• 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*/ ε)
Cycle checking
checks only the
current path.
Redundant paths
can not be
identified and lead
to replicated work.
Implementation: DFS
• DFS could be implemented like BFS/Best-first search and just taking the last element
from the frontier (LIFO).
• However, to reduce the space complexity to , the reached data structure needs to be
removed! Options:
• Recursive implementation (cycle checking is a problem leading to infinite loops)
• Iterative implementation: Build tree and abandoned branches are removed from memory.
DFS uses ℓ
Cycle checking is only done against the current path. This is similar to Backtracking search.
A
b=2 d=1
B C Goal
m=3
D E
• Time?
The worst case is to reach a solution at maximum depth m in the last path:
Terrible compared to BFS if
• Space?
is linear in max. tree depth which is very good but only achieved if no reached data
structure and memory management (forget old branches) is used! Cycles can be
broken but redundant paths cannot be checked.
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 (like BFS)
• Time?
Consists of rebuilding trees up to times
Slower than BFS, but the same complexity class!
• Space?
O(bd) linear space. Even less than DFS since . 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 the
workhorse of AI.
Informed Search
Informed Search
• AI search problems typically have a very large search space. We would like
to improve efficiency by expanding as few nodes as possible.
• The agent can use additional information in the form of “hints” about what
promising states are to explore first. These hints are derived from
• information the agent has (e.g., a map with the goal location marked) or
• percepts coming from a sensor (e.g., a GPS sensor and coordinates of the goal).
• The agent uses a heuristic function to rank nodes in the frontier and always
select the most promising node in the frontier for expansion using the 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)
366
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
• Time?
Worst case: O(bm) like DFS
Best case: O(bm) – If is 100% accurate we only expand a single path.
• Space?
Same as time complexity.
The Optimality Problem of
Greedy Best-First search
Greedy best-first search only considers the estimated cost to the goal.
=3
n
• 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:
• The agent in the example above will stop at n with and chose the path up with a better
h (𝑛)
A* Search Example
𝑓 ( 𝑛 )=𝑔 ( 𝑛 ) +h (𝑛)
h (𝑛)
A* Search Example
𝑓 ( 𝑛 )=𝑔 ( 𝑛 ) +h (𝑛)
h (𝑛)
A* Search Example
𝑓 ( 𝑛 )=𝑔 ( 𝑛 ) +h (𝑛)
h (𝑛)
A* Search Example
𝑓 ( 𝑛 )=𝑔 ( 𝑛 ) +h (𝑛)
h (𝑛)
A* Search Example
𝑓 ( 𝑛 )=𝑔 ( 𝑛 ) +h (𝑛)
h (𝑛)
BFS vs. A* Search
BFS
A* A*
Source: Wikipedia
Implementation of A* Search
Path cost to + heuristic from to goal = estimate of the total cost
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. This is often too much unless a very
good heuristic is know.
Designing Heuristic Functions
Heuristics for the 8-puzzle
= number of misplaced tiles
= total Manhattan distance (number of squares from desired location
of each tile)
1 needs to move 3
Are and 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.
• What relaxation is used by and ?
• : If the rules of the 8-puzzle are relaxed so that a tile can move anywhere, then
gives the shortest solution.
• : If the rules are relaxed so that a tile can move to any adjacent square, then gives
the shortest solution.
Heuristics from Relaxed Problems
What relaxations are used in these two cases?
Euclidean distance Manhattan distance
Start state Start state
*
* * *
* * * *
Dominance: What Heuristic is
Better?
Definition: If and are both admissible heuristics and
for all , then
dominates
• Solution at depth
IDS = 3,644,035 nodes
A*() = 227 nodes
A*() = 73 nodes
• Solution at depth
IDS ≈ 54,000,000,000 nodes
A*() = 39,135 nodes
A*() = 1,641 nodes
Combining Heuristics
• That is, always pick for each node the heuristic that
is closest to the real cost to the goal .
Satisficing Search: Weighted A*
Search
• Often it is sufficient to find a “good enough” solution if it can be found very
quickly or with way less computational resources. I.e., expanding fewer
nodes.
Weighted A* search:
In finite spaces
DFS (cycle checking) No 𝑂(𝑏𝑚) 𝑂 (𝑏𝑚)
If all step
IDS Yes
costs are equal
𝑂(𝑏 𝑑) 𝑂 (𝑏𝑑)
Uniform-cost
Search Yes Yes Number of nodes with
If all step
IDS Yes
costs are equal
𝑂(𝑏 𝑑) 𝑂 (𝑏𝑑)
1 S
Agent’s State 2 S
(= program counter) 3 S
4 E
Step 2 … …
Note: The agent does not use percepts or the transition function. It blindly follows the plan.
Caution: This only works in an environment with deterministic transitions.
Complete Planning Agent for a Maze-
Solving Agent
Map
= Transition function +
initial and goal state
Nee r repla
function
o
d t
has an event percepts
op
Environment
lan
loop:
n
• Read sensors State
• Call agent Agent
function
Plan function Physical
• Execute
action in the Maze
action
pla the
physical
w
environment
n
llo
Fo
• Repeat Counter in
plan Actuators Execute
action in the
physical
environment
• The event loop calls the agent function for the next action.
• The agent function follows the plan or calls the planning function if there is no plan yet or it
thinks the current plan does not work based on the percepts (replanning).
Conclusion
• 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 transition
function as a compact description of the
transition model.
• The search tree is built on the fly, and we
have to deal with cycles, redundant
paths, and memory management.