The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Game::Theory::TwoPersonMatrix - Analyze a 2 person matrix game

VERSION

version 0.2207

SYNOPSIS

 use Game::Theory::TwoPersonMatrix;

 # zero-sum game
 my $g = Game::Theory::TwoPersonMatrix->new(
    1 => { 1 => 0.2, 2 => 0.3, 3 => 0.5 },
    2 => { 1 => 0.1, 2 => 0.7, 3 => 0.2 },
    payoff => [ [-5, 4, 6],
                [ 3,-2, 2],
                [ 2,-3, 1] ]
 );
 $g->col_reduce;
 $g->row_reduce;
 my $x = $g->saddlepoint;
 $x = $g->oddments;
 $x = $g->expected_payoff;
 my $player = 1;
 $x = $g->counter_strategy($player);
 $x = $g->play;

 # non-zero-sum game
 $g = Game::Theory::TwoPersonMatrix->new(
    1 => { 1 => 0.1, 2 => 0.2, 3 => 0.7 },
    2 => { 1 => 0.1, 2 => 0.2, 3 => 0.3, 4 => 0.4 },
    # Payoff table for the row player
    payoff1 => [ [5,3,8,2],   # 1st strategy
                 [6,5,7,1],   # 2
                 [7,4,6,0] ], # 3
    # Payoff table for the column player (opponent)
    #             1 2 3 4th strategy
    payoff2 => [ [2,0,1,3],
                 [3,4,4,1],
                 [5,6,8,2] ],
 );
 $x = $g->mm_tally;
 $x = $g->pareto_optimal;
 $x = $g->nash;
 $x = $g->expected_payoff;
 $x = $g->counter_strategy($player);
 $x = $g->play;

DESCRIPTION

Game::Theory::TwoPersonMatrix analyzes a two person matrix game of player numbers, strategies and utilities ("payoffs").

Players 1 and 2 are the "row" and "column" players, respectively. This is due to the tabular format of a matrix game:

                  Player 2
                  --------
         Strategy 0.5  0.5
 Player |   0.5    1   -1  < Payoff
    1   |   0.5   -1    1  <

A non-zero-sum game is represented by two payoff profiles, as in the SYNOPSIS and the prisoner's dilemma.

A prisoner's dilemma, where Blue is the row player, Red is the column player, and T > R > P > S is:

    \  Red |           |
      \    | Cooperate | Defect
 Blue   \  |           |
 --------------------------------
           |  \   R2   |  \   T2
 Cooperate |    \      |    \
           | R1   \    | S1   \
 --------------------------------
           |  \   S2   |  \   P2
 Defect    |    \      |    \
           | T1   \    | P1   \

And in this implementation that would be:

 $g = Game::Theory::TwoPersonMatrix->new(
    %strategies,
    payoff1 => [[ -1, -3 ], [  0, -2 ]], # Blue: [[ R1, S1 ], [ T1, P1 ]]
    payoff2 => [[ -1,  0 ], [ -3, -2 ]], # Red:  [[ R2, T2 ], [ S2, P2 ]]
 );

The two player strategies are to either cooperate (1) or defect (2). This is given by a hash for each of the two players, where the values are Boolean 1 or 0:

 %strategies = (
    1 => { 1 => $cooporate1, 2 => $defect1 }, # Blue
    2 => { 1 => $cooporate2, 2 => $defect2 }, # Red
 );

See the eg/tournament program in this distribution for examples that exercise strategic variations of the prisoner's dilemma.

METHODS

new

 $g = Game::Theory::TwoPersonMatrix->new(
    1 => { 1 => 0.5, 2 => 0.5 },
    2 => { 1 => 0.5, 2 => 0.5 },
    payoff => [ [1,0],
                [0,1] ]
 );

 $g = Game::Theory::TwoPersonMatrix->new(
    payoff1 => [ [2,3],
                 [2,1] ],
    payoff2 => [ [3,5],
                 [2,3] ],
 );

Create a new Game::Theory::TwoPersonMatrix object.

Player strategies are given by a hash reference of numbered keys - one for each strategy. The values of these are assumed to add to 1. Otherwise YMMV.

Payoffs are given by array references of lists of outcomes. For zero-sum games this is a single payoff list. For non-zero-sum games this is given as two lists - one for each player.

The number of row and column payoff values must equal the number of the player and opponent strategies, respectively.

expected_payoff

 $x = $g->expected_payoff;

Return the expected payoff of a game. This is the sum of the strategic probabilities of each payoff.

s_expected_payoff

 $g = Game::Theory::TwoPersonMatrix->new(
    1 => { 1 => '(1 - p)', 2 => 'p' },
    2 => { 1 => 1, 2 => 0 },
    payoff => [ ['a','b'], ['c','d'] ]
 );
 $x = $g->s_expected_payoff;
 # (1 - p) * 1 * a + (1 - p) * 0 * b + p * 1 * c + p * 0 * d

Return the symbolic expected payoff expression for a non-numeric game.

Using real payoff values, we solve the resulting expression for p in the eg/ examples.

counter_strategy

 $x = $g->counter_strategy($player);

Return the expected payoff for a given player, of either a zero-sum or non-zero-sum game, given pure strategies.

saddlepoint

 $x = $g->saddlepoint;

Return the saddlepoint of a zero-sum game, or undef if there is none.

A saddlepoint is simultaneously minimum for its row and maximum for its column.

oddments

 $x = $g->oddments;

Return each player's "oddments" for a 2x2 zero-sum game with no saddlepoint.

row_reduce

 $g->row_reduce;

Reduce a zero-sum game by identifying and eliminating strictly dominated rows and their associated player strategies.

col_reduce

 $g->col_reduce;

Reduce a zero-sum game by identifying and eliminating strictly dominated columns and their associated opponent strategies.

mm_tally

 $x = $g->mm_tally;

For zero-sum games, return the maximum of row minimums and the minimum of column maximums. For non-zero-sum games, return the maximum of row and column minimums.

pareto_optimal

 $x = $g->pareto_optimal;

Return the Pareto optimal outcomes for a non-zero-sum game.

nash

 $x = $g->nash;

Identify the Nash equilibria.

Given payoff pair (a,b), a is maximum for its column and b is maximum for its row.

play

 $x = $g->play;
 $x = $g->play(%strategies);

Return a single outcome for a zero-sum game, or a pair for a non-zero-sum game as a hashref keyed by each strategy chosen and with values of the payoff(s) earned.

An optional list of player strategies can be provided. This is a hash of the same type of strategies that are given to the constructor.

SEE ALSO

The eg/ and t/ scripts in this distribution.

"A Gentle Introduction to Game Theory" from which this API is derived:

http://www.amazon.com/Gentle-Introduction-Theory-Mathematical-World/dp/0821813390

http://books.google.com/books?id=8doVBAAAQBAJ

https://en.wikipedia.org/wiki/Prisoner%27s_dilemma

AUTHOR

Gene Boggs <gene@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2022 by Gene Boggs.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.