-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
agent.py
56 lines (47 loc) · 1.74 KB
/
agent.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
import random
from typing import TYPE_CHECKING
from Melodie import GridAgent
if TYPE_CHECKING:
from source.scenario import CovidScenario
from Melodie import AgentList
from source.grid import CovidSpot
from source.grid import CovidGrid
class CovidAgent(GridAgent):
scenario: "CovidScenario"
grid: "CovidGrid[CovidSpot]"
spot: "CovidSpot"
def set_category(self):
self.category = 0
def setup(self):
self.x: int = 0
self.y: int = 0
self.health_state: int = 0
self.age_group: int = 0
def move(self):
spot: "CovidSpot" = self.grid.get_spot(self.x, self.y)
stay_prob = spot.stay_prob
if random.uniform(0, 1) > stay_prob:
move_radius = 1
self.rand_move_agent(move_radius, move_radius)
def infection(self, agents: "AgentList[CovidAgent]"):
neighbors = self.grid.get_neighbors(self)
for neighbor_category, neighbor_id in neighbors:
neighbor_agent: "CovidAgent" = agents.get_agent(neighbor_id)
if neighbor_agent.health_state == 1:
if random.uniform(0, 1) < self.scenario.infection_prob:
self.health_state = 1
break
def health_state_transition(self):
if self.health_state == 1:
transition_probs: dict = self.scenario.get_transition_probs(self.age_group)
rand = random.uniform(0, 1)
if rand <= transition_probs["s1_s1"]:
pass
elif (
transition_probs["s1_s1"]
< rand
<= transition_probs["s1_s1"] + transition_probs["s1_s2"]
):
self.health_state = 2
else:
self.health_state = 3