Using the Simulation Collection

This chapter describes how to use the PLT Scheme Simulation Collection and introduces its conventions.

The simulation collection requires the PLT Scheme Science Collection and automatically loads the required portions of it using PLaneT. Code using the simulation collection often also requires the science collection and must reference it as specified in the PLT Scheme Science Collection Reference Manual[13].

2.1  An Example

The following code demonstrates the use of the PLT Scheme Simulation Collection by calculating queue and resource utilization statistics for a simple queueing model.

; Example 3 - Data Collection

(require (planet "simulation-with-graphics.ss"
                 ("williams" "simulation.plt")))
(require (planet "random-distributions.ss"
                 ("williams" "science.plt")))

(define n-attendants 2)
(define attendant #f)

(define-process (generator n)
  (do ((i 0 (+ i 1)))
      ((= i n) (void))
    (wait (random-exponential 4.0))
    (schedule now (customer i))))

(define-process (customer i)
  (with-resource (attendant)
    (work (random-flat 2.0 10.0))))

(define (run-simulation n)
  (with-new-simulation-environment
   (set! attendant (make-resource n-attendants))
   (schedule (at 0.0) (generator n))
   (accumulate (variable-statistics
                (resource-queue-variable-n attendant)))
   (accumulate (variable-history
                (resource-queue-variable-n attendant)))
   (start-simulation)
   (printf "--- Example 3 - Data Collection ---~n")
   (printf "Maximum queue length = ~a~n"
           (variable-maximum
            (resource-queue-variable-n attendant)))
   (printf "Average queue length = ~a~n"
           (variable-mean
            (resource-queue-variable-n attendant)))
   (printf "Variance             = ~a~n"
           (variable-variance
            (resource-queue-variable-n attendant)))
   (printf "Utilization          = ~a~n"
           (variable-mean
            (resource-satisfied-variable-n attendant)))
   (printf "Variance             = ~a~n"
           (variable-variance
            (resource-satisfied-variable-n attendant)))
   (print (history-plot
           (variable-history 
            (resource-queue-variable-n attendant))))))

Here is the output for the example when run for 1000 customers.

>(run-simulation 1000)
--- Example 3 - Data Collection ---
Maximum queue length = 8
Average queue length = 0.9120534884951139
Variance             = 2.2420855874934826
Utilization          = 1.4320511974417858
Variance             = 0.5885107114317054

[usage-Z-G-1.gif]
>

2.2  Loading the Simulation Collection

The PLT Scheme Simulation Collection is loaded using one of the following, depending on whether or not the graphics routines are needed:

(require (planet "simulation.ss"
                 ("williams" "simulation.plt")))
(require (planet "simulation-with-graphics.ss"
                 ("williams" "simulation.plt")))

2.3  Graphics Modules

Support for the graphical functions within the simulation collection has been separated from the fundamental simulation engine. This facilitates the use of the simulation engine, including data collection, in non-graphical environments or when alternative graphical presentations are desired.1

The graphical routines are implemented using the plot collection provided with PLT Scheme (PLoT Scheme)[7]. The plot collection is, in turn, implemented using MrEd[5]. Both of these modules are required to be present to use the graphical functions.2


1 This might be used in implementing higher-level graphical interfaces.

2 This is normally the case for PLT Scheme Version 2.07 and higher.