Math::GameTheory
The package provides descriptions and data of different games amenable for Game Theory experiments and studies.
Installation
From Zef ecosystem:
zef install Math::GameTheory
From GitHub:
zef install https://github.com/antononcube/Raku-Math-GameTheory
Game Theory data
All games known by the package "Math::GameTheory":
use Math::GameTheory;
say "Total number of known games: {game-theory-data().elems}";
game-theory-data()
# Total number of known games: 52
Games and their classes:
my @dsGames = game-theory-data(Whatever, property => "Classes").map({ $_.key X $_.value.Array }).flat(1).map({ <name property> Z=> $_ })».Hash;
@dsGames.elems
# 153
Here is a summary:
use Data::Summarizers;
sink records-summary(@dsGames)
# +------------------+--------------------------+
# | property | name |
# +------------------+--------------------------+
# | MatrixGame => 42 | MatchingPennies => 5 |
# | 2Player => 26 | VolunteersDilemma => 5 |
# | Symmetric => 24 | RockPaperScissors => 5 |
# | NPlayer => 12 | PureCoordination => 5 |
# | Social => 10 | ZeroSumRandom => 5 |
# | TreeGame => 7 | PrisonersDilemma => 4 |
# | Recreation => 6 | 3Coordination => 4 |
# | (Other) => 26 | (Other) => 120 |
# +------------------+--------------------------+
Here is a "taxonomy tree" like breakdown:
use ML::TriesWithFrequencies;
my %cat = <2Player 3Player NPlayer MatrixGame TreeGame Symmetric> Z=> (^7);
game-theory-data(Whatever, property => "Classes").values
==> { .map({ %cat.keys (&) $_ }) }()
==> { $_.map({ $_ ?? $_.keys !! 'Other' })».sort({ %cat{$_} })».List }()
==> trie-create()
==> trie-form()
# TRIEROOT => 52
# ├─2Player => 26
# │ ├─MatrixGame => 25
# │ │ └─Symmetric => 14
# │ └─NPlayer => 1
# │ └─MatrixGame => 1
# ├─3Player => 3
# │ └─MatrixGame => 3
# ├─MatrixGame => 2
# ├─NPlayer => 11
# │ └─MatrixGame => 11
# │ └─Symmetric => 8
# ├─Other => 1
# ├─Symmetric => 2
# └─TreeGame => 7
Two player games
Get the game "Chicken" (provided by the package):
my $obj = game-theory-data('Chicken')
# MatrixGame(:name("Chicken"), :number-of-players(2), :number-of-actions(("Player 1" => 2, "Player 2" => 2)))
Here is a description of the game:
$obj.description
# The name \"chicken\" has its origins in a dangerous game in which two drivers drive toward each other. Each driver has the choice to either continue or swerve. Either at least one driver swerves, or both may die in the crash. The driver who swerved will be called a chicken, meaning a coward.
Here is game's table:
$obj.html
Chicken | Swerve | Straight |
|---|
| Swerve | 0 | 0 | -1 | 1 |
|---|
| Straight | 1 | -1 | -5 | -5 |
|---|
Here is a gray-scale version of the dataset can be obtained with $obj.html(theme => 'gray-scale').
Three player games
Get the game "3Coordination" (provided by the package):
my $obj = game-theory-data('3Coordination')
# MatrixGame(:name("3Coordination"), :number-of-players(3), :number-of-actions((:Roosevelt(3), :Stalin(3), :Churchill(3))))
$obj.description
# An extension of the coordination game where payoffs are based on the number of coordinated players.
Here is game's table:
$obj.html
3Coordination | Churchill | Roosevelt | Stalin |
|---|
| Churchill | Roosevelt | Stalin | Churchill | Roosevelt | Stalin | Churchill | Roosevelt | Stalin |
|---|
| Churchill | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
|---|
| Roosevelt | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
|---|
| Stalin | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 2 |
|---|
Zero sum games
Represent a Rock Paper Scissors game as a directed graph:
use Graph;
my $g = Graph.new(edges => [Rock => "Scissors", Scissors => "Paper", Paper => "Rock"]):d;
#$g.dot(engine => 'neato', :2size, vertex-font-size => 8):svg
# Graph(vertexes => 3, edges => 3, directed => True)

Create a Rock Paper Scissors game:
use Data::Reshapers;
my @payoff-array = ($g.adjacency-matrix <<->> transpose($g.adjacency-matrix)).deepmap(-> Int:D $p { [$p, -$p] });
my $game = Math::GameTheory::MatrixGame.new(:@payoff-array, game-action-labels => ($g.vertex-list xx 2))
# MatrixGame(:name(Whatever), :number-of-players(3), :number-of-actions(("Player 1" => 3, "Player 2" => 3)))
Here is game's table:
$game.html(theme => 'default')
| Paper | Rock | Scissors |
|---|
| Paper | 0 | 0 | 1 | -1 | -1 | 1 |
|---|
| Rock | -1 | 1 | 0 | 0 | 1 | -1 |
|---|
| Scissors | 1 | -1 | -1 | 1 | 0 | 0 |
|---|
TODO
- TODO Implementation
- DONE Games data JSON file and corresponding retrieval (multi-)sub
- TODO Matrix games
- DONE
MatrixGame class - DONE HTML format of matrix game dataset
- DONE Wolfram Language (WL) representation
- TODO Payoff functions
- TODO Simpler zero-sum games initialization
- TODO Tree games
- TODO
TreeGame class - TODO Creation using WL's tree game input format
- TODO Special tree-game plots
- TODO Documentation
- DONE Complete README
- TODO Basic usage notebook
- TODO Blog post
- TODO Video demo