本文整理汇总了Python中mesa.space.MultiGrid.get_cell_list_contents方法的典型用法代码示例。如果您正苦于以下问题:Python MultiGrid.get_cell_list_contents方法的具体用法?Python MultiGrid.get_cell_list_contents怎么用?Python MultiGrid.get_cell_list_contents使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mesa.space.MultiGrid
的用法示例。
在下文中一共展示了MultiGrid.get_cell_list_contents方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: SugarscapeModel
# 需要导入模块: from mesa.space import MultiGrid [as 别名]
# 或者: from mesa.space.MultiGrid import get_cell_list_contents [as 别名]
class SugarscapeModel(Model):
def __init__(self, height=50, width=50, init_agents=500, max_metabolism=3, max_vision=10, max_init_sugar=5, min_age=30, max_age=60, init_poll=3, ex_ratio=2, ex_mod=1, poll_growth_rule=True, inheritance_rule=True):
self.height = height
self.width = width
self.init_agents = init_agents
self.init_poll = init_poll
self.max_metabolism = max_metabolism
self.max_vision = max_vision
self.max_init_sugar = max_init_sugar
self.min_age = min_age
self.max_age = max_age
self.ex_ratio = ex_ratio
self.ex_mod = ex_mod
self.replacement_rule = True
self.pollution_rule = False
self.diffusion_rule = False
self.push_rule = False
self.poll_growth_rule = poll_growth_rule
self.expend_rule = True
self.inheritance_rule = inheritance_rule
self.map = self.import_map()
self.grid = MultiGrid(height, width, torus=True)
self.schedule = RandomActivationByType(self)
self.datacollector = DataCollector({'Pollution': (lambda m: m.total_pollution),
'Wealth': (lambda m: m.total_wealth/m.init_agents),
'Agents': (lambda m: len(m.schedule.agents_by_type[ScapeAgent]))},
{'Wealth': self.collect_wealth,
'Metabolism': self.collect_metabolism,
'Vision': self.collect_vision})
self.total_wealth = 0
self.total_pollution = 0
self.populate_sugar()
self.populate_agents()
def step(self):
''' Step method run by the visualization module'''
self.schedule.step([ScapeAgent, SugarPatch])
self.datacollector.collect(self)
# if self.schedule.time == 20:
# self.pollution_rule = True
if self.schedule.time == 30:
self.push_rule = True
self.total_wealth = 0
self.total_pollution = 0
for agent in self.schedule.agents_by_type[ScapeAgent]:
self.total_wealth += agent.wealth
for patch in self.schedule.agents_by_type[SugarPatch]:
self.total_pollution += patch.pollution
def import_map(self):
''' Imports a text file into an array to be used when generating and
placing the sugar Agents into the grid
'''
f = open('Maps/sugar_map.txt', 'r')
map_list = []
for line in f:
num_list = line.split(' ')
for num in num_list:
map_list.append(int(num[0]))
return map_list
def new_agent(self, uid, inheritance):
''' Place a new agent on the sugarscape in order to replace a death'''
free = False
while not free:
location = random.choice([cell for cell in self.grid.coord_iter()])
if len(location[0]) == 1:
free = True
pos = (location[1], location[2])
patch = self.grid.get_cell_list_contents([pos])[0]
if self.inheritance_rule:
if inheritance == 'rand':
wealth = random.randint(1, self.max_init_sugar)
else:
wealth = inheritance
else:
wealth = random.randint(1, self.max_init_sugar)
agent = ScapeAgent(uid, pos, wealth, random.randint(1,self.max_metabolism), random.randint(1,self.max_vision), random.randint(self.min_age, self.max_age), patch, self.ex_ratio, self.ex_mod)
self.grid.place_agent(agent, agent.pos)
self.schedule.add(agent)
def populate_agents(self):
''' Place ScapeAgent's in random unoccupied locations on the grid with randomomized
sets of parameters
'''
cells = [(cell[1], cell[2]) for cell in self.grid.coord_iter()]
#.........这里部分代码省略.........