Skip to content

Make MultiGrid.place_agent faster#1508

Merged
rht merged 6 commits intomesa:mainfrom
ameligrana:patch-7
Nov 11, 2022
Merged

Make MultiGrid.place_agent faster#1508
rht merged 6 commits intomesa:mainfrom
ameligrana:patch-7

Conversation

@ameligrana
Copy link
Copy Markdown
Contributor

@ameligrana ameligrana commented Nov 6, 2022

The condition tested now is a constant time operation while before it was a linear time operation in relation to the size of the list of agents

@codecov
Copy link
Copy Markdown

codecov bot commented Nov 6, 2022

Codecov Report

Base: 91.32% // Head: 91.20% // Decreases project coverage by -0.11% ⚠️

Coverage data is based on head (c190361) compared to base (c201bcb).
Patch has no changes to coverable lines.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1508      +/-   ##
==========================================
- Coverage   91.32%   91.20%   -0.12%     
==========================================
  Files          15       15              
  Lines        1303     1308       +5     
  Branches      223      225       +2     
==========================================
+ Hits         1190     1193       +3     
- Misses         80       81       +1     
- Partials       33       34       +1     
Impacted Files Coverage Δ
mesa/space.py 95.73% <ø> (-0.49%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@ameligrana
Copy link
Copy Markdown
Contributor Author

ameligrana commented Nov 6, 2022

Need to resort to a somewhat in between implementation (I think that actually most of the times just the constant time condition is evaluated): any time the agent.pos position is not preassigned to where you want to place the agent in the grid it takes a constant amount of time, otherwise it goes to scan the list

mesa/space.py Outdated
"""Place the agent at the specified location, and set its pos variable."""
x, y = pos
if agent not in self.grid[x][y]:
if agent.pos != pos or agent not in self.grid[x][y]:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be agent.pos is None. The case when agent.pos is something that is not None, but is not pos either, means something is wrong. The agent might exist in both self.grid[x][y] and somewhere else.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I was thinking something similar, where agent.pos is simply set to something different, but dismissed it as unlikely. But agents being on grid and another space (e.g. network) is something I see happening. And in that case the agent.pos might as well be set to None by the network.

I think the current implementation is the most defensive mechanism, not a bad thing.

Copy link
Copy Markdown
Contributor Author

@ameligrana ameligrana Nov 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very good points but what you are saying @Corvince make me think about something to resolve ambiguities, in a sense as you point out it doesn't make much sense to have a pos variable shared by all spaces, we should create a pos variable for each agent related to each space to which the agent belongs!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By somewhere else, I meant self.grid[x_other][y_other]. agent.pos is not None happens when the modeler calls consecutive place_agent's without ensuring that an agent is removed from the grid before a place_agent happens.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, indeed this means that the current implementation is prone to errors

@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Nov 10, 2022

I'm curious, with Python, in a conditional or statement like:

if agent.pos is None or agent not in self.grid[x][y]:

If the first condition proves false, does Python still evaluate the second condition? Or does it continue directly without evaluating it?

@ameligrana
Copy link
Copy Markdown
Contributor Author

ameligrana commented Nov 10, 2022

@EwoutH i think you meant "if the first condition proves true" yes it doesn't evaluate the second one in this case

@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Nov 10, 2022

I meant that indeed. That also means that the order of the or statement matters. That's interesting, thanks!

@rht
Copy link
Copy Markdown
Contributor

rht commented Nov 11, 2022

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants