Validate non-finite time values in Model time progression and event scheduling
Describe the bug The Model class does not currently validate whether time-related inputs are finite numbers. Methods such as run_for(), run_until(), and schedule_event() accept values like float("nan"), float("inf"), and float("-inf").
For example, calling model.run_until(float("nan")) can result in model.time being set to NaN, because _advance_time() does not explicitly validate the target time before updating the simulation clock. Similarly, schedule_event() can accept a NaN event timestamp because the existing past-time comparison does not reject NaN values. This can lead to unexpected simulation time progression and unreliable event scheduling.
Expected behavior Time-related methods should validate their inputs before modifying the simulation state.
- run_for() should reject non-finite durations.
- run_until() should reject non-finite target times.
- _advance_time() should prevent non-finite values from being assigned to model.time.
- schedule_event() should reject non-finite event timestamps.
Invalid inputs should raise a clear ValueError without modifying the model's current simulation time or event queue. Valid finite timestamps and existing event scheduling behavior should remain unchanged.
To Reproduce
from mesa import Model
class TestModel(Model):
def step(self):
pass
model = TestModel()
print("Initial time:", model.time)
model.run_until(float("nan"))
print("Time after run_until:", model.time)
print("Is time NaN:", model.time != model.time)
Observed behavior The model accepts the NaN target time, and model.time becomes NaN.
Similar behavior can be investigated with:
model = TestModel()
model.run_for(float("nan"))
model = TestModel()
model.schedule_event(lambda: None, at=float("nan"))