/
riot.py
42 lines (34 loc) · 1.18 KB
/
riot.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
from mesa import Agent, Model
from mesa.time import RandomActivation
import numpy as np
class RiotAgent(Agent):
def __init__(self, unique_id, model, incitement_threshold):
super().__init__(unique_id, model)
self.rebel = False
self.incitement_threshold = incitement_threshold
def step(self):
if self.model.prop_rebellious() > self.incitement_threshold:
self.rebel = True
class RiotModel(Model):
def __init__(self, N):
super().__init__()
self.N = N
self.schedule = RandomActivation(self)
thresholds = np.random.uniform(0,1,self.N)
self.schedule = RandomActivation(self)
for i in range(self.N):
a = RiotAgent(i, self, thresholds[i])
self.schedule.add(a)
self.schedule.agents[0].rebel = True
def prop_rebellious(self):
num_rebels = 0
for a in self.schedule.agents:
if a.rebel:
num_rebels +=1
return num_rebels/self.N
def step(self):
self.schedule.step()
rm = RiotModel(500)
for i in range(15):
print(f"After {i} iterations, we have {rm.prop_rebellious()*100:.1f}% rebels")
rm.step()