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

Bugfixes to DEVS #2478

Merged
merged 31 commits into from
Nov 9, 2024
Merged

Bugfixes to DEVS #2478

merged 31 commits into from
Nov 9, 2024

Conversation

quaquel
Copy link
Member

@quaquel quaquel commented Nov 9, 2024

Summary

This PR fixes several bugs in the experimental DEVS (Discrete Event Simulation) module and its usage in the wolf-sheep benchmark model. The changes improve initialization safety and timing consistency in event scheduling.

Motive

Several subtle bugs were discovered while working on #2470:

  1. The simulator's setup process could silently overwrite already scheduled events
  2. Models could attempt to run without proper initialization
  3. The ABMSimulator had incorrect timing for the first step event

These issues needed to be addressed to ensure reliable event scheduling and simulation execution.

Implementation

The changes include:

  1. Enhanced setup validation:

    • Added checks to prevent setup when time != starttime
    • Added checks to prevent setup when events are already scheduled
    • Added validation in run methods to ensure setup has been called
  2. Moved simulator setup responsibility:

    • Models that schedule events during initialization must now call simulator.setup() inside their __init__
    • Updated wolf-sheep model to follow this pattern
  3. Fixed ABMSimulator timing:

  4. Added run_next_event() method to support step-by-step simulation execution

Usage Examples

Example of proper model initialization with DEVS:

class MyModel(Model):
    def __init__(self, simulator, seed=None):
        super().__init__(seed=seed)
        self.simulator = simulator
        self.simulator.setup(self)  # Must be called before any event scheduling
        
        # Now safe to schedule events
        self.schedule_initial_events()

Additional Notes

  • This PR isolates DEVS-related changes from Support simulators in SolaraViz #2470
  • The changes are backward-incompatible for any code that relied on the old setup behavior
  • All existing tests have been updated and new tests added to verify the fixes
  • This affects experimental features only, no stable Mesa features are impacted

@quaquel quaquel requested a review from EwoutH November 9, 2024 21:09
@quaquel quaquel added experimental Release notes label bug Release notes label breaking Release notes label labels Nov 9, 2024
Copy link

github-actions bot commented Nov 9, 2024

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 -0.5% [-0.9%, -0.1%] 🔵 +0.2% [+0.1%, +0.3%]
BoltzmannWealth large 🔵 +0.2% [-0.3%, +0.6%] 🔵 +1.0% [+0.6%, +1.4%]
Schelling small 🔵 -0.0% [-0.2%, +0.2%] 🔵 +0.6% [+0.5%, +0.8%]
Schelling large 🔵 -0.3% [-0.6%, +0.0%] 🔵 +0.3% [-0.1%, +0.7%]
WolfSheep small 🔵 +0.7% [+0.3%, +1.1%] 🔴 +78.2% [+75.4%, +80.8%]
WolfSheep large 🔵 -1.3% [-1.6%, -1.0%] 🔴 +55.1% [+53.0%, +57.2%]
BoidFlockers small 🔵 +1.5% [+0.9%, +2.0%] 🔵 +2.7% [+1.9%, +3.6%]
BoidFlockers large 🔵 +0.9% [+0.5%, +1.4%] 🔵 +0.9% [+0.3%, +1.5%]

@EwoutH EwoutH added trigger-benchmarks Special label that triggers the benchmarking CI and removed trigger-benchmarks Special label that triggers the benchmarking CI labels Nov 9, 2024
@EwoutH
Copy link
Member

EwoutH commented Nov 9, 2024

Thanks, really appreciated. Generally, it looks good. Let's wait for the benchmarks to confirm performance.

Unrelated, but to confirm, the best practice is currently to define the simulator.setup(model) insode the init of the Model class, so self.simulator.setup(self)?

Copy link

github-actions bot commented Nov 9, 2024

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +1.2% [+0.3%, +2.0%] 🔵 +1.7% [+1.5%, +1.9%]
BoltzmannWealth large 🔵 +2.0% [+1.4%, +2.7%] 🔵 +0.7% [+0.1%, +1.3%]
Schelling small 🔵 +3.3% [+2.9%, +3.6%] 🔵 +1.7% [+1.5%, +1.9%]
Schelling large 🔵 +2.9% [+2.5%, +3.3%] 🔵 +0.9% [+0.2%, +1.5%]
WolfSheep small 🔵 +0.9% [+0.5%, +1.3%] 🔴 +74.6% [+71.8%, +77.2%]
WolfSheep large 🔵 +0.4% [-0.1%, +0.9%] 🔴 +52.6% [+49.3%, +55.9%]
BoidFlockers small 🔵 +0.4% [-0.2%, +0.9%] 🔵 -2.2% [-2.8%, -1.6%]
BoidFlockers large 🔵 -0.2% [-1.0%, +0.9%] 🔵 -2.3% [-3.0%, -1.5%]

@EwoutH
Copy link
Member

EwoutH commented Nov 9, 2024

Unfortunately, the performance degradation looks reproducible and significant.

Any idea what causing it?

@quaquel
Copy link
Member Author

quaquel commented Nov 9, 2024

Unrelated, but to confirm, the best practice is currently to define the simulator.setup(model) insode the init of the Model class, so self.simulator.setup(self)?

Yes. If setup is called after events have been scheduled, you get an exception. So if __init__ results in events being scheduled, you have to do it in model.__init__. I actually think I like this over the old situation.

@quaquel
Copy link
Member Author

quaquel commented Nov 9, 2024

Unfortunately, the performance degradation looks reproducible and significant.

Any idea what causing it?

yes: inccorrect behavior before. What happend was the following

  1. grass schedules regrowth events if not fully grown
  2. simulator.setup is called, canceling all events
  3. Only full grown grass is available to eat and to reschedule itself
  4. sheep die very quickly (first 10 turns or so)
  5. wolfs die shortly after

Once I had the animation in #2470, the error was obvious.

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

Good to know the root cause!

Also shows how valuable visual debugging can be.

@quaquel quaquel merged commit c078690 into projectmesa:main Nov 9, 2024
13 checks passed
@quaquel quaquel deleted the devs branch November 9, 2024 21:47
@quaquel
Copy link
Member Author

quaquel commented Nov 9, 2024

Also shows how valuable visual debugging can be.

absolutely

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Release notes label bug Release notes label experimental Release notes label
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants