Fix AgentSet inplace shuffle (and thus RandomActivation), add tests#2007
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? |
|
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 |
|
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 mesa#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.
No the chance on a false negative is one in 12! instead of 4! (40 million instead of 24)
| 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.
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.
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 <[email protected]> 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 <[email protected]> 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. |
|
Hi, was any of these bugs present at 1.1.1? Could you clarify? Or are you sure the randomactivation scheduler used to work in 1.1.1 correctly? I cannot produce certain modelling results that I published with 1.1.1 in any of the subsequent versions. So far I tested 2.3.2 and 3.1.3. Thanks a lot that would be super helpful |
|
AgentSet was added near the end of MESA 2, so it most definately was not there for MESA 1.1.1. |
|
Ok great. Thanks for the quick reply. I used the time module still here like this in 1.1.1 mesa.time.RandomActivation(self). I am just trying to trace back what changed in (random) agent activation exactly compared to later versions. Even in 3+ versions I get strange results that I do not get using 1.1.1 |
|
Are you sure the issue is just agent activation? |
|
Not 100% of course, but given that with 1.1.1 the results look smooth, as expected given model specifications and are reproducible entirely using this version, but created abrupt strange behavior from time step 1 onwards in later versions I am also definitely not sure it is not agent activation in mesa. |
|
Of course I consider the likelihood for mesa to have a bug there quite low, as compared to my own model or how i used scheduler and time. Could you pinpoint me to the agent scheduling /sequencing in the newest release? I cannot find which module it is part of. Just so i understand what is happening in my 1.1.1 model. |
|
Agent scheduling was removed in MESA 3. All possible schedulers that existed can be implemented on top of Random activation in the simplest case is just |
|
Could also be in changes in seed / rng? If you want to get to the root of the issue, open a new issue with a Minimal, Reproducible Example |
Adds a test that checks if the
RandomActivationdoesn'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).