Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Search 口齿不清-爬山_Search_Graph_Lisp_Breadth First Search_Hill Climbing - Fatal编程技术网

Search 口齿不清-爬山

Search 口齿不清-爬山,search,graph,lisp,breadth-first-search,hill-climbing,Search,Graph,Lisp,Breadth First Search,Hill Climbing,好的,我有一个BFS的Lisp实现,我正试图将其转换为爬山搜索 下面是我的BFS代码的样子: ; The list of lists is the queue that we pass BFS. the first entry and ; every other entry in the queue is a list. BFS uses each of these lists and ; the path to search. (defun shortest-path (star

好的,我有一个BFS的Lisp实现,我正试图将其转换为爬山搜索

下面是我的BFS代码的样子:

; The list of lists is the queue that we pass BFS.  the first entry and 
; every other entry in the queue is a list.  BFS uses each of these lists and 
; the path to search.  

(defun shortest-path (start end net)   
  (BFS end (list (list start)) net))

;We pass BFS the end node, a queue containing the starting node and the graph 
;being searched(net)   

(defun BFS (end queue net)
  (if (null queue) ;if the queue is empty BFS has not found a path so exit
      nil
      (expand-queue end (car queue) (cdr queue) net)))

(defun expand-queue (end path queue net)
  (let ((node (car path)))
    (if (eql node end)   ; If the current node is the goal node then flip the path
                         ; and return that as the answer
        (reverse path)
        ; otherwise preform a new BFS search by appending the rest of 
        ; the current queue to the result of the new-paths function
        (BFS end (append queue (new-paths path node net)) net))))

; mapcar is called once for each member of the list new-paths is passed
; the results of this are collected into a list
(defun new-paths (path node net)
  (mapcar #'(lambda (n) (cons n path))
         (cdr (assoc node net))))
现在,我知道不必像在BFS中那样总是展开左侧节点,我需要展开看起来最接近目标状态的节点。
我使用的图表如下所示:

(a (b 3) (c 1))  
(b (a 3) (d 1))
我有一个转换函数来实现上述BFS实现,但现在我需要使用此图形格式将其转换为爬山

我只是不知道从哪里开始,一直在尝试,但都无济于事。我知道我主要需要更改
expand queue
函数来扩展最近的节点,但我似乎无法创建函数来确定哪个节点最近


谢谢你的帮助

在列表末尾追加内容是错误的。这是一个列表中成本最高的操作。 复制整个列表,然后追加另一个列表。您可以在递归过程中使用它,这意味着每次展开节点以创建新路径时都会使用它

如果您将项目放在队列中,则需要查看执行此操作的顺序。使用“宽度优先”时,在移动到下一个级别之前,可以访问级别中的每个节点。爬山需要使用加权函数对候选人进行“最佳”排序。因此,您需要某种函数来计算当前节点和下一个候选节点的数值。然后,您需要对候选项进行排序,并首先展开“最佳”节点。对于后进先出(后进先出)队列,这意味着最有希望的节点需要被推到最后,以便它将是第一个被扩展的节点。请注意,后进先出队列非常适合于单链接列表。先进先出(FIFO)并非如此


计算机科学的一个典型概念是数据抽象。如果后进先出队列是这样一种数据结构,则需要定义MAKE-LIFO-QUEUE、EMPTY-QUEUE-P等函数。。。然后使用这些函数来代替LIST、NULL和其他函数,它们使数据结构的用途更加清晰。这会使代码变长,但由于Lisp列表是通用的,并且可以(ab-)用于各种使用场景,因此仅查看列表操作并不能明确它们的意图。对于更复杂的图算法,这一点尤为重要

我认为函数(输入、输出、算法)和数据类型的文档非常棒。你怎么认为?例如,我不知道list of start应该是什么意思,也不知道为什么它只是list函数的两个而不是五个应用程序。依此类推。在用单链表实现的队列末尾追加是不好的。很抱歉代码不明确,我添加了一些注释来澄清它。