-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
Chore: Game Theory algorithms are missing #11804 #11859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f1ffe96
0b88bf5
313f15a
af36eb5
7df88b2
61b33f6
d5a54f4
286dec6
4dbcce6
ec99cc1
51cf80c
b143bfc
d52d9b7
61a3612
94b5cea
bd8cd27
1141964
e6a1bd6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import numpy as np | ||
|
||
|
||
def best_response_dynamics(payoff_matrix_a, payoff_matrix_b, iterations=10): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
n = payoff_matrix_a.shape[0] | ||
m = payoff_matrix_a.shape[1] | ||
|
||
# Initialize strategies | ||
strategy_a = np.ones(n) / n | ||
strategy_b = np.ones(m) / m | ||
|
||
for _ in range(iterations): | ||
# Update strategy A | ||
response_a = np.argmax(payoff_matrix_a @ strategy_b) | ||
strategy_a = np.zeros(n) | ||
strategy_a[response_a] = 1 | ||
|
||
# Update strategy B | ||
response_b = np.argmax(payoff_matrix_b.T @ strategy_a) | ||
strategy_b = np.zeros(m) | ||
strategy_b[response_b] = 1 | ||
|
||
return strategy_a, strategy_b | ||
|
||
|
||
# Example usage | ||
payoff_a = np.array([[3, 0], [5, 1]]) | ||
payoff_b = np.array([[2, 4], [0, 2]]) | ||
strategies = best_response_dynamics(payoff_a, payoff_b) | ||
print("Final strategies:", strategies) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
def fictitious_play(payoff_matrix_a, payoff_matrix_b, iterations=100): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
n = payoff_matrix_a.shape[0] | ||
m = payoff_matrix_a.shape[1] | ||
|
||
# Initialize counts and strategies | ||
counts_a = np.zeros(n) | ||
counts_b = np.zeros(m) | ||
strategy_a = np.ones(n) / n | ||
strategy_b = np.ones(m) / m | ||
|
||
for _ in range(iterations): | ||
# Update counts | ||
counts_a += strategy_a | ||
counts_b += strategy_b | ||
|
||
# Calculate best responses | ||
best_response_a = np.argmax(payoff_matrix_a @ strategy_b) | ||
best_response_b = np.argmax(payoff_matrix_b.T @ strategy_a) | ||
|
||
# Update strategies | ||
strategy_a = np.zeros(n) | ||
strategy_a[best_response_a] = 1 | ||
strategy_b = np.zeros(m) | ||
strategy_b[best_response_b] = 1 | ||
|
||
return strategy_a, strategy_b | ||
|
||
|
||
# Example usage | ||
payoff_a = np.array([[3, 0], [5, 1]]) | ||
payoff_b = np.array([[2, 4], [0, 2]]) | ||
strategies = fictitious_play(payoff_a, payoff_b) | ||
print("Fictitious Play strategies:", strategies) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
def minimax(depth, node_index, is_maximizing_player, values, alpha, beta): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: Please provide type hint for the parameter: |
||
if depth == 0: | ||
return values[node_index] | ||
|
||
if is_maximizing_player: | ||
best_value = float("-inf") | ||
for i in range(2): # Two children (0 and 1) | ||
value = minimax(depth - 1, node_index * 2 + i, False, values, alpha, beta) | ||
best_value = max(best_value, value) | ||
alpha = max(alpha, best_value) | ||
if beta <= alpha: | ||
break # Beta cut-off | ||
return best_value | ||
else: | ||
best_value = float("inf") | ||
for i in range(2): # Two children (0 and 1) | ||
value = minimax(depth - 1, node_index * 2 + i, True, values, alpha, beta) | ||
best_value = min(best_value, value) | ||
beta = min(beta, best_value) | ||
if beta <= alpha: | ||
break # Alpha cut-off | ||
return best_value | ||
|
||
|
||
# Example usage | ||
values = [3, 5, 2, 9, 0, 1, 8, 6] # Leaf node values | ||
depth = 3 # Depth of the game tree | ||
result = minimax(depth, 0, True, values, float("-inf"), float("inf")) | ||
print("The optimal value is:", result) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<<<<<<< HEAD | ||
import numpy as np | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An error occurred while parsing the file: Traceback (most recent call last):
File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
reports = lint_file(
^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 2:3.
parser error: error at 1:2: expected one of (, *, +, -, ..., AWAIT, EOF, False, NAME, NUMBER, None, True, [, break, continue, lambda, match, not, pass, ~
import numpy as np
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An error occurred while parsing the file: Traceback (most recent call last):
File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
reports = lint_file(
^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 2:3.
parser error: error at 1:2: expected one of (, *, +, -, ..., AWAIT, EOF, False, NAME, NUMBER, None, True, [, break, continue, lambda, match, not, pass, ~
import numpy as np
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An error occurred while parsing the file: Traceback (most recent call last):
File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
reports = lint_file(
^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 2:3.
parser error: error at 1:2: expected one of (, *, +, -, ..., AWAIT, EOF, False, NAME, NUMBER, None, True, [, break, continue, lambda, match, not, pass, ~
import numpy as np
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An error occurred while parsing the file: Traceback (most recent call last):
File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
reports = lint_file(
^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 2:3.
parser error: error at 1:2: expected one of (, *, +, -, ..., AWAIT, EOF, False, NAME, NUMBER, None, True, [, break, continue, lambda, match, not, pass, ~
import numpy as np
^ |
||
from scipy.optimize import linprog | ||
|
||
def find_nash_equilibrium(payoff_matrix_a, payoff_matrix_b): | ||
n = payoff_matrix_a.shape[0] | ||
m = payoff_matrix_a.shape[1] | ||
|
||
# Solve for player A | ||
c = [-1] * n # Objective: maximize A's payoff | ||
a_ub = -payoff_matrix_a # A's constraints | ||
b_ub = [-1] * m | ||
|
||
result_a = linprog(c, A_ub=a_ub, b_ub=b_ub, bounds=(0, None)) | ||
p_a = result_a.x | ||
|
||
# Solve for player B | ||
c = [-1] * m # Objective: maximize B's payoff | ||
a_ub = -payoff_matrix_b.T # B's constraints | ||
b_ub = [-1] * n | ||
|
||
result_b = linprog(c, A_ub=a_ub, b_ub=b_ub, bounds=(0, None)) | ||
p_b = result_b.x | ||
|
||
return p_a, p_b | ||
|
||
# Example usage | ||
payoff_a = np.array([[3, 0], [5, 1]]) | ||
payoff_b = np.array([[2, 4], [0, 2]]) | ||
equilibrium = find_nash_equilibrium(payoff_a, payoff_b) | ||
print("Nash Equilibrium strategies:", equilibrium) | ||
======= | ||
import numpy as np | ||
from scipy.optimize import linprog | ||
|
||
|
||
def find_nash_equilibrium(payoff_matrix_A, payoff_matrix_B): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Variable and function names should follow the Please provide type hint for the parameter: Variable and function names should follow the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: Variable and function names should follow the Please provide type hint for the parameter: Variable and function names should follow the |
||
n = payoff_matrix_A.shape[0] | ||
m = payoff_matrix_A.shape[1] | ||
|
||
# Solve for player A | ||
c = [-1] * n # Objective: maximize A's payoff | ||
A_ub = -payoff_matrix_A # A's constraints | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the |
||
b_ub = [-1] * m | ||
|
||
result_A = linprog(c, A_ub=A_ub, b_ub=b_ub, bounds=(0, None)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the |
||
p_A = result_A.x | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the |
||
|
||
# Solve for player B | ||
c = [-1] * m # Objective: maximize B's payoff | ||
A_ub = -payoff_matrix_B.T # B's constraints | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the |
||
b_ub = [-1] * n | ||
|
||
result_B = linprog(c, A_ub=A_ub, b_ub=b_ub, bounds=(0, None)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the |
||
p_B = result_B.x | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the |
||
|
||
return p_A, p_B | ||
|
||
|
||
# Example usage | ||
payoff_A = np.array([[3, 0], [5, 1]]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the |
||
payoff_B = np.array([[2, 4], [0, 2]]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the |
||
equilibrium = find_nash_equilibrium(payoff_A, payoff_B) | ||
print("Nash Equilibrium strategies:", equilibrium) | ||
>>>>>>> 51cf80c355f4a1fbfba6aa04bbb0fdf1292dcb2f |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
def shapley_value(payoff_matrix): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: As there is no test file in this pull request nor any test function or class in the file Please provide type hint for the parameter: |
||
n = payoff_matrix.shape[0] | ||
shapley_values = np.zeros(n) | ||
|
||
for i in range(n): | ||
for s in range(1 << n): # All subsets of players | ||
if (s & (1 << i)) == 0: # i not in S | ||
continue | ||
|
||
s_without_i = s & ~(1 << i) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An error occurred while parsing the file: Traceback (most recent call last):
File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
reports = lint_file(
^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 11:3.
parser error: error at 10:2: expected one of (, *, +, -, ..., AWAIT, EOF, False, NAME, NUMBER, None, True, [, break, continue, lambda, match, not, pass, ~
s_without_i = s & ~(1 << i)
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An error occurred while parsing the file: Traceback (most recent call last):
File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
reports = lint_file(
^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 11:3.
parser error: error at 10:2: expected one of (, *, +, -, ..., AWAIT, EOF, False, NAME, NUMBER, None, True, [, break, continue, lambda, match, not, pass, ~
s_without_i = s & ~(1 << i)
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An error occurred while parsing the file: Traceback (most recent call last):
File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
reports = lint_file(
^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 11:3.
parser error: error at 10:2: expected one of (, *, +, -, ..., AWAIT, EOF, False, NAME, NUMBER, None, True, [, break, continue, lambda, match, not, pass, ~
s_without_i = s & ~(1 << i)
^ |
||
marginal_contribution = payoff_matrix[s][i] - ( | ||
payoff_matrix[s_without_i][i] if s_without_i else 0 | ||
) | ||
shapley_values[i] += marginal_contribution / ( | ||
len(bin(s)) - 2 | ||
) # Normalize by size of S | ||
|
||
return shapley_values | ||
|
||
|
||
# Example usage | ||
payoff_matrix = np.array([[1, 2], [3, 4]]) | ||
shapley_vals = shapley_value(payoff_matrix) | ||
print("Shapley Values:", shapley_vals) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function:
best_response_dynamics
. If the function does not return a value, please provide the type hint as:def function() -> None:
As there is no test file in this pull request nor any test function or class in the file
game_theory/best_response_dynamics.py
, please provide doctest for the functionbest_response_dynamics
Please provide type hint for the parameter:
payoff_matrix_a
Please provide type hint for the parameter:
payoff_matrix_b
Please provide type hint for the parameter:
iterations