def __init__(self, agents, width, height):
super().__init__(agents)
if len(agents) > width * height:
raise Exception('there must be enough space for all agents')
# seutp grid
self.space = nx.grid_2d_graph(width, height)
self.width = width
self.height = height
# place agents
positions = self.space.nodes()
random.shuffle(positions)
for agent in self.agents:
pos = positions.pop()
self.sync(self.place(agent, pos))
# setup vacant positions
for pos in positions:
self.space.node[pos] = {'agent': None}
评论列表
文章目录