广度优先于统一成本LISP

时间:2014-09-30 22:06:10

标签: lisp artificial-intelligence common-lisp

我是LISP的初学者,我正在进行一项练习,要求我修改先呼吸,以便将其转换为统一成本搜索。

毋庸置疑,我非常困惑。我理解广度优先搜索和统一成本搜索如何工作的基础知识,但我无法实现它。

我有一些广度优先搜索的代码以及下面的节点和扩展方法的结构,但我在开发统一成本搜索时遇到了麻烦。

我知道它应该采用成本最低的路径,但我不确定如何实现它。我不太了解在整个地图上工作时应该如何保存所有这些路径和边缘(我正在使用的地图是罗马尼亚路线图,我确定谷歌将提出一份副本)

无论如何,我到目前为止的代码如下:

;The Problem: 
setq ex2 '(
             (Arad           ( (Zerind 75) (Sibiu 140) (Timisoara 118)                   ) )
             (Oradea         ( (Zerind 71) (Sibiu 151)                                   ) )
             (Zerind         ( (Oradea 71) (Arad 75)                                     ) )
             (Timisoara      ( (Lugoj 111) (Arad 118)                                    ) )
             (Lugoj          ( (Timisoara 111) (Medhadia 70)                             ) )
             (Mehadia        ( (Lugoj 70) (Drobeta 75)                                   ) )
             (Drobeta        ( (Mehadia 75) (Craiova 120)                                ) )
             (Sibiu          ( (Oradea 151) (Arad 140) (Rimnicu-Vilcea 80) (Fagaras 99)  ) )
             (Rimnicu-Vilcea ( (Sibiu 80) (Craiova 146) (Pitesti 97)                     ) )
             (Craiova        ( (Rimnicu-Vilcea 146) (Drobeta 120) (Pitesti 138)          ) )
             (Fagaras        ( (Sibiu 99) (Bucharest 211)                                ) )
             (Pitesti        ( (Rimnicu-Vilcea 97) (Craiova 138) (Bucharest 101)         ) )
             (Bucharest      ( (Fagaras 211) (Pitesti 101) (Giurgiu 90) (Urziceni 85)    ) )
             (Giurgiu        ( (Bucharest 90)                                            ) )
             (Neami          ( (Iasi 87)                                                 ) )
             (Iasi           ( (Neami 87) (Vasiui 92)                                    ) )
             (Vasiui         ( (Iasi 92) (Urziceni 142)                                  ) )
             (Urziceni       ( (Bucharest 85) (Hirsova 98) (Vasiui 142)                  ) )
             (Hirsova        ( (Urziceni 98) (Eforie 86)                                 ) )
             (Eforie         ( (Hirsova 86)                                              ) )
) )

(setq problem3 (list 'Arad 'Bucharest ex2))

 ;return the start state from a problem
(defun initial-state (problem)
   (car problem))

; return the goal state from a problem
(defun goal-state (problem)
   (cadr problem))

; return the successor list from a problem
(defun adj-list (problem)
   (caddr problem))

; construct a node from a state and a parent state
(defun make-node (state parent value depth)
   (list state parent value depth))

; get the state from a node
(defun get-state (node)
   (car node))

; get the parent state from a node
(defun get-parent (node)
   (cadr node))

; get the value from a node
(defun get-value (node)
   (caddr node))

; get the depth from a node
(defun get-depth (node)
   (cadddr node))

 ;determine if a node represents the goal of a problem
(defun goal-test (problem node)
   (cond ((equal (goal-state problem) (get-state node)) T)
         (T nil)))

; create a list of nodes which represents the states adjacent to the state represented by node
(defun expand (node problem)
   (expand-aux node (cadr (assoc (get-state node) (adj-list problem))) nil ))

; helper function for expand
(defun expand-aux (node states L)
   (cond ((null states) L)
         (T (expand-aux node 
                        (cdr states) 
                        (append L (list (make-node (caar states)
                                        (get-state node)
                                        (+ (get-value node) (cadar states))
                                        (+ (get-depth node) 1) )))))))

; recover the solution path from the final (goal) node and the list of expanded nodes
(defun solution (node nodes)
   (solution-aux node nodes nil))

; helper function for solution
(defun solution-aux (node nodes L &aux temp)
   (setq temp (parent node nodes))
   (cond ((equal temp nil) (cons (get-state node)  L))
         (T (solution-aux temp nodes (cons (get-state node) L)))))

; find the parent node of a node in a list of nodes
(defun parent (node nodes)
   (assoc (get-parent node) nodes))

; determine if a node's state is already the state of a node in nodes
(defun already-present (node nodes)
   (cond ((assoc (get-state node) nodes) T)
         (T nil)))

;;;;; Breadth-first Search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun breadth (problem)
   (breadth-aux problem                                              ; the problem
                (list (make-node (initial-state problem) nil 0 0))   ; the fringe list
                nil))                                                ; the closed list

(defun breadth-aux (problem fringe closed)
   (cond ((null fringe)                          ; no place else to look, so fail
              'Failure)

     ((goal-test problem (car fringe))       ; we have reach the goal, so return solution
              (solution (car fringe) closed))

     ((already-present (car fringe) closed)  ; this state was already reached, so go
              (breadth-aux problem               ; on without processing it
                           (cdr fringe)
                           closed))

     (T                                      ; process this node and go on
              (breadth-aux problem
                           (append (cdr fringe) (expand (car fringe) problem))
                           (cons (car fringe) closed)))))

;;;;;;; Uniform-cost Search ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun uniform-cost (problem)
  (uniform-aux problem
               (list (make-node (initial-state problem) nil 0 0))
               nil))

(defun uniform-aux (problem fringe closed)
  (cond ((null fringe) 'Failure)

        ((goal-test problem (car fringe))
         (solution (car fringe) closed))

        ((already-present (car fringe) closed)
         (uniform-aux problem (cdr fringe) closed))

        (T (uniform-aux problem
                        (append (cdr fringe) (expand 
                                              (car fringe) 
                                              problem))
                           (cons (car fringe) closed)))))

0 个答案:

没有答案