# Chapter 11Simulation Classes

## 11.3  Example - Simulation Classes

; Example 4 - Classes

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

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

(define-process-class generator%
(init-field (n 1000))
(do ((i 0 (+ i 1)))
((= i n) (void))
(wait (random-exponential 4.0))
(make-object customer% i)))

(define-process-class customer%
(init-field i)
(begin
(send attendant request)
(work (random-flat 2.0 10.0))
(send attendant relinquish)))

(define (run-simulation n)
(with-new-simulation-environment
(set! attendant (make-object resource% n-attendants))
(make-object generator% n)
(accumulate (variable-statistics
(send attendant queue-variable-n)))
(accumulate (variable-history
(send attendant queue-variable-n)))
(start-simulation)
(printf "--- Example 4 - Classes ---~n")
(printf "Maximum queue length = ~a~n"
(variable-maximum
(send attendant queue-variable-n)))
(printf "Average queue length = ~a~n"
(variable-mean
(send attendant queue-variable-n)))
(printf "Variance             = ~a~n"
(variable-variance
(send attendant queue-variable-n)))
(printf "Utilization          = ~a~n"
(variable-mean
(send attendant satisfied-variable-n)))
(printf "Variance             = ~a~n"
(variable-variance
(send attendant satisfied-variable-n)))
(print (history-plot
(variable-history
(send attendant queue-variable-n))))))

>(run-simulation 1000)
--- Example 4 - Classes ---
Maximum queue length = 8
Average queue length = 0.9120534884951139
Variance             = 2.2420855874934826
Utilization          = 1.4320511974417858
Variance             = 0.5885107114317054