;;; -*- Mode: Lisp; Syntax: Common-Lisp; -*- File: uncertainty/agents/decision-agents.lisp

;;;; Decision-Theoretic Agents (Chapters 12 and 16)

;;; A belief state is an alist distribution of (state . probability).  The
;;; models are functions: (TRANSITION s a s2) is P(s2 | s, a), (SENSOR e s)
;;; is P(e | s), and (UTILITY a s) is the utility of the outcome s of the
;;; action a.

(defun bayes-update (belief likelihood)
  "Condition BELIEF on evidence whose likelihood in state s is (LIKELIHOOD s)."
  (normalize-distribution
   (mapcar #'(lambda (pair) (cons (car pair) (* (cdr pair) (funcall likelihood (car pair)))))
           belief)))

(defun predict-belief (belief action transition)
  "The distribution of the state after ACTION, from the belief state."
  (mapcar #'(lambda (pair)
              (let ((s2 (car pair)))
                (cons s2 (sum belief #'(lambda (b)
                                         (* (cdr b) (funcall transition (car b) action s2)))))))
          belief))

(defun expected-utility (action belief utility)
  "The expected utility of ACTION, when the outcome is distributed as BELIEF."
  (sum belief #'(lambda (pair) (* (cdr pair) (funcall utility action (car pair))))))

(defun max-expected-utility (actions belief utility)
  (reduce #'max (mapcar #'(lambda (a) (expected-utility a belief utility)) actions)))

;;;; A Decision-Theoretic Agent [Figure 12.1]

(defun make-dt-agent-program (&key prior actions transition sensor utility)
  "Return an agent program that keeps a belief state, updates it with the
  last action and the new percept, and selects the action with the highest
  expected utility, given the distribution of the outcomes of each action."
  (let ((belief prior)
        (action nil))
    #'(lambda (percept)
        (setf belief (bayes-update (if action
                                       (predict-belief belief action transition)
                                     belief)
                                   #'(lambda (s) (funcall sensor percept s))))
        (setf action (the-biggest #'(lambda (a)
                                      (expected-utility
                                       a (predict-belief belief a transition) utility))
                                  actions)))))

;;;; Value of Information and an Information-Gathering Agent [Figure 16.9]

(defstruct evidence-variable
  name values
  likelihood                  ; (likelihood value s) = P(variable = value | s)
  (cost 0))

(defun value-of-perfect-information (variable belief actions utility)
  "VPI(E_j): the expected gain in utility from observing VARIABLE before
  acting, over acting now, given the belief state."
  (- (sum (evidence-variable-values variable)
          #'(lambda (value)
              (let* ((joint (mapcar #'(lambda (pair)
                                        (cons (car pair)
                                              (* (cdr pair)
                                                 (funcall (evidence-variable-likelihood variable)
                                                          value (car pair)))))
                                    belief))
                     (p-value (sum joint #'cdr)))
                (if (zerop p-value)
                    0
                  (* p-value (max-expected-utility
                              actions (normalize-distribution joint) utility))))))
     (max-expected-utility actions belief utility)))

(defun make-information-gathering-agent (&key belief actions utility evidence)
  "Return an agent program for a one-shot decision whose percepts are
  observations (name . value) of the EVIDENCE variables.  It requests the
  variable with the highest ratio of value of information to cost, if its
  value exceeds its cost, and otherwise returns the best action."
  #'(lambda (percept)
      (when percept
        (let ((variable (find (car percept) evidence :key #'evidence-variable-name)))
          (setf belief (bayes-update belief
                                     #'(lambda (s)
                                         (funcall (evidence-variable-likelihood variable)
                                                  (cdr percept) s)))
                evidence (remove variable evidence))))
      (flet ((vpi (variable)
               (value-of-perfect-information variable belief actions utility)))
        (let ((j (and evidence
                      (the-biggest #'(lambda (v) (/ (vpi v) (evidence-variable-cost v)))
                                   evidence))))
          (if (and j (> (vpi j) (evidence-variable-cost j)))
              (list 'request (evidence-variable-name j))
            (the-biggest #'(lambda (a) (expected-utility a belief utility))
                         actions))))))
