-
Notifications
You must be signed in to change notification settings - Fork 1
/
MultiprocessVaryingPunishments.py
445 lines (361 loc) · 13.8 KB
/
MultiprocessVaryingPunishments.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
########################################################
# 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
import matplotlib.pyplot as plt
def run_simulations_sw(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
punishment = params[3]
###################################
# 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)
pop.parameters.payoff = [1.0, 0.0, 1.0, 0.0, 2.0, punishment, 0.0, 0.0, 0.0]
# 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]
return results
def run_simulations_grid(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
punishment = params[3]
###################################
# 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('grid', m1=m1, m2=m2, n=n, k=k, p=p)
pop.parameters.payoff = [1.0, 0.0, 1.0, 0.0, 2.0, punishment, 0.0, 0.0, 0.0]
# 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]
return results
if __name__ == '__main__':
num_of_pops = 1
maxtime = 5000
poolsize = 20
pc_step = 0.025
iters = 200
# poolsize = 5
# pc_step = 0.1
# iters = 10
# Remember to change this as necessary
print('Population structure is small world network: n = 900, c=4, p=0.0')
print(f'{iters} populations for each density')
print(f'Max time is {maxtime} time steps')
punishments = [-8, -7.5, -7, -6.5, -6, -5.5, -5, -4.5, -4, -3.5, -3, -2.5, -2, -1.5, -1, -0.5, 0]
crit_fracs_sw = []
print('Beginning Small World')
pc = 0
for punishment in punishments:
print(punishment)
real_prob = 0
# pc = 0
old_prob = 0
while real_prob<0.5:
print(pc)
params = [[pc, num_of_pops, maxtime, punishment] for _ in range(iters)]
pool = multiprocessing.Pool(processes=poolsize)
result = pool.map(run_simulations_sw, params)
real_advs = len([x for x in result if x[0]==1 or x[2]==1])
if real_advs > iters/2:
crit_frac = pc - pc_step*(real_advs/iters - 0.5)/(real_advs/iters - old_prob)
pc -= pc_step
break
else:
pc += pc_step
old_prob = real_advs/iters
print(f'Critical p_c value is {crit_frac}')
if crit_frac>0:
crit_fracs_sw.append(crit_frac)
else:
crit_fracs_sw.append(0)
crit_fracs_grid = []
print('Beginning Grid')
pc = 0
for punishment in punishments:
print(punishment)
real_prob = 0
# pc = 0
old_prob = 0
while real_prob<0.5:
print(pc)
params = [[pc, num_of_pops, maxtime, punishment] for _ in range(iters)]
pool = multiprocessing.Pool(processes=poolsize)
result = pool.map(run_simulations_grid, params)
real_advs = len([x for x in result if x[0]==1 or x[2]==1])
if real_advs > iters/2:
crit_frac = pc - pc_step*(real_advs/iters - 0.5)/(real_advs/iters - old_prob)
pc -= pc_step
break
else:
pc += pc_step
old_prob = real_advs/iters
print(f'Critical p_c value is {crit_frac}')
if crit_frac>0:
crit_fracs_grid.append(crit_frac)
else:
crit_fracs_grid.append(0)
SMALL_SIZE = 14
MEDIUM_SIZE = 18
BIGGER_SIZE = 22
plt.rc('font', size=SMALL_SIZE) # controls default text sizes
plt.rc('axes', titlesize=SMALL_SIZE) # fontsize of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE) # fontsize of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE) # fontsize of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE) # legend fontsize
plt.rc('figure', titlesize=BIGGER_SIZE) # fontsize of the figure title
plt.plot(punishments, crit_fracs_sw)
plt.plot(punishments, crit_fracs_grid)
plt.plot(punishments, [1/(3-2*x) for x in punishments])
plt.xlabel('$B-C$ punishment')
plt.ylabel('Critical $p_C$')
plt.legend(['Small-world', 'Grid', 'Well-Mixed'])
plt.tight_layout()
plt.savefig('varying_BC_punishment.png', dpi=300)