Skip to content

Commit

Permalink
examples/virus: Restructure
Browse files Browse the repository at this point in the history
- VirusAgent in agents.py
- Flatten structure
- Remove old visualisation code
  • Loading branch information
EwoutH committed Oct 16, 2024
1 parent ca51e7c commit d7a3834
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 217 deletions.
4 changes: 2 additions & 2 deletions examples/basic/virus_on_network/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ Directly run the file ``run.py`` in the terminal. e.g.

## Files

* ``run.py``: Launches a model visualization server.
* ``model.py``: Contains the agent class, and the overall model class.
* ``server.py``: Defines classes for visualizing the model (network layout) in the browser via Mesa's modular server, and instantiates a visualization server.
* ``agents.py``: Contains the agent class.
* ``app.py``: Contains the code for the interactive Solara visualization.

## Further Reading

Expand Down
70 changes: 70 additions & 0 deletions examples/basic/virus_on_network/agents.py
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()
2 changes: 1 addition & 1 deletion examples/basic/virus_on_network/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from matplotlib.figure import Figure
from matplotlib.ticker import MaxNLocator
from mesa.visualization import SolaraViz, Slider, make_space_matplotlib
from virus_on_network.model import State, VirusOnNetwork, number_infected
from model import State, VirusOnNetwork, number_infected


def agent_portrayal(graph):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import math
from enum import Enum

import mesa
from mesa import Model
import networkx as nx


class State(Enum):
SUSCEPTIBLE = 0
INFECTED = 1
RESISTANT = 2
from agents import VirusAgent, State


def number_state(model, state):
Expand All @@ -27,7 +23,7 @@ def number_resistant(model):
return number_state(model, State.RESISTANT)


class VirusOnNetwork(mesa.Model):
class VirusOnNetwork(Model):
"""
A virus model with some number of agents
"""
Expand Down Expand Up @@ -102,65 +98,3 @@ def step(self):
def run_model(self, n):
for i in range(n):
self.step()


class VirusAgent(mesa.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()
2 changes: 0 additions & 2 deletions examples/basic/virus_on_network/requirements.txt

This file was deleted.

3 changes: 0 additions & 3 deletions examples/basic/virus_on_network/run.py

This file was deleted.

140 changes: 0 additions & 140 deletions examples/basic/virus_on_network/virus_on_network/server.py

This file was deleted.

0 comments on commit d7a3834

Please sign in to comment.