On this page:
2.1 An Example
2.2 Loading the Simulation Collection
2.3 Graphics Modules
Version: 4.1.3

2 Using the Simulation Collection

    2.1 An Example

    2.2 Loading the Simulation Collection

    2.3 Graphics Modules

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

2.1 An Example

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

The PLT Scheme 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 functionality from the science collection (e.g., for random number distributions or histograms) and must reference it as specified in the Science Collection: Reference Manual.

  #lang scheme/base
  ; Example 3 - Data Collection
  (require (planet williams/simulation/simulation-with-graphics))
  (require (planet williams/science/random-distributions))
  
  (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)))
     (write-special
      (history-plot (variable-history
                     (resource-queue-variable-n attendant))))))
  
  (run-simulation 1000)

The following shows the resulting printed output and plot.

--- Example 3 - Data Collection ---
Maximum queue length = 8
Average queue length = 0.9120534884951139
Variance             = 2.2420855874934826
Utilization          = 1.4320511974417858
Variance             = 0.5885107114317054

2.2 Loading the Simulation Collection

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

  (require (planet williams/simulation/simulation))
  (require (planet williams/simulation/simulation-with-graphics))

2.3 Graphics Modules

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

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

The graphical routines are implemented using the plot collection (PLoT) provided with PLT Scheme. The plot collection is, in turn, implemented using the PLT Graphics Toolkit MrED. Both of these modules are required to be present to use the graphical functions.