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, provides automatic data collection.
9.1 The setcell Structure
Structure:
setcell 
Contract: (struct setcell ((next setcell?) (previous setcell?) (priority real?) (item any?))) 

The setcell
structure represents an item in a set.
9.2 The set Structure
Structure:
set 
Contract: (struct set ((variablen variable?) (firstcell (union/c setcell? #f)) (lastcell (union/c setcell? #f)) (type (oneof/c #:fifo #:lifo #:priority)))) 

The set
structure represents a collection of items.
Function:
(makeset type) (makeset) 
Contract: (case> (> (oneof/c #:fifo #:lifo #:priority) set?) (> set?)) 

Function:
(setn set value) 
Contract: (> set? naturalnumber/c) 

9.3 Set Operations
Function:
(setempty? x) 
Contract: (> any? boolean?) 

Function:
(setfirst set) 
Contract: (> set? any) 

Function:
(setlast set) 
Contract: (> set? any) 

Function:
(setforeachcell set proc) 
Contract: (> set? procedure? void) 

Function:
(setforeach set proc) 
Contract: (> set? procedure? void) 

Function:
(setfindcell set item) 
Contract: (> set? any? (union/c setcell? #f)) 

Function:
(setinsertcellfirst! set cell 
Contract: (> set? setcell? void?) 

Function:
(setinsertfirst! set item) 
Contract: (> set? any? void?) 

Function:
(setinsertcelllast! set cell 
Contract: (> set? setcell? void?) 

Function:
(setinsertlast! set item) 
Contract: (> set? any? void?) 

Function:
(setinsertcellfirst! set cell 
Contract: (> set? setcell? void?) 

Function:
(setinsertfirst! set item priority) 
Contract: (> set? any? real? void?) 

Function:
(setremovecell! set cell 
Contract: (> set? setcell? void?) 

Function:
(setremoveitem! set item) 
Contract: (> set? any? (union/c setcell? #f)) 

Function:
(setremovefirst cell! set errorthunk) (setremovefirstcell! set 
Contract: (case> (> set? procedure? any) (> set? setcell?)) 

Function:
(setremovefirst! set errorthunk) (setremovefirst! set 
Contract: (case> (> set? procedure? any) (> set? any)) 

Function:
(setremovelast cell! set errorthunk) (setremovelastcell! set 
Contract: (case> (> set? procedure? any) (> set? setcell?)) 

Function:
(setremovelast! set errorthunk) (setremovelast! set 
Contract: (case> (> set? procedure? any) (> set? any)) 

9.3.1 Generic Routines
Function:
(setinsert! set item priority) (setinsert! set item) 
Contract: (case> (> set? any? real? void?) (> set? any? void?)) 

 #:fifo  the item is inserted at the end of the set. The priority, if provided, is ignored.
 #:lifo  the item is inserted at the beginning of the set. The priority, if provided, is ignored.
 #:priority  the item is inserted in the set according to the priority. If priority is not provided, 100 is used.
Function:
(setremove! set item) (setremove! set) 
Contract: (case> (> set? any? void?) (> set? any?)) 

If an item is not specified, then this function removes the dirst item from the set and returns it. An error is signaled if the set is empty.
9.4 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 discreteevent model of the same system. The furnace itself is modeled by a set.
This simulation model is derived from an example in Introduction to Combined DiscreteContinuous Simulation Using SIMSCRIPT II.5 by AbdelMoaty M Fayek[1].
;;; Model 1  Discrete Event Model (require (planet "simulationwithgraphics.ss" ("williams" "simulation.plt"))) (require (planet "randomdistributions.ss" ("williams" "science.plt"))) ;;; Simulation Parameters (define endtime 720.0) (define npits 7) ;;; Data collection variables (define totalingots 0) (define waittime (makevariable)) ;;; Model Definition (define randomsources (makerandomsourcevector 2)) (define furnace #f) (define pit #f) (define (scheduler) (let loop () (schedule now (ingot)) (wait (randomexponential (vectorref randomsources 0) 1.5)) (loop))) (defineprocess (ingot) (let ((arrivetime (currentsimulationtime))) (withresource (pit) (setvariablevalue! waittime ( (currentsimulationtime) arrivetime)) (setinsert! furnace self) (work (randomflat (vectorref randomsources 1) 4.0 8.0)) (setremove! furnace self)) (set! totalingots (+ totalingots 1)))) (define (stopsim) (printf "Report after ~a Simulated Hours  ~a Ingots Processed~n" (currentsimulationtime) totalingots) (printf "~n Ingot Waiting Time Statistics ~n") (printf "Mean Wait Time = ~a~n" (variablemean waittime)) (printf "Variance = ~a~n" (variablevariance waittime)) (printf "Maximum Wait Time = ~a~n" (variablemaximum waittime)) (printf "~n Furnace Utilization Statistics ~n") (printf "Mean No. of Ingots = ~a~n" (variablemean (setvariablen furnace))) (printf "Variance = ~a~n" (variablevariance (setvariablen furnace))) (printf "Maximum No. of Ingots = ~a~n" (variablemaximum (setvariablen furnace))) (printf "Minimum No. of Ingots = ~a~n" (variableminimum (setvariablen furnace))) (printf "~a~n" (historyplot (variablehistory (setvariablen furnace)) "Furnace Utilization History")) (stopsimulation)) (define (initialize) (set! totalingots 0) (set! waittime (makevariable)) (set! pit (makeresource npits)) (set! furnace (makeset)) (accumulate (variablehistory (setvariablen furnace))) (tally (variablestatistics waittime)) (schedule (at endtime) (stopsim)) (schedule (at 0.0) (scheduler))) (define (runsimulation) (withnewsimulationenvironment (initialize) (startsimulation)))
The following is the output from the simulation.
>(runsimulation) 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
>