forked from swamiiyer/woa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
continuous_rmain.py
143 lines (120 loc) · 5.05 KB
/
continuous_rmain.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# This is the realtime version of the script that runs the simulation of
# the model for the game using parameters specified by the user. The results
# of simulation are not saved in this case, but are rendered as plots on the
# screen as the simulation is taking place.
#
# This script can also be used to replay an already-run simulation. In this
# case, the script loads the results.pkl file from the simulation and
# renders the plots on the screen.
import math, os, pickle, pylab, random
import matplotlib.cm as cm
from continuous import *
def run(params, payoff):
"""
Run simulation of the model for the game using the parameters specified
in params map, and the given payoff function. If a results.pkl file is
present in the current folder, then the script replays the simulation.
Otherwise, the simulation is run anew.
"""
# Canvas for drawing.
pylab.figure(1, figsize = (12, 6))
pylab.ion()
pylab.draw()
trait_matrix = []
# Replay of an experiment.
if os.path.isfile("results.pkl"):
fh = open("results.pkl", "r")
pickle.load(fh)
population = pickle.load(fh)
fh.close()
for time_step in range(0, params["generations"] / \
params["report_freq"] + 1):
print("Generation %d of %d" %(time_step * params["report_freq"],
params["generations"]))
traits = []
for p in population:
traits.append(p.get_trait_list()[time_step])
plot_trait_distribution(params, trait_matrix, traits)
plot_trait_histogram(params, traits)
# New experiment.
else:
start = 0
# Create a population.
population = [Player(i) for i in range(0, params["population"])]
# Create a network.
net = network.build_network(population, params["network_topology"],
params["network_params"])
# Seed the random number generator for the experiment.
random.seed(params["seed"])
# Assign a trait for each individual in the population.
for p in population:
p.inherit_trait(params["init_trait"])
p.commit_inheritance()
# Create a dynamics module based on network (complete or other)
# type and update rule selected.
dynamics = dynamics_module(params["network_topology"],
params["update_rule"])(net, params, payoff)
# The dynamics.
for time_step in range(0, params["generations"]):
# Pre interaction.
dynamics.pre_interaction()
# Plot results at report_freq.
if time_step % params["report_freq"] == 0:
print("Generation %d of %d" %(time_step, params["generations"]))
traits = []
for p in population:
traits.append(p.get_trait())
plot_trait_distribution(params, trait_matrix, traits)
plot_trait_histogram(params, traits)
# Interact.
for count in range(0, params["population"]):
dynamics.interact()
# Post interaction.
dynamics.post_interaction()
# Update.
for count in range(0, params["population"]):
dynamics.update()
# Post update; commit trait inheritance.
dynamics.post_update()
# Keep the final plot window around until the user shuts it.
pylab.show()
pylab.close(1)
def plot_trait_distribution(params, trait_matrix, traits):
"""
Plot the time evolution of trait distribution given the params map
containing the experimental parameters, the trait matrix containing
history of traits, and current list of traits.
"""
l = pylab.histogram(traits, 100, range = (0, params["max_trait"]), normed = False)[0]
r = []
for bin in l:
trait = bin / (1.0 * params["population"])
r.append(1.0 - trait)
trait_matrix.append(r)
pylab.subplot(121).clear()
pylab.xlabel(r"$x$")
pylab.ylabel(r"$t$")
pylab.imshow(trait_matrix, interpolation = "bilinear",
origin = "l", cmap = cm.gray,
extent = [0, params["max_trait"], 1, \
len(trait_matrix) * params["report_freq"]])
pylab.axis("tight")
ax = pylab.gca()
ax.yaxis.major.formatter.set_powerlimits((0,0))
pylab.draw()
def plot_trait_histogram(params, traits):
"""
Plot a histogram of number of individuals versus trait values given
the params map containg the experimental parameters, and the list of traits
for a particular time step.
"""
pylab.subplot(122).clear()
pylab.xlabel(r"$x$")
pylab.ylabel(r"$I$")
pylab.hist(traits, 100, range = (0, params["max_trait"]),
facecolor = "black")
pylab.xlim(0, params["max_trait"])
pylab.ylim(0, params["population"])
ax = pylab.gca()
ax.yaxis.major.formatter.set_powerlimits((0,0))
pylab.draw()