Solving Problem by Searching
Solving Problem by Searching
Intelligence
Solving
problems by
searching
AIMA Chapter 3
Conten
ts
What are
Uninformed Informed
search State space Tree
search search
problems? search
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
�
�
�
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.
Phases: stat
e
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}
• As𝑑a𝐴𝐴𝑟
EFFECT: change the agent’s location according to
𝑓𝑓: 𝑆 × 𝐴 → 𝑆 or 𝑠 ′ =
Initial state Actions: {N, E, S, W} function:
𝑟𝑒𝑠𝑢𝑙𝑡(𝑎, 𝑠)
Transitions
𝑠 𝑎 𝑠𝑠
1 Function implemented
2 g i
3 as a table
4 a representing the state 1 S 2
5 space
as a graph. 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
𝑎𝑐𝑡𝐴𝐴𝑜𝑛𝑠(4) = {𝐸, 𝑆, 𝑁}
transition function. E.g.,
Example: Romania
Vacation
• On vacation in Romania; currently in Arad
• Flight leaves tomorrow from Bucharest
Goal states
Goal states
Issue: Transition model is not
a tree! It can have
redundant
Cycles
paths
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 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.
• Nodes vs. states: Each tree node b
represents a
…
state of the system. If redundant Solution path
path cannot be
prevented then state can be represented by f Node representing
multiple nodes. 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.
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.
• 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
can be in.
State representation
𝑥1
• Reachable states are defined by the initial state and
𝑥2
the
transition model. Not all states may be reachable from …
the initial state.
• 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.
2 × 2 = 22 =
4 3×2×1=
6
Factorial: 𝑛! = 𝑛 × 𝑛 − 1 × ⋯ ×
2×1
#Python
import math
print
(math.factor
ial(23))
Source: Permutations/Combinations Cheat Sheets by Oleksii Trekhleb
https://itnext.io/permutations-combinations-algorithms-cheat-sheet-68c14879aba5
= 𝐶 𝑛, 𝑟 =𝑛
3 �
�
= 𝐶 it is the number
Binomial Coefficient:
𝑟
2
�
3 of ways can we choose 𝑟 out of 𝑛 objects?
Read as “n choose r” because
�
#Python
import scipy.special
→ 22
Robot location
→2
• Can be in 1 out of 2 rooms.
Total: 2 × 2 2 = 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.
Positions the agent All arrangements with All arrangements All possible boards.
𝑛 < 39 =
can be in. 8 queens on the of 9 elements.
𝑛≤ 19,683
board.
𝑛 < 2 ≈ 1.8 × 9!
n = Number of
64
1019
white Many boards are not
squares. Half is legal (e.g., all x’s)
9!
We can only have 8 unreachable:
𝑛= =
𝑛 ≈ 4.4 × 2
queens:
64
8 181,440
= 109
Uninformed
Search
Uninformed search
strategies
The search algorithm/agent is not provided information about how
close a state is to the goal state.
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
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
Transitio implemented by
n returning a list of
function Nodes.
Properties of Breadth-first
search
• Complete?
d: depth of the optimal solution
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?
1 + 𝑏 + 𝑏2 + ⋯ + 𝑏𝑑 = 𝑂(𝑏𝑑 )
Sum of the number of nodes created in at each level in a b-ary tree of depth d:
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 b: maximum branching factor
search
• 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)
• Implementation: best-first search where the frontier is a priority queue ordered by lower 𝑓𝑓(𝑛)
• Expansion rule: Expand node in the frontier with the least path cost from the initial state.
=
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*/ ε)
DFS uses 𝑃 = ∞
Cycle checking is only done against the current path. This is similar to Backtracking search.
Could be the time to reach a solution at maximum depth m in the last path: 𝑂 𝑏𝑚
• Time?
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 b: maximum branching factor
search
•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
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?
deepeni
ng
search
(IDS)
Implementation:
IDS
• Optimal?
Yes, if step cost = 1
• Time?
𝑑 𝑏1 + (𝑑 − 1)𝑏2 + … + 1𝑏𝑑
Consists of rebuilding trees up to d times
= 𝑂(𝑏 𝑑 )
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
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
Heuristic function ℎ(𝑛) estimates the cost of reaching a node
function
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
𝑓𝑓𝑛 = ℎ(𝑛)
using
Search
Implementation of greedy best-
first search
Heuristic ℎ(𝑛) so we expand the node with the lowest estimated
cost
•Optimal?
No d: depth of the optimal solution
m: max. depth of tree
b: maximum branching factor
•Time?
accurate
•Space?
Same as time complexity.
How can we fix the optimality
problem with greedy best-first
search?
search 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
ℎ(𝑛
)
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
𝑓𝑓 𝑛 = 𝑔
using
Search 𝑛 + ℎ(𝑛)
Implementation of A*
Path cost to 𝑛 + heuristic from 𝑛 to goal = estimate of the total
Search
𝑔 𝑛 + ℎ(𝑛)
cost
𝑓𝑓(𝑛)
frontier is determined by
n’ (other goal)
𝑔 𝑛′
𝑓𝑓 𝑛
⟺ 𝑔 𝑛′
≥ 𝐶∗
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
(exponential)
•Space?
Same as time complexity.
Designing heuristic
functions
ℎ1(𝑛) = number of misplaced tiles
Heuristics for the 8-puzzle
ℎ1(𝑠𝑡𝑎𝑟𝑡) = 8
ℎ2(𝑠𝑡𝑎𝑟𝑡) = 3
+1+2+2+2+3+3+2 =
18
Are ℎ1 and ℎ2 1 needs to move 3
positions
admissible?
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(𝑠𝑡𝑎𝑟𝑡) =
ℎ2(𝑠𝑡𝑎𝑟𝑡)
=
Heuristics from relaxed
problems
What relaxations are used in these two cases?
Euclidean distance Manhattan distance
Start state Start state
*
* * *
* * * *
Dominance: What heuristic is
•𝑑 = 24 IDS ≈
54,000,000,000 nodes
A*(ℎ1) = 39,135
nodes A*(ℎ2) = 1,641
nodes
Combining
heuristics
f 𝑛 = 𝑔 𝑛 + W × ℎ(𝑛)
𝑔 𝑛 +W× (𝟏
𝟏<W <
ℎ 𝑛 ∞)
Weighted A* search:
𝑔𝑛 (W = 0)
ℎ𝑛 W =∞
Uniform cost search/BFS:
Greedy best-first search:
Example of weighted A*
search
𝑂(𝑏𝑚) 𝑂(𝑏𝑚)
In finite spaces
DFS (cycle checking) No
𝑂(𝑏𝑑) 𝑂(𝑏𝑑)
If all step
IDS Yes
costs are equal
𝑂(𝑏𝑑) 𝑂(𝑏𝑑)
If all step
IDS Yes
costs are equal
st case: 𝑂(𝑏𝑚)
t case: 𝑂(𝑏𝑑)
Greedy best- In finite spaces Depends on
first Search No Wor heuristic
(cycles checking)
Bes
• 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.