### 10Sets

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.1The set-cell Structure

 (struct set-cell (next previous priority item) #:mutable)
next : set-cell?
previous : set-cell?
priority : real?
item : any/c
Represents an item in a set.

#### 10.2The 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)
Represents a collection of items whose length is implemented as a variable to facilitate automatic data collection.
• variable-nA 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-cellThe set cell representing the first item in the set or #f if the set is empty.

• last-cellThe set cell representing the last item in the set or #f if the set is empty.

• typeThe type of the set. This is used for the generic set operations. Valid values are:
• #:fifoa first-in-first-out set (i.e. a queue)

• #:lifoa last-in-first-out set (i.e. a stack)

• #:prioritya priority set (i.e. a set whose elements are ordered by priority).

 (make-set [type]) → set? type : (one-of/c #:fifo #:lifo #:priority) = #:fifo
Returns a new, empty set of the specified type. If type is not specified, a first-in-first-out set is created (i.e. a queue).

 (set-n set) → exact-nonnegative-integer? set : set?
Returns the value of the n field of the variable-n field of set. This is the number of element in the set. Note that this is stored in a vriable to facilitate automatic data collection of the number of elements in a set.

#### 10.3Set Operations

 (set-empty? set) → boolean? set : set?
Returns true, #t, if set is empty, that is if the number of elements in the set is 0, and false, #f, otherwise.

 (set-first set) → any set : set?
Returns the first element of set. An error is signaled if set is empty.

 (set-last set) → any set : set?
Returns the last element of set. An error is signaled if set is empty.

 (set-for-each-cell set proc) → any set : set? proc : (-> set-cell? any)
Iterates over the cells in set and applies the procedure proc to each cell in turn.

 (set-for-each set proc) → any set : set? proc : (-> any/c any)
Iterates over the elements in set and applied the procesure proc to each element in turn.

 (set-find-cell set item) → (or/c set-cell? false/c) set : set? item : any/c
Returns the first cell found in set that contains item or #t if it is not found.

 (set-insert-cell-first! set cell) → any set : set? cell : set-cell?
Inserts cell as the first element of set. This is independent of the type of the set.

 (set-insert-first! set item) → any set : set? item : any/c
Creates a new cell containing item and inserts it as the first element of set. This is independent of the type of the set.

 (set-insert-cell-last! set cell) → any set : set? cell : set-cell?
Inserts cell as the first element of set. This is independent of the type of the set.

 (set-insert-last! set item) → any set : set? item : any/c
Creates a new cell containing item and inserts it as the last element of set. This is independent of the type of the set.

 (set-insert-cell-priority! set cell) → any set : set? cell : set-cell?
Inserts cell into set according to its priority. This is independent of the type of the set.

 (set-insert-priority! set item priority) → any set : set? item : any/c priority : real?
Creates a new cell containing item with the specified priority and inserts it into set according to its priority. This is independent of the type of the set.

 (set-remove-cell! set cell) → any set : set? cell : set-cell?
Removes cell from set. No error is raised if cell is not found.

 (set-remove-item! set item) → (or/c set-cell? false/c) set : set? item : any/c
Removes the cell containing item from set. If item is found, the cell containing it is returned, otherwise #f is returned.

 (set-remove-first-cell! set error-thunk) → any set : set? error-thunk : (-> any) (set-remove-first-cell! set) → set-cell? set : set?
Removes the first cell from set and returns it. If set is empty and error-think is provided, it is evaluated and the result returned. Otherwise, if set is empty, an error is raised.

 (set-remove-first! set error-thunk) → any set : set? error-thunk : (-> any) (set-remove-first! set) → any set : set?
Removes the first item from set and returns it. If set is empty and error-think is provided, it is evaluated and the result returned. Otherwise, if set is empty, an error is raised.

 (set-remove-last-cell! set error-thunk) → any set : set? error-thunk : (-> any) (set-remove-last-cell! set) → set-cell? set : set?
Removes the last cell from set and returns it. If set is empty and error-think is provided, it is evaluated and the result returned. Otherwise, if set is empty, an error is raised.

 (set-remove-last! set error-thunk) → any set : set? error-thunk : (-> any) (set-remove-last! set) → any set : set?
Removes the last item from set and returns it. If set is empty and error-think is provided, it is evaluated and the result returned. Otherwise, if set is empty, an error is raised.

#### 10.4Generic Set Routines

 (set-insert! set item [priority]) → any set : set? item : any/c priority : real? = 100
Create a cell containing item and inserts it into set with the given priority according to the type of the set.

• #:fifoitem is inserted at the end of set. The priority, if provided, is ignored.

• #:lifoitem is inserted at the beginning of set. The priority, if provided, is ignored.

• #:priorityitem is inserted into set according to the priority. If priority is not provided, the default value of 100 is used.

 (set-remove! set item) → any set : set? item : any/c (set-remove! set) → any set : set?
If item is specified, the cell in set containing the element is removed and returned.

If item is not provided, removes the first element from set and returns it. An error is signaled if set is empty.

#### 10.5Example - 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