山登り法の検索を行うために変換しようとしている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
いますが、どのノードが最も近いかを判別する関数を作成できないようです。
助けてくれてありがとう!