Repository navigation
Fix run_until skipping events scheduled for the current time - #4048
Conversation
7f831b9 to
d5e3ccd
Compare
|
@quaquel ready for a review |
|
When introducing the current implementation of event scheduling in mesa 3.5, we had a long discussion on time. I don't have the time atm to look it up but should be findable quite easily. I would appreciate checking and linking to that this discussion. |
|
@quaquel found it, it's this one: #2921 read through it. they settled on keeping run_for/run_until simple, stopping conditions go in run_while, and no heartbeat. the case where until equals the current time never came up. from the code: schedule_event allows an event at the current time, and run_until only complains when end_time is before model.time. the <= check in _advance_time (from #3329, for the rewind bug #3327) is the only thing blocking it. so this pr just changes that check to <. |
|
Thanks for reaching out and opening a PR. I think you’re right and events scheduled now should be executed without moving time forward. The order and mechanics could be difficult though. @quaquel, do you agree? |
|
That is not exactly the discussion I had in mind. I tried to find it myself but haven't succeeded so far. Anyway, at some point we discussed when to advance time: before executing an event or after executing an event. I think we agree on before the event and that is also what the code does.
So if we advance time before executing, So yes, I agree with @EwoutH. I also think that the proposed fix is the correct one (and bring the error message in line with the code, which currently is not the case. |
|
thanks @quaquel, updated both warnings to say the end time is earlier than the current time. added message checks to the existing tests too. |
AshNicolus
left a comment
There was a problem hiding this comment.
Tested the mechanics against main, since that was the open question. With the model at t=3: an event at now fires under both run_until(now) and run_for(0); an event at now that schedules another at now fires both, in order; a schedule_recurring starting at now fires once and keeps its next at 4; the step at 3 doesn't run again; run_until(now) with nothing queued is now a silent no-op; and on a fresh model an event at 0 fires with run_until(0) without triggering a step. time doesn't emit CHANGED (same value), so a DataRecorder gets no extra snapshot, and its final row at 3 now shows the event's effect. The 3 new tests fail on main, and the full suite passes here (1105).
flowchart LR
A["run_until(3) at time 3<br/>event queued at 3"] --> B{"_advance_time:<br/>until vs time"}
B -->|"main: until <= time"| C["warn + return<br/>event waits until t=4"]
B -->|"PR: until < time"| D["event loop runs"]
D --> E["event at 3 fires<br/>time stays 3"]
flowchart TD
S["run_until(now)"] --> G{"until < time?"}
G -->|yes| W["warn: end time is earlier<br/>than current time"]
G -->|no| P["pop next event"]
P --> Q{"event.time <= until?"}
Q -->|yes| X["time = event.time (unchanged)<br/>execute"]
X -->|"may schedule more at now"| P
Q -->|no| R["push back, stop"]
R --> T["time = until<br/>(same value: no CHANGED signal)"]
T --> E["RUN_ENDED: recorder final row"]
| Case (model at t=3) | main | this PR |
|---|---|---|
event at now, run_until(now) |
not fired, warning | fired at 3 |
event at now, run_for(0) |
not fired, warning | fired at 3 |
| event at now schedules another at now | nothing fires | both fire, in order |
no events, run_until(now) |
warning | silent no-op |
fresh model, event at 0, run_until(0) |
not fired | fired at 0, no step |
schedule_recurring starting at now |
not fired | fires at 3, next stays at 4 |
DataRecorder row at 3 after event sets value = 99 |
0 |
99 |
Side note, not from this PR: model.time is annotated float, but run_until(7) leaves it as int 7 already on main, and with this change run_until(3) at 3.0 also turns it into 3. Maybe worth a float(until) in a follow-up. LGTM.
|
@AshNicolus saw #4052, thanks for picking that up. it covers the int time issue separately, so i'll keep this PR focused on events at the current time. |
|
Something I’m thinking about: should we add a keyword and make this configurable? I was thinking of Would this make sense? Are there use cases for it? |
|
@EwoutH stopping before a boundary event could help when a parameter needs to be changed from outside the model. scheduled changes can already use event priorities. one detail is that leaving events pending while advancing time would still trigger RUN_ENDED, so a recorder can write the state before those events run. i'd keep the default true and discuss the option in a follow-up, so we can agree on that behavior without widening this fix. would that work? |
* fix: process events due at the current time in run_until * fix: align time advancement warnings with the guards --------- Co-authored-by: Jan Kwakkel <[email protected]> (cherry picked from commit c9600b8)
Pre-PR Checklist
Summary
run_until(model.time) skips events scheduled for the current time: it warns that the end time "is not larger than current time" and returns, so the event only fires one step later. schedule_event accepts an event at the current time (it only rejects times in the past), so run_until should process it.
Bug / Issue
Fixes #4047
The early return in Model._advance_time (if until <= self.time:) warns and returns before the event loop runs, so an event due exactly at until == self.time is skipped. That guard was added in #3329 for #3327, but #3327 was about run_until going backward (until < time); the equality case looks unintended.
Implementation
Changed the guard in _advance_time (mesa/model.py) from
until <= self.timetountil < self.time, so events due exactly at the end time are processed and the warning only fires when the end time is actually in the past.Testing
Additional Notes
None.