NOTE: if you are viewing this page in Google Chrome, we strongly advise to install the MathJax Plugin for Github.
This package is based on the UvAAxelrod package, which Rick Groeneweg, Václav Ocelík and Sebastian Krapohl developed for the research project GLOBAL CODE. The package simulates prisoner's dilemma between any number of agents in a particular environment. Agents can, but need not, differ from one another based on three variables: M, d, and r.
For any agent pair x,y the payoffs are calculated as follows:
For all values of M, d, and r. This satisfies the condition for the prisoner's dilemma:
and
The variables can be a constant, or can be drawn from a normal or power distribution. The user can specify any combination of constants and distribution. For example, M can be set to 5 with 0 variation, d can have an average of 0.1 and a standard deviation of 0.2, and r can have an average of 0.1 and a shape of 4. This allows the user to create a population of agents that is completely homogenous, completely heterogenous, and anywhere in between.
Running a tournament is fairly straightforward. We import all files in the package, as wel as numpy
and scipy
.
import sys
sys.path.insert(1, '../')
from heterogenous_game_theory import *
import numpy as np
import matplotlib.pyplot as plt
import scipy as sp
seed = 1024
np.random.seed(seed)
Next, we create a set of agents with a particular distribution and check the parameters of these agents. Notice how we input "power" into E[0]
to indicate we want a pareto distribution. If we set homogenous = True
, we need not create M
, E
, or I
separately.
tour_type = "hetero_mdr_sd_min"
M = [5, 0.5]
R = [0.4, 0.05]
D = [0.3, 0.05]
agents = get_agents(homogenous = False, number_of_agents = 100, M = M, D = D, R = R)
check_parameters(agents)
compare_payoff_function(agents, default_payoff_functions)
check_parameters
prints the characteristics of the first twenty agents, as well as a histogram of all agents' characteristics. compare_payoff_function
prints the payoffs of the first ten agent pairs. These can be used in conjunction to quickly inspect if everything worked as intended.
Finally, we run the tournament and save the results.
tour = Tournament.create_play_tournament(
agents = agents,
max_rounds = 10000,
strategy_list = [defect, tit_for_tat, generous_tit_for_tat, cooperate],
payoff_functions = default_payoff_functions,
surveillance_penalty = True,
self_reward = selfreward, #default function
playing_each_other = True,
nr_strategy_changes = 10,
mutation_rate = 0.1,
init_fitnes_as_m = False,
noise = 0.05,
)
draw_stack(tour)
C_D_ratios_per_round_var(tour, constant = 1)
outliers = count_outliers(tour, constants = np.arange(0.5, 3.1, 0.1))
data = {'S.D.': list(outliers.keys()), 'Counts': list(outliers.values())}
df = pd.DataFrame.from_dict(data)
print(df)
df.to_csv("data/outliers/" + str(seed) + "_outlier_counts.csv", encoding='utf-8', index = False, float_format='%.1f')
save_tournament_csv(tour, seed = str(seed), tour_type = tour_type)
draw_stack
draws a stackplot of market share on the y-axis and round number on the x-axis. This plot allows us to inspect how the different strategies in our population have captured market share in the simulation.
C_D_ratios_per_round_var
plots the average cooperation ratio on the y-axis and the round number on the x-axis. The black line constitutes the average of the entire tournament. The yellow and red lines capture the upper and lower bounds of the desired standard deviation, the default being 1. Points outside the yellow and red lines signal periods of high or low levels of cooperation. This plot allows us to inspect how the average cooperation ratio evolves over the tournament, as well as how stable this level is.
The output of the simulation is saved in two csv files. The first saves the average cooperation rate of each round, while the second saves the absolute number of extreme periods of cooperation/defection for a standard deviation ranging from 0.5 to 3. A period starts whenever the line exits the area between the yellow and red line and ends whenever the line returns.