Game Theory
Game Theory
code:
import numpy as np
return (3, 3) # Both players cooperate, they both receive a moderate payoff
return (1, 1) # Both players betray, they both receive a low payoff
o/p:
code:
import numpy as np
if result.success:
optimal_strategy_player1 = result.x[0]
optimal_strategy_player2 = result.x[1]
else:
if result.success:
print(result)
o/P:
success: False
status: 3
fun: None
x: None
nit: 1
marginals: None
marginals: None
marginals: None
marginals: None
Code:
def create_game_tree():
game_tree = Digraph('GameTree')
return game_tree
game_tree = create_game_tree()
o/p
game_tree.png
4)Strategic Form-Elimination of dominant strategy
code:
import numpy as np
[1, 4, 6],
[0, 3, 2]])
def eliminate_dominant_strategy(matrix):
if all(matrix[row, col] >= matrix[other_row, col] for col in range(cols)) and row != other_row:
rows -= 1
return eliminate_dominant_strategy(matrix)
if all(matrix[row, col] >= matrix[row, other_col] for row in range(rows)) and col != other_col:
cols -= 1
return eliminate_dominant_strategy(matrix)
return matrix
result_matrix = eliminate_dominant_strategy(game_matrix)
print(game_matrix)
print(result_matrix)
o/p
[[3 2 5]
[1 4 6]
[0 3 2]]
[[6]]
code:
import numpy as np
def find_minimax_strategy(game_matrix):
max_of_minima = np.max(row_minima)
minimax_strategy = np.zeros(len(row_minima))
minimax_strategy[minimax_strategy_indices] = 1 / len(minimax_strategy_indices)
return minimax_strategy
[1, 4]])
minimax_strategy = find_minimax_strategy(game_matrix)
print("Game Matrix:")
print(game_matrix)
print("\nMinimax Strategy:")
print(minimax_strategy)
o/p
Game Matrix:
[[3 2]
[1 4]]
Minimax Strategy:
[1. 0.]
6)Perfect information games, trees, players assigned to nodes, payoffs, backward Induction,
subgame perfect equilibrium.
code:
import numpy as np
import graphviz
game = nash.Game(game_matrix)
equilibria = game.support_enumeration()
equilibrium = list(equilibria)[0]
print("Nash Equilibrium:")
print(equilibrium)
def create_game_tree():
return dot
game_tree = create_game_tree()
o/p
Nash Equilibrium:
game_tree.png
code:
a=2
b=1
c=0
d=3
A = np.array([[a, b],
[c, d]])
B = -np.transpose(A)
game = nash.Game(A, B)
equilibria = game.support_enumeration()
for eq in equilibria:
player1_strategy, player2_strategy = eq
print()
o/p
8)Repeated Games
import numpy as np
return (3, 3) # Both players cooperate, they both receive a moderate payoff
return (1, 1) # Both players betray, they both receive a low payoff
def play_repeated_game(num_rounds):
total_payoff_player1 = 0
total_payoff_player2 = 0
for _ in range(num_rounds):
total_payoff_player1 += payoff_player1
total_payoff_player2 += payoff_player2
num_rounds = 10
o/p
code:
import numpy as np
def neg_joint_utility(params):
equilibrium = result.x
o/p