Skip to content

Keep model.time a float when run_until or events get ints - #4052

Merged
EwoutH merged 1 commit into
mesa:mainfrom
AshNicolus:fix/model-time-float
Nov 10, 2026
Merged

EwoutH merged 1 commit into
mesa:mainfrom
AshNicolus:fix/model-time-float

Conversation

@AshNicolus

Copy link
Copy Markdown
Contributor

Pre-PR Checklist

  • This PR is a bug fix, not a new feature or enhancement.

Summary

model.time turned into an int after run_until(5) or inside an event scheduled at an int time, while run_for always gives a float. It now stays a float.

Bug / Issue

Fixes #4051.

_advance_time is the one place model.time is assigned, and it used until and event.time exactly as passed in. I first noticed this while testing #4048.

flowchart LR
    A["run_for(5)"] -->|"time + 5.0"| F["model.time = 5.0"]
    B["run_until(5)"] -->|"self.time = until"| I["model.time = 5 (int)"]
    C["schedule_event(at=2)"] -->|"self.time = event.time"| I
    I -->|"step() adds 1"| I2["stays int: 6"]
    I -.->|"this PR: float(...) at both assignments"| F
Loading
Case main this PR
run_until(5) 5 5.0
run_until(4) then step() 5 5.0
inside an event at=2 2 2.0
JSONDataRecorder rows after run_until(3) 0.0, 1.0, 2.0, 3 0.0, 1.0, 2.0, 3.0

Implementation

Both assignments in _advance_time now convert to float: self.time = float(event.time) and self.time = float(until). Nothing else changes.

Testing

  • Added test_time_stays_float_with_int_times (an event at at=2, run_until(4), then step()). It fails on main and passes here.
  • Full suite passes with pytest tests/ -Werror (1104 passed), and ruff is clean.
  • Merges cleanly with Fix run_until skipping events scheduled for the current time #4048, which changes the guard in the same function. With both applied, all 26 tests in test_schedule_run.py pass.

Additional Notes

The new test sits at the end of TestEdgeCases, not next to where #4048 adds its test, so either can merge first.

_advance_time assigned the end time and each event's time as given, so
run_until(5) or schedule_event(at=2) left model.time as an int, while
run_for always gave a float. That showed up as mixed 3.0 / 3 times in
JSONDataRecorder output and t=5 vs t=5.0 in labels. Convert both
assignments to float.
@github-actions github-actions Bot added the testing Release notes label label Nov 8, 2026
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

Performance benchmarks:

Model Size Init time [95% CI] Run time [95% CI]
BoltzmannWealth small 🔵 +0.4% [-0.8%, +1.7%] 🔵 +1.6% [+1.3%, +1.9%]
BoltzmannWealth large 🔵 +1.5% [-0.8%, +3.5%] 🔵 -5.9% [-11.8%, +0.4%]
Schelling small 🔵 -3.3% [-4.2%, -2.3%] 🔵 -0.1% [-1.0%, +0.9%]
Schelling large 🔵 -0.3% [-1.3%, +0.7%] 🟢 -6.4% [-9.3%, -3.8%]
WolfSheep small 🔵 -0.5% [-1.7%, +0.6%] 🔵 +4.1% [-0.7%, +8.8%]
WolfSheep large 🔵 -0.5% [-3.7%, +2.3%] 🔵 -3.2% [-8.7%, +2.0%]
SugarscapeG1mt small 🔵 +5.1% [+2.9%, +7.5%] 🔵 +0.9% [-1.1%, +3.1%]
SugarscapeG1mt large 🔴 +11.2% [+8.5%, +14.6%] 🔵 +3.8% [+1.8%, +5.8%]
BoidFlockers small 🔵 +1.0% [+0.1%, +1.8%] 🔵 -1.0% [-1.5%, -0.5%]
BoidFlockers large 🔴 +7.5% [+5.8%, +9.2%] 🔵 +1.2% [+0.3%, +2.1%]
MultiLevelAllianceModel small 🔴 +19.5% [+17.2%, +21.7%] 🔴 +4.1% [+3.1%, +5.1%]
MultiLevelAllianceModel large 🔴 +13.3% [+11.8%, +14.7%] 🔴 +41.3% [+40.0%, +42.8%]

@mesa-triage-bot

Copy link
Copy Markdown

#4057 also targets the float-time bug in #4051. These two PRs should be considered together so a maintainer can choose which carries the fix.


Automated triage note, written by an AI model for the Mesa maintainers. It can be wrong, and it doesn't make decisions: maintainers do. If it missed the point, say so here.

@EwoutH

EwoutH commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

@AshNicolus Thanks for the bug and the fix. Could you compare your approach with #4057? I'm inclined to give you the first shot since you filed the issue and opened the PR first, but I would like to know differences between the fixes and their implications.

@AshNicolus

Copy link
Copy Markdown
Contributor Author

Thanks @EwoutH. Compared both on current main:

#4052 #4057
model.time after run_until(5), then step() 5.0, 6.0 5.0, 6.0
model.time inside an event at=2 2.0 2.0
schedule_event(at=2).time 2 2.0
next_scheduled_time, recurring from 3 3 3.0
merges with main yes conflicts
flowchart LR
    I["int from user<br/>run_until(5), at=2"] --> E["Event.time"]
    I --> U["until"]
    E -->|"#4057: float() here"| M["model.time"]
    U -->|"#4057: float() here"| M
    E -.->|"#4052: float() only<br/>when assigning model.time"| M
    E --> N["event.time /<br/>next_scheduled_time<br/>(public, typed float)"]
Loading

If you'd rather have Event.time covered too, I can add the cast in Event.__init__ here (that makes the event one in _advance_time unnecessary), or this can go with #4057 once it's rebased. Either is fine with me.

@EwoutH

EwoutH commented Nov 10, 2026

Copy link
Copy Markdown
Member

Thanks for explaining. Are there any obvious cases which could introduce unintended behavior from model.time being a float and Event.time being an int?

@AshNicolus

Copy link
Copy Markdown
Contributor Author

Not that I could find. Python compares ints and floats exactly, so I checked the spots where they meet:

  • Ordering: an event at=2 and one at=2.0 tie on time and are ordered by priority as usual.
  • model.time == event.time, dict keys and next_scheduled_time - model.time all behave the same.

The only visible difference is the type itself: event.time prints as 2 and stays an int if someone writes it to JSON. (Ints above 2**53 would also lose precision when cast, which isn't a realistic model time.)

So it's about consistency more than behaviour. If you'd like Event.time to be a float too, I'm happy to add the cast in Event.__init__ here.

@EwoutH EwoutH left a comment

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.

Thanks, I think for now this is enough.

@EwoutH EwoutH added the bug Release notes label label Nov 10, 2026
@EwoutH
EwoutH merged commit 1d3b61e into mesa:main Nov 10, 2026
18 checks passed
EwoutH pushed a commit that referenced this pull request Nov 22, 2026
_advance_time assigned the end time and each event's time as given, so
run_until(5) or schedule_event(at=2) left model.time as an int, while
run_for always gave a float. That showed up as mixed 3.0 / 3 times in
JSONDataRecorder output and t=5 vs t=5.0 in labels. Convert both
assignments to float.

(cherry picked from commit 1d3b61e)
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.

model.time becomes an int after run_until or events scheduled at int times

2 participants