本文整理汇总了Python中mesa.space.SingleGrid.place_agent方法的典型用法代码示例。如果您正苦于以下问题:Python SingleGrid.place_agent方法的具体用法?Python SingleGrid.place_agent怎么用?Python SingleGrid.place_agent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mesa.space.SingleGrid
的用法示例。
在下文中一共展示了SingleGrid.place_agent方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ShapesModel
# 需要导入模块: from mesa.space import SingleGrid [as 别名]
# 或者: from mesa.space.SingleGrid import place_agent [as 别名]
class ShapesModel(Model):
def __init__(self, N, width=20, height=10):
self.running = True
self.N = N # num of agents
self.headings = ((1, 0), (0, 1), (-1, 0), (0, -1)) # tuples are fast
self.grid = SingleGrid(width, height, torus=False)
self.schedule = RandomActivation(self)
self.make_walker_agents()
def make_walker_agents(self):
unique_id = 0
while True:
if unique_id == self.N:
break
x = random.randrange(self.grid.width)
y = random.randrange(self.grid.height)
pos = (x, y)
heading = random.choice(self.headings)
# heading = (1, 0)
if self.grid.is_cell_empty(pos):
print("Creating agent {2} at ({0}, {1})"
.format(x, y, unique_id))
a = Walker(unique_id, self, pos, heading)
self.schedule.add(a)
self.grid.place_agent(a, pos)
unique_id += 1
def step(self):
self.schedule.step()
示例2: TestSingleGrid
# 需要导入模块: from mesa.space import SingleGrid [as 别名]
# 或者: from mesa.space.SingleGrid import place_agent [as 别名]
class TestSingleGrid(unittest.TestCase):
'''
Test the SingleGrid object.
Since it inherits from Grid, all the functionality tested above should
work here too. Instead, this tests the enforcement.
'''
def setUp(self):
'''
Create a test non-toroidal grid and populate it with Mock Agents
'''
width = 3
height = 5
self.grid = SingleGrid(width, height, True)
self.agents = []
counter = 0
for x in range(width):
for y in range(height):
if TEST_GRID[x][y] == 0:
continue
counter += 1
# Create and place the mock agent
a = MockAgent(counter, None)
self.agents.append(a)
self.grid.place_agent(a, (x, y))
def test_enforcement(self):
'''
Test the SingleGrid empty count and enforcement.
'''
assert len(self.grid.empties) == 9
a = MockAgent(100, None)
with self.assertRaises(Exception):
self.grid._place_agent((0, 1), a)
# Place the agent in an empty cell
self.grid.position_agent(a)
# Test whether after placing, the empty cells are reduced by 1
assert a.pos not in self.grid.empties
assert len(self.grid.empties) == 8
for i in range(10):
self.grid.move_to_empty(a)
assert len(self.grid.empties) == 8
# Place agents until the grid is full
empty_cells = len(self.grid.empties)
for i in range(empty_cells):
a = MockAgent(101 + i, None)
self.grid.position_agent(a)
assert len(self.grid.empties) == 0
a = MockAgent(110, None)
with self.assertRaises(Exception):
self.grid.position_agent(a)
with self.assertRaises(Exception):
self.move_to_empty(self.agents[0])
示例3: Foraging
# 需要导入模块: from mesa.space import SingleGrid [as 别名]
# 或者: from mesa.space.SingleGrid import place_agent [as 别名]
class Foraging(Model):
number_of_bean = 0
number_of_corn = 0
number_of_soy = 0
def __init__(self, width=50, height=50, torus=True, num_bug=50, seed=42, strategy=None):
super().__init__(seed=seed)
self.number_of_bug = num_bug
if not(strategy in ["stick", "switch"]):
raise TypeError("'strategy' must be one of {stick, switch}")
self.strategy = strategy
self.grid = SingleGrid(width, height, torus)
self.schedule = RandomActivation(self)
data = {"Bean": lambda m: m.number_of_bean,
"Corn": lambda m: m.number_of_corn,
"Soy": lambda m: m.number_of_soy,
"Bug": lambda m: m.number_of_bug,
}
self.datacollector = DataCollector(data)
# create foods
self._populate(Bean)
self._populate(Corn)
self._populate(Soy)
# create bugs
for i in range(self.number_of_bug):
pos = self.grid.find_empty()
bug = Bug(i, self)
bug.strategy = self.strategy
self.grid.place_agent(bug, pos)
self.schedule.add(bug)
def step(self):
self.schedule.step()
self.datacollector.collect(self)
if not(self.grid.exists_empty_cells()):
self.running = False
def _populate(self, food_type):
prefix = "number_of_{}"
counter = 0
while counter < food_type.density * (self.grid.width * self.grid.height):
pos = self.grid.find_empty()
food = food_type(counter, self)
self.grid.place_agent(food, pos)
self.schedule.add(food)
food_name = food_type.__name__.lower()
attr_name = prefix.format(food_name)
val = getattr(self, attr_name)
val += 1
setattr(self, attr_name, val)
counter += 1
示例4: TestSingleGrid
# 需要导入模块: from mesa.space import SingleGrid [as 别名]
# 或者: from mesa.space.SingleGrid import place_agent [as 别名]
class TestSingleGrid(unittest.TestCase):
'''
Test the SingleGrid object.
Since it inherits from Grid, all the functionality tested above should
work here too. Instead, this tests the enforcement.
'''
def setUp(self):
'''
Create a test non-toroidal grid and populate it with Mock Agents
'''
self.grid = SingleGrid(3, 5, True)
self.agents = []
counter = 0
for y in range(3):
for x in range(5):
if TEST_GRID[y][x] == 0:
continue
counter += 1
# Create and place the mock agent
a = MockAgent(counter, None)
self.agents.append(a)
self.grid.place_agent(a, (x, y))
def test_enforcement(self):
'''
Test the SingleGrid empty count and enforcement.
'''
assert len(self.grid.empties) == 10
a = MockAgent(100, None)
with self.assertRaises(Exception):
self.grid._place_agent((1, 0), a)
# Place the agent in an empty cell
self.grid.position_agent(a)
assert a.pos not in self.grid.empties
assert len(self.grid.empties) == 9
for i in range(10):
self.grid.move_to_empty(a)
assert len(self.grid.empties) == 9
# Place agents until the grid is full
for i in range(9):
a = MockAgent(101 + i, None)
self.grid.position_agent(a)
assert len(self.grid.empties) == 0
a = MockAgent(110, None)
with self.assertRaises(Exception):
self.grid.position_agent(a)
with self.assertRaises(Exception):
self.move_to_empty(self.agents[0])
示例5: PD_Model
# 需要导入模块: from mesa.space import SingleGrid [as 别名]
# 或者: from mesa.space.SingleGrid import place_agent [as 别名]
class PD_Model(Model):
'''
Model class for iterated, spatial prisoner's dilemma model.
'''
schedule_types = {"Sequential": BaseScheduler,
"Random": RandomActivation,
"Simultaneous": SimultaneousActivation}
# This dictionary holds the payoff for this agent,
# keyed on: (my_move, other_move)
payoff = {("C", "C"): 1,
("C", "D"): 0,
("D", "C"): 1.6,
("D", "D"): 0}
def __init__(self, height, width, schedule_type, payoffs=None):
'''
Create a new Spatial Prisoners' Dilemma Model.
Args:
height, width: Grid size. There will be one agent per grid cell.
schedule_type: Can be "Sequential", "Random", or "Simultaneous".
Determines the agent activation regime.
payoffs: (optional) Dictionary of (move, neighbor_move) payoffs.
'''
self.running = True
self.grid = SingleGrid(height, width, torus=True)
self.schedule_type = schedule_type
self.schedule = self.schedule_types[self.schedule_type](self)
# Create agents
for x in range(width):
for y in range(height):
agent = PD_Agent((x, y), self)
self.grid.place_agent(agent, (x, y))
self.schedule.add(agent)
self.datacollector = DataCollector({
"Cooperating_Agents":
lambda m: len([a for a in m.schedule.agents if a.move == "C"])
})
def step(self):
self.datacollector.collect(self)
self.schedule.step()
def run(self, n):
'''
Run the model for a certain number of steps.
'''
for _ in range(n):
self.step()
示例6: TestSingleGrid
# 需要导入模块: from mesa.space import SingleGrid [as 别名]
# 或者: from mesa.space.SingleGrid import place_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