首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Common LISP运行爬山搜索时出现的问题

使用Common LISP运行爬山搜索时出现的问题
EN

Stack Overflow用户
提问于 2020-02-15 14:27:49
回答 1查看 115关注 0票数 0

这是我的书中的代码,我需要使用它来使用我们预定义的节点运行爬山搜索。我还成功地运行了其他一些搜索功能,比如Best-first-- (“看起来”很相似)。在运行这个爬山搜索时,它无法运行。我是lisp的新手,所以我不明白为什么会发生这种情况。顺便说一句,我正在运行Allegro CL。函数如下;

代码语言:javascript
复制
    ;; A simple hill-climbing search example adapted from Winston & Horn

(setf (get 's 'neighbors) '(a b)
      (get 'a 'neighbors) '(s c d)
      (get 'b 'neighbors) '(s e)
      (get 'c 'neighbors) '(a d e)
      (get 'd 'neighbors) '(a c e)
      (get 'e 'neighbors) '(b c)
      (get 'f 'neighbors) '(d))
;; I added this for distance assuming that it uses straight line distance in order to 
   search.. not sure if that is correct    
(setf (get 's 'distance) 65
      (get 'a 'distance) 50
      (get 'b 'distance) 48
      (get 'c 'distance) 45
      (get 'd 'distance) 30
      (get 'e 'distance) 28
      (get 'f 'distance) 0)

(defun straight-line-distance (node-1 node-2)
    (let ((coordinates-1 (get node-1 'coordinates))
              (coordinates-2 (get node-2 'coordinates)))
           (sqrt (+ (expt (- (first coordinates-1)
                             (first coordinates-2))
                           2)
                     (expt (- (second coordinates-1)
                              (second coordinates-2))
                           2)))))

(defun closerp (path-1 path-2 target-node)
    (< (straight-line-distance (first path-1) target-node)
           (straight-line-distance (first path-2) target-node)))

(defun hill-climb (start finish &optional
                       (queue (list (list start))))
     (cond ((endp queue) nil)
       ((equal finish (first (first queue)))
           (reverse (first queue)))
       (t (hill-climb start finish
                      (append (sort (extend (first queue))
                                   #'(lambda (p1 p2) (closerp p1 p2 finish)))
                              (rest queue))))))

(defun extend (path)
    (print (reverse path))
    (mapcar #'(lambda (new-node) (cons new-node path))
             (remove-if #'(lambda (neighbor) (member neighbor path))
                        (get (first path) 'neighbors))))

im调用的函数看起来像这样的(hill-climb 's 'd)

我得到的错误如下;

代码语言:javascript
复制
Error: attempt to call `CLOSERP' which is an undefined function.
[condition type: UNDEFINED-FUNCTION]
EN

回答 1

Stack Overflow用户

发布于 2020-02-15 15:31:24

我通过编辑(defun straight-line-distance (node-1 node-2)函数修复了它。一定是出了什么差错。我重写了它,并使用了这个;

代码语言:javascript
复制
(defun straight-line-distance (node-1 node-2)
    (let ((coordinates-1 (get node-1 'coordinates))
                (coordinates-2 (get node-2 'coordinates)))
             (sqrt (+ (expt (- (first coordinates-1)
                               (first coordinates-2))
                             2)
                       (expt (- (second coordinates-1)
                                (second coordinates-2))
                             2)))))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60236435

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档