Artificial Intelligence Problem solving by searching.

64
Artificial Intelligence Problem solving by searching

Transcript of Artificial Intelligence Problem solving by searching.

Page 1: Artificial Intelligence Problem solving by searching.

Artificial IntelligenceProblem solving by searching

Page 2: Artificial Intelligence Problem solving by searching.

Problem Solving by Searching

Search Methods : informed (Heuristic)

search

Page 3: Artificial Intelligence Problem solving by searching.

3

Using problem specific knowledge to aid searching

Without incorporating knowledge into searching, one can have no bias (i.e. a preference) on the search space.

Without a bias, one is forced to look everywhere to find the answer. Hence, the complexity of uninformed search is intractable.

Search everywhere!!

Page 4: Artificial Intelligence Problem solving by searching.

Informed Search Strategies

Best First Search

Page 5: Artificial Intelligence Problem solving by searching.

Informed Search Strategies

Greedy Searcheval-fn: f(n) = h(n)

Page 6: Artificial Intelligence Problem solving by searching.

6

Greedy Search

A

B

D

C

E

F

I

99

211

G

H

80

Start

Goal

97

101

75118

111

f(n) = h (n) = straight-line distance heuristic

State Heuristic: h(n)

A 366

B 374

C 329

D 244

E 253

F 178

G 193

H 98

I 0

140

Page 7: Artificial Intelligence Problem solving by searching.

7

Greedy Search

A

B

D

C

E

F

I

99

211

G

H

80

Start

Goal

97

101

75118

111

f(n) = h (n) = straight-line distance heuristic

State Heuristic: h(n)

A 366

B 374

C 329

D 244

E 253

F 178

G 193

H 98

I 0

140

Page 8: Artificial Intelligence Problem solving by searching.

8

Greedy Search

A

B

D

C

E

F

I

99

211

G

H

80

Start

Goal

97

101

75118

111

f(n) = h (n) = straight-line distance heuristic

State Heuristic: h(n)

A 366

B 374

C 329

D 244

E 253

F 178

G 193

H 98

I 0

140

Page 9: Artificial Intelligence Problem solving by searching.

9

Greedy Search

A

B

D

C

E

F

I

99

211

G

H

80

Start

Goal

97

101

75118

111

f(n) = h (n) = straight-line distance heuristic

State Heuristic: h(n)

A 366

B 374

C 329

D 244

E 253

F 178

G 193

H 98

I 0

140

Page 10: Artificial Intelligence Problem solving by searching.

10

Greedy Search

A

B

D

C

E

F

I

99

211

G

H

80

Start

Goal

97

101

75118

111

f(n) = h (n) = straight-line distance heuristic

State Heuristic: h(n)

A 366

B 374

C 329

D 244

E 253

F 178

G 193

H 98

I 0

140

Page 11: Artificial Intelligence Problem solving by searching.

11

Greedy Search

A

B

D

C

E

F

I

99

211

G

H

80

Start

Goal

97

101

75118

111

f(n) = h (n) = straight-line distance heuristic

State Heuristic: h(n)

A 366

B 374

C 329

D 244

E 253

F 178

G 193

H 98

I 0

140

Page 12: Artificial Intelligence Problem solving by searching.

12

Greedy Search

A

B

D

C

E

F

I

99

211

G

H

80

Start

Goal

97

101

75118

111

f(n) = h (n) = straight-line distance heuristic

State Heuristic: h(n)

A 366

B 374

C 329

D 244

E 253

F 178

G 193

H 98

I 0

140

Page 13: Artificial Intelligence Problem solving by searching.

13

Greedy Search

A

B

D

C

E

F

I

99

211

G

H

80

Start

Goal

97

101

75118

111

f(n) = h (n) = straight-line distance heuristic

State Heuristic: h(n)

A 366

B 374

C 329

D 244

E 253

F 178

G 193

H 98

I 0

140

Page 14: Artificial Intelligence Problem solving by searching.

14

Greedy Search

A

B

D

C

E

F

I

99

211

G

H

80

Start

Goal

97

101

75118

111

f(n) = h (n) = straight-line distance heuristic

State Heuristic: h(n)

A 366

B 374

C 329

D 244

E 253

F 178

G 193

H 98

I 0

140

Page 15: Artificial Intelligence Problem solving by searching.

15

Greedy Search

A

B

D

C

E

F

I

99

211

G

H

80

Start

Goal

97

101

75118

111

f(n) = h (n) = straight-line distance heuristic

State Heuristic: h(n)

A 366

B 374

C 329

D 244

E 253

F 178

G 193

H 98

I 0

140

Page 16: Artificial Intelligence Problem solving by searching.

16

Greedy Search: Tree Search

AStart

Page 17: Artificial Intelligence Problem solving by searching.

17

Greedy Search: Tree Search

A

BC

E

Start75118

140 [374][329]

[253]

Page 18: Artificial Intelligence Problem solving by searching.

18

Greedy Search: Tree Search

A

BC

E

F

99

GA

80

Start75118

140 [374][329]

[253]

[193]

[366][178]

Page 19: Artificial Intelligence Problem solving by searching.

19

Greedy Search: Tree Search

A

BC

E

F

I

99

211

GA

80

Start

Goal

75118

140 [374][329]

[253]

[193]

[366][178]

E[0][253]

Page 20: Artificial Intelligence Problem solving by searching.

20

Greedy Search: Tree Search

A

BC

E

F

I

99

211

GA

80

Start

Goal

75118

140 [374][329]

[253]

[193]

[366][178]

E[0][253]

Path cost(A-E-F-I) = 253 + 178 + 0 = 431

dist(A-E-F-I) = 140 + 99 + 211 = 450

Page 21: Artificial Intelligence Problem solving by searching.

21

Greedy Search: Optimal ?

A

B

D

C

E

F

I

99

211

G

H

80

Start

Goal

97

101

75118

111

f(n) = h (n) = straight-line distance heuristic

dist(A-E-G-H-I) =140+80+97+101=418

State Heuristic: h(n)

A 366

B 374

C 329

D 244

E 253

F 178

G 193

H 98

I 0

140

Page 22: Artificial Intelligence Problem solving by searching.

22

Greedy Search: Complete ?

A

B

D

C

E

F

I

99

211

G

H

80

Start

Goal

97

101

75118

111

f(n) = h (n) = straight-line distance heuristic

State Heuristic: h(n)

A 366

B 374

** C 250

D 244

E 253

F 178

G 193

H 98

I 0

140

Page 23: Artificial Intelligence Problem solving by searching.

23

Greedy Search: Tree Search

AStart

Page 24: Artificial Intelligence Problem solving by searching.

24

Greedy Search: Tree Search

A

BC

E

Start75118

140 [374][250]

[253]

Page 25: Artificial Intelligence Problem solving by searching.

25

Greedy Search: Tree Search

A

BC

E

D

111

Start75118

140 [374][250]

[253]

[244]

Page 26: Artificial Intelligence Problem solving by searching.

26

Greedy Search: Tree Search

A

BC

E

D

111

Start75118

140 [374][250]

[253]

[244]

C[250]Infinite Branch !

Page 27: Artificial Intelligence Problem solving by searching.

27

Greedy Search: Tree Search

A

BC

E

D

111

Start75118

140 [374][250]

[253]

[244]

C

D

[250]

[244]

Infinite Branch !

Page 28: Artificial Intelligence Problem solving by searching.

28

Greedy Search: Tree Search

A

BC

E

D

111

Start75118

140 [374][250]

[253]

[244]

C

D

[250]

[244]

Infinite Branch !

Page 29: Artificial Intelligence Problem solving by searching.

29

Greedy Search: Time and Space Complexity ?

A

B

D

C

E

F

I

99

211

G

H

80

Start

Goal

97

101

75118

111

140

• Greedy search is not optimal.

• Greedy search is incomplete without systematic checking of repeated states.

• In the worst case, the Time and Space Complexity of Greedy Search are both O(bm)

Where b is the branching factor and m the maximum path length

Page 30: Artificial Intelligence Problem solving by searching.

Informed Search Strategies

A* Search

eval-fn: f(n)=g(n)+h(n)

Page 31: Artificial Intelligence Problem solving by searching.

31

A* (A Star) A* uses a heuristic function which

combines g(n) and h(n): f(n) = g(n) + h(n)

g(n) is the exact cost to reach node n from the initial state. Cost so far up to node n.

h(n) is an estimation of the remaining cost to reach the goal.

Page 32: Artificial Intelligence Problem solving by searching.

32

A* (A Star)

n

g(n)

h(n)

f(n) = g(n)+h(n)

Page 33: Artificial Intelligence Problem solving by searching.

33

A* Search

f(n) = g(n) + h (n)

g(n): is the exact cost to reach node n from the initial state.

State Heuristic: h(n)

A 366

B 374

C 329

D 244

E 253

F 178

G 193

H 98

I 0

A

B

D

C

E

F

I

99

211

G

H

80

Start

Goal

97

101

75118

111

140

Page 34: Artificial Intelligence Problem solving by searching.

34

A* Search: Tree SearchA Start

Page 35: Artificial Intelligence Problem solving by searching.

35

A* Search: Tree SearchA

BC E

Start

75118140

[393]

[449][447

]

Page 36: Artificial Intelligence Problem solving by searching.

36

A* Search: Tree SearchA

BC E

F

99

G

80

Start

75118140

[393]

[449][447

]

[417]

[413]

Page 37: Artificial Intelligence Problem solving by searching.

37

A* Search: Tree SearchA

BC E

F

99

G

80

Start

75118140

[393]

[449][447

]

[417]

[413]

H

97

[415]

Page 38: Artificial Intelligence Problem solving by searching.

38

A* Search: Tree SearchA

BC E

F

I

99

G

H

80

Start

97

101

75118140

[393]

[449][447

]

[417]

[413]

[415]

Goal [418]

Page 39: Artificial Intelligence Problem solving by searching.

39

A* Search: Tree SearchA

BC E

F

I

99

G

H

80

Start

97

101

75118140

[393]

[449][447

]

[417]

[413]

[415]

Goal [418]

I [450]

Page 40: Artificial Intelligence Problem solving by searching.

40

A* Search: Tree SearchA

BC E

F

I

99

G

H

80

Start

97

101

75118140

[393]

[449][447

]

[417]

[413]

[415]

Goal [418]

I [450]

Page 41: Artificial Intelligence Problem solving by searching.

41

A* Search: Tree SearchA

BC E

F

I

99

G

H

80

Start

97

101

75118140

[393]

[449][447

]

[417]

[413]

[415]

Goal [418]

I [450]

Page 42: Artificial Intelligence Problem solving by searching.

A* with h() not Admissible

h() overestimates the cost to reach the goal state

Page 43: Artificial Intelligence Problem solving by searching.

43

A* Search: h not admissible !

A

B

D

C

E

F

I

99

211

G

H

80

Start

Goal

97

101

75118

111

f(n) = g(n) + h (n) – (H-I) Overestimated

g(n): is the exact cost to reach node n from the initial state.

State Heuristic: h(n)

A 366

B 374

C 329

D 244

E 253

F 178

G 193

H 138

I 0

140

Page 44: Artificial Intelligence Problem solving by searching.

44

A* Search: Tree SearchA Start

Page 45: Artificial Intelligence Problem solving by searching.

45

A* Search: Tree SearchA

BC E

Start

75118140

[393]

[449][447

]

Page 46: Artificial Intelligence Problem solving by searching.

46

A* Search: Tree SearchA

BC E

F

99

G

80

Start

75118140

[393]

[449][447

]

[417]

[413]

Page 47: Artificial Intelligence Problem solving by searching.

47

A* Search: Tree SearchA

BC E

F

99

G

80

Start

75118140

[393]

[449][447

]

[417]

[413]

H

97

[455]

Page 48: Artificial Intelligence Problem solving by searching.

48

A* Search: Tree SearchA

BC E

F

99

G

H

80

Start

97

75118140

[393]

[449][447

]

[417]

[413]

[455]

Goal I [450]

Page 49: Artificial Intelligence Problem solving by searching.

49

A* Search: Tree SearchA

BC E

F

99

G

H

80

Start

97

75118140

[393]

[449][447

]

[417]

[413]

[455]

Goal I [450]

D[473]

Page 50: Artificial Intelligence Problem solving by searching.

50

A* Search: Tree SearchA

BC E

F

99

G

H

80

Start

97

75118140

[393]

[449][447

]

[417]

[413]

[455]

Goal I [450]

D[473]

Page 51: Artificial Intelligence Problem solving by searching.

51

A* Search: Tree SearchA

BC E

F

99

G

H

80

Start

97

75118140

[393]

[449][447

]

[417]

[413]

[455]

Goal I [450]

D[473]

Page 52: Artificial Intelligence Problem solving by searching.

52

A* Search: Tree SearchA

BC E

F

99

G

H

80

Start

97

75118140

[393]

[449][447

]

[417]

[413]

[455]

Goal I [450]

D[473]

A* not optimal !!!

Page 53: Artificial Intelligence Problem solving by searching.

A* Algorithm

A* with systematic checking for repeated states …

Page 54: Artificial Intelligence Problem solving by searching.

54

A* Algorithm1. Search queue Q is empty.2. Place the start state s in Q with f value h(s).3. If Q is empty, return failure.4. Take node n from Q with lowest f value. (Keep Q sorted by f values and pick the first element).5. If n is a goal node, stop and return solution.6. Generate successors of node n.7. For each successor n’ of n do:

a) Compute f(n’) = g(n) + cost(n,n’) + h(n’).b) If n’ is new (never generated before), add n’ to Q. c) If node n’ is already in Q with a higher f value,

replace it with current f(n’) and place it in sorted order in Q.

End for8. Go back to step 3.

Page 55: Artificial Intelligence Problem solving by searching.

55

A* Search: Analysis

A

B

D

C

E

F

I

99

211

G

H

80

Start

Goal

97

101

75118

111

140

•A* is complete except if there is an infinity of nodes with f < f(G).

•A* is optimal if heuristic h is admissible.

•Time complexity depends on the quality of heuristic but is still exponential.

•For space complexity, A* keeps all nodes in memory. A* has worst case O(bd) space complexity, but an iterative deepening version is possible (IDA*).

Page 56: Artificial Intelligence Problem solving by searching.

A* Algorithm

A* with systematic checking for repeated states

An Example: Map Searching

Page 57: Artificial Intelligence Problem solving by searching.

57

SLD Heuristic: h()Straight Line Distances to Bucharest

Town SLDArad 366

Bucharest

0

Craiova 160

Dobreta 242

Eforie 161

Fagaras 178

Giurgiu 77

Hirsova 151

Iasi 226

Lugoj 244

Town SLDMehadai 241

Neamt 234

Oradea 380

Pitesti 98

Rimnicu 193

Sibiu 253

Timisoara 329

Urziceni 80

Vaslui 199

Zerind 374

We can use straight line distances as an admissible heuristic as they will never overestimate the cost to the goal. This is because there is no shorter distance between two cities than the straight line distance. Press space to continue with the slideshow.

Page 58: Artificial Intelligence Problem solving by searching.

58

Arad

Bucharest

OradeaZerind

Faragas

Neamt

Iasi

Vaslui

Hirsova

Eforie

Urziceni

Giurgui

Pitesti

Sibiu

Dobreta

Craiova

Rimnicu

Mehadia

Timisoara

Lugoj

87

92

142

86

98

86

211

101

90

99

151

71

75

140118

111

70

75

120

138

146

97

80

140

80

97

101

Sibiu

Rimnicu

Pitesti

Distances Between Cities

Page 59: Artificial Intelligence Problem solving by searching.

Greedy Search in Action …

Map Searching

Page 60: Artificial Intelligence Problem solving by searching.

60

Press space to see an Greedy search of the Romanian map featured in the previous slide. Note: Throughout the animation all nodes are labelled with f(n) = h(n). However,we will be using the abbreviations f, and h to make the notation simpler

OradeaZerind

Fagaras

Sibiu

RimnicuTimisoara

Arad

We begin with the initial state of Arad. The straight line distance from Arad to Bucharest (or h value) is 366 miles. This gives us a total value of ( f = h ) 366 miles. Press space to expand the initial state of Arad.

F= 366

F= 366

F= 374

F= 374

F= 253

F=253F= 329

F= 329

Once Arad is expanded we look for the node with the lowest cost. Sibiu has the lowest value for f. (The straight line distance from Sibiu to the goal state is 253 miles. This gives a total of 253 miles). Press space to move to this node and expand it.

We now expand Sibiu (that is, we expand the node with the lowest value of f ). Press space to continue the search.

F= 178

F= 178

F= 380

F= 380

F= 193

F= 193

We now expand Fagaras (that is, we expand the node with the lowest value of f ). Press space to continue the search.

BucharestF= 0

F= 0

Page 61: Artificial Intelligence Problem solving by searching.

A* in Action …

Map Searching

Page 62: Artificial Intelligence Problem solving by searching.

62

Press space to see an A* search of the Romanian map featured in the previous slide. Note: Throughout the animation all nodes are labelled with f(n) = g(n) + h(n). However,we will be using the abbreviations f, g and h to make the notation simpler

OradeaZerind

Fagaras

Pitesti

Sibiu

Craiova

RimnicuTimisoara

Bucharest

Arad

We begin with the initial state of Arad. The cost of reaching Arad from Arad (or g value) is 0 miles. The straight line distance from Arad to Bucharest (or h value) is 366 miles. This gives us a total value of ( f = g + h ) 366 miles. Press space to expand the initial state of Arad.

F= 0 + 366

F= 366

F= 75 + 374

F= 449

F= 140 + 253

F= 393F= 118 + 329

F= 447

Once Arad is expanded we look for the node with the lowest cost. Sibiu has the lowest value for f. (The cost to reach Sibiu from Arad is 140 miles, and the straight line distance from Sibiu to the goal state is 253 miles. This gives a total of 393 miles). Press space to move to this node and expand it.

We now expand Sibiu (that is, we expand the node with the lowest value of f ). Press space to continue the search.

F= 239 + 178

F= 417

F= 291 + 380

F= 671

F= 220 + 193

F= 413

We now expand Rimnicu (that is, we expand the node with the lowest value of f ). Press space to continue the search.

F= 317 + 98

F= 415F= 366 + 160

F= 526

Once Rimnicu is expanded we look for the node with the lowest cost. As you can see, Pitesti has the lowest value for f. (The cost to reach Pitesti from Arad is 317 miles, and the straight line distance from Pitesti to the goal state is 98 miles. This gives a total of 415 miles). Press space to move to this node and expand it.

We now expand Pitesti (that is, we expand the node with the lowest value of f ). Press space to continue the search.

We have just expanded a node (Pitesti) that revealed Bucharest, but it has a cost of 418. If there is any other lower cost node (and in this case there is one cheaper node, Fagaras, with a cost of 417) then we need to expand it in case it leads to a better solution to Bucharest than the 418 solution we have already found. Press space to continue.

F= 418 + 0

F= 418

In actual fact, the algorithm will not really recognise that we have found Bucharest. It just keeps expanding the lowest cost nodes (based on f ) until it finds a goal state AND it has the lowest value of f. So, we must now move to Fagaras and expand it. Press space to continue.

We now expand Fagaras (that is, we expand the node with the lowest value of f ). Press space to continue the search.

Bucharest(2)F= 450 + 0

F= 450

Once Fagaras is expanded we look for the lowest cost node. As you can see, we now have two Bucharest nodes. One of these nodes ( Arad – Sibiu – Rimnicu – Pitesti – Bucharest ) has an f value of 418. The other node (Arad – Sibiu – Fagaras – Bucharest(2) ) has an f value of 450. We therefore move to the first Bucharest node and expand it. Press space to continue

BucharestBucharestBucharest

We have now arrived at Bucharest. As this is the lowest cost node AND the goal state we can terminate the search. If you look back over the slides you will see that the solution returned by the A* search pattern ( Arad – Sibiu – Rimnicu – Pitesti – Bucharest ), is in fact the optimal solution. Press space to continue with the slideshow.

Page 63: Artificial Intelligence Problem solving by searching.

63

Consistent HeuristicConsistent Heuristic

The admissible heuristic h is consistent (or satisfies the monotone restriction) if for every node N and every successor N’ of N:

h(N) c(N,N’) + h(N’)

(triangular inequality) A consistent heuristic is admissible.

N

N’ h(N)

h(N’)

c(N,N’)

Page 64: Artificial Intelligence Problem solving by searching.

64

When to Use Search Techniques The search space is small, and

There are no other available techniques, or

It is not worth the effort to develop a more efficient technique

The search space is large, and There is no other available techniques,

and There exist “good” heuristics