examples/example-3.ss
#lang scheme/base
; 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)))
   (write-special (history-plot (variable-history 
                                 (resource-queue-variable-n attendant))))))

(run-simulation 1000)