Skip to content

Commit

Permalink
examples: Convert virus_on_network to simple namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
rht authored and tpike3 committed May 24, 2022
1 parent 8077cdf commit e2ccd6d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
15 changes: 6 additions & 9 deletions examples/virus_on_network/virus_on_network/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
from enum import Enum
import networkx as nx

from mesa import Agent, Model
from mesa.time import RandomActivation
from mesa.datacollection import DataCollector
from mesa.space import NetworkGrid
import mesa


class State(Enum):
Expand All @@ -30,7 +27,7 @@ def number_resistant(model):
return number_state(model, State.RESISTANT)


class VirusOnNetwork(Model):
class VirusOnNetwork(mesa.Model):
"""A virus model with some number of agents"""

def __init__(
Expand All @@ -47,8 +44,8 @@ def __init__(
self.num_nodes = num_nodes
prob = avg_node_degree / self.num_nodes
self.G = nx.erdos_renyi_graph(n=self.num_nodes, p=prob)
self.grid = NetworkGrid(self.G)
self.schedule = RandomActivation(self)
self.grid = mesa.space.NetworkGrid(self.G)
self.schedule = mesa.time.RandomActivation(self)
self.initial_outbreak_size = (
initial_outbreak_size if initial_outbreak_size <= num_nodes else num_nodes
)
Expand All @@ -57,7 +54,7 @@ def __init__(
self.recovery_chance = recovery_chance
self.gain_resistance_chance = gain_resistance_chance

self.datacollector = DataCollector(
self.datacollector = mesa.DataCollector(
{
"Infected": number_infected,
"Susceptible": number_susceptible,
Expand Down Expand Up @@ -106,7 +103,7 @@ def run_model(self, n):
self.step()


class VirusAgent(Agent):
class VirusAgent(mesa.Agent):
def __init__(
self,
unique_id,
Expand Down
29 changes: 13 additions & 16 deletions examples/virus_on_network/virus_on_network/server.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import math

from mesa.visualization.ModularVisualization import ModularServer
from mesa.visualization.UserParam import UserSettableParameter
from mesa.visualization.modules import ChartModule
from mesa.visualization.modules import NetworkModule
from mesa.visualization.modules import TextElement
import mesa

from .model import VirusOnNetwork, State, number_infected


Expand Down Expand Up @@ -52,8 +49,8 @@ def get_agents(source, target):
return portrayal


network = NetworkModule(network_portrayal, 500, 500)
chart = ChartModule(
network = mesa.visualization.NetworkModule(network_portrayal, 500, 500)
chart = mesa.visualization.ChartModule(
[
{"Label": "Infected", "Color": "#FF0000"},
{"Label": "Susceptible", "Color": "#008000"},
Expand All @@ -62,7 +59,7 @@ def get_agents(source, target):
)


class MyTextElement(TextElement):
class MyTextElement(mesa.visualization.TextElement):
def render(self, model):
ratio = model.resistant_susceptible_ratio()
ratio_text = "&infin;" if ratio is math.inf else f"{ratio:.2f}"
Expand All @@ -74,7 +71,7 @@ def render(self, model):


model_params = {
"num_nodes": UserSettableParameter(
"num_nodes": mesa.visualization.UserSettableParameter(
"slider",
"Number of agents",
10,
Expand All @@ -83,10 +80,10 @@ def render(self, model):
1,
description="Choose how many agents to include in the model",
),
"avg_node_degree": UserSettableParameter(
"avg_node_degree": mesa.visualization.UserSettableParameter(
"slider", "Avg Node Degree", 3, 3, 8, 1, description="Avg Node Degree"
),
"initial_outbreak_size": UserSettableParameter(
"initial_outbreak_size": mesa.visualization.UserSettableParameter(
"slider",
"Initial Outbreak Size",
1,
Expand All @@ -95,7 +92,7 @@ def render(self, model):
1,
description="Initial Outbreak Size",
),
"virus_spread_chance": UserSettableParameter(
"virus_spread_chance": mesa.visualization.UserSettableParameter(
"slider",
"Virus Spread Chance",
0.4,
Expand All @@ -104,7 +101,7 @@ def render(self, model):
0.1,
description="Probability that susceptible neighbor will be infected",
),
"virus_check_frequency": UserSettableParameter(
"virus_check_frequency": mesa.visualization.UserSettableParameter(
"slider",
"Virus Check Frequency",
0.4,
Expand All @@ -113,7 +110,7 @@ def render(self, model):
0.1,
description="Frequency the nodes check whether they are infected by " "a virus",
),
"recovery_chance": UserSettableParameter(
"recovery_chance": mesa.visualization.UserSettableParameter(
"slider",
"Recovery Chance",
0.3,
Expand All @@ -122,7 +119,7 @@ def render(self, model):
0.1,
description="Probability that the virus will be removed",
),
"gain_resistance_chance": UserSettableParameter(
"gain_resistance_chance": mesa.visualization.UserSettableParameter(
"slider",
"Gain Resistance Chance",
0.5,
Expand All @@ -134,7 +131,7 @@ def render(self, model):
),
}

server = ModularServer(
server = mesa.visualization.ModularServer(
VirusOnNetwork, [network, MyTextElement(), chart], "Virus Model", model_params
)
server.port = 8521

0 comments on commit e2ccd6d

Please sign in to comment.