Simulation Control (Basic)
The PLT Scheme Simulation Collection provides functionality for basic simulation control. Basic simulation control includes the simulation main loop for controlling the execution of events and processes, scheduling events and processes, and simulating passing of (simulated) time (i.e. waiting and working).
4.1 Scheduling Events and Processes
One of the main characteristics of discrete simulation models is the ability to schedule procedural simulation elements (e.g. events and processes) to occur at specified (simulated) times. Typically, an event list is maintained that contains entries for each of the scheduled elements.
In the simulation collection, each simulation environment has three such event lists: the now event list, the future event list, and the continuous event list. The now event list contains entries for elements that are to be executed before the simulation clock is advanced, i.e. now. The future event list contains entries for elements that are to be executed in the (simulated) future. The continuous event list is used for continuous simulation models and is described in Chapter 10 Continuous Simulation Models.)
Macro:
(schedule now (function . arguments) [#:priority priority]) (schedule (at time) (function . arguments) [#:priority priority]) (schedule (in duration) (function . arguments) [#:priority priority]) (schedule (when event) (function . arguments) [#:priority priority]) (schedule time (function . arguments) [#:priority priority]) |
|
|
(current-simulation-environment) is used.The time is specified using one of the following forms:
now- The event or process is scheduled to be executed before the simulation clock is advanced. That is, it is added to the now event list.(at time)- The event or process is scheduled to be executed at the specified time. An error is signaled if time is in the (simulated) past. That is, time must be greater than or equal to(simulation-environment-time simulation-environment).(in duration)- The event or process is scheduled to be executed after the specified duration has elapsed. This is equivalent to(at (+ duration (simulation-environment-time simulation-environment)). An error is signaled if duration is negative.(when event)- The event or process is scheduled to be executed at the same time as (i.e. when) the specified event is executed. This is called a linked event.time - This is equivalent to
(at time).
4.2 Controlling the Simulation Main Loop
Function:
(start-simulation) |
|
Contract: (-> any) |
|
|
loop-next and loop-exit continuations in the current simulation environment. It then executes events and processes from the event list(s) until either there are no more to be executed, or the simulation main loop is explicitly exited.The following pseudo code shows the processing for the main loop, including continuous simulation models.
simulation main loop:
save the exit continuation
loop:
save the next continuation
if the now event list is not empty
execute the next now (discrete) event
else if the future event list is not empty
if the continuous event list is not empty
execute the continuous events until
the time of the next future event
;; this may terminate early and reenter
;; the loop via the next continuation
else
execute the next future (discrete) event
else
if the continuous event list is not empty
execute the continuous events forever
;; this may terminate early and reenter
;; the loop via the next continuation
;; otherwise, the loop would never end
else
exit
end loop
end simulation main loop
Function:
(stop-simulation) |
|
Contract: (-> any) |
|
|
(current-simulation-loop-exit).
A convenient usage of stop-simulation is to schedule its execution at a point in time the execution is to run until. For example:
(schedule (at 1000.0) (stop-simulation))
will cause the simulation to end at (simulated) time 1000.0. [Note that it may terminate earlier if at any earlier (simulated) time there are no more events to execute.]
4.3 Simulating Waiting and Working
Function:
(wait/work delay) (wait delay) (work delay) |
|
Contract: (-> real? any) |
|
|
wait/work function simulates the passage of (simulated) time. The event or process calling the function will wait for the specified delay. wait and work are aliases for the wait/work function.