Repository navigation
Keep model.time a float when run_until or events get ints - #4052
Conversation
_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.
|
Performance benchmarks:
|
|
#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. |
|
@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. |
|
Thanks @EwoutH. Compared both on current main:
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)"]
If you'd rather have |
|
Thanks for explaining. Are there any obvious cases which could introduce unintended behavior from |
|
Not that I could find. Python compares ints and floats exactly, so I checked the spots where they meet:
The only visible difference is the type itself: So it's about consistency more than behaviour. If you'd like |
EwoutH
left a comment
There was a problem hiding this comment.
Thanks, I think for now this is enough.
_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)
Pre-PR Checklist
Summary
model.timeturned into an int afterrun_until(5)or inside an event scheduled at an int time, whilerun_foralways gives a float. It now stays a float.Bug / Issue
Fixes #4051.
_advance_timeis the one placemodel.timeis assigned, and it useduntilandevent.timeexactly 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"| Frun_until(5)55.0run_until(4)thenstep()55.0at=222.0JSONDataRecorderrows afterrun_until(3)0.0, 1.0, 2.0, 30.0, 1.0, 2.0, 3.0Implementation
Both assignments in
_advance_timenow convert to float:self.time = float(event.time)andself.time = float(until). Nothing else changes.Testing
test_time_stays_float_with_int_times(an event atat=2,run_until(4), thenstep()). It fails onmainand passes here.pytest tests/ -Werror(1104 passed), and ruff is clean.test_schedule_run.pypass.Additional Notes
The new test sits at the end of
TestEdgeCases, not next to where #4048 adds its test, so either can merge first.