-
Notifications
You must be signed in to change notification settings - Fork 926
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
Fix AgentSet inplace shuffle (and thus RandomActivation), add tests #2007
Conversation
Adds a test that checks if the RandomActivation doesn't trigger agents in a sequential order. In theory this could give false positives (a test passing when it shouldn't, but that chance is around ~0.1^18).
612caa2
to
a7a2c87
Compare
Performance benchmarks:
|
If you change def do_each(self, method, shuffle=False):
if shuffle:
self._agents.shuffle(inplace=True)
self._agents.do(method) it works correctly. I am going to check RandomActivationByType as well. Do I have write access to this? |
I also think this bug fix will actually produce a speedup because we avoid a needless copy operation of the AgentSet. RandomActivationByType should not have this bug in the first place because it does not rely on |
b7603d9
to
90f0408
Compare
Thanks! For clarity, we should document the difference between |
@quaquel could you add unittests for |
do you mean for AgentSet.shuffle? |
for more information, see https://pre-commit.ci
Can you trigger the benchmarks? I am curious to see what this fix has done with those given that it removes an unnecessary copy operation. |
/rerun-benchmarks |
…into test-randomactivation
for more information, see https://pre-commit.ci
Looks good! Benchmarks depend on #2002. Can’t approve this PR, but looks good to me. Recommend squash merge. |
fix for the second issue in projectmesa#2006. If super is not present, we create the data structure but forget to add the agent to it. This is just a backward compatibility fix.
…into test-randomactivation
No the chance on a false negative is one in 12! instead of 4! (40 million instead of 24)
@@ -54,6 +54,7 @@ def __init__(self, unique_id: int, model: Model) -> None: | |||
except AttributeError: | |||
# model super has not been called | |||
self.model.agents_ = defaultdict(dict) | |||
self.model.agents_[type(self)][self] = None |
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.
This fix is out of the scope of this PR, but it's fine to add it here.
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.
Yeah preferably this would have been a seperate PR.
…2007) * tests: Add test to check if RandomActivation is not sequential Adds a test that checks if the RandomActivation doesn't trigger agents in a sequential order. In theory this could give false positives (a test passing when it shouldn't, but that chance is around ~0.1^18). * fix for RandomActivation bug fixes #2006 * add agentset.shuffle unittest * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * ruff fix * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add agent to model.agent correctly even if super was not called fix for the second issue in #2006. If super is not present, we create the data structure but forget to add the agent to it. This is just a backward compatibility fix. * test: Shuffle more agents to prevent false negatives No the chance on a false negative is one in 12! instead of 4! (40 million instead of 24) --------- Co-authored-by: Jan Kwakkel <j.h.kwakkel@tudelft.nl> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
…2007) * tests: Add test to check if RandomActivation is not sequential Adds a test that checks if the RandomActivation doesn't trigger agents in a sequential order. In theory this could give false positives (a test passing when it shouldn't, but that chance is around ~0.1^18). * fix for RandomActivation bug fixes #2006 * add agentset.shuffle unittest * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * ruff fix * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * add agent to model.agent correctly even if super was not called fix for the second issue in #2006. If super is not present, we create the data structure but forget to add the agent to it. This is just a backward compatibility fix. * test: Shuffle more agents to prevent false negatives No the chance on a false negative is one in 12! instead of 4! (40 million instead of 24) --------- Co-authored-by: Jan Kwakkel <j.h.kwakkel@tudelft.nl> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
I was torn. It was raised in the same issue, and it was just 1 line. |
Adds a test that checks if the
RandomActivation
doesn't trigger agents in a sequential order. This test now fails, because as noted in #2006, it currently does do it in sequential order.In theory this could give false positives (a test passing when it shouldn't, but that chance is around ~0.1^18).