-
Notifications
You must be signed in to change notification settings - Fork 929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cell space: Place agents in AgentSet randomly on (empty) cells #2530
Comments
Yes either have a cells = self.space.choices_empty_cells(100,) # with replacement, follows random.choices
cells = self.space.sample_empty_cells(100) # no replacement, follows random.sample |
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 |
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 |
A Looking at the code again, there are actually a couple more options on how to implement this. A 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. |
|
I quickly checked, @Corvince, do you remember why |
No I don't remember exactly, but yes, the |
Great insight! So now it's just: some_empty_cells = self.random.sample(self.space.empties.cells, k=100)
some_agents = MyAgent.create_agents(n=100, cell=some_empty_cells) Shows how powerful and flexible the cell space is. |
I added this to some of the examples so it is clear to others as well how to do this. I am closing this issue. |
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
(#2351), combined you can create and place all your agents with two lines of code.The text was updated successfully, but these errors were encountered: