Я слежу за этим вирусом в сети пример в Mesa. Цель этого примера - когда есть зараженный агент, он будет осматривать своих ближайших соседей, когда они подвержены заражению, он может заразиться с некоторой вероятностью.
Однако, если вы проверите его результат шаг за шагом (возможно, придется попытаться пару раз, чтобы увидеть проблему), вы заметите, что иногда зараженный агент может не только заразить своих прямых соседей, но он также может заразить соседей прямых соседей, что нежелательные.
Кажется, проблема l ie in:
def try_to_infect_neighbors(self):
neighbors_nodes = self.model.grid.get_neighbors(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
Журнал отладки показывает:
Starting var:.. self = <project_material.model_agent.VirusAgent object at 0x00000240CAFDAC18>
23:15:09.558977 call 44 def try_infect_neighbors(self):
23:15:09.559976 line 45 neighbors_nodes = self.model.grid.get_neighbors(self.pos, include_center=False)
New var:....... neighbors_nodes = [1, 6, 9]
23:15:09.559976 line 46 neighbors = self.model.grid.get_cell_list_contents(neighbors_nodes)
New var:....... neighbors = [<project_material.model_agent.VirusAgent object...l_agent.VirusAgent object at 0x00000240CAFDA6A0>]
23:15:09.559976 line 48 for neighbor in neighbors:
New var:....... neighbor = <project_material.model_agent.VirusAgent object at 0x00000240CAFDA390>
23:15:09.559976 line 49 if neighbor.state is State.SUSCEPTIBLE:
23:15:09.559976 line 51 neighbor.state = State.INFECTED
23:15:09.559976 line 48 for neighbor in neighbors:
Modified var:.. neighbor = <project_material.model_agent.VirusAgent object at 0x00000240CAFDA160>
23:15:09.559976 line 49 if neighbor.state is State.SUSCEPTIBLE:
23:15:09.559976 line 51 neighbor.state = State.INFECTED
23:15:09.559976 line 48 for neighbor in neighbors:
Modified var:.. neighbor = <project_material.model_agent.VirusAgent object at 0x00000240CAFDA6A0>
23:15:09.559976 line 49 if neighbor.state is State.SUSCEPTIBLE:
23:15:09.559976 line 51 neighbor.state = State.INFECTED
23:15:09.559976 line 48 for neighbor in neighbors:
23:15:09.559976 return 48 for neighbor in neighbors:
Return value:.. None
Starting var:.. self = <project_material.model_agent.VirusAgent object at 0x00000240CAFDA6A0>
23:15:09.559976 call 44 def try_infect_neighbors(self):
23:15:09.559976 line 45 neighbors_nodes = self.model.grid.get_neighbors(self.pos, include_center=False)
New var:....... neighbors_nodes = [2, 3, 4]
23:15:09.559976 line 46 neighbors = self.model.grid.get_cell_list_contents(neighbors_nodes)
New var:....... neighbors = [<project_material.model_agent.VirusAgent object...l_agent.VirusAgent object at 0x00000240CAFDAC18>]
23:15:09.559976 line 48 for neighbor in neighbors:
New var:....... neighbor = <project_material.model_agent.VirusAgent object at 0x00000240CAFDAF98>
23:15:09.559976 line 49 if neighbor.state is State.SUSCEPTIBLE:
23:15:09.559976 line 51 neighbor.state = State.INFECTED
23:15:09.559976 line 48 for neighbor in neighbors:
Modified var:.. neighbor = <project_material.model_agent.VirusAgent object at 0x00000240CAFDA1D0>
23:15:09.559976 line 49 if neighbor.state is State.SUSCEPTIBLE:
23:15:09.559976 line 51 neighbor.state = State.INFECTED
23:15:09.559976 line 48 for neighbor in neighbors:
Modified var:.. neighbor = <project_material.model_agent.VirusAgent object at 0x00000240CAFDAC18>
23:15:09.559976 line 49 if neighbor.state is State.SUSCEPTIBLE:
23:15:09.559976 line 48 for neighbor in neighbors:
23:15:09.559976 return 48 for neighbor in neighbors:
Return value:.. None
Starting var:.. self = <project_material.model_agent.VirusAgent object at 0x00000240CAFDA160>
23:15:09.559976 call 44 def try_infect_neighbors(self):
23:15:09.559976 line 45 neighbors_nodes = self.model.grid.get_neighbors(self.pos, include_center=False)
New var:....... neighbors_nodes = [1, 4]
23:15:09.559976 line 46 neighbors = self.model.grid.get_cell_list_contents(neighbors_nodes)
New var:....... neighbors = [<project_material.model_agent.VirusAgent object...l_agent.VirusAgent object at 0x00000240CAFDAC18>]
23:15:09.559976 line 48 for neighbor in neighbors:
New var:....... neighbor = <project_material.model_agent.VirusAgent object at 0x00000240CAFDA390>
23:15:09.560975 line 49 if neighbor.state is State.SUSCEPTIBLE:
23:15:09.563972 line 48 for neighbor in neighbors:
Modified var:.. neighbor = <project_material.model_agent.VirusAgent object at 0x00000240CAFDAC18>
23:15:09.563972 line 49 if neighbor.state is State.SUSCEPTIBLE:
23:15:09.563972 line 48 for neighbor in neighbors:
23:15:09.563972 return 48 for neighbor in neighbors:
Return value:.. None
Starting var:.. self = <project_material.model_agent.VirusAgent object at 0x00000240CAFDA390>
23:15:09.563972 call 44 def try_infect_neighbors(self):
23:15:09.563972 line 45 neighbors_nodes = self.model.grid.get_neighbors(self.pos, include_center=False)
New var:....... neighbors_nodes = [4, 6]
23:15:09.563972 line 46 neighbors = self.model.grid.get_cell_list_contents(neighbors_nodes)
New var:....... neighbors = [<project_material.model_agent.VirusAgent object...l_agent.VirusAgent object at 0x00000240CAFDA160>]
23:15:09.563972 line 48 for neighbor in neighbors:
New var:....... neighbor = <project_material.model_agent.VirusAgent object at 0x00000240CAFDAC18>
23:15:09.563972 line 49 if neighbor.state is State.SUSCEPTIBLE:
23:15:09.563972 line 48 for neighbor in neighbors:
Modified var:.. neighbor = <project_material.model_agent.VirusAgent object at 0x00000240CAFDA160>
23:15:09.563972 line 49 if neighbor.state is State.SUSCEPTIBLE:
23:15:09.563972 line 48 for neighbor in neighbors:
23:15:09.563972 return 48 for neighbor in neighbors:
Return value:.. None
Иногда кажется, что назначен прямой сосед с self
, а затем он автоматически активируется и запускает a in susceptible_neighbors
l oop. Не могу понять, как это исправить.