examples/open-loop-ex1.ss
#lang scheme/base
; Open Loop Example

(require (planet "simulation-with-graphics.ss"
                 ("williams" "simulation.plt")))
(require (planet "random-distributions.ss"
                 ("williams" "science.plt")))

(define attendant #f)

(define (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)
    (wait/work (random-flat 2.0 10.0))))

(define (run-simulation n1 n2)
  (with-new-simulation-environment
   (let ((max-attendants (make-variable)))
     (tally (variable-statistics max-attendants))
     (tally (variable-history max-attendants))
     (do ((i 1 (+ i 1)))
         ((> i n1) (void))
       (with-new-simulation-environment
        (set! attendant (make-resource +inf.0))
        (schedule (at 0.0) (generator n2))
        (start-simulation)
        (set-variable-value! max-attendants
         (variable-maximum (resource-satisfied-variable-n attendant)))))
     (printf "--- Open Loop Example ---~n")
     (printf "Number of experiments      = ~a~n"
             (variable-n max-attendants))
     (printf "Minimum maximum attendants = ~a~n"
             (variable-minimum max-attendants))
     (printf "Maximum maximum attendants = ~a~n"
             (variable-maximum max-attendants))
     (printf "Mean maximum attendants    = ~a~n"
             (variable-mean max-attendants))
     (printf "Variance                   = ~a~n"
             (variable-variance max-attendants))
     (write-special (history-plot (variable-history max-attendants)
                                  "Maximum Attendants"))
     (newline))))
  
(run-simulation 1000 1000)