;;; -*- Mode: Lisp; Syntax: Common-Lisp; -*- File: multiagent/test-multiagent.lisp

;;;; Test cases for multiagent decision making

(deftest multiagent
  "The doubles tennis problem (Figure 18.1): the two joint plans of"
  "Section 18.1 both return the ball and leave an actor at the net."
  ((setq tennis (make-doubles-tennis-problem)))
  ((joint-plan-solves? tennis (merge-actor-plans '(((go a right-baseline) (hit a ball))
                                                   ((noop b) (noop b)))))
   *)
  ((joint-plan-solves? tennis (merge-actor-plans '(((go a left-net) (noop a))
                                                   ((go b right-baseline) (hit b ball)))))
   *)
  "If A follows the first plan and B the second, both hit the ball at once."
  ((joint-plan-result tennis (merge-actor-plans '(((go a right-baseline) (hit a ball))
                                                  ((go b right-baseline) (hit b ball)))))
   :fail)
  "A central planner finds a joint plan of two steps."
  ((find-joint-plan tennis 2)
   (and (= (length *) 2) (joint-plan-solves? tennis *)))
  "The prisoner's dilemma (Section 18.2.1): testify (0) is a dominant"
  "strategy for both Ali and Bo, and (testify, testify) the only equilibrium."
  ((setq ali '((-5 0) (-10 -1)) bo '((-5 -10) (0 -1))))
  ((dominant-strategy ali) 0)
  ((dominant-strategy (transpose bo)) 0)
  ((dominates? '(-5 0) '(-10 -1)) *)
  ((dominates? '(1 1) '(1 0)) 'nil)
  ((dominates? '(1 1) '(1 0) nil) *)
  ((iterated-dominance ali bo) '((0) (0)))
  ((pure-nash-equilibria ali bo) '((0 0)))
  "Iterated dominance needs two rounds when no strategy is dominant at first."
  ((iterated-dominance '((3 1) (2 0) (4 -1)) '((1 2) (3 1) (0 1)))
   '((0) (1)))
  "Matching pennies has no pure equilibrium; its value is 0, played 1/2-1/2."
  ((pure-nash-equilibria '((1 -1) (-1 1)) '((-1 1) (1 -1))) 'nil)
  ((multiple-value-list (solve-zero-sum-game '((1 -1) (-1 1))))
   '(0 (1/2 1/2) (1/2 1/2)))
  "Two-finger Morra: E plays one with probability 7/12, and the value is -1/12."
  ((multiple-value-list (solve-zero-sum-game '((2 -3) (-3 4))))
   '(-1/12 (7/12 5/12) (7/12 5/12)))
  "A glove game: a has a left glove, b and c a right glove each."
  ((setq gloves #'(lambda (c) (if (and (member 'a c) (or (member 'b c) (member 'c c))) 1 0))))
  ((shapley-value '(a b c) gloves) '((a . 2/3) (b . 1/6) (c . 1/6)))
  ((in-core? '(a b c) gloves '((a . 1) (b . 0) (c . 0))) *)
  ((in-core? '(a b c) gloves '((a . 1/3) (b . 1/3) (c . 1/3))) 'nil)
  ((in-core? '(a b c) gloves '((a . 1) (b . 1) (c . 0))) 'nil)
  "Voting: 4 voters prefer A > B > C, 3 prefer B > C > A and 2 C > B > A."
  ((setq ballots (append (make-list 4 :initial-element '(a b c))
                         (make-list 3 :initial-element '(b c a))
                         (make-list 2 :initial-element '(c b a)))))
  ((plurality-winner ballots) 'a)
  ((borda-winner ballots) 'b)
  ((condorcet-winner ballots) 'b)
  ((condorcet-winner '((a b c) (b c a) (c a b))) 'nil)
  "The Vickrey auction: the highest bidder pays the second highest bid."
  ((multiple-value-list (vickrey-auction '((ann . 10) (bob . 7) (cy . 12)))) '(cy 10))
  ((multiple-value-list (vickrey-auction '((ann . 10)))) '(ann 10))
  "The contract net awards each task to the cheapest bidder."
  ((contract-net '(paint wire) '(r1 r2)
                 #'(lambda (agent task)
                     (cdr (assoc (list agent task)
                                 '(((r1 paint) . 5) ((r2 paint) . 3) ((r1 wire) . 4))
                                 :test #'equal))))
   '((paint r2 . 3) (wire r1 . 4)))
  ((contract-net '(fly) '(r1) #'(lambda (agent task) (declare (ignore agent task)) nil))
   '((fly)))
  "Alternating offers with equal discount factors 1/2: the first gets 2/3."
  ((multiple-value-list (alternating-offers-bargaining 1/2 1/2)) '(2/3 1/3))
  ((multiple-value-list (alternating-offers-bargaining 0 9/10)) '(1/10 9/10))
  )
