;;; -*- Mode: Lisp; Syntax: Common-Lisp; -*- File: search/agents/online-agents.lisp

;;;; Online Search Agents: Online DFS and LRTA*

;;; An online agent does not know the results of its actions until it
;;; tries them.  Its percept is the current state.  It uses the problem
;;; only for what an online agent may know in advance: the actions
;;; applicable in a state (the CARs of SUCCESSORS), the goal test, the
;;; step cost and the heuristic H-COST.  The agent programs return nil
;;; (stop) at a goal.

;;;; Online search problems given as an explicit graph

(defstructure (online-graph-problem (:include problem))
  "A problem on an explicit graph: an alist from each state to an alist of
  (action . next-state) pairs, with an alist of heuristic estimates."
  (graph nil)
  (h-costs nil))

(defmethod successors ((problem online-graph-problem) state)
  (cdr (assoc state (online-graph-problem-graph problem) :test #'equal)))

(defmethod h-cost ((problem online-graph-problem) state)
  (or (cdr (assoc state (online-graph-problem-h-costs problem) :test #'equal))
      0))

(defun online-actions (problem state)
  "The actions applicable in STATE."
  (mapcar #'car (successors problem state)))

(defun make-one-dim-state-space-problem (&key (initial-state 'state3))
  "The one-dimensional state space of [Fig. 4.23], with goal State5."
  (make-online-graph-problem
   :initial-state initial-state
   :goal 'state5
   :graph '((state1 (right . state2))
	    (state2 (right . state3) (left . state1))
	    (state3 (right . state4) (left . state2))
	    (state4 (right . state5) (left . state3))
	    (state5 (right . state6) (left . state4))
	    (state6 (left . state5)))
   :h-costs '((state1 . 8) (state2 . 9) (state3 . 2)
	      (state4 . 2) (state5 . 4) (state6 . 3))))

(defun run-online-agent (program problem &optional (max-steps 100))
  "Move an online agent PROGRAM through the states of PROBLEM, giving it
  the current state as percept, until it stops.  Return the final state
  and the list of actions taken."
  (let ((state (problem-initial-state problem))
	(actions nil))
    (dotimes (i max-steps)
      (let ((action (funcall program state)))
	(when (null action) (return))
	(push action actions)
	(setf state (cdr (assoc action (successors problem state))))))
    (values state (nreverse actions))))

;;;; Online depth-first search

(defun make-online-dfs-agent-program (problem)
  "Return the program of an online depth-first exploration agent, which
  backtracks physically and so needs reversible actions. [Fig. 4.20]"
  (let ((result (make-hash-table :test #'equal))
	(untried (make-hash-table :test #'equal))
	(unbacktracked (make-hash-table :test #'equal))
	(s nil)
	(a nil))
    #'(lambda (s1)
	(if (goal-test problem s1)
	    (setf a nil)
	  (progn
	    (unless (nth-value 1 (gethash s1 untried))
	      (setf (gethash s1 untried) (online-actions problem s1)))
	    (when s
	      (setf (gethash (list s a) result) s1)
	      (push s (gethash s1 unbacktracked)))
	    (cond ((gethash s1 untried)
		   (setf a (pop (gethash s1 untried))))
		  ((null (gethash s1 unbacktracked))
		   (setf a nil))
		  (t ;; an action b such that result[s1, b] = POP(unbacktracked[s1])
		   (let ((back (pop (gethash s1 unbacktracked))))
		     (setf a (find-if #'(lambda (b)
					  (equal (gethash (list s1 b) result)
						 back))
				      (online-actions problem s1))))))))
	(setf s s1)
	a)))

;;;; Learning real-time A*

(defun make-lrta*-agent-program (problem)
  "Return the program of an LRTA* agent, which learns the cost-to-goal
  estimate H of the states it visits.  Untried actions are assumed to lead
  straight to the goal at the cost of the current estimate. [Fig. 4.23]"
  (let ((result (make-hash-table :test #'equal))
	(h (make-hash-table :test #'equal))
	(s nil)
	(a nil))
    (labels ((lrta*-cost (s a s1)
	       (if (null s1)
		   (h-cost problem s)
		 (+ (lrta*-step-cost problem s a s1) (gethash s1 h))))
	     (best-cost (s)
	       (reduce #'min (online-actions problem s)
		       :key #'(lambda (b)
				(lrta*-cost s b (gethash (list s b) result)))))
	     (best-action (s)
	       (the-smallest #'(lambda (b)
				 (lrta*-cost s b (gethash (list s b) result)))
			     (online-actions problem s))))
      #'(lambda (s1)
	  (if (goal-test problem s1)
	      (setf a nil)
	    (progn
	      (unless (nth-value 1 (gethash s1 h))
		(setf (gethash s1 h) (h-cost problem s1)))
	      (when s
		(setf (gethash (list s a) result) s1)
		(setf (gethash s h) (best-cost s)))
	      (setf a (best-action s1))))
	  (setf s s1)
	  a))))

(defun lrta*-step-cost (problem s a s1)
  "The cost of the step from S to S1 by A, according to PROBLEM."
  (edge-cost problem (make-node :state s) a s1))
