I am trying to modify an existing Hill-climb function, which takes two node names (such as A and E), and has an optional parameter which is used recursively (a queue). I'm trying to define a function 'cheaper' that evaluates if one path is cheaper than another. Also, instead of one goal node, I'm trying to pass a list of goal nodes, which the function, upon reaching one of those nodes, stops evaluating.
The problem is my function won't return anything except the start node I've input and an empty list.
Here is my network/graph and associated costs:
(setf (get 's 'coordinates) '(0 3)
(get 'a 'coordinates) '(4 6)
(get 'b 'coordinates) '(7 6)
(get 'c 'coordinates) '(11 9)
(get 'd 'coordinates) '(2 0)
(get 'e 'coordinates) '(9 2)
(get 'f 'coordinates) '(11 3))
(setf (get 's 'cost) 0
(get 'a 'cost) 16
(get 'b 'cost) 4
(get 'c 'cost) 10
(get 'd 'cost) 5
(get 'e 'cost) 12
(get 'f 'cost) 14)
And here is my modified Hill-climb function:
(defun hill-climb (start finish &optional (queue (list (list start))))
(cond ((endp queue) nil)
((member (first (first queue)) finish)
(reverse (first queue)))
(t (hill-climb start finish (append (sort (extend (first queue))
#'(lambda (p1 p2)
(cheaper p1 p2
finish)))
(rest queue))))))
Finally, here are the 'cost' and 'cheaper' functions:
(defun cost (path)
(apply '+ (mapcar #'(lambda (x) (get x 'cost)) path)))
(defun cheaper (p1 p2)
(< (cost p1)
(cost p2)))
EDIT: Sorry, and here's 'extend':
(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))))