Open EwoutH opened 2 days ago
Yes either have a place_agents
method, or have a way of getting a list of positions drawn either with it without replacement:
cells = self.space.choices_empty_cells(100,) # with replacement, follows random.choices
cells = self.space.sample_empty_cells(100) # no replacement, follows random.sample
once we agree on how to go about it: do it via sampling/choosing empty cells or do it via placing agents, or even have both?
Sorry, didn't recognize there also was a question in there. I would say its a two-stage function:
self.space.place(agents, unique=False, empty=False)
I am not sure. A key design choice with the new discrete spaces is to put the agent central and in control of its movement. So, to me it makes more sense to just sample/choice the cells, pass a cell to the agent as an argument and let the agent place itself via self.cell = cell
. In that design, there is no need for a space-level place method.
Interesting, I'm not that familiar with the cell space yet.
In that case you could doe something like:
some_agents = MyAgent.create_agents(n=100)
cells = self.space ...
some_agents.map(lambda agent, cell: setattr(agent, 'cell', cell), zip(agents, cells))
That map is a bit cumbersome.
However, changing it around might work, right?
cells = self.space ...
some_agents = MyAgent.create_agents(n=100, cell=cells)
Can create_agents
handle a CellCollection?
A CellCollection
is a sequence so it supports item-based access. This is what is used in create_agents
, so yes that should work.
Looking at the code again, there are actually a couple more options on how to implement this. A DiscreteSpace
has an empties
attribute and aan all_cells
attribute. Both return a CellCollection
, so sample
and choices
might also be implemented on the CellCollection
so you could do
cells = self.space.empties.sample(100)
cells = self.space.empties.choices(100)
cells = self.space.all_cells.sample(100)
cells = self.space.all_cells.choices(100)
and then
class MyAgent(CellAgent):
def __init__(self, model, cell):
super().__init__(model)
self.cell = cell
agents = MyAgent.create_agents(100, cells)
So we don't need anything new at all to do this. Damn. Powerful stuff.
I will update a few example models tomorrow.
sample
and choices
are currently not implemented on CellCollection
, but self.random.sample(self.space.empties, 100)
might already work.
Often you create a bunch of Agents, and then you need to place them on the grid. Either you loop over them, or do some other unwieldy thing, none of it is elegant.
So it would be great if there was a simple function that would get all agents and place them on, optionally empty, cells. This could be implemented faster than placing them individually, since now only once has to be checked which cells are available / empty.
It's especially handy to be used together with
create_agents
(https://github.com/projectmesa/mesa/pull/2351), combined you can create and place all your agents with two lines of code.