Skip to content

Fix AgentSet inplace shuffle (and thus RandomActivation), add tests#2007

Merged
rht merged 10 commits intomesa:mainfrom
EwoutH:test-randomactivation
Jan 26, 2024
Merged

Fix AgentSet inplace shuffle (and thus RandomActivation), add tests#2007
rht merged 10 commits intomesa:mainfrom
EwoutH:test-randomactivation

Conversation

@EwoutH
Copy link
Copy Markdown
Member

@EwoutH EwoutH commented Jan 26, 2024

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).

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).
@EwoutH EwoutH force-pushed the test-randomactivation branch from 612caa2 to a7a2c87 Compare January 26, 2024 12:30
@github-actions
Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
Schelling small 🔵 -1.3% [-1.5%, -1.1%] 🔵 -0.5% [-0.6%, -0.3%]
Schelling large 🔵 -1.4% [-1.5%, -1.3%] 🔵 -0.2% [-1.1%, +0.7%]
WolfSheep small 🔵 -0.2% [-0.5%, +0.2%] 🔵 -0.2% [-0.3%, -0.1%]
WolfSheep large 🔵 +0.7% [-0.0%, +1.3%] 🔵 -0.4% [-1.4%, +0.4%]
BoidFlockers small 🔵 -0.2% [-0.6%, +0.2%] 🔵 -0.4% [-1.0%, +0.3%]
BoidFlockers large 🔵 +0.1% [-0.3%, +0.5%] 🔵 -0.2% [-1.0%, +0.7%]

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Jan 26, 2024

If you change do_each to

    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?

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Jan 26, 2024

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 do_each

@quaquel quaquel force-pushed the test-randomactivation branch from b7603d9 to 90f0408 Compare January 26, 2024 13:05
@EwoutH
Copy link
Copy Markdown
Member Author

EwoutH commented Jan 26, 2024

Thanks! For clarity, we should document the difference between agents, _agents and agents_ somewhere

@EwoutH
Copy link
Copy Markdown
Member Author

EwoutH commented Jan 26, 2024

@quaquel could you add unittests for shuffle? And maybe we should check coverage of the AgentSet again at some point (not this PR).

@EwoutH EwoutH added the bug Release notes label label Jan 26, 2024
@quaquel
Copy link
Copy Markdown
Member

quaquel commented Jan 26, 2024

do you mean for AgentSet.shuffle?

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Jan 26, 2024

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.

@rht
Copy link
Copy Markdown
Contributor

rht commented Jan 26, 2024

/rerun-benchmarks

@EwoutH
Copy link
Copy Markdown
Member Author

EwoutH commented Jan 26, 2024

Looks good! Benchmarks depend on #2002.

Can’t approve this PR, but looks good to me. Recommend squash merge.

@EwoutH EwoutH requested a review from Corvince January 26, 2024 14:46
@EwoutH EwoutH changed the title tests: Add test to check if RandomActivation is not sequential Fix AgentSet inplace shuffle (and thus RandomActivation), add tests Jan 26, 2024
quaquel and others added 3 commits January 26, 2024 16:52
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)
@EwoutH EwoutH requested a review from rht January 26, 2024 16:39
except AttributeError:
# model super has not been called
self.model.agents_ = defaultdict(dict)
self.model.agents_[type(self)][self] = None
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 fix is out of the scope of this PR, but it's fine to add it here.

Copy link
Copy Markdown
Member Author

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.

@rht rht merged commit 29f5dad into mesa:main Jan 26, 2024
EwoutH added a commit that referenced this pull request Jan 26, 2024
…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>
EwoutH added a commit that referenced this pull request Jan 26, 2024
…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>
@quaquel
Copy link
Copy Markdown
Member

quaquel commented Jan 26, 2024

I was torn. It was raised in the same issue, and it was just 1 line.

@yannickoswald
Copy link
Copy Markdown

yannickoswald commented Nov 4, 2025

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

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Nov 4, 2025

AgentSet was added near the end of MESA 2, so it most definately was not there for MESA 1.1.1.

@yannickoswald
Copy link
Copy Markdown

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

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Nov 4, 2025

Are you sure the issue is just agent activation?

@yannickoswald
Copy link
Copy Markdown

yannickoswald commented Nov 4, 2025

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.

@yannickoswald
Copy link
Copy Markdown

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.

@quaquel
Copy link
Copy Markdown
Member

quaquel commented Nov 4, 2025

Agent scheduling was removed in MESA 3. All possible schedulers that existed can be implemented on top of AgentSet.

Random activation in the simplest case is just model.agents.shuffle_do("step")

@EwoutH
Copy link
Copy Markdown
Member Author

EwoutH commented Nov 4, 2025

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

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

Labels

bug Release notes label testing Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants