Skip to content

Commit 9178473

Browse files
committed
Add best_response_dynamics.py for implementing best response dynamics in game theory
1 parent e5fbbdf commit 9178473

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

game_theory/best_response_dynamics.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
def best_response_dynamics(payoff_matrix_a, payoff_matrix_b, iterations=10):
2+
n = payoff_matrix_a.shape[0]
3+
m = payoff_matrix_a.shape[1]
4+
5+
# Initialize strategies
6+
strategy_a = np.ones(n) / n
7+
strategy_b = np.ones(m) / m
8+
9+
for _ in range(iterations):
10+
# Update strategy A
11+
response_a = np.argmax(payoff_matrix_a @ strategy_b)
12+
strategy_a = np.zeros(n)
13+
strategy_a[response_a] = 1
14+
15+
# Update strategy B
16+
response_b = np.argmax(payoff_matrix_b.T @ strategy_a)
17+
strategy_b = np.zeros(m)
18+
strategy_b[response_b] = 1
19+
20+
return strategy_a, strategy_b
21+
22+
# Example usage
23+
payoff_a = np.array([[3, 0], [5, 1]])
24+
payoff_b = np.array([[2, 4], [0, 2]])
25+
strategies = best_response_dynamics(payoff_a, payoff_b)
26+
print("Final strategies:", strategies)

0 commit comments

Comments
 (0)