-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Scheduler: track agents with dict instead of list #386
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
Conversation
| """ Run reporters and collect agent-level variables. """ | ||
| agent_vars = {} | ||
| for agent in model.schedule.agents: | ||
| for agent in model.schedule.agents.values(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIRC the ordering is non-deterministic in python<3.6. Yet deterministic execution is crucial in scientific simulations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that preserving order is important. I'm not sure whether in Python <3.6 iterating over values() is guaranteed to preserve order any better than just iterating over the keys, and/or for that order to be the same as the order in which the agents were added (which should be the default non-random behavior).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A sure-fire fix is to use collections.OrderedDict. But this will be slower since in Python 3 it is not written in C. But correctness > speed in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it has to be a defaultdict + collections.OrderedDict.
| """ Execute the step of all the agents, one at a time. """ | ||
| for agent in self.agents[:]: | ||
| agent.step() | ||
| agent_keys = list(self.agents.keys()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, in all the places where .values() are used, the order does not matter since they are for data collection/record or asserting properties. This is the only place where the order matters.
But in one way or another, order preserving still has to be enforced so that users running a Mesa simulation don't accidentally fall into the pitfall -- principle of least surprise.
|
@dmasad, I just figured that the deterministic run problem can be resolved with I repeated more than 30 times, with all the result having the same order. If this were to happen by chance, the probability would be ( Since there is no way to set Edit: ugh, I forgot to power 0.25 by 5 |
|
There are 3 ways to resolve this:
I am inclined with 1, since 3 makes it harder to run Mesa. The version in Debian stable as of this timestamp is still 3.5.3 https://packages.debian.org/stretch/python3, but 3.6.1 in macOS/Homebrew https://github.com/Homebrew/homebrew-core/blob/dd7a66070b32a07b31ca2cbf4b3509cad5971226/Formula/python3.rb#L4. |
|
Is there a reason not to just use OrderedDicts? |
|
Yes, that will do, |
39f6155 to
3ebae89
Compare
|
/cc @csmaxwell for tracking... this is continuation of #305 |
|
@rht What's the status on this one? Looks like there is a conflict (possibly just stale). |
|
Yeah, I have just rebased this. |
An agent's unique id is generated by via `model.next_id()` - Update the wolf_sheep example - Update batchrunner, datacollection to use dict - Update the tests
|
Rebased again. |
|
Closing this one since it's incorporated into #510 |
Based on #305. Fix #302.