This chapter describes how to use the Simulation Collection and introduces its conventions.
The following code demonstrates the use of the Simulation Collection by calculating and plotting queue and resource utilization statistics for a simple queueing model.
The Simulation Collection requires the Science Collection and automatically loads the required portions of it using PLaneT. Because random numbers and distributions are so frequently required by simulation models, the random number sources and distributions from the science collection, (planet williams/science/random-source) and (planet williams/science/random-distributions), are automatically provided by the simulation collection.
#lang racket ; Example 3 - Data Collection (require (planet williams/simulation/simulation-with-graphics)) ; n-attendants : exact-nonnegative-integer? = 2 ; attendant : (or/c resource? false/c) (define n-attendants 2) (define attendant #f) ; (generator n) -> void? ; n : exact-nonnegative-integer? (define (generator n) (for ((i (in-range n))) (wait (random-exponential 4.0)) (schedule #:now (customer i)))) ; process (customer i) -> void? ; i : exact-nonnegative-integer? (define-process (customer i) (with-resource (attendant) (work (random-flat 2.0 10.0)))) ; (run-simulation n) -> void? ; n : exact-nonnegative-integer? (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))) (printf "~a~n" (history-plot (variable-history (resource-queue-variable-n attendant)))))) ; Run the simulation for 1000 customers. (run-simulation 1000)
The following shows the resulting printed output and plot.
--- Example 3 - Data Collection ---
Maximum queue length = 8
Average queue length = 0.9141863095434611
Variance = 2.2453788694193957
Utilization = 1.4320511974417858
Variance = 0.5885107114317054
The 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))
For example, this might be used in implementing higher-level graphical interfaces.
Support for the graphical functions within the Simulation Collection has been separated from the fundamental modeling and simulation 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.
The graphical routines are implemented using the plot collection (PLoT) provided with Racket. The plot collection is, in turn, implemented using the Racket Graphics Toolkit GRacket. Both of these modules are required to be present to use the graphical functions. Note that Version 4.0 of the Simulation Collection use new plot collection and therefore requires Racket Version 5.2 or later.