19/10/2024, 13:20 Game Theory Lab Assignment 1 - Colab
keyboard_arrow_down Assignment 1
Rohit Negi
ID: 21106053
Date: 18-09-2024
Two tech companies, AlphaTech and BetaSoft, must make strategic choices over 10 rounds. Payff matrix -
B
Innovate Copy
Innovate (5, 5) (-2, 10)
A
Copy (10, -2) (1, 1)
Develop a program to simulate a strategic interaction between AlphaTech and BetaSoft over 10 rounds:
AlphaTech will always choose to Copy.
BetaSoft will Innovate in the first round and then mimic AlphaTech's previous choice in the following rounds
import random
# Initialize scores for AlphaTech and BetaSoft
alpha_score = 0
beta_score = 0
def simulate_round(alpha_choice, beta_choice):
global alpha_score, beta_score
if alpha_choice == "Innovate" and beta_choice == "Innovate":
alpha_score += 5
beta_score += 5
elif alpha_choice == "Innovate" and beta_choice == "Copy":
alpha_score -= 2
beta_score += 10
elif alpha_choice == "Copy" and beta_choice == "Innovate":
alpha_score += 10
beta_score -= 2
elif alpha_choice == "Copy" and beta_choice == "Copy":
alpha_score += 1
beta_score += 1
return alpha_score, beta_score
def case_1():
print("Case 1: AlphaTech always chooses to Copy")
# Initial conditions
alpha_score, beta_score = 0, 0
for round_num in range(1, 11):
alpha_choice = "Copy" # AlphaTech always copies
beta_choice = "Innovate" if round_num == 1 else "Copy" # BetaSoft's strategy: Innovate 1st round, Copy thereafter
alpha_score, beta_score = simulate_round(alpha_choice, beta_choice)
print(f"Round {round_num}: AlphaTech={alpha_choice}, BetaSoft={beta_choice} --> AlphaTech Score: {alpha_score}, BetaSoft Score: {beta_
print(f"Final Scores after 10 rounds: AlphaTech={alpha_score}, BetaSoft={beta_score}\n")
def case_2():
print("Case 2: BetaSoft mimics AlphaTech's previous choice after the first round")
# Initial conditions
alpha_score, beta_score = 0, 0
beta_choice = "Innovate" # BetaSoft Innovates in the first round
for round_num in range(1, 11):
alpha_choice = "Copy" # AlphaTech always copies
if round_num > 1:
beta_choice = alpha_prev_choice # BetaSoft mimics AlphaTech's previous choice
alpha_score, beta_score = simulate_round(alpha_choice, beta_choice)
alpha_prev_choice = alpha_choice # Store AlphaTech's previous choice for the next round
https://colab.research.google.com/drive/1bJGpKjMJ4GDENvxSy7QDGBWpFzOnv1I3#scrollTo=bm6FvUQovPdN&printMode=true 1/5
19/10/2024, 13:20 Game Theory Lab Assignment 1 - Colab
print(f"Round {round_num}: AlphaTech={alpha_choice}, BetaSoft={beta_choice} --> AlphaTech Score: {alpha_score}, BetaSoft Score: {beta_
print(f"Final Scores after 10 rounds: AlphaTech={alpha_score}, BetaSoft={beta_score}\n")
# Run both cases
case_1()
case_2()
Case 1: AlphaTech always chooses to Copy
Round 1: AlphaTech=Copy, BetaSoft=Innovate --> AlphaTech Score: 10, BetaSoft Score: -2
Round 2: AlphaTech=Copy, BetaSoft=Copy --> AlphaTech Score: 11, BetaSoft Score: -1
Round 3: AlphaTech=Copy, BetaSoft=Copy --> AlphaTech Score: 12, BetaSoft Score: 0
Round 4: AlphaTech=Copy, BetaSoft=Copy --> AlphaTech Score: 13, BetaSoft Score: 1
Round 5: AlphaTech=Copy, BetaSoft=Copy --> AlphaTech Score: 14, BetaSoft Score: 2
Round 6: AlphaTech=Copy, BetaSoft=Copy --> AlphaTech Score: 15, BetaSoft Score: 3
Round 7: AlphaTech=Copy, BetaSoft=Copy --> AlphaTech Score: 16, BetaSoft Score: 4
Round 8: AlphaTech=Copy, BetaSoft=Copy --> AlphaTech Score: 17, BetaSoft Score: 5
Round 9: AlphaTech=Copy, BetaSoft=Copy --> AlphaTech Score: 18, BetaSoft Score: 6
Round 10: AlphaTech=Copy, BetaSoft=Copy --> AlphaTech Score: 19, BetaSoft Score: 7
Final Scores after 10 rounds: AlphaTech=19, BetaSoft=7
Case 2: BetaSoft mimics AlphaTech's previous choice after the first round
Round 1: AlphaTech=Copy, BetaSoft=Innovate --> AlphaTech Score: 29, BetaSoft Score: 5
Round 2: AlphaTech=Copy, BetaSoft=Copy --> AlphaTech Score: 30, BetaSoft Score: 6
Round 3: AlphaTech=Copy, BetaSoft=Copy --> AlphaTech Score: 31, BetaSoft Score: 7
Round 4: AlphaTech=Copy, BetaSoft=Copy --> AlphaTech Score: 32, BetaSoft Score: 8
Round 5: AlphaTech=Copy, BetaSoft=Copy --> AlphaTech Score: 33, BetaSoft Score: 9
Round 6: AlphaTech=Copy, BetaSoft=Copy --> AlphaTech Score: 34, BetaSoft Score: 10
Round 7: AlphaTech=Copy, BetaSoft=Copy --> AlphaTech Score: 35, BetaSoft Score: 11
Round 8: AlphaTech=Copy, BetaSoft=Copy --> AlphaTech Score: 36, BetaSoft Score: 12
Round 9: AlphaTech=Copy, BetaSoft=Copy --> AlphaTech Score: 37, BetaSoft Score: 13
Round 10: AlphaTech=Copy, BetaSoft=Copy --> AlphaTech Score: 38, BetaSoft Score: 14
Final Scores after 10 rounds: AlphaTech=38, BetaSoft=14
Given the payoff matrix for a Prisoner's Dilemma game:
Prisoner B
Cooperate Defect
Cooperate (-1, -1) (-5, 0)
Prisoner A
Defect (0, -5) (-3, -3)
Write a program to identify and display all Nash Equilibria in this game.
# Payoff matrix for Player A and Player B
# Each entry in the matrix is (Player A's payoff, Player B's payoff)
payoff_matrix = {
'Cooperate': {'Cooperate': (3, 3), 'Defect': (0, 5)},
'Defect': {'Cooperate': (5, 0), 'Defect': (1, 1)}
}
def is_nash_equilibrium(strategy_a, strategy_b):
"""
Check if a pair of strategies (strategy_a, strategy_b) is a Nash Equilibrium.
"""
a_payoff = payoff_matrix[strategy_a][strategy_b][0] # Player A's payoff
b_payoff = payoff_matrix[strategy_a][strategy_b][1] # Player B's payoff
# Check if Player A would deviate from strategy_a
a_best_response = max(payoff_matrix['Cooperate'][strategy_b][0], payoff_matrix['Defect'][strategy_b][0])
a_deviation = a_payoff >= a_best_response # True if Player A won't deviate
# Check if Player B would deviate from strategy_b
b_best_response = max(payoff_matrix[strategy_a]['Cooperate'][1], payoff_matrix[strategy_a]['Defect'][1])
b_deviation = b_payoff >= b_best_response # True if Player B won't deviate
return a_deviation and b_deviation
def find_nash_equilibria():
"""
Find all Nash Equilibria in the game.
"""
nash_equilibria = []
strategies = ['Cooperate', 'Defect']
https://colab.research.google.com/drive/1bJGpKjMJ4GDENvxSy7QDGBWpFzOnv1I3#scrollTo=bm6FvUQovPdN&printMode=true 2/5
19/10/2024, 13:20 Game Theory Lab Assignment 1 - Colab
for strategy_a in strategies:
for strategy_b in strategies:
if is_nash_equilibrium(strategy_a, strategy_b):
nash_equilibria.append((strategy_a, strategy_b))
return nash_equilibria
# Find and display Nash Equilibria
nash_equilibria = find_nash_equilibria()
if nash_equilibria:
print("Nash Equilibria found:")
for equilibrium in nash_equilibria:
print(f"Player A: {equilibrium[0]}, Player B: {equilibrium[1]}")
else:
print("No Nash Equilibria found.")
Two companies, TechCorp and NetSoft, are deciding on their pricing strategies for a new software product. The choices available are High Price
and Low Price, with the following payoff matrix:
NetSoft
High Price Low Price
High Price (8, 8) (2, 10)
TechCorp
Low Price (10, 2) (5, 5)
Develop a program that identifies the dominant strategy for each company based on the given payoff matrix
def find_dominant_strategy(payoff_matrix):
rows = len(payoff_matrix)
cols = len(payoff_matrix[0])
# Check dominant strategy for player 1
for col in range(cols):
for row in range(rows):
if payoff_matrix[row][col][0] < payoff_matrix[row][col][1]:
break
else:
# If we complete the loop without breaking, col is a dominant strategy for player 1
yield (0, col)
# Check dominant strategy for player 2
for row in range(rows):
for col in range(cols):
if payoff_matrix[row][col][1] < payoff_matrix[row][col][0]:
break
else:
# If we complete the loop without breaking, row is a dominant strategy for player 2
yield (1, row)
return None
# Example usage:
payoff_matrix = [
[(8, 8), (2, 10)],
[(10, 2), (5, 5)]
]
dominant_strategies = find_dominant_strategy(payoff_matrix)
for dominant_strategy in dominant_strategies:
if dominant_strategy is not None:
player, strategy = dominant_strategy
if player == 0:
print(f"Dominant strategy for Player 1: Choose strategy {strategy}")
elif player == 1:
print(f"Dominant strategy for Player 2: Choose strategy {strategy}")
else:
print("No dominant strategy found.")
Dominant strategy for Player 1: Choose strategy 0
Dominant strategy for Player 2: Choose strategy 0
https://colab.research.google.com/drive/1bJGpKjMJ4GDENvxSy7QDGBWpFzOnv1I3#scrollTo=bm6FvUQovPdN&printMode=true 3/5
19/10/2024, 13:20 Game Theory Lab Assignment 1 - Colab
In a repeated game between Player A (available strategies are M, N) and Player B (available strategies are X, Y) over three rounds, each round
has the following payoff matrix:
Player B
X Y
M (3, 2) (1, 4)
Player A
N (2, 3) (0, 1)
Task: Write a program to solve this repeated game using backward induction. Identify and display the optimal strategies and payoffs for both
players across all three rounds.
# Payoff matrix for each round (Player A's strategy vs Player B's strategy)
payoff_matrix = {
'M': {'X': (3, 2), 'Y': (1, 4)},
'N': {'X': (2, 3), 'Y': (0, 1)}
}
def get_optimal_strategy_for_round():
"""
Identifies the optimal strategy for both players in a single round
based on their payoffs in the payoff matrix.
"""
# Determine Player A's optimal strategy by checking which strategy gives a higher payoff
a_strategy = None
a_best_response = -float('inf')
for a_choice in payoff_matrix:
# Player A compares their payoffs for both of Player B's strategies
a_payoff_if_b_plays_x = payoff_matrix[a_choice]['X'][0]
a_payoff_if_b_plays_y = payoff_matrix[a_choice]['Y'][0]
# Player A wants to maximize their lowest possible payoff (worst-case scenario)
a_min_payoff = min(a_payoff_if_b_plays_x, a_payoff_if_b_plays_y)
if a_min_payoff > a_best_response:
a_strategy = a_choice
a_best_response = a_min_payoff
# Determine Player B's optimal strategy by checking which strategy gives a higher payoff
b_strategy = None
b_best_response = -float('inf')
for b_choice in ['X', 'Y']:
# Player B compares their payoffs for both of Player A's strategies
b_payoff_if_a_plays_m = payoff_matrix['M'][b_choice][1]
b_payoff_if_a_plays_n = payoff_matrix['N'][b_choice][1]
# Player B wants to maximize their lowest possible payoff (worst-case scenario)
b_min_payoff = min(b_payoff_if_a_plays_m, b_payoff_if_a_plays_n)
if b_min_payoff > b_best_response:
b_strategy = b_choice
b_best_response = b_min_payoff
return a_strategy, b_strategy, a_best_response, b_best_response
def repeated_game(num_rounds=3):
"""
Solves the repeated game using backward induction, starting from the final round and working backwards.
Returns the optimal strategies and payoffs for both players across all rounds.
"""
optimal_strategies = []
total_a_payoff = 0
total_b_payoff = 0
for round_num in range(num_rounds, 0, -1):
print(f"Solving for Round {round_num}:")
a_strategy, b_strategy, a_payoff, b_payoff = get_optimal_strategy_for_round()
# Store the optimal strategies for the current round
optimal_strategies.append((round_num, a_strategy, b_strategy))
# Accumulate the total payoffs for both players
total_a_payoff += a_payoff
https://colab.research.google.com/drive/1bJGpKjMJ4GDENvxSy7QDGBWpFzOnv1I3#scrollTo=bm6FvUQovPdN&printMode=true 4/5
19/10/2024, 13:20 Game Theory Lab Assignment 1 - Colab
total_b_payoff += b_payoff
print(f"Optimal strategy for Player A: {a_strategy}")
print(f"Optimal strategy for Player B: {b_strategy}")
print(f"Player A's payoff in this round: {a_payoff}")
print(f"Player B's payoff in this round: {b_payoff}\n")
# Return the results
optimal_strategies.reverse() # Reverse to display from Round 1
return optimal_strategies, total_a_payoff, total_b_payoff
# Run the repeated game for 3 rounds
strategies, total_a, total_b = repeated_game()
print("Final strategies for all rounds:")
for round_num, a_strategy, b_strategy in strategies:
print(f"Round {round_num}: Player A chose {a_strategy}, Player B chose {b_strategy}")
print(f"\nTotal payoff for Player A after 3 rounds: {total_a}")
print(f"Total payoff for Player B after 3 rounds: {total_b}")
Solving for Round 3:
Optimal strategy for Player A: M
Optimal strategy for Player B: X
Player A's payoff in this round: 1
Player B's payoff in this round: 2
Solving for Round 2:
Optimal strategy for Player A: M
Optimal strategy for Player B: X
Player A's payoff in this round: 1
Player B's payoff in this round: 2
Solving for Round 1:
Optimal strategy for Player A: M
Optimal strategy for Player B: X
Player A's payoff in this round: 1
Player B's payoff in this round: 2
Final strategies for all rounds:
Round 1: Player A chose M, Player B chose X
Round 2: Player A chose M, Player B chose X
Round 3: Player A chose M, Player B chose X
Total payoff for Player A after 3 rounds: 3
Total payoff for Player B after 3 rounds: 6
https://colab.research.google.com/drive/1bJGpKjMJ4GDENvxSy7QDGBWpFzOnv1I3#scrollTo=bm6FvUQovPdN&printMode=true 5/5