63
CS B351: INTRO TO AI AND COMPUTER SIMULATION Instructor: Kris Hauser http://cs.indiana.edu/~hauserk 1

CS B351 : Intro to AI and Computer Simulation

  • Upload
    aviva

  • View
    31

  • Download
    2

Embed Size (px)

DESCRIPTION

CS B351 : Intro to AI and Computer Simulation. Instructor: Kris Hauser http://cs.indiana.edu/~hauserk. Agenda. Searching, data structures, and algorithms Breadth-first search Depth-first search Uniform-cost search. Defining a Search Problem. S. State space S - PowerPoint PPT Presentation

Citation preview

Page 1: CS  B351 :  Intro to AI and Computer Simulation

CS B351: INTRO TO AI AND COMPUTER SIMULATIONInstructor: Kris Hauserhttp://cs.indiana.edu/~hauserk

1

Page 2: CS  B351 :  Intro to AI and Computer Simulation

AGENDA Searching, data structures, and algorithms Breadth-first search Depth-first search Uniform-cost search

2

Page 3: CS  B351 :  Intro to AI and Computer Simulation

DEFINING A SEARCH PROBLEM

3

State space S Successor function: x S SUCC(x) 2S

Initial state s0 Goal test: xS GOAL?(x) =T or F Arc cost

S

Page 4: CS  B351 :  Intro to AI and Computer Simulation

STATE GRAPH

4

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 component

Page 5: CS  B351 :  Intro to AI and Computer Simulation

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 !

5

I

G

Page 6: CS  B351 :  Intro to AI and Computer Simulation

6

SEARCH GRAPH != STATE GRAPH

835

24 71 6

835

24 71 6

835

24

7

1 6

835

24 71 6

835

247

1 6

835

24 71 6

If states are allowed to be revisited,the search tree may be infinite even

when the state space is finite

Page 7: CS  B351 :  Intro to AI and Computer Simulation

7

SEARCH GRAPH NODE != STATE

PARENT-NODE835

24 71 6

STATE

Depth of a node N = length of path from root to N (depth of the root = 0)

BOOKKEEPING

5Path-Cost

5DepthRightAction

Expanded yes...

CHILDREN

Page 8: CS  B351 :  Intro to AI and Computer Simulation

FRINGE OF SEARCH TREE The fringe is the set of all search nodes that

haven’t been expanded yet

835

24 71 6

835

24 71 6

835

24

7

1 6

835

24 71 6

835

724

1 6

835

24 71 6

Page 9: CS  B351 :  Intro to AI and Computer Simulation

9

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

Page 10: CS  B351 :  Intro to AI and Computer Simulation

10

SEARCH ALGORITHM #1SEARCH#11. If GOAL?(initial-state) then return initial-

state2. 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)

Expansion of N

Page 11: CS  B351 :  Intro to AI and Computer Simulation

BLIND SEARCH STRATEGIES11

Page 12: CS  B351 :  Intro to AI and Computer Simulation

12

BLIND STRATEGIES Breadth-first

Bidirectional

Depth-first Depth-limited Iterative deepening

Uniform-Cost(variant of breadth-first)

Arc cost = 1

Arc cost = c(action) 0

Page 13: CS  B351 :  Intro to AI and Computer Simulation

13

BREADTH-FIRST STRATEGY New nodes are inserted at the end of

FRINGE

2 3

4 5

1

6 7

FRINGE = (1)

Page 14: CS  B351 :  Intro to AI and Computer Simulation

14

BREADTH-FIRST STRATEGY New nodes are inserted at the end of

FRINGE

FRINGE = (2, 3)2 3

4 5

1

6 7

Page 15: CS  B351 :  Intro to AI and Computer Simulation

15

BREADTH-FIRST STRATEGY New nodes are inserted at the end of

FRINGE

FRINGE = (3, 4, 5)2 3

4 5

1

6 7

Page 16: CS  B351 :  Intro to AI and Computer Simulation

16

BREADTH-FIRST STRATEGY New nodes are inserted at the end of

FRINGE

FRINGE = (4, 5, 6, 7)2 3

4 5

1

6 7

Page 17: CS  B351 :  Intro to AI and Computer Simulation

17

PERFORMANCE MEASURES Completeness

A search algorithm is complete if it finds a solution whenever one exists[What about the case when no solution exists?]

OptimalityA search algorithm is optimal if it returns a minimum-cost path whenever a solution exists

ComplexityIt measures the time and amount of memory required by the algorithm

Page 18: CS  B351 :  Intro to AI and Computer Simulation

18

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

Page 19: CS  B351 :  Intro to AI and Computer Simulation

19

EVALUATION b: branching factor d: depth of shallowest goal node Breadth-first search is:

Complete? Not complete? Optimal? Not optimal?

Page 20: CS  B351 :  Intro to AI and Computer Simulation

20

EVALUATION b: branching factor d: depth of shallowest goal node Breadth-first search is:

Complete Optimal if step cost is 1

Number of nodes generated:???

Page 21: CS  B351 :  Intro to AI and Computer Simulation

21

EVALUATION b: branching factor d: depth of shallowest goal node Breadth-first search is:

Complete Optimal if step cost is 1

Number of nodes generated: 1 + b + b2 + … + bd = ???

Page 22: CS  B351 :  Intro to AI and Computer Simulation

22

EVALUATION b: branching factor d: depth of shallowest goal node Breadth-first search is:

Complete Optimal if step cost is 1

Number of nodes generated: 1 + b + b2 + … + bd = (bd+1-1)/(b-1) = O(bd)

Time and space complexity is O(bd)

Page 23: CS  B351 :  Intro to AI and Computer Simulation

23

TIME AND MEMORY REQUIREMENTSd # Nodes Time Memory2 111 .01 msec 11 Kbytes4 11,111 1 msec 1 Mbyte6 ~106 1 sec 100 Mb8 ~108 100 sec 10 Gbytes10 ~1010 2.8 hours 1 Tbyte12 ~1012 11.6 days 100 Tbytes14 ~1014 3.2 years 10,000 Tbytes

Assumptions: b = 10; 1,000,000 nodes/sec; 100bytes/node

Page 24: CS  B351 :  Intro to AI and Computer Simulation

24

TIME AND MEMORY REQUIREMENTSd # Nodes Time Memory2 111 .01 msec 11 Kbytes4 11,111 1 msec 1 Mbyte6 ~106 1 sec 100 Mb8 ~108 100 sec 10 Gbytes10 ~1010 2.8 hours 1 Tbyte12 ~1012 11.6 days 100 Tbytes14 ~1014 3.2 years 10,000 Tbytes

Assumptions: b = 10; 1,000,000 nodes/sec; 100bytes/node

Page 25: CS  B351 :  Intro to AI and Computer Simulation

25

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)

12

14

11

15

10

13

9

5 6 7 8

4321

12

15

11

14

10

13

9

5 6 7 8

4321

?

Page 26: CS  B351 :  Intro to AI and Computer Simulation

26

BIDIRECTIONAL STRATEGY2 fringe queues: FRINGE1 and FRINGE2

s

Time and space complexity is O(bd/2) O(bd) if both trees have the same branching factor bQuestion: What happens if the branching factor is different in each direction?

Page 27: CS  B351 :  Intro to AI and Computer Simulation

27

DEPTH-FIRST STRATEGY New nodes are inserted at the front of

FRINGE1

2 3

4 5FRINGE = (1)

Page 28: CS  B351 :  Intro to AI and Computer Simulation

28

DEPTH-FIRST STRATEGY New nodes are inserted at the front of

FRINGE1

2 3

4 5FRINGE = (2, 3)

Page 29: CS  B351 :  Intro to AI and Computer Simulation

29

DEPTH-FIRST STRATEGY New nodes are inserted at the front of

FRINGE1

2 3

4 5FRINGE = (4, 5, 3)

Page 30: CS  B351 :  Intro to AI and Computer Simulation

30

DEPTH-FIRST STRATEGY New nodes are inserted at the front of

FRINGE1

2 3

4 5

Page 31: CS  B351 :  Intro to AI and Computer Simulation

31

DEPTH-FIRST STRATEGY New nodes are inserted at the front of

FRINGE1

2 3

4 5

Page 32: CS  B351 :  Intro to AI and Computer Simulation

32

DEPTH-FIRST STRATEGY New nodes are inserted at the front of

FRINGE1

2 3

4 5

Page 33: CS  B351 :  Intro to AI and Computer Simulation

33

DEPTH-FIRST STRATEGY New nodes are inserted at the front of

FRINGE1

2 3

4 5

Page 34: CS  B351 :  Intro to AI and Computer Simulation

34

DEPTH-FIRST STRATEGY New nodes are inserted at the front of

FRINGE1

2 3

4 5

Page 35: CS  B351 :  Intro to AI and Computer Simulation

35

DEPTH-FIRST STRATEGY New nodes are inserted at the front of

FRINGE1

2 3

4 5

Page 36: CS  B351 :  Intro to AI and Computer Simulation

36

DEPTH-FIRST STRATEGY New nodes are inserted at the front of

FRINGE1

2 3

4 5

Page 37: CS  B351 :  Intro to AI and Computer Simulation

37

DEPTH-FIRST STRATEGY New nodes are inserted at the front of

FRINGE1

2 3

4 5

Page 38: CS  B351 :  Intro to AI and Computer Simulation

38

EVALUATION b: branching factor d: depth of shallowest goal node m: maximal depth of a leaf node Depth-first search is:

Complete? Optimal?

Page 39: CS  B351 :  Intro to AI and Computer Simulation

39

EVALUATION b: branching factor d: 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 + b2 + … + bm = O(bm)

Time complexity is O(bm) Space complexity is O(bm) [or O(m)] [Reminder: Breadth-first requires O(bd) time

and space]

Page 40: CS  B351 :  Intro to AI and Computer Simulation

40

DEPTH-LIMITED SEARCH Depth-first with depth cutoff k (depth at

which nodes are not expanded)

Three possible outcomes: Solution Failure (no solution) Cutoff (no solution within cutoff)

Page 41: CS  B351 :  Intro to AI and Computer Simulation

41

ITERATIVE DEEPENING SEARCH Provides the best of both breadth-first and

depth-first search

Main idea:

IDSFor k = 0, 1, 2, … do:

Perform depth-first search with depth cutoff k(i.e., only generate nodes with depth

k)

Totally horrifying !

Page 42: CS  B351 :  Intro to AI and Computer Simulation

42

ITERATIVE DEEPENING

Page 43: CS  B351 :  Intro to AI and Computer Simulation

43

ITERATIVE DEEPENING

Page 44: CS  B351 :  Intro to AI and Computer Simulation

44

ITERATIVE DEEPENING

Page 45: CS  B351 :  Intro to AI and Computer Simulation

45

PERFORMANCE Iterative deepening search is:

Complete Optimal if step cost =1

Time complexity is: (d+1)(1) + db + (d-1)b2 + … + (1) bd = O(bd)

Space complexity is: O(bd) or O(d)

Page 46: CS  B351 :  Intro to AI and Computer Simulation

46

CALCULATIONdb + (d-1)b2 + … + (1) bd

= bd + 2bd-1 + 3bd-2 +… + db= (1 + 2b-1 + 3b-2 + … + db-d) bd

(Si=1,…, ib(1-i)) bd = bd (b/(b-1))2

Page 47: CS  B351 :  Intro to AI and Computer Simulation

47

NUMBER OF GENERATED NODES (BREADTH-FIRST & ITERATIVE DEEPENING) d = 5 and b = 2

BF ID1 1 x 6 = 62 2 x 5 = 104 4 x 4 = 168 8 x 3 = 2416 16 x 2 =

3232 32 x 1 =

3263 120

120/63 ~ 2

Page 48: CS  B351 :  Intro to AI and Computer Simulation

48

NUMBER OF GENERATED NODES (BREADTH-FIRST & ITERATIVE DEEPENING) d = 5 and b = 10

BF ID1 610 50100 4001,000 3,00010,000 20,000100,000 100,000111,111 123,456 123,456/111,111 ~ 1.111

Page 49: CS  B351 :  Intro to AI and Computer Simulation

RECAP BFS: Complete, optimal

O(bd) time and space DFS: Not complete nor optimal

O(bd) space, unbounded time ID: Complete, optimal

O(bd) space, O(bd) time

49

Page 50: CS  B351 :  Intro to AI and Computer Simulation

RELATED TOPICS Non-unit costs Revisited states Heuristic search

50

Page 51: CS  B351 :  Intro to AI and Computer Simulation

NON-UNIT COSTS Each arc has cost c > 0

The cost of the path to each node N is g(N) = costs of arcs

Breadth-first is no longer optimal

51

Why?

Page 52: CS  B351 :  Intro to AI and Computer Simulation

UNIFORM-COST SEARCH Expand node in FRINGE with the cheapest path

52

S0

1A

5B

15CS G

A

B

C

51

15

10

55

G11

G10

Suboptimal path!

Page 53: CS  B351 :  Intro to AI and Computer Simulation

SEARCH ALGORITHM #2SEARCH#21. If GOAL?(initial-state) then return initial-state2. 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 N10. INSERT(N’,FRINGE) 53

The goal test is appliedto a node when this node isexpanded, not when it isgenerated.

Page 54: CS  B351 :  Intro to AI and Computer Simulation

54

REVISITED STATES

8-queens

No

assembly planning

Few

1 2 34 5

67 8

8-puzzle and robot navigation

Many

search tree is finite search tree is infinite

Page 55: CS  B351 :  Intro to AI and Computer Simulation

AVOIDING REVISITED STATES Requires comparing state descriptions Breadth-first search:

Store all states associated with generated nodes in VISITED

If the state of a new node is in VISITED, then discard the node

55

Page 56: CS  B351 :  Intro to AI and Computer Simulation

AVOIDING REVISITED STATES Requires comparing state descriptions Breadth-first search:

Store all states associated with generated nodes in VISITED

If the state of a new node is in VISITED, then discard the node

56

Implemented as hash-table or as explicit data structure with flags

Page 57: CS  B351 :  Intro to AI and Computer Simulation

EXPLICIT DATA STRUCTURES

Robot navigation VISITED: array initialized to 0, matching grid When grid position (x,y) is visited, mark

corresponding position in VISITED as 1 Size of the entire state space!

57

Page 58: CS  B351 :  Intro to AI and Computer Simulation

HASH TABLES

58

1

23 45 6

78 1 2 3

4 567 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) time

Page 59: CS  B351 :  Intro to AI and Computer Simulation

AVOIDING REVISITED STATES Depth-first search:

Solution 1: Store all states associated with nodes in current path

in VISITED If the state of a new node is in VISITED, then discard

the node ??

59

Page 60: CS  B351 :  Intro to AI and Computer Simulation

AVOIDING REVISITED STATES Depth-first search:

Solution 1: Store all states associated with nodes in current path

in VISITED If 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 ! 60

Page 61: CS  B351 :  Intro to AI and Computer Simulation

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 S

So: 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

61

Page 62: CS  B351 :  Intro to AI and Computer Simulation

SEARCH ALGORITHM #3SEARCH#31. If GOAL?(initial-state) then return initial-state2. 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 state8. 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)

62

Page 63: CS  B351 :  Intro to AI and Computer Simulation

HOMEWORK Readings: R&N Ch. 3.5

63