examples/closed-loop-ex1.rkt
#lang racket/base
; Closed Loop Example

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

(define n-attendants 2)
(define attendant #f)

(define (generator n)
  (for ((i (in-range n)))
    (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 n1 n2)
  (let ((avg-queue-length (make-variable)))
    (tally (variable-statistics avg-queue-length))
    (tally (variable-history avg-queue-length))
    (for ((i (in-range n1)))
      (with-new-simulation-environment
       (set! attendant (make-resource n-attendants))
       (schedule #:at 0.0 (generator n2))
       (start-simulation)
       (set-variable-value! avg-queue-length
         (variable-mean (resource-queue-variable-n attendant)))))
    (printf "--- Closed Loop Example ---~n")
    (printf "Number of attendants         = ~a~n" n-attendants)
    (printf "Number of experiments        = ~a~n"
            (variable-n avg-queue-length))
    (printf "Minimum average queue length = ~a~n"
            (variable-minimum avg-queue-length))
    (printf "Maximum average queue length = ~a~n"
            (variable-maximum avg-queue-length))
    (printf "Mean average queue length    = ~a~n"
            (variable-mean avg-queue-length))
    (printf "Variance                     = ~a~n"
            (variable-variance avg-queue-length))
    (print (history-plot (variable-history avg-queue-length)
                                 "Average Queue Length"))
    (newline)))
  
(run-simulation 1000 1000)