-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffusion_model.py
40 lines (27 loc) · 974 Bytes
/
diffusion_model.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
"""Model diffusion through random motion of particles."""
import agentpy as ap
class Particle(ap.Agent):
direction = [
(0, 0),
(1, 0),
(0, 1),
(-1, 0),
(0, -1),
]
def setup(self):
self.displacement = [0, 0]
def set_random_displacement(self):
self.displacement = self.model.random.choice(Particle.direction)
class DiffusionModel(ap.Model):
def setup(self):
self.agents = ap.AgentList(self, self.p.agents, Particle)
self.grid = ap.Grid(self, (self.p.n_rows, self.p.n_cols))
positions = (self.p.initial_location,) * self.p.agents
self.grid.add_agents(self.agents, positions=positions)
self.histogram = None
def update(self):
self.histogram = self.grid.apply(len, field="agents")
self.agents.set_random_displacement()
def step(self):
for agent in self.agents:
self.grid.move_by(agent, agent.displacement)