-
Notifications
You must be signed in to change notification settings - Fork 1
/
MultiprocessCode.py
210 lines (170 loc) · 6.5 KB
/
MultiprocessCode.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
########################################################
# Creates multiple random populations and plays the game for a given number of
# time steps or until fixation is achieved
# Uses multiprocessing to utilize multiple cores simultaneously
# Copyright (C) 2023 Matthew Jones
########################################################
import multiprocessing
from PopulationClass import Population
def run_simulations(params):
###################################
#Parameters to adjust
###################################
# Random Population
# n = 900
# p = 0.01
# K Regular Population
# n = 900
# k = 5
# SW Population
n = 900
c = 4
p = 0
# Scale Free Population
# n = 900
# c = 5
# Grid Population
# m1 = 30
# m2 = 30
# n = m1*m2
# k = 8
# p = 0
#All Population Structures
num_of_pops = params[1]
maxtime = params[2]
psdetection = 20
###################################
# Initialize
###################################
fcdensity = params[0]
factcheckers = fcdensity * n
real_fixations = 0
fake_fixations = 0
real_advantages = 0
fake_advantages = 0
real_fix_times = 0
fake_fix_times = 0
for population_number in range(num_of_pops):
pop = Population('smallworld', n=n, c=c, p=p)
# Create initial strategies
pop.preset_random()
pop.add_n_factcheckers(factcheckers)
###################################
# Run the simulation
###################################
# Initialization
oldlist = [True]*pop.popsize
olderlist = [True]*pop.popsize
newlist = pop.reals_list()
t = 0
steady = False
count = 0
periodic_count = 0
# Run the simulation to a steady state
while not steady:
t += 1
# print(f'time = {t}')
pop.update_step()
olderlist = oldlist
oldlist = newlist
newlist = pop.reals_list()
reals = pop.count_reals()
# Detect if a strategy has completely fixated
if reals == pop.popsize - factcheckers:
#print('The real news strategy has completely fixated')
real_fixations += 1
real_fix_times += t
steady = True
if reals == 0:
#print('The fake news strategy has completely fixated')
fake_fixations += 1
fake_fix_times += t
steady = True
# Detect if the system has reached a fixed state, determine the larger strategy
if oldlist == newlist:
count += 1
else:
count = 0
if count == psdetection:
t -= psdetection
#print('The system has reached a fixed state')
if reals >= (pop.popsize-factcheckers)/2:
#print('The real news strategy has more players')
real_fixations += 1
real_fix_times += t
else:
#print('The fake news strategy has more players')
fake_fixations += 1
fake_fix_times += t
steady = True
# Detect if the system is in a periodic loop
if olderlist == newlist:
periodic_count += 1
else:
periodic_count = 0
if periodic_count == psdetection:
t -= psdetection
#print('The system has reached a periodic loop')
pop.update_step()
reals += pop.count_reals()
if reals >= pop.popsize-factcheckers:
#print('The real news strategy has more players')
real_fixations += 1
real_fix_times += t
else:
#print('The fake news strategy has more players')
fake_fixations += 1
fake_fix_times += t
steady = True
# If we reach the time limit:
if t == maxtime:
#print('The system has not reached a fixed state')
if reals >= (pop.popsize-factcheckers)/2:
#print('The real news strategy has more players')
real_advantages += 1
else:
#print('The fake news strategy has more players')
fake_advantages += 1
steady = True
# Normalize the results
if real_fixations != 0:
real_fix_times = real_fix_times / real_fixations
if fake_fixations != 0:
fake_fix_times = fake_fix_times / fake_fixations
real_fixations = real_fixations/num_of_pops
fake_fixations = fake_fixations/num_of_pops
real_advantages = real_advantages/num_of_pops
fake_advantages = fake_advantages/num_of_pops
results = [real_fixations, fake_fixations]
results += [real_advantages, fake_advantages]
results += [real_fix_times, fake_fix_times]
return results
if __name__ == '__main__':
fcpoints = 10
num_of_pops = 10
maxtime = 5000
poolsize = 5
# Remember to change this as necessary
print('Population structure is small world network: n = 900, c=4, p=0.0')
print(f'{num_of_pops} populations for each density')
print(f'Max time is {maxtime} time steps')
print(f'There are {fcpoints} density data points from 0 to 1')
params = [ [i/fcpoints] for i in list(range(fcpoints))]
for param in params:
param.append(num_of_pops)
param.append(maxtime)
pool = multiprocessing.Pool(processes=poolsize)
result = pool.map(run_simulations, params)
#print(result)
probs1 = [p[0] for p in result]
probs2 = [p[1] for p in result]
probs3 = [p[2] for p in result]
probs4 = [p[3] for p in result]
time1 = [p[4] for p in result]
time2 = [p[5] for p in result]
print(f'Real fixation probs are {probs1}')
print(f'Fake fixation probs are {probs2}')
print(f'Real advantage probs are {probs3}')
print(f'Fake advantage probs are {probs4}')
print(f'Mean real fixation times are {time1}')
print(f'Mean fake fixation times are {time2}')