Sei sulla pagina 1di 10

LISP,&DFS&'❹➂❽➂ !

➂❹➆➀&'❹➈❽❻
(defun dfs (state)
(cond ((goalp state)(list state))
(Depth-first search)
(t (let ((childs (succ state))) ,-➂&./&❶-❽➂❸&.➂❹-❸&❹➄❽❸&❻❹.❽➈➀&➃❹'/4❸&.➂❹-❸ 
(loop for c in childs
for solution = (dfs c)
❸➀❻.❸❸
do (when solution 4.❹❽,&!❹➂➆❸&.➂❹-❸&❷❽➂.&❸❽❸❽&❻❹.❽➈➀&/,❸&.➂❹-❸ &
(return-from dfs
(cons state solution)))))))) ❸4❹-,&/,❸&.➂❹-❸&4❻,➄&❸❹❹'&!➂❹➆&❽➀➆,&➁❽.➂-&➃❽, &
◗➁.➆➈❹❸&4❷➅&❽➈➀&➀'➂➀❘&.❽.❹4❽4'
➉➆❸&..&❻.❹➈&/➀&➃❽❽❷➆'&4➂❹➀❿❘&❽4➂❶➀&❹4!❻➄&/➀'&➁❽.➂-&!4&➁❽4➂'➄ 
◗❹/❹➀➂,&➁.❻.
DFS (state path)
if goalp(state) return(path)
else
for c in succ(state)
DFS(c, path state)

_______________________________________________________________ _______________________________________________________________
©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T ©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T

(defun dfs-l (state depth)


(cond ((goalp state)(list state)) !➂❹➆&.➀,❶❸&➁➆&DFS
((> depth 0)
(let ((childs (succ state)))
➀❶➆➂➀&❹/&❽➈❹➅➄❽/&➇➄➆➀&➅➄❿❸➀&➀❹➀➆&❸➀➆➂➀'&➁.❽4❹❶➀/❸ 
(loop for c in childs !➂❹➆&.➀,❶❸&➁➆& DFS,&❿❑❷,&➁❽'➂.'➂&./❺&➆❹➄➂➀&❽❷❿ 
for solution = DFS (state, depth, path)
(dfs-l c (1- depth)) if goalp(state) return(path)
do (when solution else
(return-from dfs-l if depth=0 then return FAIL
(cons state else
solution)))))))) for c in succ(state)
DFS(c, depth-1, path state)

_______________________________________________________________ _______________________________________________________________
©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T ©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T
➃❹4❿❺&.❹'❽4❷&&!➂❹➆➀&'❹➈❽❻ &!➂❹➆➀&'❹➈❽❻,&➁❽.➂-&❻❹.❽➈&4❷➅➀&/➂❶❹❷
/❹❸& b&4'/❿&b❙n&4.❹❽❸&➀❿➀&/❹❸&➆❶4&➀❿,&➁❽4➂'➄'&➁❽.➂-❸&4➈➅➂  1

❽❻❿❹➄❸&'❹➈❽❻❸&!➂❹➆&/❹❸&n&❹&.➂❹-➀&➁❽❷➀❽&➀'&❽➀➂❽➅!➂❸&4➈➅➂❸ 2 10

3 7 11 13

n=3 4 5 6 8 9 12 14 15 16

b=4
!➂❹➆❸&.➀,❶❸&❹➄❽❸&D&4'/❿&b❙D&&➀'&➃❹4❿❺&'4❷❽&➆❹4❶❸&❸4!➂,&➃❿➀ 

_______________________________________________________________ _______________________________________________________________
©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T ©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T

4➂❹➀❿❘&D&!➂❹➆➀&❷➆&➉➆❸&❽.➂-&➀❿&./&'❹➈❽❻❸&❻.➈❽&➆❹4❶❸&❸4!➂,  ❝'❹➈❽❻❸&.❹➀❽➆❽&&!➂❹➆➀&'❹➈❽❻
bD + 1 −1 L&4'/❿&➁❽.➂-& L&❹❻.❹➈❽&❺/❹&❸4❼➂➀&.❹4❽'❽&'❹➈❽❻❸&➆❽❶❽&,❹❼❸&❸4!➂, 
◗➁❽❻❹.❽➈& ➃❹4.❽➈❸&❾4❹/&❹➄❽❸
b −1

L
D L
D

_______________________________________________________________ _______________________________________________________________
©➉❽,❹!4➂&➀❹/'  ❛ ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T ©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T
❺❹,❺,➀&➁❹4❶❽&❽❷❽➂&➀❹❷❶&D&&.❽.❽➆,❹&❸,❹'❻&D&!➂❹➆❸&.➀,❶❸&.➆❽,!  .❹➀❽➆❽&&!➂❹➆➀&'❹➈❽❻
◗D,&.❽➀/❽-➄➄❹➈!/&'❹➈❽❻❸&.❹➀❽➆❽'&❹4❿❺❘&➁❹-➆&➁❽,/'➂ ➀❽➆❽&'❹➈❽❻➀&❽❹❿❽➅❸&➀❷❶&❸➀❹❷❶&❸4❼➂❸&❽.➂-&.-❹,!&4'/❿ 
L
D D

_______________________________________________________________ _______________________________________________________________
©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T ©➉❽,❹!4➂&➀❹/'  ❜ ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T

DFS&➀'&.❹➂➀'
➁❽❽!❹&❸❷❽➂,&➃❹4.➈&4❽❺❻❽❹&4❹-➆❽'&❻❼,❹➂&➁/&➁➀'&/4!➄&'❹➈❽❻&➁.❽4❹❶➀/ 
➃❹4.➈
4❹-➆❽'&➀➀❿,&❻❼,❹➂&/➀&◗!➂❹➆❸&.➀,❶❸&/➀➀❘&❽4❹!➂❸&➁.❽4❹❶➀/❸&❽,❶➀ 
❸4❽-➆&.❻❼,❹➂&!➂❹➆❸&.➀,❶❸&➁➆&➁.❽4❹❶➀/❸&❽,❶➀ 
➁➀'&➁.❽4❹❶➀/❸& D ≥ L&4'/❿ 
➁❽❽!&4'/❿&➃❹4.➈&/➀➀&4❹-➆❽&/❹❸&❝➁➀'&❹➄❽/&➁.❽4❹❶➀/❸& D < L&4'/❿ 
➃❹4.➈

L D

_______________________________________________________________ _______________________________________________________________
©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T ©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T
➇4❶,&!➂❹➆➀&'❹➈❽❻ !➂❹➆➀&'❹➈❽❻,&➃❹4.➈❸&.❹❿❽/
➉➆&➀/❿&'❹➈❽❻❸&,❻4➂➀&➁❽➅❻❽.➂&❸➀➆➂➀'&➁❽➂.❽4❹❶➀/❸  4❽❻➂&➀➆,&❹/❘&4.❹❽,&4-!❸&➃❹4.➈❸&➀❹➀➅➂&./&/-➂➄'&❻❼,❹➂&/➀ 
➁❽➈4❶&❽,❶➀&➁❶&➁➀'&❹➄❽❸&DFS-L  ◗❽➀/➂❽➄❽➂
➀'&➁❽!➀❻&4➂❹➀❿❘&➁❽.➂-&➀'&,4&4➈➅➂&❽❑➆&❶-❹❽❽&,-➂'&➃❿.❽&➁❽➈4❶,&➁➀❹/ 
◗.❹,4&➁❽➂➆➈&❹❻.❹➈❽&➁❽,-➂❸&➇4❶
❝➁❽,-➂❸&4➈➅➂,&❽➀/-➄➄❹➈➅!/&❸❽❸❽&'❹➈❽❻❸&➉➆&➀❷❹❶➅&➃❿.❽&❽➄❹-❽!&❸4!➂, 
1
2 2 1

3 3 3 3 2

4 4 4 4 4 4 4 4
3

_______________________________________________________________ _______________________________________________________________
©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T ©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T

➀❹➀➅➂&.!❽❷,&➁➆&DFS ➁❽➈4❶,&DFS➀&.❹➄❹4.➈
DFS (state, depth, path)
if goalp(state) return(path) ❻.❹➈&4,❿&'❷❻&.➂❹-&➁/,&!❹❷,➀❹&❹❻.❹➈'&➁❽.➂-❸&.➂❽'4&➀➆&4❹➂'➀&➃.❽➄ &
else 4,➆,
if depth=0 then return FAIL
else ➁.❽4❹❶➀/❸&➀'&➃❹4❿❺❸&.'❽4❷&./&.❿➈❹❸&❹❺❿&.➈➅❹. 
for c in succ(state)
if c is not in path then .➀,❹!➂&❸➄❽/&➃❿➀❹&.❽➀/❽-➄➄❹➈➅!/➀&.❽4/➄❽➀➂
DFS(c, depth-1, path state) ❸➀❻.❸❸&.➂❹-➂&➀❹➀➅➂,&/-➂➄&'❷❻&.➂❹-&➁/,&!❹❷,➀&➃.❽➄ &
❹❻❹.❽➈&4❽❻➂&./&4!❽❽.&❾/&➀❶➆➂,&❸➆❹➄.&➆➄➂.&❹❺❿&❸!❽❷,
.➂❹-&➀❿&➀'

_______________________________________________________________ _______________________________________________________________
©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T ©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T
(defun dfs-l-g (state depth parents)
Backtracking&'❹➈❽❻ (cond ((goalp state)(list state))
!➂❹➆➀&'❹➈❽❻&➀➆&❸❽-/❽4❹ & ((> depth 0)
(let ((childs (succ state)))
.-❹,!&.❹➆-➂/,&❸➄❹.➄&4,➆➂❸&.❽-!➄❹➈&4'/❿&❹,&'➂.'❸➀&➃.❽➄ & (loop for c in childs
➁❽4❹❼4➈❹/ for solution =
(when (not (member c parents
.4!❹❻&❷❻/&➃,&.4-❽➂&❸4❹❷-❹4➈❸&.➂❹-&➀'&➁❽➄,❸&➀❿&./&❻.➈➀&➁❹!➂, & :test #'equalp))
(dfs-l-g c (1- depth)
F❹❿❹&!➂❹➆➀&❹.❹/&.4!❹❻&❽➄'&➃,&.4-❽➂&!➂❹➆➀&❹.❹/ (cons state parents)))
➁❽4➂'➄'&➁❽.➂-❸&4➈➅➂&➁❹➂❽➅!➂&➃❹4❿❺,&4.❹❽&❷❹➆&.❽➄❹❿➅❻&❸4❹❷-❹4➈ & do (when solution
(return-from dfs-l-g
➃❹4❻/❸&4❹❼4➈❹/➀&➆❽,-➂&.➂❹-&➀❿,&4➂'➄&➃❿&❹➂❿❘ D&'❹➈❽❻❸&!➂❹➆&/❹❸ (cons state solution))))))))
◗❸➅❹➄'

_______________________________________________________________ _______________________________________________________________
©➉❽,❹!4➂&➀❹/'  ❛ ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T ©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T

!➂❹➆➀&'❹➈❽❻&➀'&.❹➄❹4.❽ backtracking&'❹➈❽❻,&➁❽.➂-&4❹-❽&4❷➅➀&/➂❶❹❷
➃❹4❿❺,&❽➄❹❿➅❻ & 11

'❹➈❽❻&❽❷➆-,&➃❹4.➈&/-➂➀&➀❹❷❶&❽❹❿❽➅&➁❽❽!&➁❽,4&❸4❼➂&❽,-➂&➁❽➂❽❽!&➁/ & 2 2 10 3
➁❽❼➆❹➂
4 3 5 7 11 11 12 13
4➂❹➀❿&❽.❼❽'&❹➄❽❸&'❹➈❽❻❸&❽❺/&➀,❶❹➂❸&!➂❹➆❸➂&➃❼!&➃❹4.➈❸&❾4❹/&➁/, &
.❻❼,❹➂&➃❹4.➈❸&./❽-➂
4 5 6 8 9 12 14 15 16
6 7 8 9 10 13 14 15 16
backrackin 2
depth first 2

_______________________________________________________________ _______________________________________________________________
©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T ©➉❽,❹!4➂&➀❹/'  ❜ ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T
,❻❹4➀&'❹➈❽❻ ❝!➂❹➆➀&'❹➈❽❻&➀'&.❹➄❹4➅❻
◗Breadth first search❘& ➁❽❽➈❹➅➄❽/&❹➀❽➈/❹&➁❽➀❹❷❶&➁❽➈➄➆➀&➆➀!❸➀&➀❹➀➆&'❹➈❽❻❸&!➂❹➆&.➀,❶❸&❽➀, &
4.❹❽,&➃❼!❸&!➂❹➆❸&➀➆,&.➂❹-❸&❷❽➂.&❸❽❸❽&❻❹.❽➈➀&/,❸&.➂❹-❸ & ➃❹4.❽➈&/-➂❽&/➀&❽❺/&!➂❹➆❸&.➀,❶❸➂&➀❹❷❶&➃❹4.❽➈❸&❾4❹/&➁/ &
❽➈➀&➀'➂➀❘&.❽.❹4❽4'&❸4❹-,&/,❸&.➂❹-❸&4❻,➄&❸❹❹'&!➂❹➆&❽➀➆,&➁❽.➂-&➃❽, & 4.❹❽,&4-!❸&❻4❿❸,&❹➄❽/&/-➂➄'&➃❹4.➈ 
◗❻❹.❽➈❸&4❷➅
BFS (init-state)
OPEN ← (node(Si,NIL))
While OPEN ≠ ( )
next ← pop(OPEN)
if goalp(next.state) then return(trace(next))
OPEN ← OPEN succ(next.state)

_______________________________________________________________ _______________________________________________________________
©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T ©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T

(defun node-state (node)(first node))


,❻❹4➀&'❹➈❽❻,&➁❽.➂-&❻❹.❽➈&4❷➅➀&/➂❶❹❷ (defun node-parent (node)(second node))
1 (defun bfs (state)
(let ((open (list (list state nil))))
2 3 (loop while open
for next-node = (pop open)
4 5 6 7 do
(when (goalp (node-state next-node))
(return-from bfs
(reverse (trace-back next-node))))
8 9 10 11 12 13 14 15 16 (setf open
(nconc open
(loop for s in
(succ (node-state next-node))
collect (list s next-node)))))))

_______________________________________________________________ _______________________________________________________________
©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T ©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T
(defun find-state (state s-list)
(find state s-list ➇4❶➀&➁/.❹➂&,❻❹4➀&'❹➈❽❻
:test #'equalp :key #'node-state)) ➁❽➈4❶,&➁❶&➃❹❿➄&❷❹,➆.&❸➀➆➂➀'&❸4❹❷-❹4➈❸ 
(defun bfs-g (state) ❹❻.❹➈'&➁❽.➂-❸&.4❽➂'&❽❑➆&➁❽➈4❶&❽..,&4❺❹❻&'❹➈❽❻&➆❹➄➂➀&➃.❽➄&➃/❿&➁❶ 
(let ((open (list (list state nil)))
(close nil)) ➃❿➀❹&.❹❽➀/❽-➄➄❹➈➅!/&➃❸&❹❽'❿➆&4,❿&➃❹4❿❺❸&.❹'❽4❷&!➂❹➆➀&'❹➈❽❻➀&❷❹❶❽➄, 
(loop while open ❸!❽❷,❸&./&➇❽➅❹❸➀&❶❹❸➄
for next-node = (pop open)
do BFS-G (state)
(when (goalp (node-state next-node)) OPEN ← (node(state,NIL))
(return-from bfs-g While OPEN ≠ ( )
(reverse (trace-back next-node)))) next ← pop(OPEN)
(push next-node close)
push (next, CLOSE )
(setf open if goalp(next.state) then return(trace(next))
(nconc open for s in succ(next.state)
(loop for s in if s ∉ OPEN and s ∉ CLOSE then
(succ (node-state next-node))
when (not (or (find-state s open) OPEN ← OPEN s
(find-state s close)))
collect (list s next-node)))))))

_______________________________________________________________ _______________________________________________________________
©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T ©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T

,❻❹4➀&'❹➈❽❻&➀'&.❹➄❹4➅❻❹&.❹➄❹4.❽ ➁❽➆❹-❽,&&,❻❹4➀&'❹➈❽❻
❝.❹➄❹4.❽ ❝➃❹4❿❺&.❹'❽4❷
◗➃❹4.➈&➁❽❽!&➃❿/&➁/❘&➃❹4.➈&./❽-➂&❻❽❼,➂ & 4.❹❽❸&➀❿➀&&➉➆,&➁❽.➂-❸&4➈➅➂&/❹❸&➆❶4&➀❿,&➁❽4➂'➄'&➁❽.➂-❸&4➈➅➂ &
❾4❹/&/❹❸&L&❹&.➂❹-➀&➁❽❷➀❽&➀'&❽➀➂❽➅!➂❸&4➈➅➂❸&/❹❸&b&4'/❿&&& b − 1
L+1
❽➀➂❽➄❽➂&➁❽❷➆-&4➈➅➂&➀➆,&➃❹4.➈&/-❹➂ &
b −1
❝.❹➄❹4➅❻ 4.❹❽,&4-!❸&➃❹4.➈❸
❾4❹/,&❽➀/❽-➄➄❹➈➅!/&&➀❹❷❶&'❹➈❽❻&❽❷➆-&4➈➅➂❘&➀❽➆❽&/➀&❷/➂&'❹➈❽❻ & ❝'❹➈❽❻❸&.❹➀❽➆❽
◗➃❹4.➈❸ L −1
❸➂4❸&/❽❸&.❻.❹➈➂'&❸➄❹4❻/❸&❸➂4❸G&➁❽.➂-& b b −1−1&❹❻.❹➈❽&,❹❼❸&❸4!➂, 
◗➃❹4.➈❸&❾4❹/,&.❹❽➀/❽-➄➄❹➈➅!/❘&.❹❸❹,❶&➃❹4❿❺&.❹'❽4❷ & ➁❽.➂-❸&➀❿&❹❻.❹➈❽G&➁❽.➂-& bb −−11&❹❻.❹➈❽&➆❹4❶❸&❸4!➂,&&H➁❽➀➆❸➂&&!❻4➂,
L

H➁❽➀➆❸➂&&!❻4➂,
❝➃❹4.➈❸&.❹❿❽/
4.❹❽,&4-!❸&➃❹4.➈❸&➀❹➀➅➂&./❽-➂&.❻❼,❹➂ &

_______________________________________________________________ _______________________________________________________________
©➉❽,❹!4➂&➀❹/'  ❛ ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T ©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T
.❽.❶4❷❸&❸!➂➆❸ .❽.❶4❷❸&❸!➂➆❸
ID (state)
for d= 0 to max-depth (iterative deepening)
solution ← DFS-L(state,d) ❽4/➄❽➀&➃❹4❿❺&➁➆&❷❹,➆➀&❹➄➀&4'➈/➂&!➂❹➆➀&'❹➈❽❻ 
if solution ≠ {} then return solution
❽➀➂❽❼➈❹/&➃❹4.➈&❻❽❼,➂&,❻❹4➀&'❹➈❽❻ 
.❽.❶4❷❸❸&❸!➂➆❸❸&K❹➈❽❻&❻.❹➈&.❹➄❹4.❽❸&❽➄'&./&,➀'➀&❽❷❿ 
(defparameter *max-depth* 100)
➃❹4.➈&➁/,&&&!➂❹➆&.➀,❶❸&➁➆&❸➀❽❻.&!➂❹➆➀&'❹➈❽❻&➀❽➆➈➂&➁.❽4❹❶➀/❸ 
(defun id (state) ❸/➀❸&❾❿❹&&!➂❹➆&.➀,❶❸&➁➆&!➂❹➆➀&'❹➈❽❻&➀➆➈❹➂&/-➂➄&❹➄❽/
(loop for d from 0 to *max-depth*
for solution = (dfs-l state d)
❸❽-4❼❽/,&!➂❹➆❸&.➀,❶❸&❷,➀➂❘&➁❽'❹➈❽❻❸&➃❽,&4,❷&4❿❹❺&❹➄❽/&➁.❽4❹❶➀/❸ 
when solution do (return-from id solution))) ◗.➂❷❹!❸
1 2 3 4

......

_______________________________________________________________ _______________________________________________________________
©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T ©➉❽,❹!4➂&➀❹/'  ❜ ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T

.❽4➈➅➂&/➂❶❹❷ ➁❽➆❹-❽,&&.❽.❶4❷❸&❸!➂➆❸
b=10❹&L=5&&❻❽➄➄  ➃❹4.➈❸&❾4❹/,&.❽4/➄❽➀&❸➄❽❸&➃❹4❿❺❸&.'❽4❷ 
❝,❻❹4➀&'❹➈❽❻  4.❹❽,&4-!❸&❹➄❽❸&/-➂➄'&➃❹4.➈❸ 
❹➄❽❸&❻.➈❽'&➁❽.➂-❸&4➈➅➂❹&.❽➀➂❽➅!➂&➃❹4❿❺&.'❽4❷& ❸'➆➂➀& L-1&❷➆&&.❹➂4❸&➀'&➁❽'❹➈❽❻❸&➀❿&&'❹➈❽❻&➃➂❺,&➁❽➂➀'➂ 
1+10+100+1000+10000+100000=111,111 ➁❽❺,❺❹,➂
.❽.❶4❷❸&❸!➂➆❸&'❹➈❽❻ ❾❿&➀❿&,4&❹➄❽/&❺❹,❺,❸&➁❽➀➆,&➁❽❺❿4.➂&➉➆&➀'&➁❽.➂-❸&,❹4'&➃❹❹❽❿ 
5*10=50&.❽➀➂❽➅!➂&➃❹4❿❺&.'❽4❷ ❹➄❽❸&❹❻.❹➈❽'&➁❽.➂-❸&4➈➅➂ 
6+50+400+3000+20000+100000=123,456&❻.➈❽ L ⋅ 1 + ( L − 1)b + ( L − 2)b 2 + L + 1b L
&❽➈&➃❹4❿❺&.'❽4❷&❹➄❼!❸&L&➀'&➃➂❺&.➀❷❶❸&.4❹➂.&4➂❹➀❿ ➀❿&&➁❽➂➆➈&L&❻.❹➈➂&'4❹'❸&➁❽-➆& L&➁❽❻.➈➂&& L&❹➄❽❸&➃❹4.➈❸&❾4❹/❘ &
◗➁❽➂➆➈&L-1&❻.❹➈➂&❸❽➄'❸&❸➂4,&.➂❹-

_______________________________________________________________ _______________________________________________________________
©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T ©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T
'❹➂❽➂
❝❽➀➂❽❼➈❹/&➃❹4.➈&/-➂❽'&❻❽❼,❸➀&❽❷❿ ❽➀➂❽➄❽➂&4❽❻➂&➀➆,&➃❹4.➈&./❽-➂
.➄❽❹➂➂&.4➂'➄&❹❻.❹➈&/➀&❾/&❹4-❹❽'&➁❽.➂-❸&.➂❽'4  Uniformcost search
❸➂❽'4❸&.➀❽❻.➂&❻!➀➄&❻❹.❽➈➀&.➂❹-❸  ,❻❹4➀&'❹➈❽❻&➀➆&❸❽-/❽4❹ &
❹4❹-❽&➃➂❺,&/➀❹&❹❻❹.❽➈&❽➄➈➀&.➂❹-&❽,❶➀&!❷,➄&➁❹❽➅❸&❽/➄.  ➃❽,&4,➆➂&➀❿➀&❽,❹❽❻&4❽❻➂&❸4❽❷❶➂❸&4❽❻➂&.❽-!➄❹➈,&'➂.'➂ 
./&➃❿❷➆➀&'❽&❻❹.❽➈➀&➃❽.➂➂❹&➁❽❽!&4,❿'&.➂❹-&.4-❽➂&❸4❹❷-❹4➈❸&➁/, & ➁❽.➂-
4.❹❽&➀❹❺❸&❸4❹❸➀&➆❽,-❽'&❾❿&➆❽,-➂❸ '➈❻➂&,❻❹4➀&'❹➈❽❻&4'/❿❘&❽➀➂❽➄❽➂&4❽❻➂&➀➆,&➃❹4.➈&/❹-➂➀&'❹➈❽❻❸&.4❼➂ &
◗❽➀➂❽➄❽➂&➁❽❷➆-&4➈➅➂&➀➆,&➃❹4.➈
❽➀➂❽➄❽➂&❹❽➀/&➀❹➀➅➂❸&4❽❻➂'&.➂❹-❸&❹➄❽❸&❻❹.❽➈➀&/,❸&.➂❹-❸ 
◗4.❹❽&,❹❼&➃❹4.➈&❹➄'❽❹&➃❿.❽❘&➃❹4.➈&/-➂➄&4'/❿&❸!❽➅➈➂&❸➄❽/&❸4❹❷-❹4➈❸ &
➀'&4❽❻➂❸➂&➀❹❷❶&➁❽❽!➀❻❸&➁❽➀❹➀➅➂❸&➀❿ &4❽❻➂&4'/❿&❸!❽➅➈➂&❸4❹❷-❹4➈❸ &
/-➂➄'&➃❹4.➈❸

_______________________________________________________________ _______________________________________________________________
©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T ©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T

Uniform-cost-search(state)
OPEN ← (node(state,NIL,0)) ; ➀❹➀➅➂&4❽❻➂&❸4❹❸&,-➂ Uniform cost&➁.❽4❹❶➀/
While OPEN ≠ ( )
next ← pop(OPEN)
push (next,CLOSE)
4❽❻➂&.❽-!➄❹➈&4,➆➂&.❽-!➄❹➈&❽➈❹➅&,-➂&❽.➀❻.❸&,-➂&❝❼➀!
if goalp(next.state) then return(trace(next))
for s in succ(next.state)
4.❹❽,&➀❹❺❸&➃❹4.➈❸&➀❹➀➅➂&&❝❼➀➈
if NOT find-state(s,CLOSE) then
new-cost← ←next.g+cost(next.state,s)
old-node ← find-state(s,OPEN)
if old-node ≠ {} then
if old-node.g > new-cost then
old-node.g ← new-cost
old-node.parent ← next
insert(old-node,OPEN -{old-node})
end
else
4❽❻➂❸&❽➈➀&➁❽/.➂❸&➁❹!➂➀&➅➄❿❸❹&'❷❻&.➂❹-&4-❽
insert(node(s,next,new-cost),OPEN,g)
end
end
end
return FAIL

_______________________________________________________________ _______________________________________________________________
©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T ©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T
uniform cost&'❹➈❽❻➀&/➂❶❹❷
➁❽.➂-❸&❻❹.❽➈&4❷➅&./&➁❽❶-❽➂&➁❽➀❹➀❻❸&&➁❽4,➆➂❸&❽4❽❻➂&➁❸&➁❽/➀➂❸&➁❽4➈➅➂❸
1

7 15 3 9
3 2 6
2 5 1 6 9 8 7 3 4
7 4 5

4 8 7 5 8 1 4
➁❽/➂12❻❹/❽➈26❷➅ 1
8 /9:26❽❻➂ 1

_______________________________________________________________ _______________________________________________________________
©➉❽,❹!4➂&➀❹/'  ❛ ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T ©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T

,❻❹4➀&'❹➈❽❻&.➂❹➆➀&Uniform-cost Uniform-cost&.❹➂➀'
,❻❹4➀&'❹➈❽❻➀&❹➂❿&.❹➄❹4➅❻❹&.❹➄❹4.❽&➁.❹/&Unifrom-cost&'❹➈❽❻➀  ❸➂➀'&❸➄❽/&Uniform-cost&➇➅❹➄&❽/➄.&/➀➀ 
❽➀➂❽➄❽➂&4❽❻➂&➀➆,&➃❹4.➈&./❽-➂&➀'&➃❹4.❽❸&➇➅❹➄ &
1/2 1
&❷❽➂.&❹➄❽❸&4,➆➂&4❽❻➂&&❽❼4➈&❸4!➂❿&,❻❹4➀&'❹➈❽❻&.❹/4➀&➃.❽➄ &
1/4 Goal

1/8

1/16

➁❽❽!➂❸&δ > 0&➁❽❽!&❝❽/➄.❸&./&➇❽➅❹➄&➁/


➁➀'&➁.❽4❹❶➀/❸&❽❺/& ∀s ∈ S, ∀s' ∈ succ( s )[cos t ( s, s' ) ≥ δ ]

_______________________________________________________________ _______________________________________________________________
©➉❽,❹!4➂&➀❹/'   ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T ©➉❽,❹!4➂&➀❹/'  ❜ ➁❽4❹❹➆&➁❽'❹➈❽❻&&❸➄❽,➀&/❹,➂&T

Potrebbero piacerti anche