好的,我有一个Lisp实现的BFS(广度优先搜索),我想把它转换成做爬山算法。
这是我的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
函数来扩展最接近的节点,但我似乎无法编写一个确定哪个节点最接近的函数。谢谢帮助!