Skip to content

Commit 4dbcce6

Browse files
authored
chore: nash.py added to game theory
1 parent 286dec6 commit 4dbcce6

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

game_theory/nash_equilibrium.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import numpy as np
2+
from scipy.optimize import linprog
3+
4+
def find_nash_equilibrium(payoff_matrix_A, payoff_matrix_B):
5+
n = payoff_matrix_A.shape[0]
6+
m = payoff_matrix_A.shape[1]
7+
8+
# Solve for player A
9+
c = [-1] * n # Objective: maximize A's payoff
10+
A_ub = -payoff_matrix_A # A's constraints
11+
b_ub = [-1] * m
12+
13+
result_A = linprog(c, A_ub=A_ub, b_ub=b_ub, bounds=(0, None))
14+
p_A = result_A.x
15+
16+
# Solve for player B
17+
c = [-1] * m # Objective: maximize B's payoff
18+
A_ub = -payoff_matrix_B.T # B's constraints
19+
b_ub = [-1] * n
20+
21+
result_B = linprog(c, A_ub=A_ub, b_ub=b_ub, bounds=(0, None))
22+
p_B = result_B.x
23+
24+
return p_A, p_B
25+
26+
# Example usage
27+
payoff_A = np.array([[3, 0], [5, 1]])
28+
payoff_B = np.array([[2, 4], [0, 2]])
29+
equilibrium = find_nash_equilibrium(payoff_A, payoff_B)
30+
print("Nash Equilibrium strategies:", equilibrium)

0 commit comments

Comments
 (0)