10 Sets
In the simulation collection, a set is a general structure that maintains a doubly-linked list of its elements. The length of the list, i.e. the n field, is implemented using a variable and, therefore, provides automatic data collection of its value.
10.1 The set-cell Structure
(struct set-cell (next previous priority item) #:mutable) next : set-cell? previous : set-cell? priority : real? item : any/c
10.2 The set Structure
(struct set (variable-n first-cell last-cell type) #:mutable) variable-n : variable? first-cell : (or/c set-cell? false/c) last-cell : (or/c set-cell? false/c) type : (one-of/c '#:fifo '#:lifo '#:priority)
variable-n—
A variable that sores the number of elements in the set. This allows automatic data collection of the number of elements in the set. Note that the actual number of elements is available (as an exact-nonnegative-integer?) using the pseudo-field set-n. first-cell—
The set cell representing the first item in the set or #f if the set is empty. last-cell—
The set cell representing the last item in the set or #f if the set is empty. - type—
The type of the set. This is used for the generic set operations. Valid values are: #:fifo—
a first-in-first-out set (i.e. a queue) #:lifo—
a last-in-first-out set (i.e. a stack) #:priority—
a priority set (i.e. a set whose elements are ordered by priority).
(make-set [type]) → set? type : (one-of/c #:fifo #:lifo #:priority) = #:fifo
(set-n set) → exact-nonnegative-integer? set : set?
10.3 Set Operations
(set-empty? set) → boolean? set : set?
(set-first set) → any set : set?
(set-last set) → any set : set?
(set-insert-cell-first! set cell) → any set : set? cell : set-cell?
(set-insert-cell-last! set cell) → any set : set? cell : set-cell?
(set-insert-cell-priority! set cell) → any set : set? cell : set-cell?
(set-remove-cell! set cell) → any set : set? cell : set-cell?
(set-remove-first-cell! set error-thunk) → any set : set? error-thunk : (-> any) (set-remove-first-cell! set) → set-cell? set : set?
(set-remove-first! set error-thunk) → any set : set? error-thunk : (-> any) (set-remove-first! set) → any set : set?
(set-remove-last-cell! set error-thunk) → any set : set? error-thunk : (-> any) (set-remove-last-cell! set) → set-cell? set : set?
(set-remove-last! set error-thunk) → any set : set? error-thunk : (-> any) (set-remove-last! set) → any set : set?
10.4 Generic Set Routines
#:fifo—
item is inserted at the end of set. The priority, if provided, is ignored. #:lifo—
item is inserted at the beginning of set. The priority, if provided, is ignored. #:priority—
item is inserted into set according to the priority. If priority is not provided, the default value of 100 is used.
If item is not provided, removes the first element from set and returns it. An error is signaled if set is empty.
10.5 Example - Furnace Model 1
The furnace model will be used in Chapter 10 Continuous Simulation Models to illustrate building a continuous model. This initial model is a purely discrete-event of the same system. The furnace itself is modeled by a set.
This simulation model is derived from an example in Introduction to Combined Discrete-Continuous Simulation Using SIMSCRIPT II.5 by Abdel-Moaty M Fayek [Fayek02].
#lang racket/base ; Model 1 - Discrete Event Model (require (planet williams/simulation/simulation-with-graphics)) ; Simulation Parameters (define end-time 720.0) (define n-pits 7) ; Data collection variables (define total-ingots 0) (define wait-time (make-variable)) ; Model Definition (define random-sources (make-random-source-vector 2)) (define furnace #f) (define pit #f) (define (scheduler) (for ((i (in-naturals))) (schedule #:now (ingot)) (wait (random-exponential (vector-ref random-sources 0) 1.5)))) (define-process (ingot) (let ((arrive-time (current-simulation-time))) (with-resource (pit) (set-variable-value! wait-time (- (current-simulation-time) arrive-time)) (set-insert! furnace self) (work (random-flat (vector-ref random-sources 1) 4.0 8.0)) (set-remove! furnace self)) (set! total-ingots (+ total-ingots 1)))) (define (stop-sim) (printf "Report after ~a Simulated Hours - ~a Ingots Processed~n" (current-simulation-time) total-ingots) (printf "~n-- Ingot Waiting Time Statistics --~n") (printf "Mean Wait Time = ~a~n" (variable-mean wait-time)) (printf "Variance = ~a~n" (variable-variance wait-time)) (printf "Maximum Wait Time = ~a~n" (variable-maximum wait-time)) (printf "~n-- Furnace Utilization Statistics --~n") (printf "Mean No. of Ingots = ~a~n" (variable-mean (set-variable-n furnace))) (printf "Variance = ~a~n" (variable-variance (set-variable-n furnace))) (printf "Maximum No. of Ingots = ~a~n" (variable-maximum (set-variable-n furnace))) (printf "Minimum No. of Ingots = ~a~n" (variable-minimum (set-variable-n furnace))) (write-special (history-plot (variable-history (set-variable-n furnace)) "Furnace Utilization History")) (newline) (stop-simulation)) (define (initialize) (set! total-ingots 0) (set! wait-time (make-variable)) (set! pit (make-resource n-pits)) (set! furnace (make-set)) (accumulate (variable-history (set-variable-n furnace))) (tally (variable-statistics wait-time)) (schedule #:at 0.0 (scheduler)) (schedule #:at end-time (stop-sim))) (define (run-simulation) (with-new-simulation-environment (initialize) (start-simulation))) (run-simulation)
The following is the resulting output.
Report after 720.0 Simulated Hours - 479 Ingots Processed |
|
-- Ingot Waiting Time Statistics -- |
Mean Wait Time = 0.1482393804317038 |
Variance = 0.24601817483957691 |
Maximum Wait Time = 3.593058032365832 |
|
-- Furnace Utilization Statistics -- |
Mean No. of Ingots = 4.0063959874393795 |
Variance = 3.2693449366238347 |
Maximum No. of Ingots = 7 |
Minimum No. of Ingots = 0 |