0% found this document useful (0 votes)
7 views

Assignment 07

This document contains code for two questions on game theory. Question 1 simulates the prisoner's dilemma game between two players over multiple rounds, tracking their choices and payoffs to identify a dominant strategy. Question 2 simulates the rock-paper-scissors game using mixed strategies, tracking average payoffs over iterations to determine the equilibrium strategies.

Uploaded by

Yogen PS48
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Assignment 07

This document contains code for two questions on game theory. Question 1 simulates the prisoner's dilemma game between two players over multiple rounds, tracking their choices and payoffs to identify a dominant strategy. Question 2 simulates the rock-paper-scissors game using mixed strategies, tracking average payoffs over iterations to determine the equilibrium strategies.

Uploaded by

Yogen PS48
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment 07

Game Theory
Question 01 --
import random
# Define the payoffs
payoffs = {
('C', 'C'): (3, 3),
('D', 'D'): (1, 1),
('C', 'D'): (0, 5),
('D', 'C'): (5, 0)
}
# Initialize players
player_a = {'name': 'Player A', 'payoff': 0}
player_b = {'name': 'Player B', 'payoff': 0}

# Number of rounds
num_rounds = 10
de_matrix =[]
point_matrix=[]

# Simulation loop
for round_num in range(num_rounds):
print(f"Round {round_num + 1}:")

# Prompt both players to choose


choice_a = random.choice(['C', 'D'])
choice_b = random.choice(['C', 'D'])

print(f"{player_a['name']} chooses {choice_a}")


print(f"{player_b['name']} chooses {choice_b}")

de_matrix.append([choice_a,choice_b])

# Calculate and display payoffs


payoff_a, payoff_b = payoffs[(choice_a, choice_b)]
player_a['payoff'] += payoff_a
player_b['payoff'] += payoff_b

point_matrix.append([payoff_a,payoff_b])

print(f"{player_a['name']} gets {payoff_a} points")


print(f"{player_b['name']} gets {payoff_b} points")
print()
print(f"{player_a['name']} have total {player_a['payoff']} payoff points")
print(f"{player_b['name']} have total {player_b['payoff']} payoff points")
print()

#print matrix
# print("Decision matrix")
# for row in de_matrix:
# print(row[0],row[1])
print()
print("Rounds A B")
i=0
for row in point_matrix:
i=i+1
print(f"Round{i}",row[0],row[1])

# Calculate average payoffs


avg_payoff_a = player_a['payoff'] / num_rounds
avg_payoff_b = player_b['payoff'] / num_rounds

# Print results
print(f"Average payoff for {player_a['name']}: {avg_payoff_a}")
print(f"Average payoff for {player_b['name']}: {avg_payoff_b}")

# Determine dominant strategy (if any)


if avg_payoff_a > avg_payoff_b:
dominant_strategy = 'Cooperate (C)'
elif avg_payoff_b > avg_payoff_a:
dominant_strategy = 'Defect (D)'
else:
dominant_strategy = 'No dominant strategy'

print(f"Dominant strategy: {dominant_strategy}")

Output
Question 2 – (Mixed strategy)
import random

# Initialize mixed strategies for both players


p_R = random.random() # Player A's initial probability of choosing Rock
p_P = random.random() # Player A's initial probability of choosing Paper
p_S = 1 - p_R - p_P # Player A's initial probability of choosing Scissors

q_R = random.random() # Player B's initial probability of choosing Rock


q_P = random.random() # Player B's initial probability of choosing Paper
q_S = 1 - q_R - q_P # Player B's initial probability of choosing Scissors

# Number of iterations in the simulation


num_iterations = 10000

# Initialize total payoffs for both players


total_payoff_A = 0
total_payoff_B = 0

for _ in range(num_iterations):
# Player A chooses an action based on mixed strategy
action_A = random.choices(['Rock', 'Paper', 'Scissors'], weights=[p_R, p_P, p_S])[0]

# Player B chooses an action based on mixed strategy


action_B = random.choices(['Rock', 'Paper', 'Scissors'], weights=[q_R, q_P, q_S])[0]

# Calculate payoffs based on chosen actions


if (action_A, action_B) == ('Rock', 'Scissors') or (action_A, action_B) == ('Paper',
'Rock') or (action_A, action_B) == ('Scissors', 'Paper'):
payoff_A = 1
payoff_B = -1
elif (action_B, action_A) == ('Rock', 'Scissors') or (action_B, action_A) == ('Paper',
'Rock') or (action_B, action_A) == ('Scissors', 'Paper'):
payoff_A = -1
payoff_B = 1
else:
payoff_A = 0
payoff_B = 0

# Update total payoffs for both players


total_payoff_A += payoff_A
total_payoff_B += payoff_B

# Calculate average payoffs


avg_payoff_A = total_payoff_A / num_iterations
avg_payoff_B = total_payoff_B / num_iterations

# Print results
print("Average Payoff for Player A:", avg_payoff_A)
print("Average Payoff for Player B:", avg_payoff_B)

# Display final mixed strategies


print("Player A's Mixed Strategy (p_R, p_P, p_S):", (p_R, p_P, p_S))
print("Player B's Mixed Strategy (q_R, q_P, q_S):", (q_R, q_P, q_S))

Output –

You might also like