本文整理匯總了Python中mesa.space.SingleGrid.iter_neighbors方法的典型用法代碼示例。如果您正苦於以下問題:Python SingleGrid.iter_neighbors方法的具體用法?Python SingleGrid.iter_neighbors怎麽用?Python SingleGrid.iter_neighbors使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類mesa.space.SingleGrid
的用法示例。
在下文中一共展示了SingleGrid.iter_neighbors方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: SchellingModel
# 需要導入模塊: from mesa.space import SingleGrid [as 別名]
# 或者: from mesa.space.SingleGrid import iter_neighbors [as 別名]
class SchellingModel(Model):
'''Model class for Schelling segregation model'''
def __init__(self, height=20, width=20, density=.8, group_ratio=.66, minority_ratio=.5, homophily=3):
self.height = height
self.width = width
self.density = density
self.group_ratio = group_ratio
self.minority_ratio = minority_ratio
self.homophily = homophily
self.happy = 0
self.segregated = 0
self.schedule = RandomActivation(self)
self.grid = SingleGrid(height, width, torus=False)
self.place_agents()
self.datacollector = DataCollector( {'happy': (lambda m: m.happy), 'segregated': (lambda m: m.segregated)})
self.running = True
def step(self):
'''Run one step of model'''
self.schedule.step()
self.calculate_stats()
self.datacollector.collect(self)
if self.happy == self.schedule.get_agent_count():
self.running = False
def place_agents(self):
for cell in self.grid.coord_iter():
x, y = cell[1:3]
if random.random() < self.density:
if random.random() < self.group_ratio:
if random.random() < self.minority_ratio:
group = 0
else:
group = 1
else:
group = 2
agent = SchellingAgent((x,y), group)
self.grid.position_agent(agent, (x,y))
self.schedule.add(agent)
for agent in self.schedule.agents:
count = 0
for neighbour in self.grid.iter_neighbors(agent.pos, moore=False):
if neighbour.group == agent.group:
count += 1
agent.similar = count
def calculate_stats(self):
happy_count = 0
avg_seg = 0
for agent in self.schedule.agents:
avg_seg += agent.similar
if agent.similar >= self.homophily:
happy_count += 1
self.happy = happy_count
self.segregated = avg_seg/self.schedule.get_agent_count()