2016-12-07 79 views
0

我試圖定義一個A *在口齒不清CLISP - 翻譯成僞實際Lisp代碼

僞代碼搜索算法可以發現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

1.請修復縮進(例如,使用emacs編輯您的代碼); 2.請準確粘貼錯誤信息文本; 3.請使用'let'而不是'setq'作爲局部變量。 – sds

回答

2

您正在使用的「擴展」 loop形式,所以你需要做的

(loop while ... 
    do ...) 

,而不是

(loop while ... 
    ...) 

這是什麼錯誤告訴你:它想要一個關鍵字while條件後,它找到了正文的第一條語句。

您可能要更仔細地檢查The LOOP Facility

PS。請綁定局部變量使用letloop,with,然後將它們設置爲setq。否則,你的變量是全球特價。

PPS。請正確縮進您的代碼;你可以使用Emacs。

+0

已經編輯我的問題!我會盡力謝謝! – xicocana

+0

是的,謝謝,我對你的錯誤的猜測是正確的,你錯過了身體前的「做」。 – sds