Skip to content

Reimplement schedulers to use AgentSet#1926

Merged
tpike3 merged 9 commits intomesa:mainfrom
quaquel:schedulers
Jan 3, 2024
Merged

Reimplement schedulers to use AgentSet#1926
tpike3 merged 9 commits intomesa:mainfrom
quaquel:schedulers

Conversation

@quaquel
Copy link
Copy Markdown
Member

@quaquel quaquel commented Dec 23, 2023

This PR builds on #1916 and updates the schedulers to use the new AgentSet class and/or weakrefs. It also adds a new feature: schedulers take an optional agents keyword argument in __init__ so you can spawn a scheduler with all agents.

My aim with this PR is to be fully backward compatible but raise warnings when methods that have become redundant are being used. The current code passes all unit tests.

Still to do:

  • - update docs
  • - check type annotations
  • - double check to ensure backward compatible behavior
  • - code formatting
  • - add some additional tests

@codecov
Copy link
Copy Markdown

codecov bot commented Dec 23, 2023

Codecov Report

Attention: 3 lines in your changes are missing coverage. Please review.

Comparison is base (7bd42e7) 79.14% compared to head (37bdb57) 80.21%.
Report is 2 commits behind head on main.

Files Patch % Lines
mesa/time.py 94.54% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1926      +/-   ##
==========================================
+ Coverage   79.14%   80.21%   +1.06%     
==========================================
  Files          15       15              
  Lines        1098     1137      +39     
  Branches      236      245       +9     
==========================================
+ Hits          869      912      +43     
+ Misses        198      195       -3     
+ Partials       31       30       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@quaquel quaquel marked this pull request as draft December 23, 2023 19:57
@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Dec 24, 2023

Looks like a nice start!

I think a big conceptual design decision we need to make is if we want to support multiple schedulers. Did we do so in the past?

If we only have one scheduler, we could simplify and assume a lot, for example that all agents are in the scheduler. We don't have to explicitly add or remove them, or even keep track of them in the scheduler. There would also be one single step/time clock. It would probably allow deprecating all schedulers, and moving a lot of the requesting of behavior to AgentSet.do(). It would be a flexible approach with lot possibilities, like described in #1912.

However, if we do want to support multiple schedulers, we need to leave a lot of stuff in the scheduler, at least the time of that scheduler and which agents are part of it. Personally, I can't try of that many cases you can't solve with the AgentSet approach, but there might be any.

Also curious what @jackiekazil @tpike3 and @Corvince think on this matter.

@quaquel
Copy link
Copy Markdown
Member Author

quaquel commented Dec 24, 2023

I have used multiple schedulers in the past (pre RandomActivationByType), and more generally, I like its flexibility. Also, this PR's only aim is to keep the current Schedulers up to date by using AgentSet. Discussing the future of schedulers is beyond the scope of this PR.

As indicated in #1912, one possible direction could be to shift to a central EventList. This creates a central understanding of time. All existing functionality of the various schedulers could be rebuilt on such an event list.

mesa/time.py Outdated
if not self._remove_warning_given:
self._remove_warning_given = True
warnings.warn(
"Because of the shift to using weakrefs, it is no longer needed to explicitly remove"
Copy link
Copy Markdown
Member

@EwoutH EwoutH Dec 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this holds up as long a you use a single scheduler, and assume agents in model == agents in schedulers. However, as you let that assumption go you might want to remove agents from a scheduler but not from a model.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, I find it weird you still have to explicitly add agents to the scheduler, but don't explicitly have to remove them.

Copy link
Copy Markdown
Member Author

@quaquel quaquel Dec 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comes back to having one or more than one scheduler. If you have only one, then yes, there is no need to add or remove them. But that would be a backward incompatible change and thus is outside the scope of this PR.

further tweaks based on feedback from @EwoutH
@quaquel
Copy link
Copy Markdown
Member Author

quaquel commented Dec 24, 2023

I have reviewed the code several times, fixed the docs, and tested against various examples. This is ready for a proper check. @EwoutH, @Corvince, @jackiekazil, or @tpike3 do you have a chance to check this?

@Corvince
Copy link
Copy Markdown
Contributor

Probably not in the coming days, but I really look forward to review this!

@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Dec 24, 2023

Will try to review it later today!

Copy link
Copy Markdown
Member

@EwoutH EwoutH left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good in general, and agree with the intend of this PR.

However, I think that we only should make minimal changes until we decide which direction to go with the schedulers (None, one per model, or multiple). So while this PR is good, I think the next step is a broad discussion about how we see the future of activating agents to do things.

stacklevel=2,
)

agent_keys = [agent.unique_id for agent in self._agents]
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed on deprecating in this case.

However, while in this way it's probably fine, we're technically deprecating something for which the replacement (AgentSet) is still experimental. Might be something to keep in mind.

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.

Again, this is outside the scope of this PR, but I want to do a separate discussion on unique_id. Now that it is no longer needed for the schedulers, its only function is to identify agents when gathering statistics.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree - outside of this pr - suggested place for the discussion?

Copy link
Copy Markdown
Member Author

@quaquel quaquel Jan 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that #1933 also relies on unique_id, we can perhaps discuss it there. Alternatively, we can have a broader discussion on the future of data collection and make it part of that.

@quaquel quaquel changed the title [WIP] Reimplement schedulers to use AgentSet Reimplement schedulers to use AgentSet Dec 29, 2023
@quaquel quaquel marked this pull request as ready for review December 29, 2023 12:55
@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Jan 1, 2024

@quaquel What's your guidance on this regarding the next release? Would you recommend having this in the 2.2.0 release, or to release 2.2.0 first and merge this on the 2.3 development branch?

@jackiekazil
Copy link
Copy Markdown
Member

@quaquel

RE: Release -- would like to hear your thoughts on the release integration mentioned by @EwoutH.
Re: scheduler - +1 to what you said -- multiple schedulers is a requirements.

RE: Review -- I looked over the code this evening. I will do a test run tomorrow evening.

@quaquel
Copy link
Copy Markdown
Member Author

quaquel commented Jan 2, 2024

  1. @EwoutH : Ideally, make it part of 2.2
  2. @jackiekazil: thanks for the clarity on having multiple schedulers as a requirement. This does, however, leads into a next question: what tracks the global simulation time?

Copy link
Copy Markdown
Member

@tpike3 tpike3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks at @quaquel I think this really highlights a new and positive path for Mesa.

@tpike3 tpike3 merged commit 811b605 into mesa:main Jan 3, 2024
@EwoutH EwoutH added the enhancement Release notes label label Jan 9, 2024
@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Jan 26, 2024

Unfortunately, this PR, together with an implementation error in #1916, broke RandomActivation, and more important, went completely unnoticed for multiple weeks, until an user reported it in #2006. We should evaluate very critically how such a major unintended behavior could slip through the cracks.

@quaquel
Copy link
Copy Markdown
Member Author

quaquel commented Jan 26, 2024

  1. we did not test AgentSet.shuffle properly.
  2. We moved the schedulers to AgenSet for 2.2. We should have waited with this for 2.3 (although I doubt we would have seen these bugs before then)

@EwoutH
Copy link
Copy Markdown
Member

EwoutH commented Jan 26, 2024

(and thus the next questions are, should we alter our review process, testing standards or something else, and if so, how)

@quaquel
Copy link
Copy Markdown
Member Author

quaquel commented Jan 26, 2024

See #2007. The fix can be found there.

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

Labels

enhancement Release notes label

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants