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

DEVS/simulator: Use Model time #2248

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 12 additions & 20 deletions mesa/experimental/devs/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ def __init__(self, time_unit: type, start_time: int | float):
self.start_time = start_time
self.time_unit = time_unit

self.time = self.start_time
self.model = None

def check_time_unit(self, time: int | float) -> bool: ...
Expand All @@ -47,27 +46,22 @@ def setup(self, model: Model) -> None:
"""
self.event_list.clear()
self.model = model

def reset(self):
"""Reset the simulator by clearing the event list and removing the model to simulate"""
self.event_list.clear()
self.model = None
self.time = self.start_time

def run_until(self, end_time: int | float) -> None:
while True:
try:
event = self.event_list.pop_event()
except IndexError: # event list is empty
self.time = end_time
self.model._time = end_time
break

if event.time <= end_time:
self.time = event.time
self.model._time = event.time
event.execute()
else:
self.time = end_time
self._schedule_event(event) # reschedule event
self.model._time = end_time
self._schedule_event(event)
break

def run_for(self, time_delta: int | float):
Expand All @@ -78,7 +72,7 @@ def run_for(self, time_delta: int | float):
plus the time delta

"""
end_time = self.time + time_delta
end_time = self.model._time + time_delta
self.run_until(end_time)

def schedule_event_now(
Expand Down Expand Up @@ -129,15 +123,14 @@ def schedule_event_absolute(
SimulationEvent: the simulation event that is scheduled

"""
if self.time > time:
if self.model._time > time:
raise ValueError("trying to schedule an event in the past")

event = SimulationEvent(
time,
function,
priority=priority,
function_args=function_args,
function_kwargs=function_kwargs,
...
)
self._schedule_event(event)
return event
Expand All @@ -164,7 +157,7 @@ def schedule_event_relative(

"""
event = SimulationEvent(
self.time + time_delta,
self.model._time + time_delta,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps betraying my java background, but changing attributes of other objects directly like this makes me uncomfortable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that. I think in this case it allows the model to be the ground truth of time. We should document it as "A DEVS simulator is an alternative way to manage agent activation and progress time".

function,
priority=priority,
function_args=function_args,
Expand Down Expand Up @@ -253,19 +246,18 @@ def run_until(self, end_time: int) -> None:
try:
event = self.event_list.pop_event()
except IndexError:
self.time = end_time
self.model._time = end_time
break

if event.time <= end_time:
self.time = event.time
self.model._time = event.time
if event.fn() == self.model.step:
self.schedule_event_next_tick(
self.model.step, priority=Priority.HIGH
)

event.execute()
else:
self.time = end_time
self.model._time = end_time
self._schedule_event(event)
break

Expand All @@ -277,7 +269,7 @@ def run_for(self, time_delta: int):
plus the time delta

"""
end_time = self.time + time_delta - 1
end_time = self.model._time + time_delta - 1
self.run_until(end_time)


Expand Down
Loading