|
| 1 | +from mesa import Agent, Model |
| 2 | +from mesa.datacollection import DataCollector |
| 3 | +from mesa.space import MultiGrid |
| 4 | +from mesa.time import RandomActivation |
| 5 | + |
| 6 | + |
| 7 | +class SIRAgent(Agent): |
| 8 | + def __init__( |
| 9 | + self, unique_id, model, recovery_time_range=(8, 12), max_agent_step_size=1 |
| 10 | + ): |
| 11 | + |
| 12 | + super().__init__(unique_id, model) |
| 13 | + self.state = "S" |
| 14 | + self.infected_time = 0 |
| 15 | + self.recovery_time_range = recovery_time_range |
| 16 | + self.max_agent_step_size = max_agent_step_size |
| 17 | + self.pos_history = [] |
| 18 | + self.interactions = {} |
| 19 | + |
| 20 | + def step(self): |
| 21 | + if self.state == "I": |
| 22 | + self.infected_time += 1 |
| 23 | + # check if agent is within the recovery window |
| 24 | + if self.infected_time >= self.recovery_time_range[0]: |
| 25 | + # if so, recover probabilistically |
| 26 | + if ( |
| 27 | + self.random.random() < 0.5 |
| 28 | + or self.infected_time >= self.recovery_time_range[1] |
| 29 | + ): |
| 30 | + self.state = "R" |
| 31 | + |
| 32 | + # if still infected, try to infect others |
| 33 | + if self.state == "I": |
| 34 | + neighbors = self.model.grid.get_neighbors( |
| 35 | + self.pos, moore=True, include_center=False |
| 36 | + ) |
| 37 | + for neighbor in neighbors: |
| 38 | + if ( |
| 39 | + neighbor.state == "S" |
| 40 | + and self.random.random() < self.model.infection_rate |
| 41 | + ): |
| 42 | + neighbor.state = "I" |
| 43 | + |
| 44 | + possible_moves = self.model.grid.get_neighborhood( |
| 45 | + self.pos, moore=True, include_center=False, radius=self.max_agent_step_size |
| 46 | + ) |
| 47 | + |
| 48 | + new_position = self.random.choice(possible_moves) |
| 49 | + self.model.grid.move_agent(self, new_position) |
| 50 | + self.pos_history.append(new_position) |
| 51 | + |
| 52 | + # record interactions with other agents in this step |
| 53 | + neighbors = self.model.grid.get_neighbors( |
| 54 | + self.pos, moore=True, include_center=False |
| 55 | + ) |
| 56 | + |
| 57 | + for neighbor in neighbors: |
| 58 | + if neighbor.unique_id in self.interactions: |
| 59 | + self.interactions[neighbor.unique_id] += 1 |
| 60 | + else: |
| 61 | + self.interactions[neighbor.unique_id] = 1 |
| 62 | + |
| 63 | + |
| 64 | +class SIRModel(Model): |
| 65 | + def __init__( |
| 66 | + self, |
| 67 | + grid_width, |
| 68 | + grid_height, |
| 69 | + N, |
| 70 | + infection_rate, |
| 71 | + recovery_time_range, |
| 72 | + max_agent_step_size=1, |
| 73 | + n_initial_infections=1, |
| 74 | + max_iterations=1000, |
| 75 | + change_threshold=0.001, |
| 76 | + ): |
| 77 | + |
| 78 | + super().__init__() |
| 79 | + self.num_agents = N |
| 80 | + self.grid = MultiGrid(grid_width, grid_height, True) |
| 81 | + self.schedule = RandomActivation(self) |
| 82 | + self.infection_rate = infection_rate |
| 83 | + self.recovery_time_range = recovery_time_range |
| 84 | + self.max_agent_step_size = max_agent_step_size |
| 85 | + self.max_iterations = max_iterations |
| 86 | + self.current_iteration = 0 |
| 87 | + self.change_threshold = change_threshold |
| 88 | + # track the ratio of infected agents in the previous step |
| 89 | + self.previous_infected_ratio = None |
| 90 | + |
| 91 | + # create agents |
| 92 | + for i in range(self.num_agents): |
| 93 | + # half the agents will have a large step size, half small |
| 94 | + step_size = max_agent_step_size if i % 2 == 0 else 1 |
| 95 | + a = SIRAgent(i, self, recovery_time_range, max_agent_step_size=step_size) |
| 96 | + self.grid.place_agent( |
| 97 | + a, |
| 98 | + ( |
| 99 | + self.random.randrange(self.grid.width), |
| 100 | + self.random.randrange(self.grid.height), |
| 101 | + ), |
| 102 | + ) |
| 103 | + # initialize the position history with the starting position |
| 104 | + a.pos_history.append(a.pos) |
| 105 | + self.schedule.add(a) |
| 106 | + |
| 107 | + # randomly infect a specified number of agents |
| 108 | + initial_infected_agents = self.random.sample( |
| 109 | + self.schedule.agents, n_initial_infections |
| 110 | + ) |
| 111 | + for agent in initial_infected_agents: |
| 112 | + agent.state = "I" |
| 113 | + agent.infected_time = 0 # initialize infection duration |
| 114 | + |
| 115 | + self.datacollector = DataCollector( |
| 116 | + model_reporters={ |
| 117 | + "Susceptible": lambda m: self.count_state("S"), |
| 118 | + "Infected": lambda m: self.count_state("I"), |
| 119 | + "Recovered": lambda m: self.count_state("R"), |
| 120 | + }, |
| 121 | + agent_reporters={ |
| 122 | + "State": "state", |
| 123 | + "Infection_Duration": "infection_duration", |
| 124 | + }, |
| 125 | + ) |
| 126 | + # this line is necessary for doing multiple model runs simultaneously |
| 127 | + # we will do this later in the lecture |
| 128 | + self.running = True |
| 129 | + |
| 130 | + def step(self): |
| 131 | + self.datacollector.collect(self) |
| 132 | + self.schedule.step() |
| 133 | + self.current_iteration += 1 |
| 134 | + |
| 135 | + # check for stopping condition based on maximum iterations |
| 136 | + if self.current_iteration >= self.max_iterations: |
| 137 | + self.running = False |
| 138 | + |
| 139 | + # check for stopping condition based on change in infected ratio |
| 140 | + current_infected_ratio = self.count_state("I") |
| 141 | + if self.previous_infected_ratio is not None: |
| 142 | + change = abs(current_infected_ratio - self.previous_infected_ratio) |
| 143 | + if change < self.change_threshold: |
| 144 | + self.running = False |
| 145 | + self.previous_infected_ratio = current_infected_ratio |
| 146 | + |
| 147 | + def count_state(self, state_name): |
| 148 | + count = sum([1 for a in self.schedule.agents if a.state == state_name]) |
| 149 | + return count / self.num_agents |
0 commit comments