Chapter3a_Artificial_Intelligence
Chapter3a_Artificial_Intelligence
Search techniques
Basanta Joshi, PhD
[email protected]
Lecture notes can be downloaded from
www.basantajoshi.com.np
Water Jug Problem
Given a full 5-gallon jug and a full 2-gallon jug, fill the 2-gallon jug with
exactly one gallon of water.
• State: ?
• Initial State: ?
5
• Operators: ? 2
• Goal State: ?
Water Jug Problem
Operator table
Empty5 5, 2 5, 1 5, 0
Empty2 4, 2 4, 1 4, 0
3, 2 3, 1 3, 0
2to5
2, 2 2, 1 2, 0
5to2
1, 2 1, 1 1, 0
5to2part
0, 2 0, 1 0, 0
Water jug solution
5, 2 5, 1 5, 0
4, 2 4, 1 4, 0
3, 2 3, 1 3, 0
2, 2 2, 1 2, 0
1, 2 1, 1 1, 0
0, 2 0, 1 0, 0
Formalizing search III
• EXPAND
– Generate all successor nodes of a given node
• GOAL-TEST
– Test if state satisfies all goal conditions
• QUEUEING-FUNCTION
– Used to maintain a ranked list of nodes that are candidates
for expansion
Bookkeeping
• Completeness
– Guarantees finding a solution whenever one exists
• Time complexity
– How long (worst or average case) does it take to find a solution?
Usually measured in terms of the number of nodes expanded
• Space complexity
– How much space is used by the algorithm? Usually measured in terms
of the maximum size of the “nodes” list during the search
• Optimality/Admissibility
– If a solution is found, is it guaranteed to be an optimal one? That is, is
it the one with minimum cost?
Uninformed vs. informed search
G
Find a path from A to E
Graph Searching Problems
• Generic search algorithm: given a graph, start nodes,
and goal nodes, incrementally explore paths from the
start nodes.
Is A a goal state?
24
Breadth-first search
• Expand shallowest unexpanded node
• Implementation:
• fringe is a FIFO queue, i.e., new successors go
at end
Expand:
fringe = [B,C]
Is B a goal state?
25
Breadth-first search
• Expand shallowest unexpanded node
•Implementation:
fringe is a FIFO queue, i.e., new successors go at
end
Expand:
fringe=[C,D,E]
Is C a goal state?
26
Breadth-first search
• Expand shallowest unexpanded node
• Implementation:
• fringe is a FIFO queue, i.e., new successors go
at end
Expand:
fringe=[D,E,F,G]
Is D a goal state?
27
Example
BFS
Properties of breadth-first search
• Complete? Yes it always reaches goal (if b is finite)
• Time? 1+b+b2+b3+… +bd + (bd+1-b)) = O(bd+1)
(this is the number of nodes we generate)
• Space? O(bd+1) (keeps every node in memory,
either in fringe or on a path to fringe).
• Optimal? Yes (if we guarantee that deeper solutions
are less optimal, e.g. step-cost=1).
29
Time complexity of breadth-first search
Space complexity of breadth-first search
BFS Weakness
• Exponential Growth
Time and memory requirements for breadth-first search,
assuming a branching factor of 10, 100 bytes per node
and searching 1000 nodes/second
Is A a goal state?
36
Depth-first search
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO queue, i.e., put successors at front
queue=[B,C]
Is B a goal state?
37
Depth-first search
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO queue, i.e., put successors at front
queue=[D,E,C]
Is D = goal state?
38
Depth-first search
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO queue, i.e., put successors at front
queue=[H,I,E,C]
Is H = goal state?
39
Depth-first search
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO queue, i.e., put successors at front
queue=[I,E,C]
Is I = goal state?
40
Depth-first search
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO queue, i.e., put successors at front
queue=[E,C]
Is E = goal state?
41
Depth-first search
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO queue, i.e., put successors at front
queue=[J,K,C]
Is J = goal state?
42
Depth-first search
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO queue, i.e., put successors at front
queue=[K,C]
Is K = goal state?
43
Depth-first search
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO queue, i.e., put successors at front
queue=[C]
Is C = goal state?
44
Depth-first search
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO queue, i.e., put successors at front
queue=[F,G]
Is F = goal state?
45
Depth-first search
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO queue, i.e., put successors at front
queue=[L,M,G]
Is L = goal state?
46
Depth-first search
• Expand deepest unexpanded node
• Implementation:
• fringe = LIFO queue, i.e., put successors at front
queue=[M,G]
Is M = goal state?
47
Properties of depth-first search
A
50
Iterative deepening search L=0
51
Iterative deepening search L=1
52
Iterative deepening search L=2
53
Iterative Deepening Search L=3
54
Iterative deepening search
• Number of nodes generated in a depth-limited search to depth
d with branching factor b:
NDLS = b0 + b1 + b2 + … + bd-2 + bd-1 + bd
• Complete? Yes
• Time? O(bd)
• Space? O(bd)
• Optimal? Yes, if step cost = 1 or increasing
function of depth.
56
Example IDS
57
Bi-directional search
• Alternate searching from the start state toward the goal and
from the goal state toward the start.
• Stop when the frontiers intersect.
• Works well only when there are unique start and goal states.
• Requires the ability to generate “predecessor” states.
• Can (sometimes) lead to finding a solution more quickly.
• Time complexity: O(bd/2). Space complexity: O(bd/2).
Bidirectional Search
• Idea
• simultaneously search forward from S and backwards from G
• stop when both “meet in the middle”
• need to keep track of the intersection of 2 open sets of nodes
• What does searching backwards from G mean
• need a way to specify the predecessors of G
• this can be difficult,
• e.g., predecessors of checkmate in chess?
• which to take if there are multiple goal states?
• where to start if there is only a goal test, no explicit list?
58
Bi-Directional Search
Complexity: time and
space complexity are:
O( d /
b 2
)
59
Comparing Search Strategies
b – branching factor d – depth of optimal solution
m – maximum depth l – depth limit
61
Repeated states
• Failure to detect repeated states can turn a
linear problem into an exponential one!
62
Avoiding Repeated States
C C S B S
State Space
Example of a Search Tree
• Graph search optimal but memory inefficient
• never generate a state generated before
• must keep track of all possible states (uses a lot of memory)
• e.g., 8-puzzle problem, we have 9! = 362,880 states
• approximation for DFS/DLS: only avoid states in its (limited)
memory: avoid looping paths.
(this does not avoid the problem on the previous slide)
• Graph search optimal for BFS and UCS, not for DFS.
63
Graph Search
function graph-search (problem, QUEUEING-FUNCTION)
;; problem describes the start state, operators, goal test, and operator costs
;; queueing-function is a comparator function that ranks two states
;; graph-search returns either a goal node or failure
nodes = MAKE-QUEUE(MAKE-NODE(problem.INITIAL-STATE))
closed = {}
loop
if EMPTY(nodes) then return "failure"
node = REMOVE-FRONT(nodes)
if problem.GOAL-TEST(node.STATE) succeeds
then return node.SOLUTION
if node.STATE is not in closed
then ADD(node, closed)
nodes = QUEUEING-FUNCTION(nodes,
EXPAND(node, problem.OPERATORS))
end
;; Note: The goal test is NOT done when nodes are generated
;; Note: closed should be implemented as a hash table for efficiency
Graph Search Strategies
http://www.cs.rmit.edu.au/AI-Search/Product/
http://aima.cs.berkeley.edu/demos.html (for more demos)
64