33
44import mesa
55import networkx as nx
6- from mesa .experimental .cell_space import FixedAgent , Network
76
87
98class State (Enum ):
@@ -13,7 +12,7 @@ class State(Enum):
1312
1413
1514def number_state (model , state ):
16- return sum (1 for a in model .agents if a .state is state )
15+ return sum (1 for a in model .grid . get_all_cell_contents () if a .state is state )
1716
1817
1918def number_infected (model ):
@@ -47,7 +46,7 @@ def __init__(
4746 self .num_nodes = num_nodes
4847 prob = avg_node_degree / self .num_nodes
4948 self .G = nx .erdos_renyi_graph (n = self .num_nodes , p = prob )
50- self .grid = Network (self .G )
49+ self .grid = mesa . space . NetworkGrid (self .G )
5150
5251 self .initial_outbreak_size = (
5352 initial_outbreak_size if initial_outbreak_size <= num_nodes else num_nodes
@@ -77,13 +76,12 @@ def __init__(
7776 )
7877
7978 # Add the agent to the node
80- a . cell = self .grid [ node ]
79+ self .grid . place_agent ( a , node )
8180
8281 # Infect some nodes
8382 infected_nodes = self .random .sample (list (self .G ), self .initial_outbreak_size )
84- for node in infected_nodes :
85- for agent in self .grid [node ].agents :
86- agent .state = State .INFECTED
83+ for a in self .grid .get_cell_list_contents (infected_nodes ):
84+ a .state = State .INFECTED
8785
8886 self .running = True
8987 self .datacollector .collect (self )
@@ -106,7 +104,7 @@ def run_model(self, n):
106104 self .step ()
107105
108106
109- class VirusAgent (FixedAgent ):
107+ class VirusAgent (mesa . Agent ):
110108 """
111109 Individual Agent definition and its properties/interaction methods
112110 """
@@ -130,10 +128,12 @@ def __init__(
130128 self .gain_resistance_chance = gain_resistance_chance
131129
132130 def try_to_infect_neighbors (self ):
133- neighbors_nodes = self .cell .neighborhood
131+ neighbors_nodes = self .model .grid .get_neighborhood (
132+ self .pos , include_center = False
133+ )
134134 susceptible_neighbors = [
135135 agent
136- for agent in neighbors_nodes . agents
136+ for agent in self . model . grid . get_cell_list_contents ( neighbors_nodes )
137137 if agent .state is State .SUSCEPTIBLE
138138 ]
139139 for a in susceptible_neighbors :
0 commit comments