pacman ai : id-dfs - github pagestw-csie-sprout.github.io/.../week14/id_dfs_hsinchu.pdf · pacman...

54
Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索) 多拉A夢

Upload: others

Post on 09-Jun-2020

5 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

Pacman AI : ID-DFSIterative deepening depth-first search

(迭代加深搜索)

多拉A夢

Page 2: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

⾸首先給你⼀一張地圖

Page 3: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

A -> B的最短距離?

Page 4: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

先不管這麼複雜的地圖XD

Page 5: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

先來看看簡單的地圖

Page 6: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

這樣⼦子簡單多了吧:D

Page 7: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

But how ?

??

?

?

? ?

??

Page 8: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

觀察假設我們只能上下左右⾛走....

Page 9: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

觀察距離原點1

Page 10: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

觀察距離原點2

Page 11: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

觀察距離原點3

Page 12: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

觀察這個點距離原點為1

Page 13: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

觀察這個點距離原點為2

Page 14: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

觀察這個點距離原點為3...

Page 15: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

觀察總有⼀一天會淹沒抓到這隻可愛的⽼老⿏鼠(X)

Page 16: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

觀察這時候... 我們就可以知道⽼老⿏鼠的距離(笑)

Page 17: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

觀察就算有障礙物....

Page 18: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

介紹DFS

• 深度優先搜索

• 亂七⼋八糟的⼝口訣:『可以右轉就盡量右轉,如果⾛走到底退回來往左轉』

Page 19: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 20: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 21: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 22: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 23: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 24: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 25: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 26: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 27: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 28: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 29: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 30: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 31: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 32: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 33: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 34: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 35: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 36: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 37: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 38: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 39: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 40: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

BF

CE

H

I J

G

D

K

Page 41: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

如何實作DFS?

Page 42: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

想想遞回:D

Page 43: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

可是他⼀一次就衝到底了..A

BF

CE

H

I J

G

D

K

Page 44: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

要怎麼做到像是觀察到的結果...A

BF

CE

H

I J

G

D

K

Page 45: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

限制他的範圍!!A

BF

CE

H

I J

G

D

K

Page 46: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

限制他的範圍!!深度1A

BF

CE

H

I J

G

D

K

Page 47: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

限制他的範圍!!深度2A

BF

CE

H

I J

G

D

K

Page 48: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

限制他的範圍!!深度3A

BF

CE

H

I J

G

D

K

Page 49: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

限制他的範圍!!深度4A

BF

CE

H

I J

G

D

K

Page 50: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

⼀一層⼀一層推進....A

BF

CE

H

I J

G

D

K

Page 51: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

這就是為什麼叫『迭代加深』A

BF

CE

H

I J

G

D

K

Page 52: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

既然有了ID-DFS...• 最深要多深可以⾃自⼰己訂:D

• 記錄兩點間的最短距離 d[x1][y1][x2][y2] (⼀一種記錄⽅方式)

• 如果有發現更短的距離... 就去更新他

• 在深度d的時候發現想要找的⺫⽬目標:D

• 搜到最深還找不到⺫⽬目標 T^T

Page 53: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)

Pacman AI ⼩小提⽰示• 我們可以思考⼀一下:

• Pacman 接下來要⾛走『上』『下』『左』『右』

• 哪⼀一種⾛走法⽐比較好呢?

• 如何決定⽐比較好的⾛走法:D?

• 預測:如果Pacman⾛走了下⼀一格之後,⻤⿁鬼 和 Pacman之間的最短距離

• 當有兩隻⻤⿁鬼距離Pacman⼀一樣的時候 ....

Page 54: Pacman AI : ID-DFS - GitHub Pagestw-csie-sprout.github.io/.../week14/ID_DFS_HsinChu.pdf · Pacman AI : ID-DFS Iterative deepening depth-first search (迭代加深搜索)