Skip to content

package to run evolutionary prisoner's dilemmas with heterogeneous actors

Notifications You must be signed in to change notification settings

vocelik/heterogenous_game_theory

Repository files navigation

Heterogenous Game Theory

NOTE: if you are viewing this page in Google Chrome, we strongly advise to install the MathJax Plugin for Github.

Introduction

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:

$$ T = (x_{d} + 2 * x_{r}) * y_{m} $$ $$ R = (x_{d} + x_{r}) * y_m $$ $$ P = x_{r} * y_{m} $$ $$ S = 0 $$

For all values of M, d, and r. This satisfies the condition for the prisoner's dilemma:

$$ T>R>P>S $$

and

$$ 2R > T + S $$

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.

Instructions

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.

check_parameters

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.

draw_stack

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.

coop_ratio

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.

About

package to run evolutionary prisoner's dilemmas with heterogeneous actors

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published