0% found this document useful (0 votes)
2 views

03_search

The document discusses search problems in artificial intelligence, focusing on designing goal-based agents in known, fully observable, and deterministic environments. It outlines the components of search problems, including initial state, actions, transition models, goal states, and path costs, and explains the importance of search strategies and tree search algorithms. Various examples, such as the Romania vacation scenario and the vacuum world, illustrate the concepts of state space and transition models.

Uploaded by

Surya Basnet
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

03_search

The document discusses search problems in artificial intelligence, focusing on designing goal-based agents in known, fully observable, and deterministic environments. It outlines the components of search problems, including initial state, actions, transition models, goal states, and path costs, and explains the importance of search strategies and tree search algorithms. Various examples, such as the Romania vacation scenario and the vacuum world, illustrate the concepts of state space and transition models.

Uploaded by

Surya Basnet
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 85

CS 5/7320

Artificial Intelligence

Solving problems
by searching
AIMA Chapter 3

Slides by Michael Hahsler


based on slides by Svetlana Lazepnik
with figures from the AIMA textbook.

This work is licensed under a Creative Commons


Attribution-ShareAlike 4.0 International License.
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 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

𝑎𝑎𝑠𝑠 = argmina∈A [𝑐𝑐𝑐𝑐𝑐𝑐𝑐𝑐 𝑠𝑠, 𝑠𝑠1 , 𝑠𝑠2 , … , 𝑠𝑠𝑛𝑛 𝑠𝑠𝑛𝑛 ∈ 𝑆𝑆 𝑔𝑔𝑔𝑔𝑔𝑔𝑔𝑔 ]


What are search problems?
• We will consider the problem of designing Initial state
goal-based agents in known, fully
1
observable, deterministic environments.
• For now, we consider only a discrete
environment using an atomic state
representation (states are just labeled 1, 2, 3,
…).
• The state space is the set of all possible states
of the environment and some states are Goal
z
marked as goal states. 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).
Search problem components
• Initial state: state description Initial state Actions: {N, E, S, W}
Transitions
• Actions: set of possible actions 𝐴𝐴 1
g i
• Transition model: a function that 4
defines the new state resulting from
performing an action in the current
state
a
• Goal state: state description
• Path cost: the sum of step costs
z Goal
state
Notes: Discretization grid

• 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
• As an action schema:
𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴𝐴 𝑔𝑔𝑔𝑔 𝑑𝑑𝑑𝑑𝑑𝑑
Original Description PRECOND: no wall in direction 𝑑𝑑𝑑𝑑𝑑𝑑
EFFECT: change the agent’s location according to 𝑑𝑑𝑑𝑑𝑑𝑑
Initial state Actions: {N, E, S, W} • As a function:
Transitions 𝑓𝑓: 𝑆𝑆 × 𝐴𝐴 → 𝑆𝑆 or 𝑠𝑠 ′ = 𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟𝑟(𝑎𝑎, 𝑠𝑠)
1 Function implemented
2 g i as a table representing 𝑠𝑠 𝑎𝑎 𝑠𝑠𝑠
3 the state space
4 a as a graph.
1 S 2
5
2 N 1
2 S 3
… … …
4 E a
4 S 5
4 N 3
… … …
z

Discretization grid
Goal state • Available actions in a state come from the
transition function. E.g.,
𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎𝑎(4) = {𝐸𝐸, 𝑆𝑆, 𝑁𝑁}

Note: Known and deterministic is a property of the transition function!


Original Description

Example: Romania Vacation


• On vacation in Romania; currently in Arad
• Flight leaves tomorrow from Bucharest

• Initial state: Arad State Space/Transition model


• Actions: Drive from Defined as a graph
one city to another.
• Transition model
and states: If you go
from city A to city B,
you end up in city B.
• Goal state: Bucharest
• Path cost: Sum of
edge costs.

Distance in miles
Example: Vacuum world
State Space

Goal states

• Initial State: Defined by agent location and dirt location.


• Actions: Left, right, suck
There are 8 possible
• Transition model: Clean a location or move. atomic states of the
system.
• Goal state: All locations are clean.
Why is the number of
• Path cost: E.g., number if actions states for n possible
locations 𝑛𝑛 2𝑛𝑛 ?
Example: Sliding-tile puzzle
• Initial State: A given configuration.
• Actions: Move blank left, right, up, down
• Transition model: Move a tile
• Goal state: Tiles are arranged empty and 1-8 in order
• Path cost: 1 per tile move.

State space size


Each state describes the location of each tile (including the
empty one). ½ of the permutations are unreachable.
• 8-puzzle: 9!/2 = 181,440 states
• 15-puzzle: 16!/2 ≈ 1013 states
• 24-puzzle: 25!/2 ≈ 1025 states
Example: Robot motion planning

• Initial State: Current arm position.


• States: Real-valued coordinates of robot joint angles.
• Actions: Continuous motions of robot joints.
• Goal state: Desired final configuration (e.g., object is grasped).
• Path cost: Time to execute, smoothness of path, etc.
Solving search problems
Given a search
problem definition Construct a
How do we find the
search tree
• Initial state
optimal solution
for the state
• Actions (sequence of
space graph!
• Transition model actions/states)?
• Goal state
• Path cost
Initial state
State space

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

Non-cycle redundant paths


Multiple paths to get to the same state
Goal states
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 stare.
• An action child node is reached by an edge
a
representing an action. The corresponding state Edge = Action
is defined by the transition model.
• Trees cannot have cycles (loops) or multiple Child node Non-cycle
paths to the same state. These are called b c redundant
redundant paths. Cycles in the search space path
must be broken to prevent infinite loops.
Removing other redundant paths improves
search efficiency.
• A path through the tree corresponds to a d e e
sequence of actions (states).

… … …
Cycle
• A solution is a path ending in a node
representing a goal state.
b
• Nodes vs. states: Each tree node represents a
state of the system. If redundant path cannot be Solution path
prevented then state can be represented by
multiple nodes.
f Node representing
a Goal state
Differences between typical Tree search and
AI search
Typical tree search AI tree/graph search

• Assumes a given tree that fits • The search tree is too large to fit into
in memory. memory.
a. Builds parts of the tree from the
initial state using the transition
function representing the graph.
b. Memory management is very
important.

• Trees have by construction no • The search space is typically a very


cycles or redundant paths. large and complicated graph.
Memory-efficient cycle checking is
very important to avoid infinite loops
or minimize searching parts of the
search space multiple times.
• Checking redundant paths often
requires too much memory and we
accept searching the same part
multiple times.
Tree Search Algorithm Outline

1. Initialize the frontier (set of unexplored know nodes)


using the starting state/root node.
2. While the frontier is not empty:
a) Choose next frontier node to expand according to
search strategy.
b) If the node represents a goal state, return it as the
solution.
c) Else expand the node (i.e., apply all possible actions to
the transition model) and add its children nodes
representing the newly reached states to the frontier.
Tree search example
Frontier

Transition model
Tree search example
1. Expand Arad

Frontier

Transition model
Tree search example
Frontier
2. Expand Sibiu

Transition model
Example of
a cycle

We could have
also expanded
Timisoara or
Zerind!
Search strategies

• A search strategy is defined by picking the order of node expansion.

• Strategies are evaluated along the following dimensions:


• Completeness: does it always find a solution if one exists?
• Optimality: does it always find a least-cost solution?
• Time complexity: how long does it take?
• Space complexity: how much memory does it need?

• 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 environment State representation
can be in.
• Reachable states are defined by the initial state and the
transition model. Not all states may be reachable from 𝑥𝑥1
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.

State Space Size Estimation The state consists of


variables called fluents
that represent
• Even if the used algorithm represents the state space conditions that can
using atomic states, we may know that internally they change over time.
have a factored representation that can be used to
estimate the problem size.
• The basic rule to calculate (estimate) the state space size
for factored state representation with 𝑛𝑛 fluents
(variables) is:
𝑥𝑥1 × 𝑥𝑥2 × ⋯ × 𝑥𝑥𝑛𝑛
where ⋅ is the number of possible values.
In how many ways can we
order/arrange n objects?

2 × 2 = 22 = 4
3×2×1=6

Factorial: 𝑛𝑛! = 𝑛𝑛 × 𝑛𝑛 − 1 × ⋯ × 2 × 1

#Python
import math

print (math.factorial(23))

Source: Permutations/Combinations Cheat Sheets by Oleksii Trekhleb


https://itnext.io/permutations-combinations-algorithms-cheat-sheet-68c14879aba5
𝑛𝑛
Binomial Coefficient: = 𝐶𝐶 𝑛𝑛, 𝑟𝑟 = 𝑛𝑛 𝐶𝐶𝑟𝑟
3 𝑟𝑟
=3 Read as “n choose r” because it is the number
2
of ways can we choose 𝑟𝑟 out of 𝑛𝑛 objects?
𝑛𝑛 𝑛𝑛(𝑛𝑛−1)
Special case for 𝑟𝑟 = 2: =
2 2

#Python
import scipy.special

# the two give the same


results
scipy.special.binom(10, 5)
scipy.special.comb(10, 5)
Source: Permutations/Combinations Cheat Sheets by Oleksii Trekhleb
https://itnext.io/permutations-combinations-algorithms-cheat-sheet-68c14879aba5
Examples: What is the state space size?

Dirt
• Permutation: A and B are different rooms, order
does matter!
• With repetition: Dirt can be in both rooms.
• There are 2 options (clean/dirty)

→ 22

Robot location
• Can be in 1 out of 2 rooms.
→2

Total: 2 × 22 = 23 = 8

r … # of rooms
n … options
Examples: What is the state space size?
Often a rough upper limit is sufficient to determine how hard the search problem is.

Maze 8-queens problem 8-puzzle problem Tic-tac-toe


Examples: What is the state space size?
Often a rough upper limit is sufficient to determine how hard the search problem is.

Maze 8-queens problem 8-puzzle problem Tic-tac-toe

Positions the agent All arrangements with All arrangements All possible boards.
can be in. 8 queens on the of 9 elements.
board. 𝑛𝑛 < 39 = 19,683
n = Number of 𝑛𝑛 ≤ 9!
64
white squares. 𝑛𝑛 < 2 ≈ 1.8 × 1019 Many boards are not
Half is legal (e.g., all x’s)
We can only have 8 unreachable:
queens: 9!
𝑛𝑛 = = 181,440
𝑛𝑛 = 64
8
≈ 4.4 × 109 2
Uninformed Search
Uninformed search strategies

The search algorithm/agent is not provided information about how


close a state is to the goal state.

It blindly searches following a simple strategy until it finds the goal


state by chance.

Search strategies we will discuss:

Breadth-first search
Uniform-cost search
Depth-first search
Iterative deepening search
Breadth-first search (BFS)

Expansion rule: Expand shallowest unexpanded node in the frontier


(=FIFO).

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

Expand adds the next level


below node to the search tree.

reached makes sure we do not


visit nodes twice (e.g., in a
cycle or other redundant path).
Fast lookup is important.

31
Implementation: Expanding the search tree

• AI tree search creates the search tree while searching.


• The EXPAND function uses the current search tree node (i.e., current
state) and the problem description to create new nodes for all
reachable states.
• It tries all actions in the current state by checking the transition
function (RESULTS) and then returns a list of new nodes for the
frontier.
Node structure for
the search tree.
Yield can also be
Transition implemented by
function returning a list of
Nodes.
Properties of Breadth-first search
d: depth of the optimal solution
• Complete? m: max. depth of tree
Yes b: maximum branching factor

• 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!

• Complete? d: depth of the optimal solution


Yes, if all step cost is greater than some small positive constant ε > 0 m: max. depth of tree
b: maximum branching factor
• 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*/ ε)

See Dijkstra's algorithm on Wikipedia


Implementation: Best-First Search Strategy

The order for expanding the


frontier is determined by
f(n) = path cost from the
initial state to node n.

This check is the difference


to BFS! It visits a node again
See BFS for function EXPAND. if it can be reached by a
better (cheaper) path.
36
Depth-first
search (DFS)
• Expansion rule:
Expand deepest
unexpanded
node in the
frontier (last
added).
• Frontier: stack
(LIFO)
• No reached data
structure!
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.
Cycle checking is only done against the current path. This is similar to Backtracking search.
DFS uses ℓ = ∞

If we only keep the current path from


the root to the current node in
memory, then we can only check
against that path to prevent cycles, but
we cannot prevent other redundant
paths. We also need to make sure the
frontier does not contain the same
state more then once!

See BFS for function EXPAND.


Properties of depth-first search
• Complete?
• Only in finite search spaces. Cycles can be avoided by checking for repeated states
along the path.
• Incomplete in infinite search spaces (e.g., with cycles).

• Optimal? d: depth of the optimal solution


No – returns the first solution it finds. m: max. depth of tree
b: maximum branching factor

• 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

• Time: 𝑂𝑂(𝑏𝑏𝑚𝑚) – worst case is expanding all paths.


• Space: 𝑂𝑂(𝑏𝑏𝑏𝑏) - if it only stores the frontier nodes and the current path.

A
b=2 d=2

B C Goal
m=4

D E

H Goal DFS finds this goal first  Not optimal!

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?

Use depth-restricted DFS and gradually increase the depth.

1. Check if the root node is the goal.


2. Do a DFS searching for a path of length 1
3. If goal not found, do a DFS searching for a path of length 2
4. If goal not found, do a DFS searching for a path of length 3
5. …
Iterative
deepening
search
(IDS)
Implementation: IDS

See BFS for function EXPAND.


Properties of iterative deepening search

• Complete? d: depth of the optimal solution


Yes m: max. depth of tree
b: maximum branching factor

• 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

• AI search problems are typically very large. 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 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

Goal state Goal state


Heuristic for the Romania problem
Drive from Arad to Bucharest using a table with straight-line distances.

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

Alternative through Rimnicu Vilcea:


140 + 80 + 97 + 101 = 418 miles
Implementation of greedy best-first search

Best-First Expand the frontier


using
Search 𝑓𝑓 𝑛𝑛 = ℎ(𝑛𝑛)
Implementation of greedy best-first search
Heuristic 𝒉𝒉(𝒏𝒏) so we expand the node with the lowest estimated cost

The order for expanding the


frontier is determined by
f(n)

This check is the different to


BFS! It visits a node again if it
See BFS for function EXPAND. can be reached by a better
(cheaper) path.
55
Properties of greedy best-first search

• 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?

ℎ = 1 is always better than ℎ = 2.


Greedy best-first will go this way
and never reconsider!
A* search n’
𝑔𝑔(𝑛𝑛) = 2
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:
𝑓𝑓(𝑛𝑛) = 𝑔𝑔(𝑛𝑛) + ℎ(𝑛𝑛)
𝑔𝑔(𝑛𝑛): 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

Note: For greedy best-first search we just used 𝑓𝑓(𝑛𝑛) = ℎ(𝑛𝑛).


A* search example
Expansion rule: 𝑓𝑓 𝑛𝑛 = 𝑔𝑔(𝑛𝑛) + ℎ(𝑛𝑛) =
Expand the node with
the smallest f(n)

ℎ(𝑛𝑛)
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

Best-First Expand the frontier


using
Search 𝑓𝑓 𝑛𝑛 = 𝑔𝑔 𝑛𝑛 + ℎ(𝑛𝑛)
Implementation of A* Search
Path cost to 𝑛𝑛 + heuristic from 𝑛𝑛 to goal = estimate of the total cost
𝑔𝑔 𝑛𝑛 + ℎ(𝑛𝑛)

The order for expanding the


frontier is determined by
𝑓𝑓(𝑛𝑛)

This check is different to


BFS! It visits a node again if it
See BFS for function EXPAND. can be reached by a better
(cheaper) redundant path.
67
Optimality: Admissible heuristics

Definition: A heuristic ℎ is admissible if for every node 𝑛𝑛,


ℎ 𝑛𝑛 ≤ ℎ∗ (𝑛𝑛), where ℎ∗ (𝑛𝑛) is the true cost to reach the goal
state from 𝑛𝑛.
I.e., an admissible heuristic is a lower bound and never
overestimates the true cost to reach the goal.

Example: straight line distance never overestimates the actual


road distance.

Theorem: If ℎ is admissible, A* is optimal.


Proof of Optimality of A* 𝑓𝑓(𝑛𝑛) = 𝑔𝑔(𝑛𝑛) + ℎ(𝑛𝑛)
For goal states: 𝑓𝑓(𝑛𝑛∗ ) = 𝑔𝑔(𝑛𝑛) + 0

n* (goal)
Any unexplored node 𝑛𝑛 has:
𝐶𝐶 ∗ = 𝑓𝑓(𝑛𝑛∗ ) = 𝑔𝑔(𝑛𝑛∗ ) + 0 n
𝑓𝑓 𝑛𝑛 ≥ 𝑓𝑓(𝑛𝑛∗ )

n’ (other goal)
𝑔𝑔 𝑛𝑛′ ≥ 𝑓𝑓 𝑛𝑛 ⟺ 𝑔𝑔 𝑛𝑛′ ≥ 𝐶𝐶 ∗

• Suppose A* terminates its search at goal 𝑛𝑛∗ at a cost of 𝐶𝐶 ∗ = 𝑓𝑓 𝑛𝑛∗ .


• All unexplored nodes 𝑛𝑛 have 𝑓𝑓 𝑛𝑛 ≥ 𝑓𝑓(𝑛𝑛∗ ) or they would have been explored before 𝑛𝑛∗ .
• Since 𝑓𝑓(𝑛𝑛) is an optimistic estimate, it is impossible for 𝑛𝑛 to have a successor goal state 𝑛𝑛𝑛 with
𝐶𝐶𝐶 < 𝐶𝐶 ∗ .
• This proofs that 𝑛𝑛∗ must be an optimal solution.
Guarantees of A*

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?
Euclidean distance Manhattan distance
Start state Start state

Goal state Goal state


Heuristics from subproblems

• Let ℎ3(𝑛𝑛) be the cost of getting a subset of tiles


(say, 1,2,3,4) into their correct positions. The final order of
the * tiles does not matter.
• Small subproblems are often easy to solve.
• Can precompute and save the exact solution cost for every
or many possible subproblem instances – pattern database.

*
* * *
* * * *
Dominance: What heuristic is better?

Definition: If ℎ1 and ℎ2 are both admissible heuristics


and ℎ2(𝑛𝑛) ≥ ℎ1(𝑛𝑛) for all 𝑛𝑛, then
ℎ2 dominates ℎ1

Is ℎ1 or ℎ2 better for A* search?


• A* search expands every node with
𝑓𝑓(𝑛𝑛) < 𝐶𝐶 ∗  ℎ(𝑛𝑛) < 𝐶𝐶 ∗ – 𝑔𝑔(𝑛𝑛)
• ℎ2 is never smaller than ℎ1. A* search with ℎ2 will expand
less nodes and is therefore better.
Dominance

• Typical search costs for the 8-puzzle (average number of


nodes expanded for different solution depths 𝑑𝑑):

• 𝑑𝑑 = 12 IDS = 3,644,035 nodes


A*(ℎ1) = 227 nodes
A*(ℎ2) = 73 nodes

• 𝑑𝑑 = 24 IDS ≈ 54,000,000,000 nodes


A*(ℎ1) = 39,135 nodes
A*(ℎ2) = 1,641 nodes
Combining heuristics

• Suppose we have a collection of admissible


heuristics ℎ1, ℎ2, … , ℎ𝑚𝑚, but none of them
dominates the others.
• Combining them is easy:
ℎ(𝑛𝑛) = max{ℎ1(𝑛𝑛), ℎ2(𝑛𝑛), … , ℎ𝑚𝑚(𝑛𝑛)}

• 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.

• We could use inadmissible heuristics in A* search (e.g., by multiplying ℎ(𝑛𝑛)


with a factor 𝑊𝑊) that sometimes overestimate the optimal cost to the goal
slightly.
1. It potentially reduces the number of expanded nodes significantly.
2. This will break the algorithm’s optimality guaranty!

f 𝑛𝑛 = 𝑔𝑔 𝑛𝑛 + 𝑊𝑊 × ℎ(𝑛𝑛)

Weighted A* search: 𝒈𝒈 𝒏𝒏 + 𝑾𝑾 × 𝒉𝒉 𝒏𝒏 (𝟏𝟏 < 𝑾𝑾 < ∞)

The presented algorithms are special cases:


A* search: 𝑔𝑔 𝑛𝑛 + ℎ 𝑛𝑛 (𝑊𝑊 = 1)
Uniform cost search/BFS: 𝑔𝑔 𝑛𝑛 (𝑊𝑊 = 0)
Greedy best-first search: ℎ 𝑛𝑛 𝑊𝑊 = ∞
Example of weighted A* search

Breadth-first Search (BFS) Exact A* Search Weighted A* Search


𝑓𝑓 𝑛𝑛 = # actions to reach n 𝑓𝑓 𝑛𝑛 = 𝑔𝑔(𝑛𝑛) + ℎ𝐸𝐸𝐸𝐸𝐸𝐸𝐸𝐸 (𝑛𝑛) 𝑓𝑓(𝑛𝑛) = 𝑔𝑔(𝑛𝑛) + 5 ℎ𝐸𝐸𝐸𝐸𝐸𝐸𝐸𝐸 (𝑛𝑛)

Source and Animation: Wikipedia


Memory-bounded search

• The memory usage of A* (search tree and frontier) can still be


exorbitant.
• How can we make A* more memory-efficient while maintaining
completeness and optimality?
• Iterative deepening A* search.
• Recursive best-first search, SMA*: Forget some subtrees but remember
the best f-value in these subtrees and regenerate them later if necessary.

• Problems: memory-bounded strategies can be complicated to


implement and suffer from “memory thrashing” (regenerating
forgotten nodes like IDS).
Implementation as Best-first Search
• All discussed search strategies can be implemented using Best-first search.
• Best-first search expands always the node with the minimum value of an
evaluation function 𝒇𝒇(𝒏𝒏).

Search Strategy Evaluation function 𝒇𝒇(𝒏𝒏)


BFS (Breadth-first search) 𝑔𝑔(𝑛𝑛) (=uniform path cost)
Uniform-cost Search 𝑔𝑔(𝑛𝑛) (=path cost)
DFS/IDS (see note below) −𝑔𝑔(𝑛𝑛)
Greedy Best-first Search ℎ(𝑛𝑛)
(weighted) A* Search 𝑔𝑔 𝑛𝑛 + 𝑊𝑊 × ℎ(𝑛𝑛)

• Note: DFS/IDS is typically implemented differently to achieve the lower


space complexity.
Summary: Uninformed search strategies
Time Space
Algorithm Complete? Optimal?
complexity complexity
BFS (Breadth- Yes If all step 𝑂𝑂(𝑏𝑏𝑑𝑑 ) 𝑂𝑂(𝑏𝑏𝑑𝑑 )
first search) costs are equal

Uniform-cost Yes Yes Number of nodes with 𝑔𝑔 𝑛𝑛 ≤ 𝐶𝐶 ∗


Search
In finite spaces
DFS (cycle checking) No 𝑂𝑂(𝑏𝑏𝑚𝑚) 𝑂𝑂(𝑏𝑏𝑏𝑏)

If all step
IDS Yes 𝑂𝑂(𝑏𝑏𝑑𝑑 ) 𝑂𝑂(𝑏𝑏𝑏𝑏)
costs are equal

b: maximum branching factor of the search tree


d: depth of the optimal solution
m: maximum length of any path in the state space
C*: cost of optimal solution
Summary: All search strategies
Time Space
Algorithm Complete? Optimal?
complexity complexity
BFS (Breadth- Yes If all step 𝑂𝑂(𝑏𝑏𝑑𝑑 ) 𝑂𝑂(𝑏𝑏𝑑𝑑 )
first search) costs are equal

Uniform-cost
Yes Yes Number of nodes with 𝑔𝑔(𝑛𝑛) ≤ 𝐶𝐶 ∗
Search

DFS In finite spaces No 𝑂𝑂(𝑏𝑏𝑚𝑚) 𝑂𝑂(𝑏𝑏𝑏𝑏)


(cycles checking)

If all step
IDS Yes 𝑂𝑂(𝑏𝑏𝑑𝑑 ) 𝑂𝑂(𝑏𝑏𝑏𝑏)
costs are equal

Greedy best- In finite spaces Depends on Worst case: 𝑂𝑂(𝑏𝑏𝑚𝑚)


No
first Search (cycles checking) heuristic Best case: 𝑂𝑂(𝑏𝑏𝑏𝑏)

Number of nodes with


A* Search Yes Yes 𝑔𝑔(𝑛𝑛) + ℎ(𝑛𝑛) ≤ 𝐶𝐶 ∗
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
description using a compact transition model.
• The search tree is built on the fly, and we have to deal with cycles and
redundant paths.

• IDS is a memory efficient methods used in AI often for uninformed


search.
• Informed search uses heuristics based on knowledge or percepts to
improve search (i.e., A* to expand fewer nodes).

You might also like