我正在尝试在lisp中定义A *搜索算法
可以找到伪代码here。
这是我到目前为止所做的:
;;; A*
(defun a* (problem)
;;;;;;;;;;;;;;;;;;;;;;;;;;; cameFrom -> Parent do node
;;;;;;;;;;;;;;;;;;;;;;;;;;; gScore -> node-g
;;;;;;;;;;;;;;;;;;;;;;;;;;; fScore -> node-g + node-h
(setq closedSet '())
(setq openSet (list (make-node :state (problem-initial-state problem))))
(setq listaVizinhos '())
(loop while(not(eq openSet nil))
(setq tamanho_openS (list-length (openSet))) ;
(setq current (nth 0 openSet)) ;
(dotimes (i tamanho_openS) ;PARA ENCONTRAR O NODE COM MENOR FSCORE
(if (< (+ (node-g (nth i openSet)) (node-h (nth i openSet))) (+ (node-g current) (node-h current))) ; CURRENT
(setq current (nth i openSet)))) ;
(if (funcall (problem-fn-isGoal problem) (node-state current)) (return-from a* (solucao current))) ; caso current seja solucao -> retorna-o para a funcao solucao (que cria a solucao pedida)
(remove current openSet) ; retira o curretn da lista dos abertos
(append (list(current)) closedSet) ; introduz curretn na lista dos fechados
(setf estadosVisinhos (nextStates (node-state current))) ; nextestates de current-state
(setf tamanho_estadosVizinhos (list-length (estadosVisinhos)))
(dotimes (i tamanho_estadosVizinhos) ;
(setf visinho (make-node :parent current :state (nth i estadosVisinhos))) ;PARA CRIAR LISTA COM TODOS NODES VIZINHOS DE CURRENT
(append (list(visinho)) listaVizinhos)) ; LISTAVIZINHOS
(loop for vizinho in listaVizinhos do
(if (member vizinho closedSet) (continue))
(setq tentative_gScore (+ (node-g current) (dist_between current vizinho)))
(if (not(member vizinho closedSet)) (append (list(vizinho)) openSet)) ;
(if (>= (tentative_gScore) (node-g vizinho)) (continue)) ; MAYBE CONDS AQUI
(setq (node-g vizinho) tentative_gScore)
(setq (node-f vizinho) (+ (node-g vizinho) (compute-heuristic (node-state vizinho))))
)
)
(return-from a* nil))
我的节点结构是:
;;; Definition of a search node
;;; * parent - the parent node
;;; * state - the state of the node
;;; * f - estimate of the cost
;;; * g - cost from the initial node to the current node
;;; * h - estimate of the cost from the current node to the nearest goal
(defstruct node
parent
state
f
g
h)
在我的翻译中,我使用其他功能,但我已经测试过并且是正确的。
当我尝试编译我的代码时,我收到语法错误,但我不知道在哪里..
编辑: 错误讯息:
LOOP: illegal syntax near (setq tamanho_openS (list-length (openSet))) in loop (and then the terminal prints the entire loop)
答案 0 :(得分:2)
您正在使用&#34;扩展&#34; loop表单,因此您需要执行
(loop while ...
do ...)
而不是
(loop while ...
...)
错误告诉你:它需要while
条件后的关键字,并找到了正文的第一个语句。
您可能需要更仔细地审核The LOOP Facility。
PS。在使用let
进行设置之前,请使用with
或loop
,setq
绑定本地变量。否则你的变量是全局特价。
PPS。请正确缩进您的代码;你可以使用Emacs。