Skip to content

Instantly share code, notes, and snippets.

@luxbock

luxbock/core.clj Secret

Last active December 31, 2015 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luxbock/6d8250cdc12b9eb27567 to your computer and use it in GitHub Desktop.
Save luxbock/6d8250cdc12b9eb27567 to your computer and use it in GitHub Desktop.
(defn game-n
[N A & game-elements]
{:pre [every? true? (map unique? A)]}
(let [payoffs (payoff-matrix N A game-elements)]
{:players (range N)
:actions A
:payoffs payoffs
:utility-fn (fn utility-fn
([actions]
{:pre [(= N (count actions))]}
(let [indices (map #(.indexOf %1 %2) A actions)]
(apply (partial mget payoffs) indices)))
([actions player]
{:pre [(= N (count actions))
(<= player N)]}
(nth player (utility-fn actions))))}))
game-theory.core> (def pd
(game-n 2
[[:confess :deny]
[:confess :deny]]
[[:confess :confess] [1, 1]]
[[:confess :deny] [4, 0]]
[[:deny :confess] [0, 4]]
[[:deny :deny] [3, 3]]))
#'game-theory.core/pd
game-theory.core> (:utility-fn pd [:confess :confess])
#<core$game_n$utility_fn__39171 game_theory.core$game_n$utility_fn__39171@1104203>
game-theory.core> (*1 [:confess :confess])
[1 1]
game-theory.core>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment