Skip to content

Instantly share code, notes, and snippets.

@scheibo
Last active August 21, 2024 05:05
Show Gist options
  • Save scheibo/1dd2111db184e059084af2de961b69f2 to your computer and use it in GitHub Desktop.
Save scheibo/1dd2111db184e059084af2de961b69f2 to your computer and use it in GitHub Desktop.
Nash Equilibrium using Linear Programming Solvers

Problem

$$\begin{pmatrix} 3 & 2 & 5\\ 1 & 4 & 2\\ 4 & 3 & 3 \end{pmatrix} $$

lrsnash

3 3

3 2 5
1 4 2
4 3 3

-3 -2 -5
-1 -4 -2
-4 -3 -3

GLPK

Note the strategies are reversed as is the sign on the expected payoff - P1 is min player and is computing the minimum payoff for the max player P2

P1

var v;
var x1 >= 0;
var x2 >= 0;
var x3 >= 0;

minimize z:     v;

subject to c11: -3*x1 + -1*x2 + -4*x3 <= v;
subject to c12: -2*x1 + -4*x2 + -3*x3 <= v;
subject to c13: -5*x1 + -2*x2 + -3*x3 <= v;
subject to c14: x1 + x2 + x3 = 1;

end;

P2

var v;
var x1 >= 0;
var x2 >= 0;
var x3 >= 0;

maximize z:     v;

subject to c11: 3*x1 + 1*x2 + 4*x3 >= v;
subject to c12: 2*x1 + 4*x2 + 3*x3 >= v;
subject to c13: 5*x1 + 2*x2 + 3*x3 >= v;
subject to c14: x1 + x2 + x3 = 1;

end;  

Gambit

NFG 1 R "Game"
{ "P1" "P2" } { 3 3 }

3 -3 1 -1 4 -4 2 -2 4 -4 3 -3 5 -5 2 -2 3 -3