• Formulate search problem
• States: configurations of the puzzle (9! configurations)
• Actions: Move one of the movable pieces (≤4 possible)
• Performance measure: minimize total moves
• Formulate goal
• Pieces to end up in order
as shown…
• Find solution
• Sequence of pieces moved: 3,1,6,3,1,…
Review: Formulating search problems
CIS 421/521 - Intro to AI - Fall 2017 2
Review: a problem is defined by:
1. States: a set S
2. An initial state siS
3. Actions: a set A
— s Actions(s) = the set of actions that can be executed in s,
that are applicable in s.
4. Transition Model: s aActions(s) Result(s, a) sr
—sr is called a successor of s
—{si } Successors(si )* = state space
5. Path cost (Performance Measure): Must be additive—e.g. sum of distances, number of actions executed, …
—c(x,a,y) is the step cost, assumed ≥ 0
– (where action a goes from state x to state y)
6. Goal test: Goal(s)
— Can be implicit, e.g. checkmate(s)
— s is a goal state if Goal(s) is true
CIS 421/521 - Intro to AI - Fall 2017 3
Review: Solutions & Optimal Solutions
• A solution is a sequence of actions from the initial state to a goal state.
• Optimal Solution: A solution is optimal if no solution has a lower path cost.
CIS 421/521 - Intro to AI - Fall 2017 4
Outline for today’s lecture
• Search Fundamentals (AIMA 3.3)
• Introduction to Uninformed Search
• Review of Breadth first and Depth-first search
• Iterative deepening search
• Strange Subroutine: Depth-limited search
• Depth-limited search + iteration = WIN!!
CIS 421/521 - Intro to AI - Fall 2017 5
Useful Concepts
• State space: the set of all states reachable from
the initial state by any sequence of actions
• When several operators can apply to each state, this gets
large very quickly
• Might be a proper subset of the set of configurations
• Path: a sequence of actions leading from one
state sj to another state sk
• Frontier: those states that are available for
expanding (for applying legal actions to)
• Solution: a path from the initial state si to a state
sf that satisfies the goal test
CIS 421/521 - Intro to AI - Fall 2017 6
Basic search algorithms: Tree Search
• Generalized algorithm to solve search problems
(Review from Data Structures)
• Enumerate in some order all possible paths from the initial
state
• Here: search through explicit tree generation
—ROOT= initial state.
—Nodes in search tree generated through transition model
CIS 421/521 - Intro to AI - Fall 2017 7
CIS 421/521 - Intro to AI - Fall 2017 8
Review: Generalized tree search
function TREE-SEARCH(problem, strategy) return a solution or failure
Initialize frontier to the initial state of the problem
do
if the frontier is empty then return failure
choose leaf node n for expansion according to strategy & remove from frontier
if n contains goal state then return solution
else EXPAND n and add resulting nodes to the frontier
Determines search
process!!
8
8-Puzzle: States and Nodes
• A state is a (representation of a) physical configuration
• A node is a data structure constituting part of a search tree
• Here node= <state, parent-node, children, action, path-cost, depth>
• States do not have parents, children, depth or path cost!
• The EXPAND function
• uses the Actions and Transition Model to create the corresponding states
—creates new nodes from these states
—fills in the various fields of the new node data structure
parentAction= Up
PathCost = 6
Depth = 6
children
state
NodeState
7 2 4
5 6 1
8 3
CIS 421/521 - Intro to AI - Fall 2017 9
CIS 421/521 - Intro to AI - Fall 2017
8-Puzzle Search Tree
7 2 4
5 6
8 3 1
7 4
5 2 6
8 3 1
7 2 4
5 6
8 3 1
7 2 4
5 6
8 3 1
7 4
5 2 6
8 3 1
7 4
5 2 6
8 3 1
7 2 4
8 5 6
3 1
2 4
7 5 6
8 3 1
7 2 4
5 6 1
8 3
7 2
5 6 4
8 3 1
• (Nodes show state, parent,
children - leaving Action, Cost,
Depth Implicit)
10
(Suppressing useless
“backwards” moves)
11
Tree Search Flaw: Repeated states
• Failure to detect repeated states can turn a linear
problem into an exponential one!
CIS 421/521 - Intro to AI - Fall 2017
12
Solution: Graph Search!
• Simple Mod to tree search:
Check to see if a node has been visited
before adding to search queue• must keep track of all possible states
—can use a lot of memory
• e.g., 8-puzzle problem, we have 9!/2 182K states
S
B
C
S
B C
SC B S
State SpaceSearch Tree
CIS 421/521 - Intro to AI - Fall 2017
Outline for today’s lecture
• Search Fundamentals
• Introduction to Uninformed Search (AIMA 3.4.1-3)
• Review of Breadth first and Depth-first search
• Iterative deepening search
• Strange Subroutine: Depth-limited search
• Depth-limited search + iteration = WIN!!
CIS 421/521 - Intro to AI - Fall 2017 14
15
Uninformed search strategies:
• AKA “Blind search”
• Uses only information available in problem definition
Informally:
• Uninformed search: All non-goal nodes in frontier look equally good
• Informed search: Some non-goal nodes can be ranked above others.
CIS 421/521 - Intro to AI - Fall 2017 15
Search Strategies
• Review: Strategy = order of tree expansion
• Implemented by different queue structures (LIFO, FIFO, priority)
• Dimensions for evaluation
• Completeness- always find the solution?
• Optimality - finds a least cost solution (lowest path cost) first?
• Time complexity - # of nodes generated (worst case)
• Space complexity - # of nodes simultaneously in memory
(worst case)
• Time/space complexity variables
• b, maximum branching factor of search tree
• d, depth of the shallowest goal node
• m, maximum length of any path in the state space (potentially )
CIS 421/521 - Intro to AI - Fall 2017 16
Introduction to space complexity
• You know about:
• “Big O” notation
• Time complexity
• Space complexity is analogous to time complexity
• Units of space are arbitrary
• Doesn’t matter because Big O notation ignores constant
multiplicative factors
• Plausible Space units:
—One Memory word
—Size of any fixed size data structure
– eg Size of fixed size node in search tree
CIS 421/521 - Intro to AI - Fall 2017 17
Review: Breadth-first search
• Idea:
• Expand shallowest unexpanded node
• Implementation:
• frontier is FIFO (First-In-First-Out) Queue:
—Put successors at the end of frontier successor list.
CIS 421/521 - Intro to AI - Fall 2017 18
Breadth-first search (simplified)
function BREADTH-FIRST-SEARCH(problem) returns a solution, or failure
node <- a node with STATE = problem.INITIAL-STATE, PATH-COST=0
if problem.GOAL-TEST(node.STATE) then return SOLUTION(node)
frontier <- a FIFO queue with node as the only element
loop do
if EMPTY?(frontier) then return failure
node <- POP(frontier) // chooses the shallowest node in frontier
add node.STATE to explored
for each action in problem.ACTIONS(node.STATE) do
child <- CHILD-NODE(problem, node, action)
if problem.GOAL-TEST(child.STATE) then return SOLUTION(child)
frontier <- INSERT(child, frontier)
From Figure 3.11 Breadth-first search (ignores loops, repeated nodes)
Position within
queue of new items
determines search
strategy
Subtle: Node inserted into
queue only after testing to
see if it is a goal state
CIS 421/521 - Intro to AI - Fall 2017 19
Properties of breadth-first search
• Complete? Yes (if b is finite)
• Time Complexity? 1+b+b2+b3+… +bd = O(bd)
• Space Complexity? O(bd) (keeps every node in memory)
• Optimal? Yes, if cost = 1 per step
(not optimal in general)
b: maximum branching factor of search tree
d: depth of the least cost solution
m: maximum depth of the state space ()
CIS 421/521 - Intro to AI - Fall 2017 20
CIS 421/521 - Intro to AI - Fall 2017 21
Exponential Space (and time) Not Good...
• Exponential complexity uninformed search problems cannot
be solved for any but the smallest instances.
• (Memory requirements are a bigger problem than execution
time.)
Fig 3.13 Assumes b=10, 1M nodes/sec, 1000 bytes/node
DEPTH NODES TIME MEMORY
2 110 0.11 milliseconds 107 kilobytes
4 11110 11 milliseconds 10.6 megabytes
6 106 1.1 seconds 1 gigabytes
8 108 2 minutes 103 gigabytes
10 1010 3 hours 10 terabytes
12 1012 13 days 1 petabytes
14 1014 3.5 years 99 petabytles
21
Review: Depth-first search
• Idea:
• Expand deepest unexpanded node
• Implementation:
• frontier is LIFO (Last-In-First-Out) Queue:
—Put successors at the front of frontier successor list.
CIS 421/521 - Intro to AI - Fall 2017 22
Properties of depth-first search
• Complete? No: fails in infinite-depth spaces, spaces with loops
• Modify to avoid repeated states along path
complete in finite spaces
• Time? O(bm): terrible if m is much larger than d
• but if solutions are dense, may be much faster than breadth-first
• Space? O(b*m), i.e., linear space!
• Optimal? No
b: maximum branching factor of search tree
d: depth of the least cost solution
m: maximum depth of the state space ()
CIS 421/521 - Intro to AI - Fall 2017 23
Depth-first vs Breadth-first
• Use depth-first if
• Space is restricted
• There are many possible solutions with long paths
and wrong paths are usually terminated quickly
• Search can be fine-tuned quickly
• Use breadth-first if
• Possible infinite paths
• Some solutions have short paths
• Can quickly discard unlikely paths
CIS 421/521 - Intro to AI - Fall 2017 24
Outline for today’s lecture
• Formulating Search Problems – An Example
• Search Fundamentals
• Introduction to Uninformed Search
• Review of Breadth first and Depth-first search
• Iterative deepening search (AIMA 3.4.4-5)
• Strange Subroutine: Depth-limited search
• Depth-limited search + iteration = WIN!!
CIS 421/521 - Intro to AI - Fall 2017 25
26
Search Conundrum
• Breadth-first
Complete,
Optimal but uses O(bd) space
• Depth-first
Not complete unless m is bounded
Not optimal
Uses O(bm) time; terrible if m >> d
but only uses O(bm) space
How can we get the best of both?CIS 421/521 - Intro to AI - Fall 2017 26
27
Depth-limited search: A building block
• Depth-First search but with depth limit l.
• i.e. nodes at depth l have no successors.
• No infinite-path problem!
• If l = d (by luck!), then optimal
• But:
—If l < d then incomplete
—If l > d then not optimal
• Time complexity:
• Space complexity: ☺
( )lO b
O(bl)
CIS 421/521 - Intro to AI - Fall 2017 27
CIS 521 - Intro to AI28
Iterative deepening search
• A general strategy to find best depth limit l.
• Key idea: use Depth-limited search as subroutine,
with increasing l.
For l = 0 to do
depth-limited-search to level l
if it succeeds
then return solution
• Complete & optimal: Goal is always found at
depth d, the depth of the shallowest goal-node.
Could this possibly be efficient?
28CIS 421/521 - Intro to AI - Fall 2017
Nodes constructed at each deepening
• Depth 0: 0 (Given the node, doesn’t construct it.)
• Depth 1: b1 nodes
• Depth 2: b nodes + b2 nodes
• Depth 3: b nodes + b2 nodes + b3 nodes
• …
CIS 421/521 - Intro to AI - Fall 2017 29
Total nodes constructed:
• Depth 0: 0 (Given the node, doesn’t construct it.)
• Depth 1: b1 = b nodes
• Depth 2: b nodes + b2 nodes
• Depth 3: b nodes + b2 nodes + b3 nodes
• …
Suppose the first solution is the last node at depth 3:
Total nodes constructed:
3*b nodes + 2*b2 nodes + 1*b3 nodes
CIS 421/521 - Intro to AI - Fall 2017 30
31
ID search, Evaluation II: Time Complexity
• More generally, the time complexity is
• (d)b + (d-1)b2 + … + (1)bd = O(bd)
• As efficient in terms of O(…) as Breadth First Search:
• b + b2 + … + bd = O(bd)
CIS 421/521 - Intro to AI - Fall 2017 31
CIS 521 - Intro to AI32
ID search, Evaluation III
• Complete: YES (no infinite paths) ☺
• Time complexity:
• Space complexity: ☺
• Optimal: YES if step cost is 1. ☺
O(bd )
( )O bd
32CIS 421/521 - Intro to AI - Fall 2017