Events
In a simulation model, an event represents an action that will take place in the (simulated) future.
In the PLT Scheme Simulation Collection, an event represents the (simulated) future application of a procedural object to a list of arguments.
5.1 The event Structure
Structure:
event |
Contract: (struct event ((time (<=/c 0.0)) (priority real?) (process (union/c process? #f)) (function (union/c procedure? #f)) (arguments list?) (event-list (union/c event-list? #f)) (linked-event-list (union/c event-list? #f)))) |
|
function
field can contain any procedural object, including continuations, any event can call the wait/work
function. This is a slight extension to the definition of event given above in that an event may represent a sequence of actions.
Function:
(make-event time priority process function arguments) |
Contract: (-> real? real? (union/c process? #f) (union/c procedure? #f) (union/c list? #f)) |
|
Note that the make-event is not typically used in user code, instead the schedule macro is used to create and schedule an event.
5.2 Event Lists
An event list stores a list of events. Events are stored in order of their time
values.
Structure:
event-list |
Contract: (struct event-list ((events list?))) |
|
|
|
Function:
(make-event-list) |
Contract: (-> event-list?) |
|
Function:
(event-list-empty? event-list) |
Contract: (-> event-list? boolean?) |
|
Function:
(event-list-add! event-list event) |
Contract: (-> event-list? event? any) |
|
time
and priority
.
Function:
(event-list-remove! event-list event) |
Contract: (-> event-list? event? any) |
|
Function:
(event-list-pop! event-list) |
Contract: (-> event-list? event?) |
|
5.3 Example - Functions as Events
This example is a simulation model of a simple system. Customer arrivals are exponentially distributed with an interrival time of 4.0 minutes. The time a customer remains in the system is uniformally distributed between 2.0 and 10.0 minutes. The output is a simple trace of customer arrivals and departures.
The PLT Scheme Science Collection provides the random distribution functions.
; Example 0 - Functions as Events (require (planet "simulation.ss" ("williams" "simulation.plt"))) (require (planet "random-distributions.ss" ("williams" "science.plt"))) (define (generator n) (do ((i 0 (+ i 1))) ((= i n) (void)) (wait (random-exponential 4.0)) (schedule now (customer i)))) (define (customer i) (printf "~a: customer ~a enters~n" (current-simulation-time) i) (work (random-flat 2.0 10.0)) (printf "~a: customer ~a leaves~n" (current-simulation-time) i)) (define (run-simulation n) (with-new-simulation-environment (schedule (at 0.0) (generator n)) (start-simulation)))
The simulation model is executed by calling (run-simulation n)
, where n
is the number of customer to simulate through the system. For example, (run-simulation 10)
produces the following output:
>(run-simulation 10) 0.6153910608822503: customer 0 enters 5.599485116393393: customer 1 enters 6.411843645405005: customer 2 enters 8.48917994426752: customer 0 leaves 10.275428842274628: customer 1 leaves 14.749397986170655: customer 2 leaves 23.525886616767437: customer 3 enters 27.18604340910279: customer 3 leaves 32.1644631797164: customer 4 enters 33.14558760001698: customer 5 enters 39.67682614849173: customer 4 leaves 40.486553934113665: customer 6 enters 41.168084930967424: customer 5 leaves 45.72670063299798: customer 6 leaves 46.747675912143016: customer 7 enters 49.212327970772435: customer 8 enters 50.556538752352886: customer 9 enters 51.46738784004611: customer 8 leaves 52.514846525674855: customer 7 leaves 56.11635302397275: customer 9 leaves >