;;; -*- Mode: Lisp; Syntax: Common-Lisp; -*- File: planning/algorithms/state-space.lisp

;;;; Forward and Backward State-Space Search for Planning

;;; A planning problem becomes a search problem (a subtype of PROBLEM, see
;;; search/algorithms/problems.lisp), so that any of the search algorithms,
;;; e.g. A*-SEARCH or BREADTH-FIRST-SEARCH, can solve it [Sec. 11.2].  The
;;; actions of the search problem are the names of the ground actions.  Both
;;; heuristics are the level of the goal in the relaxed planning graph that
;;; ignores the delete lists, which never overestimates [Sec. 11.3].

;;;; Forward (Progression) Search

(defstructure (forward-planning-problem
	       (:include problem)
	       (:constructor create-forward-planning-problem))
  "Search forward from the initial state of a planning problem [Sec. 11.2.1]."
  (planning-problem nil)
  (actions nil))

(defun make-forward-planning-problem (planning-problem)
  "Turn a planning problem into a search problem in the space of states."
  (create-forward-planning-problem
   :initial-state (planning-canonical (planning-problem-initial planning-problem))
   :planning-problem planning-problem
   :actions (planning-ground-actions planning-problem)))

(defmethod successors ((problem forward-planning-problem) state)
  "The applicable ground actions, with the states they lead to."
  (let ((result nil))
    (dolist (action (forward-planning-problem-actions problem) (nreverse result))
      (when (planning-applicable-p action state)
	(push (cons (action-schema-name action) (planning-apply action state))
	      result)))))

(defmethod goal-test ((problem forward-planning-problem) state)
  (planning-goal-test (forward-planning-problem-planning-problem problem) state))

(defmethod h-cost ((problem forward-planning-problem) state)
  "The level of the goal in the relaxed planning graph built from STATE."
  (planning-max-level
   (planning-problem-goal (forward-planning-problem-planning-problem problem))
   (planning-atom-levels state (forward-planning-problem-actions problem))))

(defun forward-planning (planning-problem &optional (algorithm 'A*-search))
  "Solve a planning problem by forward state-space search with ALGORITHM.
  Return the plan (a list of action names) and the solution node."
  (let ((node (funcall algorithm (make-forward-planning-problem planning-problem))))
    (values (solution-actions node) node)))

;;;; Backward (Regression) Search

(defstructure (backward-planning-problem
	       (:include problem)
	       (:constructor create-backward-planning-problem))
  "Search backward from the goal of a planning problem, through sets of
  literals that describe sets of states [Sec. 11.2.2]."
  (planning-problem nil)
  (actions nil)
  (levels nil))

(defun make-backward-planning-problem (planning-problem)
  "Turn a planning problem into a search problem in the space of subgoals."
  (let ((actions (planning-ground-actions planning-problem))
	(initial (planning-canonical (planning-problem-initial planning-problem))))
    (create-backward-planning-problem
     :initial-state (planning-canonical (planning-problem-goal planning-problem))
     :planning-problem planning-problem
     :actions actions
     :levels (planning-atom-levels initial actions))))

(defun planning-relevant-p (action subgoal)
  "Is ACTION relevant to SUBGOAL: does it achieve one of its literals
  without undoing any other?"
  (let ((effect (action-schema-effect action)))
    (and (some #'(lambda (literal) (member literal subgoal :test #'equal)) effect)
	 (notany #'(lambda (literal)
		     (member (planning-negate literal) subgoal :test #'equal))
		 effect))))

(defun planning-regress (action subgoal)
  "The subgoal g' = (g - EFFECT(a)) + PRECOND(a), or nil if it is
  inconsistent (it contains a literal and its negation)."
  (let ((new (planning-canonical
	      (append (action-schema-precond action)
		      (set-difference subgoal (action-schema-effect action)
				      :test #'equal)))))
    (if (some #'(lambda (literal)
		  (member (planning-negate literal) new :test #'equal))
	      new)
	nil
      (list new))))

(defmethod successors ((problem backward-planning-problem) subgoal)
  "The relevant ground actions, with the subgoals regressed through them."
  (let ((result nil))
    (dolist (action (backward-planning-problem-actions problem) (nreverse result))
      (when (planning-relevant-p action subgoal)
	(let ((new (planning-regress action subgoal)))
	  (when new
	    (push (cons (action-schema-name action) (first new)) result)))))))

(defmethod goal-test ((problem backward-planning-problem) subgoal)
  "Is the subgoal satisfied by the initial state?"
  (planning-satisfies-p
   (planning-problem-initial (backward-planning-problem-planning-problem problem))
   subgoal))

(defmethod h-cost ((problem backward-planning-problem) subgoal)
  "The level of the subgoal in the relaxed planning graph built from the
  initial state."
  (planning-max-level subgoal (backward-planning-problem-levels problem)))

(defun backward-planning (planning-problem &optional (algorithm 'A*-search))
  "Solve a planning problem by backward state-space search with ALGORITHM.
  Return the plan (a list of action names, in execution order) and the
  solution node."
  (let ((node (funcall algorithm (make-backward-planning-problem planning-problem))))
    (values (reverse (solution-actions node)) node)))
