Reputation: 21
This is code from my book that I need to use to run a hill-climb search using our predefined nodes. I was able to run a few other search functions successfully such as Best-first-- (which "looks" similar). Upon running this hill-climb search, it failed to run. I'm new to lisp so I don't understand why this is happening. Just as a side note Im running Allegro CL. The function is below;
;; A simple hill-climbing search example adapted from Winston & Horn
(setf (get 's 'neighbors) '(a b)
(get 'a 'neighbors) '(s c d)
(get 'b 'neighbors) '(s e)
(get 'c 'neighbors) '(a d e)
(get 'd 'neighbors) '(a c e)
(get 'e 'neighbors) '(b c)
(get 'f 'neighbors) '(d))
;; I added this for distance assuming that it uses straight line distance in order to
search.. not sure if that is correct
(setf (get 's 'distance) 65
(get 'a 'distance) 50
(get 'b 'distance) 48
(get 'c 'distance) 45
(get 'd 'distance) 30
(get 'e 'distance) 28
(get 'f 'distance) 0)
(defun straight-line-distance (node-1 node-2)
(let ((coordinates-1 (get node-1 'coordinates))
(coordinates-2 (get node-2 'coordinates)))
(sqrt (+ (expt (- (first coordinates-1)
(first coordinates-2))
2)
(expt (- (second coordinates-1)
(second coordinates-2))
2)))))
(defun closerp (path-1 path-2 target-node)
(< (straight-line-distance (first path-1) target-node)
(straight-line-distance (first path-2) target-node)))
(defun hill-climb (start finish &optional
(queue (list (list start))))
(cond ((endp queue) nil)
((equal finish (first (first queue)))
(reverse (first queue)))
(t (hill-climb start finish
(append (sort (extend (first queue))
#'(lambda (p1 p2) (closerp p1 p2 finish)))
(rest queue))))))
(defun extend (path)
(print (reverse path))
(mapcar #'(lambda (new-node) (cons new-node path))
(remove-if #'(lambda (neighbor) (member neighbor path))
(get (first path) 'neighbors))))
The function im calling looks like this (hill-climb 's 'd)
The errors that I get are as followed;
Error: attempt to call `CLOSERP' which is an undefined function.
[condition type: UNDEFINED-FUNCTION]
Upvotes: 0
Views: 308
Reputation: 21
I fixed it by editing the (defun straight-line-distance (node-1 node-2)
function. There must have been an error... I rewrote it and used this;
(defun straight-line-distance (node-1 node-2)
(let ((coordinates-1 (get node-1 'coordinates))
(coordinates-2 (get node-2 'coordinates)))
(sqrt (+ (expt (- (first coordinates-1)
(first coordinates-2))
2)
(expt (- (second coordinates-1)
(second coordinates-2))
2)))))
Upvotes: 0