10 Sets
In the PLT Scheme 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, provide automatic data collection of its value.
10.1 The set-cell Structure
(struct set-cell (next |
previous |
priority |
item)) |
next : set-cell? |
previous : set-cell? |
priority : real? |
item : any? |
Represents an item in a set.
10.2 The set Structure
(struct set (variable-n |
first-cell |
last-cell |
type)) |
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-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. Bote 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 ot #f if the set is empty.
last-cell – The set cell representing the last item in the set ot #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).
(set-n set) → exact-nonnegative-integer? |
set : set? |
10.3 Set Operations
(set-empty? set) → boolean? |
set : set? |
(set-insert-priority! set item priority) → any |
set : set? |
item : any/c |
priority : real? |
(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
(set-insert! set item [priority]) → any |
set : set? |
item : any/c |
priority : real? = 100 |
#: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 scheme/base |
; Model 1 - Discrete Event Model |
(require (planet williams/simulation/simulation-with-graphics)) |
(require (planet williams/science/random-distributions)) |
; Simulation Parameters |
(define end-time 720.0) |
(define n-pits 7) |
; Data collection variables |
(define total-ingots 0) |
(define wait-time (make-variable)) |
(coode:comment "Model Definition") |
(define random-sources (make-random-source-vector 2)) |
(define furnace #f) |
(define pit #f) |
(define (scheduler) |
(let loop () |
(schedule now (ingot)) |
(wait (random-exponential (vector-ref random-sources 0) 1.5)) |
(loop))) |
(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 end-time) (stop-sim)) |
(schedule (at 0.0) (scheduler))) |
(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 |