-
Notifications
You must be signed in to change notification settings - Fork 988
RandomActivation scheduler not working after upgrade 2.1 -> 2.2.3 #2006
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Comments
Thanks for reaching out. I will test this tomorrow morning, if no one else has done it by then.
Calling |
@rht do you have time to investigate this? |
I'd need some help debugging all the problems caused by 2.1.x -> 2.2.x. Hands are full with elsewhere. |
Unfortunately, I can reproduce this issue for both Mesa 2.2.0 and 2.2.3, while the correct behavior (shuffled agent steps) on Mesa 2.1.5. @projectmesa/maintainers this is a major issue and needs immediate care, we broke I will investigate further. |
I've narrowed it down to the fact that the AgentSet's Lines 144 to 147 in 99b35fc
|
It's the inplace updating that isn't working. Without it, it works correctly: @quaquel you wrote this code and know more about the AgentSet implementation. Could assist in fixing the inplace updating? |
Thanks for your work, Mesa is an amazing framework! |
I can try this afternoon. I don't see an obvious mistake in the code. AgentSet.shuffle shuffled_agents = list(self)
self.random.shuffle(shuffled_agents)
return (
AgentSet(shuffled_agents, self.model)
if not inplace
else self._update(shuffled_agents)
) random.shuffle is inplace. List(self) returns a list of hard refs to the agents. AgentSet._update def _update(self, agents: Iterable[Agent]):
"""Update the AgentSet with a new set of agents.
This is a private method primarily used internally by other methods like select, shuffle, and sort.
"""
self._agents = weakref.WeakKeyDictionary({agent: None for agent in agents})
return self We create a new WeakKeyDictionary so it should shuffle. Accidentally, This is the code I was playing with for #1993. So my to do;
|
Thanks. I had already written an additional unittest for RandomActivation itself, we can test updates also against that (#2007). |
Found it. The problem is in the scheduler not in AgentSet. do_each should be def do_each(self, method, shuffle=False):
if shuffle:
self._agents.shuffle(inplace=True)
self._agents.do(method) |
I committed the fix to #2007. |
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.
…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>
@profConradi we just released Mesa 2.2.4 which should fix this issue. Could you confirm this is the case? |
Yes, the RandomActivation Scheduler of 2.2.4 works correctly! Thanks! |
Great to hear, thanks a lot for reporting this issue! For such a serious bug to slip thought the the review, test and release process is really unfortunate. I wrote some thoughts on it here: #1909 (comment) |
I upgraded 2.1 to 2.2.3 and in this simple test model RandomActivation scheduler does not work anymore: agent activation is done sequentially.
Furthermore if I not call super().init() in the model class, the first agent is not added to the scheduler. Is this a bug or a mistake on my part?
The text was updated successfully, but these errors were encountered: