Skip to content

Commit cdf6320

Browse files
committed
Add game theory algorithms: fictitious play, minimax algorithm, and Nash equilibrium
1 parent 9178473 commit cdf6320

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

game_theory/fictitious_play.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def fictitious_play(payoff_matrix_a, payoff_matrix_b, iterations=100):
2+
n = payoff_matrix_a.shape[0]
3+
m = payoff_matrix_a.shape[1]
4+
5+
# Initialize counts and strategies
6+
counts_a = np.zeros(n)
7+
counts_b = np.zeros(m)
8+
strategy_a = np.ones(n) / n
9+
strategy_b = np.ones(m) / m
10+
11+
for _ in range(iterations):
12+
# Update counts
13+
counts_a += strategy_a
14+
counts_b += strategy_b
15+
16+
# Calculate best responses
17+
best_response_a = np.argmax(payoff_matrix_a @ strategy_b)
18+
best_response_b = np.argmax(payoff_matrix_b.T @ strategy_a)
19+
20+
# Update strategies
21+
strategy_a = np.zeros(n)
22+
strategy_a[best_response_a] = 1
23+
strategy_b = np.zeros(m)
24+
strategy_b[best_response_b] = 1
25+
26+
return strategy_a, strategy_b
27+
28+
# Example usage
29+
payoff_a = np.array([[3, 0], [5, 1]])
30+
payoff_b = np.array([[2, 4], [0, 2]])
31+
strategies = fictitious_play(payoff_a, payoff_b)
32+
print("Fictitious Play strategies:", strategies)

game_theory/minimax_algorithm.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
def minimax(depth, node_index, is_maximizing_player, values, alpha, beta):
2+
if depth == 0:
3+
return values[node_index]
4+
5+
if is_maximizing_player:
6+
best_value = float('-inf')
7+
for i in range(2): # Two children (0 and 1)
8+
value = minimax(depth - 1, node_index * 2 + i, False, values, alpha, beta)
9+
best_value = max(best_value, value)
10+
alpha = max(alpha, best_value)
11+
if beta <= alpha:
12+
break # Beta cut-off
13+
return best_value
14+
else:
15+
best_value = float('inf')
16+
for i in range(2): # Two children (0 and 1)
17+
value = minimax(depth - 1, node_index * 2 + i, True, values, alpha, beta)
18+
best_value = min(best_value, value)
19+
beta = min(beta, best_value)
20+
if beta <= alpha:
21+
break # Alpha cut-off
22+
return best_value
23+
24+
# Example usage
25+
values = [3, 5, 2, 9, 0, 1, 8, 6] # Leaf node values
26+
depth = 3 # Depth of the game tree
27+
result = minimax(depth, 0, True, values, float('-inf'), float('inf'))
28+
print("The optimal value is:", result)

game_theory/nash_equlibrium.py

Lines changed: 30 additions & 0 deletions
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)