本文整理汇总了Python中mesa.space.SingleGrid.move_agent方法的典型用法代码示例。如果您正苦于以下问题:Python SingleGrid.move_agent方法的具体用法?Python SingleGrid.move_agent怎么用?Python SingleGrid.move_agent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mesa.space.SingleGrid
的用法示例。
在下文中一共展示了SingleGrid.move_agent方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestSingleGrid
# 需要导入模块: from mesa.space import SingleGrid [as 别名]
# 或者: from mesa.space.SingleGrid import move_agent [as 别名]
class TestSingleGrid(unittest.TestCase):
def setUp(self):
self.space = SingleGrid(50, 50, False)
self.agents = []
for i, pos in enumerate(TEST_AGENTS_GRID):
a = MockAgent(i, None)
self.agents.append(a)
self.space.place_agent(a, pos)
def test_agent_positions(self):
'''
Ensure that the agents are all placed properly.
'''
for i, pos in enumerate(TEST_AGENTS_GRID):
a = self.agents[i]
assert a.pos == pos
def test_remove_agent(self):
for i, pos in enumerate(TEST_AGENTS_GRID):
a = self.agents[i]
assert a.pos == pos
assert self.space.grid[pos[0]][pos[1]] == a
self.space.remove_agent(a)
assert a.pos is None
assert self.space.grid[pos[0]][pos[1]] is None
def move_agent(self):
agent_number = 0
initial_pos = TEST_AGENTS_GRID[agent_number]
final_pos = (7, 7)
_agent = self.agents[agent_number]
assert _agent.pos == initial_pos
assert self.space.grid[initial_pos[0]][initial_pos[1]] == _agent
assert self.space.grid[final_pos[0]][final_pos[1]] is None
self.space.move_agent(_agent, final_pos)
assert _agent.pos == final_pos
assert self.space.grid[initial_pos[0]][initial_pos[1]] is None
assert self.space.grid[final_pos[0]][final_pos[1]] == _agent
示例2: SeparationBarrierModel
# 需要导入模块: from mesa.space import SingleGrid [as 别名]
# 或者: from mesa.space.SingleGrid import move_agent [as 别名]
class SeparationBarrierModel(Model):
def __init__(self, height, width, palestinian_density, settlement_density,
settlers_violence_rate, settlers_growth_rate, suicide_rate, greed_level,
settler_vision=1, palestinian_vision=1,
movement=True, max_iters=1000):
super(SeparationBarrierModel, self).__init__()
self.height = height
self.width = width
self.palestinian_density = palestinian_density
self.settler_vision = settler_vision
self.palestinian_vision = palestinian_vision
self.settlement_density = settlement_density
self.movement = movement
self.running = True
self.max_iters = max_iters
self.iteration = 0
self.schedule = RandomActivation(self)
self.settlers_violence_rate = settlers_violence_rate
self.settlers_growth_rate = settlers_growth_rate
self.suicide_rate = suicide_rate
self.greed_level = greed_level
self.total_violence = 0
self.grid = SingleGrid(height, width, torus=False)
model_reporters = {
}
agent_reporters = {
# "x": lambda a: a.pos[0],
# "y": lambda a: a.pos[1],
}
self.dc = DataCollector(model_reporters=model_reporters,
agent_reporters=agent_reporters)
self.unique_id = 0
# Israelis and palestinans split the region in half
for (contents, x, y) in self.grid.coord_iter():
if random.random() < self.palestinian_density:
palestinian = Palestinian(self.unique_id, (x, y), vision=self.palestinian_vision, breed="Palestinian",
model=self)
self.unique_id += 1
self.grid.position_agent(palestinian, x,y)
self.schedule.add(palestinian)
elif ((y > (self.grid.height) * (1-self.settlement_density)) and random.random() < self.settlement_density):
settler = Settler(self.unique_id, (x, y),
vision=self.settler_vision, model=self, breed="Settler")
self.unique_id += 1
self.grid.position_agent(settler, x,y)
self.schedule.add(settler)
def add_settler(self, pos):
settler = Settler(self.unique_id, pos,
vision=self.settler_vision, model=self, breed="Settler")
self.unique_id += 1
self.grid.position_agent(settler, pos[0], pos[1])
self.schedule.add(settler)
def set_barrier(self,victim_pos, violent_pos):
#print("Set barrier - Greed level", self.greed_level)
visible_spots = self.grid.get_neighborhood(victim_pos,
moore=True, radius=self.greed_level + 1)
furthest_empty = self.find_furthest_empty_or_palestinian(victim_pos, visible_spots)
x,y = furthest_empty
current = self.grid[y][x]
#print ("Set barrier!!", pos, current)
free = True
if (current is not None and current.breed == "Palestinian"):
#print ("Relocating Palestinian")
free = self.relocate_palestinian(current, current.pos)
if (free):
barrier = Barrier(-1, furthest_empty, model=self)
self.grid.position_agent(barrier, x,y)
# Relocate the violent palestinian
#violent_x, violent_y = violent_pos
#if violent_pos != furthest_empty:
# violent_palestinian = self.grid[violent_y][violent_x]
# self.relocate_palestinian(violent_palestinian, furthest_empty)
def relocate_palestinian(self, palestinian, destination):
#print ("Relocating Palestinian in ", palestinian.pos, "To somehwhere near ", destination)
visible_spots = self.grid.get_neighborhood(destination,
moore=True, radius=palestinian.vision)
nearest_empty = self.find_nearest_empty(destination, visible_spots)
#print("First Nearest empty to ", palestinian.pos, " Is ", nearest_empty)
if (nearest_empty):
self.grid.move_agent(palestinian, nearest_empty)
else:
#print ("Moveing to random empty")
if (self.grid.exists_empty_cells()):
self.grid.move_to_empty(palestinian)
else:
return False
return True
def find_nearest_empty(self, pos, neighborhood):
#.........这里部分代码省略.........