examples/example-3.rkt
#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)))
   (write-special (history-plot (variable-history 
                                 (resource-queue-variable-n attendant))))
   (newline)))

;;; Run the simulation for 1000 customers.
(run-simulation 1000)