On this page:
10.1 The set-cell Structure
10.2 The set Structure
make-set
set-n
10.3 Set Operations
set-empty?
set-first?
set-last?
set-for-each-cell
set-for-each
set-find-cell
set-insert-cell-first!
set-insert-first!
set-insert-cell-last!
set-insert-last!
set-insert-cell-priority!
set-insert-priority!
set-remove-cell!
set-remove-item!
set-remove-first-cell!
set-remove-first!
set-remove-last-cell!
set-remove-last!
10.4 Generic Set Routines
set-insert!
set-remove!
10.5 Example - Furnace Model 1
Version: 4.1.3

10 Sets

    10.1 The set-cell Structure

    10.2 The set Structure

    10.3 Set Operations

    10.4 Generic Set Routines

    10.5 Example - Furnace Model 1

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.

(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. Not that this is stored in a vriable to facilitate automatic data collection of the number of elements in a set.

10.3 Set 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.4 Generic Set Routines

(set-insert! set item [priotity])  any
  set : set?
  item : any/c
  priotity : real? = 100

Create a cell containing item and inserts it into set with the given priority according to the type of the set.

(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.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