Skip to content

Commit b426535

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 8362b29 commit b426535

File tree

5 files changed

+18
-7
lines changed

5 files changed

+18
-7
lines changed

game_theory/best_response_dynamics.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def best_response_dynamics(payoff_matrix_a, payoff_matrix_b, iterations=10):
1919

2020
return strategy_a, strategy_b
2121

22+
2223
# Example usage
2324
payoff_a = np.array([[3, 0], [5, 1]])
2425
payoff_b = np.array([[2, 4], [0, 2]])

game_theory/fictitious_play.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def fictitious_play(payoff_matrix_a, payoff_matrix_b, iterations=100):
2525

2626
return strategy_a, strategy_b
2727

28+
2829
# Example usage
2930
payoff_a = np.array([[3, 0], [5, 1]])
3031
payoff_b = np.array([[2, 4], [0, 2]])

game_theory/minimax_algorithm.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ def minimax(depth, node_index, is_maximizing_player, values, alpha, beta):
33
return values[node_index]
44

55
if is_maximizing_player:
6-
best_value = float('-inf')
6+
best_value = float("-inf")
77
for i in range(2): # Two children (0 and 1)
88
value = minimax(depth - 1, node_index * 2 + i, False, values, alpha, beta)
99
best_value = max(best_value, value)
@@ -12,7 +12,7 @@ def minimax(depth, node_index, is_maximizing_player, values, alpha, beta):
1212
break # Beta cut-off
1313
return best_value
1414
else:
15-
best_value = float('inf')
15+
best_value = float("inf")
1616
for i in range(2): # Two children (0 and 1)
1717
value = minimax(depth - 1, node_index * 2 + i, True, values, alpha, beta)
1818
best_value = min(best_value, value)
@@ -21,8 +21,9 @@ def minimax(depth, node_index, is_maximizing_player, values, alpha, beta):
2121
break # Alpha cut-off
2222
return best_value
2323

24+
2425
# Example usage
2526
values = [3, 5, 2, 9, 0, 1, 8, 6] # Leaf node values
2627
depth = 3 # Depth of the game tree
27-
result = minimax(depth, 0, True, values, float('-inf'), float('inf'))
28+
result = minimax(depth, 0, True, values, float("-inf"), float("inf"))
2829
print("The optimal value is:", result)

game_theory/nash_equlibrium.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import numpy as np
22
from scipy.optimize import linprog
33

4+
45
def find_nash_equilibrium(payoff_matrix_a, payoff_matrix_b):
56
n = payoff_matrix_a.shape[0]
67
m = payoff_matrix_a.shape[1]
@@ -23,6 +24,7 @@ def find_nash_equilibrium(payoff_matrix_a, payoff_matrix_b):
2324

2425
return p_a, p_b
2526

27+
2628
# Example usage
2729
payoff_a = np.array([[3, 0], [5, 1]])
2830
payoff_b = np.array([[2, 4], [0, 2]])

game_theory/shapley_value.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import numpy as np
22

3+
34
def shapley_value(payoff_matrix):
45
n = payoff_matrix.shape[0] # Number of players
56
shapley_values = np.zeros(n) # Initialize Shapley values
@@ -13,14 +14,19 @@ def shapley_value(payoff_matrix):
1314

1415
# Calculate the value of the subset S without player i
1516
s_without_i = s & ~(1 << i) # Remove player i from the subset
16-
marginal_contribution = payoff_matrix[s][i] - (payoff_matrix[s_without_i][i] if s_without_i else 0)
17-
17+
marginal_contribution = payoff_matrix[s][i] - (
18+
payoff_matrix[s_without_i][i] if s_without_i else 0
19+
)
20+
1821
# Count the size of the subset S
19-
size_of_s = bin(s).count('1') # Number of players in subset S
20-
shapley_values[i] += marginal_contribution / (size_of_s * (n - size_of_s)) # Normalize by size of S
22+
size_of_s = bin(s).count("1") # Number of players in subset S
23+
shapley_values[i] += marginal_contribution / (
24+
size_of_s * (n - size_of_s)
25+
) # Normalize by size of S
2126

2227
return shapley_values
2328

29+
2430
# Example usage
2531
payoff_matrix = np.array([[1, 2], [3, 4]])
2632
shapley_vals = shapley_value(payoff_matrix)

0 commit comments

Comments
 (0)