On this page:
12.1 Process Classes
12.2 Resource Classes
12.3 Example – Simulation Classes
Version: 4.2.1

12 Simulation Classes

    12.1 Process Classes

    12.2 Resource Classes

    12.3 Example – Simulation Classes

12.1 Process Classes

TBD

12.2 Resource Classes

TBD

12.3 Example – Simulation Classes

This example is the same as Example 3 in Chapter 8 Data Collection. Since this example uses classes, the scheme language is required instead of the scheme/base language.

  #lang scheme
  ;  Example 4 - Classes
  
  (require (planet williams/simulation/simulation-with-graphics))
  (require (planet williams/science/random-distributions))
  
  (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)))
     (write-special (history-plot
                     (variable-history
                      (send attendant queue-variable-n))))
     (newline)))
  
  (run-simulation 1000)

The output is identical to the example in Section 8.3 Example – Data Collection (except for the heading).

--- Example 4 - Classes ---
Maximum queue length = 8
Average queue length = 0.9120534884951139
Variance             = 2.2420855874934826
Utilization          = 1.4320511974417858
Variance             = 0.5885107114317054