#lang scribble/doc @(require scribble/manual scribblings/icons (for-label scheme/base plot/plot "../simulation-with-graphics.ss")) @title[#:tag "using"]{Using the Simulation Collection} @local-table-of-contents[] This chapter describes how to use the PLT Scheme Simulation Collection and introduces its conventions. @section{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 @link["http://planet.plt-scheme.org/display.ss?package=science.plt&owner=williams"]{PLT Scheme Science Collection} and automatically loads the required portions of it using @link["http://planet.plt-scheme.org/"]{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 @link["http://planet.plt-scheme.org/package-source/williams/science.plt/3/1/planet-docs/science/index.html"]{@bold{Science Collection:} Reference Manual}. @schememod[ scheme/base (code:comment " 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. @verbatim{ --- Example 3 - Data Collection --- Maximum queue length = 8 Average queue length = 0.9120534884951139 Variance = 2.2420855874934826 Utilization = 1.4320511974417858 Variance = 0.5885107114317054 } @image["scribblings/images/example.gif"] @section{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: @schemeblock[ (require (planet williams/simulation/simulation)) (require (planet williams/simulation/simulation-with-graphics)) ] @section{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. @(margin-note finger "This might be used in implementing higher-level graphical interfaces.") The graphical routines are implemented using the plot collection @link["http://docs.plt-scheme.org/plot/index.html"]{(PLoT)} provided with PLT Scheme. The plot collection is, in turn, implemented using the PLT Graphics Toolkit @link["http://docs.plt-scheme.org/gui/index.html"]{MrED}. Both of these modules are required to be present to use the graphical functions.