;;; -*- Mode: Lisp; Syntax: Common-Lisp; -*- File: logic/algorithms/satplan.lisp

;;;; SATPlan: Planning as Satisfiability

(defun satplan (init transition goal t-max &key (sat-solver #'dpll-satisfiable))
  "Find a sequence of actions from INIT to GOAL of at most T-MAX steps, by
  translating the problem into a propositional sentence for each horizon
  0, 1, ..., T-MAX and calling SAT-SOLVER on it. [Fig. 7.22]
  TRANSITION is a list of (state . ((action . next-state) ...)) entries;
  states and actions are any objects, compared with EQUAL.  GOAL is a state,
  or a predicate on states that is true of the goal states.  Return the list
  of actions, or NIL if there is no plan (or if INIT is already a goal)."
  (dotimes (horizon (1+ t-max) nil)
    (multiple-value-bind (sentence action-symbols)
	(satplan-translate init transition goal horizon)
      (let ((model (funcall sat-solver sentence)))
	(when model
	  (return (satplan-extract-solution model action-symbols)))))))

(defun satplan-translate (init transition goal horizon)
  "Return a CNF sentence that is satisfiable iff there is a plan of exactly
  HORIZON steps, and the list of (time action symbol) triples of its action
  symbols."
  (let* ((states (let ((states (adjoin init (satplan-states transition)
				       :test #'equal)))
		   (if (functionp goal)
		       states
		     (adjoin goal states :test #'equal))))
	 (state-symbols (make-hash-table :test #'equal)) ; (state . time) -> symbol
	 (counter 0)
	 (clauses nil)
	 (action-symbols nil))
    (flet ((state-symbol (s time) (gethash (cons s time) state-symbols)))
      ;; A proposition for each state at each time
      (dolist (s states)
	(dotimes (time (1+ horizon))
	  (setf (gethash (cons s time) state-symbols)
		(make-symbol (format nil "S~D" (incf counter))))))
      ;; Initial state and goal state(s)
      (push (state-symbol init 0) clauses)
      (push (disjunction
	     (mapcar #'(lambda (s) (state-symbol s horizon))
		     (if (functionp goal)
			 (remove-if-not goal states)
		       (list goal))))
	    clauses)
      ;; A proposition for each transition at each time: it implies its
      ;; state at that time and its next state at the next time
      (setf counter 0)
      (dotimes (time horizon)
	(let ((at-time nil))
	  (dolist (entry transition)
	    (dolist (a+s (cdr entry))
	      (let ((tr (make-symbol (format nil "T~D" (incf counter)))))
		(push tr at-time)
		(push (list time (car a+s) tr) action-symbols)
		(push `(or (not ,tr) ,(state-symbol (car entry) time)) clauses)
		(push `(or (not ,tr) ,(state-symbol (cdr a+s) (1+ time)))
		      clauses))))
	  ;; Exactly one transition at each time
	  (push (disjunction at-time) clauses)
	  (loop for (tr1 . others) on at-time do
		(dolist (tr2 others)
		  (push `(or (not ,tr1) (not ,tr2)) clauses)))))
      ;; Exactly one state at each time
      (dotimes (time (1+ horizon))
	(let ((at-time (mapcar #'(lambda (s) (state-symbol s time)) states)))
	  (push (disjunction at-time) clauses)
	  (loop for (s1 . others) on at-time do
		(dolist (s2 others)
		  (push `(or (not ,s1) (not ,s2)) clauses)))))
      (values (cons 'and (nreverse clauses)) action-symbols))))

(defun satplan-states (transition)
  "All the states in a transition model, sources and destinations."
  (let ((states nil))
    (dolist (entry transition)
      (pushnew (car entry) states :test #'equal)
      (dolist (a+s (cdr entry))
	(pushnew (cdr a+s) states :test #'equal)))
    (nreverse states)))

(defun satplan-extract-solution (model action-symbols)
  "The actions whose transition symbols are true in the model, by time."
  (let ((model (if (listp model) model nil)))
    (mapcar #'second
	    (sort (remove-if-not #'(lambda (triple)
				     (cdr (assoc (third triple) model)))
				 action-symbols)
		  #'< :key #'first))))
