-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsimulate.py
104 lines (84 loc) · 3.14 KB
/
simulate.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
import random
import time
import matplotlib.pyplot as plt
from World import World
from Board import RectangularGrid
from Game import PrisonersDilemma, Player, PrisonersDilemmaPlayer, 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
"""
t = time.time()
time_max = t + time_max
iteration = 0
if show_animation:
world.board.draw()
# record statistics
if stats:
stats.record_stats(world, iteration)
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 show_animation:
world.board.quit_animation()
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 = 1.3
R = 1
P = 0.1
S = 0
game = PrisonersDilemma(T, R, P, S)
# define a world topology ("board") - e.g. grid, network
# choose a board type from Board.py
grid_height = 50
grid_width = 50
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
players = []
for i in range(num_players):
rand = random.random()
if rand < p_cooperation:
players.append(PrisonersDilemmaPlayer(Strategy.cooperate))
else:
players.append(PrisonersDilemmaPlayer(Strategy.defect))
# 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 = 15
iteration_max = 1500
show_animation = True
# 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)