Professional Documents
Culture Documents
5. MODULE 2 (PART 1)
5. MODULE 2 (PART 1)
IN AI and ML
CSA2001
J. Jayanthi
UNIT -2
contents
What are
State space Tree Uninformed Informed
search
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
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
Distance in miles
Example: Vacuum
world
State Space
Goal states
Operators:
• fill jug x from faucet
• pour contents of jug x in jug y until y full
• dump contents of jug x down drain
Goal: (2,n)
Goal states
Issue: Transition model is not a
tree! It has cycles vs. redundant
paths Cycles
Return to the same state. The search tree will create a new node!
Initial state
Redundant paths
Multiple paths to get to the same state
Goal states
Initial state
Path Path
1 2
Goal states
Search
•tree
Superimpose a “what if” tree of possible
actions and outcomes (states) on the Root node =
state space graph. Initial state
• The Root node represents the initial stare. 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
• Trees have no cycles or redundant paths. b c
Cycles in the search space must be broken. Redundant
To prevent infinite loops. Removing path
redundant paths improves
search efficiency. d e e
• A path through the tree corresponds to a Cycle
sequence of actions (states).
• A solution is a path ending in a node
representing a goal state.
b … … …
• Nodes vs. states: Each tree node Solution path
represents a state of the system. If Node representing
redundant path cannot be prevented then f
a Goal state
state can be represented by
multiple nodes.
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
in memory. into
a. Builds parts of the tree from the
memory.
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
• Reachable states are defined by the initial state and
the 𝑥1
𝑥2
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
2×2=4
3×2×1=6
Factorial: 𝑛! = 𝑛 × 𝑛 − 1 × ⋯ × 2 × 1
#Python
import math
print (math.factorial(23))
#Python
import scipy.special
Dirt
• Order of A and B does not matter!
• Repetition: Dirt can be in both
places
• There are 2 options (clean/dirty)
→ 22
Robot
• Can be in 1 out of 2 squares.
→2
Total: 2 × 22= 23
Examples: What is the state space
size?
Often a rough upper limit is sufficient to determine how hard the search problem is.
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 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:
Disadv:The large space complexity is usually a bigger problem than time!
Adv: minimal path to goal
d: depth of the optimal solution
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
Analysis
See what happens with b=10
expand 10,000 nodes/second
1,000 bytes/node
• 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!)
Note:
Disadv: not complete/ infinite (depth not limit )
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!
Comparison of
Search Techniques
DF BF
S S
Complet N Y
e
Optimal N N
Heuristic N N
Time bm bd+1
Space bm bd+1
Iterative deepening search
(IDS)
Can we
a) get DFS’s good memory footprint,
b) avoid infinite cycles, and
c) preserve BFS’s optimality guaranty?
• 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.
Bidirectional Search
Search forward from initial state to
goal AND backward from goal state to
initial state
Can prune many options
Considerations
Which goal state(s) to use
How determine when searches
overlap
Which search to use for each
direction
Here, two BFS searches
Time and space is O(bd/2)
Uniform Cost Search
(Branch&Bound)
priority queue
QueueingFn is SortByCostSoFar
Cost from root to current node n is g(n)
Add operator costs along path
First goal found is least-cost solution
Space & time can be exponential because large subtrees with inexpensive steps
may be explored before useful paths with costly steps
If costs are equal, time and space are O(bd)
Otherwise, complexity related to cost of optimal solution
Uniform-cost search
(= Dijkstra’s shortest path
• Expansion rule: Expand node in the frontier with the least path cost from the initial state.
algorithm)
• 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!
• Time?
( is O(b1+C*/ ε).
Number of nodes with path cost ≤ cost of optimal solution 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?
1+C*/ ε
O(b )
Note:
1) Uses “explored” set to avoid visiting already
explored states.
2) Uses “frontier” set to store states that remain to be
explored and expanded.
3) However, with eg uniform cost search, we need to
make a special check when node (i.e. state) is on
frontier. Details later.
Tree-search algorithms
Basic idea:
○ simulated exploration of state space by generating successors of
already-explored states (a.k.a. ~ expanding states)
Note: 1) Here we only check a node for possibly being a goal state,
after we select the node for expansion.
2) A “node” is a data structure containing state + additional info
(parent
node, etc.
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.
• Heuristic –” Rule of thumb”
• 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
Informed Searches
Best-first search, Hill climbing, Beam search, A*, IDA*, RBFS, SMA*
New terms
Heuristics
Optimal solution
Informedness
Hill climbing problems
Admissibility
New parameters
g(n) = estimated cost from initial state to state n
h(n) = estimated cost (distance) from state n to closest goal
h(n) is our heuristic
Robot path planning, h(n) could be Euclidean distance
8 puzzle, h(n) could be #tiles out of place
Search algorithms which use h(n) to guide search are heuristic search algorithms
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)
Greedy best-first search
example
Expansion rule: Expand the
node that has the lowest
of the heuristic function h(n)
value 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
Search
Implementation of greedy best-first
search
Heuristic ℎ(𝑛) so we expand the node with the lowest estimated cost
• Space?
Same as time complexity.
How can we fix the optimality problem
with greedy best-first search?
ℎ(𝑛)
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
Search
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. 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
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 ℎ1 and ℎ2 are both admissible heuristics
and ℎ2(𝑛) ≥ ℎ1(𝑛) for all 𝑛, then
ℎ2 dominates ℎ1
f𝑛 =𝑔𝑛 + W × ℎ(𝑛)
Weighted A* search: 𝑔𝑛 +W×ℎ𝑛 (𝟏𝟏 < W < ∞)
If all step
IDS Yes 𝑂(𝑏𝑑) 𝑂(𝑏𝑑)
costs are equal
Uniform-cos
Yes Yes Number of nodes with 𝑔(𝑛) ≤ 𝐶∗
t Search
If all step
IDS Yes 𝑂(𝑏𝑑) 𝑂(𝑏𝑑)
costs are equal
Bes
Number of nodes with
A* Search Yes Yes 𝑔(𝑛) + ℎ (𝑛) ≤ 𝐶∗
Conclusio
n
• 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.
Local Local
Search Chapters 4.1 &
4.2
Recap: Uninformed and Informed
Search
x
x
Utility
U
x
y
Idea:
Start with a current solution (a state) and improve the solution by moving from the
current state to a “neighboring” better state (a.k.a. performing a series of local moves).
Local Search Algorithms
states Example:
n-Queens
Goal: Put n queens on an n × n board with no two
Problem
queens on the same row, column, or diagonal.
1
Problem
A B C D
0 conflicts = utility of 0
Example: Traveling salesman problem
Goal: Find the shortest tour connecting a given set of cities
State space: all possible tours (states are not individual cities!)
State representation: Order of cities in the tour.
Objective function: minimize the length of the tour
Local neighborhood: Change the order of visiting a few cities.
Note: We have solved a different problem with uninformed/informed search! Each
city was defined as a state and the path was the solution.
Hill-Climbing Search
aka Greedy Local Search
Idea: keep a single “current” state and try to find better
neighboring states.
MotorCycleUSA.com
Example:
Goal: Put n queens on an n × n board with no two queens on the same row, column,
n-Queens Problem
or diagonal.
State space: all possible n-queen configurations. We can restrict the state space:
Only one queen per column.
State representation: row position of each queen in its column (e.g., 2, 3, 2, 3)
Objective function: minimize the number of pairwise conflicts.
Local neighborhood: Move one queen anywhere in its column.
Improvement strategy
Find a local neighboring state (move one queen within its column) to reduce
conflicts
1
Example:
n-Queens Problem
To find the best local move, we must evaluate all local neighbors (moving a queen in
its column) and calculate the objective function.
Objective value after moving
the queen to this square
Example:
n-Queens Problem
A B A B
C C
D E D E
A B A B
C C
D E D E
Variants:
Steepest-ascend hill climbing
Check all possible successors and choose the highest-valued successors.
Neighbors placed
next to each other
How to escape local maxima?
Random restart hill-climbing can help.
What about “shoulders” (called “ridges” in higher dimensional space)?
Hill-climbing that allows sideways moves and uses momentum.
Minimization vs.
Maximization
Convex vs. Non-Convex
Optimization Problems
Minimization problems
Convex Problem Non-convex Problem
State space
Simulated Annealing
The lower the temperature, the less likely the algorithm will accept a worse state.
Cooling Schedule
Simulated
Annealing Search
Guarantee: If temperature decreases slowly
enough, then simulated annealing search will
find a global optimum with probability
approaching one.
However:
This usually takes impractically long.
Evolutionary Algorithms
A Population-based Metaheuristics
Evolutionary Algorithms / Genetic
Algorithms
Individual = state
representation as
a chromosome:
row of the queen
in each column
next
generation
Search in Continuous Spaces
Discretization of Continuous Space
x
Discretization of Continuous Space
Initial state
Goal
state
Search in Continuous Spaces:
Gradient Descent
Search in Continuous Spaces:
Empirical Gradient Methods