-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiffusion_model.py
48 lines (33 loc) · 1.14 KB
/
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
41
42
43
44
45
46
47
48
"""Model diffusion through random motion of particles."""
import agentpy as ap
import numpy as np
BASE_WEIGHT = 10
directions = [
(0, 0),
(1, 0),
(0, 1),
(-1, 0),
(0, -1),
]
weights = np.ones(len(directions), dtype=int) * BASE_WEIGHT
class Particle(ap.Agent):
def setup(self):
self.displacement = [0, 0]
def set_random_displacement(self):
self.displacement = self.model.random.choices(directions, weights=weights).pop()
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.set_diffusivity()
self.histogram = None
def set_diffusivity(self):
weights[1:] = self.p.diffusivity
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)