Я пытаюсь запустить агента в определенном месте. Как вызвать атрибут из geo json для включения в модель? Агент запускается в случайном месте, но я хочу, чтобы он начинался с состояния и продолжался оттуда. Кроме того, я не уверен, где изменить количество кадров в секунду. Где я хочу изменить эти переменные?
class SchellingAgent (GeoAgent):
def __init__(self, unique_id, model, shape, agent_type=None):
"""Create a new Schelling agent.
Args:
unique_id: Unique identifier for the agent.
agent_type: Indicator for the agent's type (minority=1, majority=0)
"""
super().__init__(unique_id, model, shape)
self.atype = agent_type
def step(self):
"""Advance agent one step."""
similar = 0
different = 0
#neighbors = self.model.grid.get_neighbors(self)
neighbors = self.model.grid.get_neighbors()
if neighbors:
for neighbor in neighbors:
if neighbor.atype is None:
continue
elif neighbor.atype == self.atype:
similar += 1
else:
different += 1
# If unhappy, move:
if similar < different:
# Select an empty region
empties = [a for a in self.model.grid.agents if a.atype is None]
# Switch atypes and add/remove from scheduler
new_region = random.choice(empties)
new_region.atype = self.atype
self.model.schedule.add(new_region)
self.atype = None
self.model.schedule.remove(self)
else:
self.model.happy += 1
def __repr__(self):
return "Agent " + str(self.unique_id)
class SchellingModel (Model):
def __init__(self, density, minority_pc):
self.density = density
self.minority_pc = minority_pc
self.schedule = RandomActivation(self)
self.grid = GeoSpace(crs={"init": "epsg:4326"})
self.happy = 0
#self.datacollector = DataCollector({"happy": "happy"})
self.datacollector = DataCollector()
self.running = True
# Set up the grid with patches for every region
AC = AgentCreator(SchellingAgent, {"model": self})
agents = AC.from_file("nuts_rg_60M_2013_lvl_2.geojson")
self.grid.add_agents(agents)
# Set up agents
for agent in agents:
if random.random() < self.density:
if random.random() < self.minority_pc:
agent.atype = 1
else:
agent.atype = 0
self.schedule.add(agent)
def step(self):
"""Run one step of the model.
If All agents are happy, halt the model.
"""
self.happy = 0 # Reset counter of happy agents
self.schedule.step()
# self.datacollector.collect(self)
if self.happy == self.schedule.get_agent_count():
self.running = False