All work must be done individually You may discuss the homework with others but you may not duplicate their text or code in whole or in part eg changing variable names Honor code violations are taken very seriously ID: 503157
Download Presentation The PPT/PDF document "Homework Policies" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Homework Policies
All work must be done individuallyYou may discuss the homework with others, but you may not duplicate their text or code, in whole or in part (e.g., changing variable names)Honor code violations are taken very seriously!
1Slide2
CS
B351: Intro to AI and Computer SimulationInstructor: Kris Hauserhttp://cs.indiana.edu/~hauserk
2Slide3
Agenda
Searching, data structures, and algorithmsBreadth-first searchDepth-first searchUniform-cost search
3Slide4
Defining a Search Problem
4
State space S
Successor function:
x
S
SUCC
(x) 2
S
Initial state s
0 Goal test: xS GOAL?(x) =T or F Arc cost
SSlide5
State Graph
5
Each state is represented by a distinct node
An arc (or edge) connects a node s
to a node s’ if
s’
SUCC
(s)
The state graph may contain more than one connected componentSlide6
Solution to the Search Problem
A solution
is a path connecting the initial node to a goal node (any one)
The
cost
of a path is the sum of the arc costs along this path
An
optimal
solution is a solution path of minimum cost
There might be
no solution !
6
I
GSlide7
Search Graph
!= State Graph7
8
3
5
2
4
7
1
6
8
3
5
2
4
7
1
6
8
3
5
2
4
7
1
6
8
3
5
2
4
7
1
6
8
3
5
2
4
7
1
6
8
3
5
2
4
7
1
6
If states are allowed to be revisited,
the search tree may be infinite even
when the state space is finiteSlide8
Search Graph Node
!= State8
PARENT-NODE
8
3
5
2
4
7
1
6
STATE
Depth of a node N
= length of path from root to N
(depth of the root = 0)
BOOKKEEPING
5
Path-Cost
5
Depth
Right
Action
Expanded
yes
...
CHILDRENSlide9
Fringe of Search Tree
The fringe is the set of all search nodes that haven’t been expanded yet
8
3
5
2
4
7
1
6
8
3
5
2
4
7
1
6
8
3
5
2
4
7
1
6
8
3
5
2
4
7
1
6
8
3
5
7
2
4
1
6
8
3
5
2
4
7
1
6Slide10
Search Strategy
The fringe is the set of all search nodes that haven’t been expanded yet The fringe is implemented as a priority queue FRINGE
INSERT(
node,FRINGE
)
REMOVE(FRINGE)
The ordering of the nodes in FRINGE defines the search strategy
10Slide11
Search Algorithm #1
SEARCH#11.
If GOAL?(initial-state) then return
initial-state
2.
INSERT(
initial-
node,FRINGE
)
3.
Repeat:4. If empty(FRINGE) then return failure5. N REMOVE(FRINGE)6. s STATE(N)7. For every state s’ in SUCCESSORS(s)8. Create a new node N’ as a child of
N9. If GOAL?(
s’) then return path or goal state10. INSERT(
N’,FRINGE)11
Expansion of NSlide12
Blind Search Strategies
12Slide13
Blind Strategies
Breadth-firstBidirectionalDepth-firstDepth-limited Iterative deepeningUniform-Cost
(variant of breadth-first)
13
Arc cost = 1
Arc cost
= c(action)
0 Slide14
Breadth-First Strategy
New nodes are inserted at the end of FRINGE14
2
3
4
5
1
6
7
FRINGE = (1)Slide15
Breadth-First Strategy
New nodes are inserted at the end of FRINGE15
FRINGE = (2, 3)
2
3
4
5
1
6
7Slide16
Breadth-First Strategy
New nodes are inserted at the end of FRINGE16
FRINGE = (3, 4, 5)
2
3
4
5
1
6
7Slide17
Breadth-First Strategy
New nodes are inserted at the end of FRINGE17
FRINGE = (4, 5, 6, 7)
2
3
4
5
1
6
7Slide18
Performance Measures
CompletenessA search algorithm is complete if it finds a solution whenever one exists[What about the case when no solution exists?]
Optimality
A search algorithm is optimal if it returns a minimum-cost path whenever a solution exists
Complexity
It measures the time and amount of memory required by the algorithm
18Slide19
Important Parameters
Maximum number of successors of any state branching factor b
of the search tree
Minimal length (≠ cost) of a path between the initial and a goal state
depth d
of the shallowest goal node in the
search tree
19Slide20
Evaluation
b: branching factord: depth of shallowest goal nodeBreadth-first search is: Complete? Not complete?
Optimal? Not optimal?
20Slide21
Evaluation
b: branching factord: depth of shallowest goal nodeBreadth-first search is: Complete
Optimal
if step cost is 1
Number of nodes generated:
???
21Slide22
Evaluation
b: branching factord: depth of shallowest goal nodeBreadth-first search is: Complete
Optimal
if step cost is 1
Number of nodes generated:
1 + b + b
2
+ … +
b
d
= ??? 22Slide23
Evaluation
b: branching factord: depth of shallowest goal nodeBreadth-first search is: Complete
Optimal
if step cost is 1
Number of nodes generated:
1 + b + b
2
+ … +
b
d
= (bd+1-1)/(b-1) = O(bd) Time and space complexity is O(bd) 23Slide24
Time and Memory Requirements
d
# Nodes
Time
Memory
2
111
.01 msec
11 Kbytes
4
11,111
1 msec
1 Mbyte
6
~10
6
1 sec
100 Mb
8
~10
8
100 sec
10 Gbytes
10
~10
10
2.8 hours
1
Tbyte
12
~10
12
11.6 days
100 Tbytes
14
~10
14
3.2 years
10,000
Tbytes
24
Assumptions: b = 10; 1,000,000 nodes/sec; 100bytes/nodeSlide25
Time and Memory Requirements
d
# Nodes
Time
Memory
2
111
.01 msec
11 Kbytes
4
11,111
1 msec
1 Mbyte
6
~10
6
1 sec
100 Mb
8
~10
8
100 sec
10 Gbytes
10
~10
10
2.8 hours
1 Tbyte
12
~10
12
11.6 days
100 Tbytes
14
~10
14
3.2 years
10,000
Tbytes
25
Assumptions: b = 10; 1,000,000 nodes/sec; 100bytes/nodeSlide26
Remark
If a problem has no solution, breadth-first may run forever (if the state space is infinite or states can be revisited arbitrary many times)
26
12
14
11
15
10
13
9
5
6
7
8
4
3
2
1
12
15
11
14
10
13
9
5
6
7
8
4
3
2
1
?Slide27
Bidirectional Strategy
27
2 fringe queues: FRINGE1 and FRINGE2
s
Time and space complexity is
O(b
d/2
)
O(b
d
)
if both trees have the same branching factor b
Question: What happens if the branching factor
is different in each direction?Slide28
Depth-First Strategy
New nodes are inserted at the front of FRINGE28
1
2
3
4
5
FRINGE = (1)Slide29
Depth-First Strategy
New nodes are inserted at the front of FRINGE29
1
2
3
4
5
FRINGE = (2, 3)Slide30
Depth-First Strategy
New nodes are inserted at the front of FRINGE30
1
2
3
4
5
FRINGE = (4, 5, 3)Slide31
Depth-First Strategy
New nodes are inserted at the front of FRINGE31
1
2
3
4
5Slide32
Depth-First Strategy
New nodes are inserted at the front of FRINGE32
1
2
3
4
5Slide33
Depth-First Strategy
New nodes are inserted at the front of FRINGE33
1
2
3
4
5Slide34
Depth-First Strategy
New nodes are inserted at the front of FRINGE34
1
2
3
4
5Slide35
Depth-First Strategy
New nodes are inserted at the front of FRINGE35
1
2
3
4
5Slide36
Depth-First Strategy
New nodes are inserted at the front of FRINGE36
1
2
3
4
5Slide37
Depth-First Strategy
New nodes are inserted at the front of FRINGE37
1
2
3
4
5Slide38
Depth-First Strategy
New nodes are inserted at the front of FRINGE38
1
2
3
4
5Slide39
Evaluation
b: branching factord: depth of shallowest goal node m: maximal depth of a leaf node
Depth-first search is:
Complete?
Optimal?
39Slide40
Evaluation
b: branching factord: depth of shallowest goal node m: maximal depth of a leaf node
Depth-first search is:
Complete only for finite search tree
Not optimal
Number of nodes generated (worst case):
1 + b + b
2
+ … +
b
m = O(bm) Time complexity is O(bm) Space complexity is O(bm) [or O(m)][Reminder: Breadth-first requires O(bd) time and space]40Slide41
Depth-Limited Search
Depth-first with depth cutoff k (depth at which nodes are not expanded)Three possible outcomes:SolutionFailure (no solution)
Cutoff
(no solution within cutoff)
41Slide42
Iterative Deepening Search
Provides the best of both breadth-first and depth-first search Main idea:42
IDS
For
k
= 0, 1, 2, … do:
Perform depth-first search with
depth cutoff
k
(i.e., only generate nodes with depth
k)Totally horrifying !Slide43
Iterative Deepening
43Slide44
Iterative Deepening
44Slide45
Iterative Deepening
45Slide46
Performance
Iterative deepening search is:CompleteOptimal if step cost =1Time complexity is:
(d+1)(1) + db + (d-1)b
2
+ … + (1)
b
d
= O(
b
d
)Space complexity is: O(bd) or O(d)46Slide47
Calculation
db + (d-1)b2 + … + (1) b
d
=
b
d
+
2bd-1 + 3bd-2 +… + db = (1 + 2b-1 + 3b-2 + … + db-d)b
d (
Si=1,…, ib
(1-i))
bd = b
d (b/(b-1))2
47Slide48
Number of Generated Nodes (Breadth-First & Iterative Deepening)
d = 5 and b = 248
BF
ID
1
1
x
6 = 6
2
2
x
5 = 10
4
4
x
4 = 16
8
8
x
3 = 24
16
16
x
2 = 32
32
32
x
1 = 32
63
120
120/63 ~ 2Slide49
Number of Generated Nodes (Breadth-First & Iterative Deepening)
d = 5 and b = 1049
BF
ID
1
6
10
50
100
400
1,000
3,000
10,000
20,000
100,000
100,000
111,111
123,456
123,456/111,111 ~ 1.111Slide50
Recap
BFS: Complete, optimalO(bd) time and spaceDFS: Not complete nor optimalO(bd) space, unbounded timeID: Complete, optimal
O(bd) space, O(b
d
) time
50Slide51
Related Topics
Non-unit costsRevisited statesHeuristic search
51Slide52
Non-unit Costs
Each arc has cost c > 0The cost of the path to each node N
is
g(N)
=
costs of arcs
Breadth-first is no longer optimal
52
Why?Slide53
Uniform-Cost Search
Expand node in FRINGE with the cheapest path
53
S
0
1
A
5
B
15
C
S
G
A
B
C
5
1
15
10
5
5
G
11
G
10
Suboptimal path!Slide54
Search Algorithm #2
SEARCH#2
1.
If GOAL?(initial-state) then return
initial-state
2.
INSERT(
initial-
node,FRINGE
)
3.
Repeat:4. If empty(FRINGE) then return failure5. N REMOVE(FRINGE)6. s STATE(N)7. If GOAL?(s) then return path or goal state8
. For every state s’ in SUCCESSORS(s)
9. Create a new node N’ as a child of N
10. INSERT(N’,FRINGE)
54
The goal test is applied
to a node when this node is
expanded
, not when it isgenerated.Slide55
Revisited States
55
8-queens
No
assembly
planning
Few
1
2
3
4
5
6
7
8
8-puzzle and robot navigation
Many
search tree is finite
search tree is infiniteSlide56
Avoiding Revisited States
Requires comparing state descriptions Breadth-first search: Store all states associated with generated nodes in VISITEDIf the state of a new node is in VISITED, then discard the node
56Slide57
Avoiding Revisited States
Requires comparing state descriptions Breadth-first search: Store all states associated with generated nodes in VISITEDIf the state of a new node is in VISITED, then discard the node
57
Implemented as hash-table
or as explicit data structure with flagsSlide58
Explicit Data Structures
Robot navigationVISITED: array initialized to 0, matching gridWhen grid position (x,y) is visited, mark corresponding position in VISITED as 1
Size of the entire state space!
58Slide59
Hash Tables
59
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
0
1
2
N-1
VISITED: Hash table of size N
Hash function: map from
S
to {0,…,N-1}
If hash function is chosen carefully to minimize chance of collision, then membership testing on M states averages
O(M/N)
timeSlide60
Avoiding Revisited States
Depth-first search: Solution 1:Store all states associated with nodes in current path in VISITEDIf the state of a new node is in VISITED, then discard the node
??
60Slide61
Avoiding Revisited States
Depth-first search: Solution 1:Store all states associated with nodes in current path in VISITEDIf the state of a new node is in VISITED, then discard the node
Only avoids loops
Solution 2:
Store all generated states in VISITED
If the state of a new node is in VISITED, then discard the node
Same space complexity as breadth-first !
61Slide62
Avoiding Revisited States in Uniform-Cost Search
For any state S, when the first node N such that STATE(N) = S is expanded, the path to N is the best path from the initial state to SSo:When a node is expanded, store its state into VISITED
When a new node N is generated:
If STATE(N) is in VISITED, discard N
If there exits a node N’ in the fringe such that STATE(N’) = STATE(N), discard the node -- N or N’ -- with the highest-cost path
62Slide63
Search Algorithm #3
SEARCH#3
1.
If GOAL?(initial-state) then return
initial-state
2.
INSERT(
initial-
node,FRINGE
)
3.
Repeat:4. If empty(FRINGE) then return failure5. N REMOVE(FRINGE)6. s STATE(N)7. Add s to VISITED7.
If GOAL?(s) then return path or goal state
8. For every state s’ in SUCCESSORS(s
)9. Create a new node N’ as a child of
N10. If s’ is in VISITED then discard
N’11. If there is
N’’ in FRINGE with STATE(N’)=STATE(N’’
)12. If g(
N’’) is lower than g(N’) then discard N’
13. Otherwise discard N’’
14. INSERT(N’
,FRINGE)
63Slide64
Homework
Readings: R&N Ch. 3.5
64