-
Notifications
You must be signed in to change notification settings - Fork 2
/
simulate_asymmetric_pd.py
121 lines (102 loc) · 3.93 KB
/
simulate_asymmetric_pd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import random
import time
import matplotlib.pyplot as plt
from World import World
from Board import RectangularGrid
from Game import (
PrisonersDilemma,
AsymmetricPrisonersDilemma,
Player,
PrisonersDilemmaPlayer,
AsymmetricPrisonersDilemmaPlayer,
Strategy,
)
from SimulationStatistics import StrategyFractionsTimeSeries
def simulate(
world, stats=None, time_max=30, iteration_max=100000, show_animation=False
):
"""simulates the evolution of strategies in the world, allowing for
visualization and the recording of statistics
"""
if show_animation:
world.board.draw()
t = time.time()
time_max = t + time_max
iteration = 0
while t < time_max and iteration < iteration_max:
# perform one round of updates
world.round()
if show_animation:
world.board.draw()
# record statistics
if stats:
stats.record_stats(world, iteration)
# stop simulation based on statistics
if stats.end_simulation(world, iteration):
break
# update loop variables
iteration += 1
t = time.time()
if stats:
stats.print_results()
if __name__ == "__main__":
""" Adjust simulation parameters here """
# define the game played between two players during an interaction
# choose a game type from Game.py
T = 2.1
R = 1
P = 0.3
S = 0.1
game = AsymmetricPrisonersDilemma(T, R, P, S)
# define a world topology ("board") - e.g. grid, network
# choose a board type from Board.py
grid_height = 49
grid_width = 49
board = RectangularGrid(grid_height, grid_width)
# define the players in the world
# choose a player type from Game.py
num_players = grid_height * grid_width // 2
p_cooperation = 0.5
coops = []
for n in range(1, 10):
coops = []
for i in range(0, 11):
p = i // 10 # prob that need is not satisfied
players = []
for i in range(num_players):
rand = random.random()
if rand < p_cooperation:
players.append(
AsymmetricPrisonersDilemmaPlayer(Strategy.cooperate, p)
)
else:
players.append(
AsymmetricPrisonersDilemmaPlayer(Strategy.defect, p))
# define player update parameters
r = 0.05 # probability that a player randomly resets its strategy
q = 0.05 # conditional probability that a player resets to cooperate
noise1 = True # a boolean indicating whether Noise 1 is present
noise2 = False # a boolean indicating whether Noise 2 is present
imitation = True # a boolean indicating whether players perform imitation
migration = True # a boolean indicating whether players perform migration
M = 5 # the range of the Moore neighborhood around each cell
# simulation parameters
time_max = 30
iteration_max = 200
show_animation = False
# define the statistics to record in the simulation
# choose a simulations type from SimulationsStatistics.py
stats = StrategyFractionsTimeSeries()
# define the world to simulate evolution of strategies
world = World(
game, board, players, r, q, noise1, noise2, imitation, migration, M
)
# perform simulation
simulate(world, stats, time_max, iteration_max, show_animation)
coops.append(stats.record_stats(world, iteration_max))
# print(coops)
# f=open("/Users/malvika/Downloads/Yanninimalhu-master 4/coops.txt","w")
# f.close()
with open('/Users/malvika/Downloads/Yanninimalhu-master 4/{0}{1}.txt'.format("coops_", n), 'w') as f:
for item in coops:
f.write("%s\n" % item)