-
Notifications
You must be signed in to change notification settings - Fork 946
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- VirusAgent in agents.py - Flatten structure - Remove old visualisation code
- Loading branch information
Showing
7 changed files
with
76 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from mesa import Agent | ||
from enum import Enum | ||
|
||
|
||
class State(Enum): | ||
SUSCEPTIBLE = 0 | ||
INFECTED = 1 | ||
RESISTANT = 2 | ||
|
||
|
||
class VirusAgent(Agent): | ||
""" | ||
Individual Agent definition and its properties/interaction methods | ||
""" | ||
|
||
def __init__( | ||
self, | ||
model, | ||
initial_state, | ||
virus_spread_chance, | ||
virus_check_frequency, | ||
recovery_chance, | ||
gain_resistance_chance, | ||
): | ||
super().__init__(model) | ||
|
||
self.state = initial_state | ||
|
||
self.virus_spread_chance = virus_spread_chance | ||
self.virus_check_frequency = virus_check_frequency | ||
self.recovery_chance = recovery_chance | ||
self.gain_resistance_chance = gain_resistance_chance | ||
|
||
def try_to_infect_neighbors(self): | ||
neighbors_nodes = self.model.grid.get_neighborhood( | ||
self.pos, include_center=False | ||
) | ||
susceptible_neighbors = [ | ||
agent | ||
for agent in self.model.grid.get_cell_list_contents(neighbors_nodes) | ||
if agent.state is State.SUSCEPTIBLE | ||
] | ||
for a in susceptible_neighbors: | ||
if self.random.random() < self.virus_spread_chance: | ||
a.state = State.INFECTED | ||
|
||
def try_gain_resistance(self): | ||
if self.random.random() < self.gain_resistance_chance: | ||
self.state = State.RESISTANT | ||
|
||
def try_remove_infection(self): | ||
# Try to remove | ||
if self.random.random() < self.recovery_chance: | ||
# Success | ||
self.state = State.SUSCEPTIBLE | ||
self.try_gain_resistance() | ||
else: | ||
# Failed | ||
self.state = State.INFECTED | ||
|
||
def try_check_situation(self): | ||
if (self.random.random() < self.virus_check_frequency) and ( | ||
self.state is State.INFECTED | ||
): | ||
self.try_remove_infection() | ||
|
||
def step(self): | ||
if self.state is State.INFECTED: | ||
self.try_to_infect_neighbors() | ||
self.try_check_situation() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
140 changes: 0 additions & 140 deletions
140
examples/basic/virus_on_network/virus_on_network/server.py
This file was deleted.
Oops, something went wrong.