Skip to content
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

Merged
merged 10 commits into from
Jan 26, 2024

Conversation

EwoutH
Copy link
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
Copy link

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
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
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
Member Author

EwoutH commented Jan 26, 2024

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

@EwoutH
Copy link
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
Member

quaquel commented Jan 26, 2024

do you mean for AgentSet.shuffle?

@quaquel
Copy link
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
Contributor

rht commented Jan 26, 2024

/rerun-benchmarks

@EwoutH
Copy link
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.

tests/test_agent.py Outdated Show resolved Hide resolved
@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 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.
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
@@ -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
Copy link
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
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 projectmesa:main Jan 26, 2024
11 of 13 checks passed
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 <j.h.kwakkel@tudelft.nl>
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 <j.h.kwakkel@tudelft.nl>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@quaquel
Copy link
Member

quaquel commented Jan 26, 2024

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

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.

3 participants