[plt-scheme] Simulation : coupling sub-processes

From: Kimberly Clark/Mike Messinides (vishmess at yahoo.com)
Date: Sat Mar 11 08:17:07 EST 2006

Thanks to all who helped with my simulation.plt
problem (planet was downloading a truncated file).

I've been experimenting with the simulation package in
preparation for creating a material handling model. I
want to be able to couple many simple sub-processes
together and then model the composite behavior. Each
sub-process will be very simple, but I want to be able
to easily re-arrange and modify the sub-processes. I
started with the examples provided in the simulation
package and have arrived at the following demo of my
approach. Basically I have one process (in the
simulation package sense), with multiple sub-processes
which are just functions. Each function takes as an
argument a list of functions which represent
downstream sub-processes.

Since I'm new to Scheme and the simulation package,
I'm not sure if this approach is making good use of
the capabilities of the language and the simulation
package. I'd appreciate any comments on whether this
looks like a good way to set up a model.

(require (planet
"simulation.plt" 1 0)))
(require (planet "random-distributions.ss"("williams"
;;;demo material-handling model
(define conv-length 10)
(define conv-set #f)
(define conv #f)
(define next-conv #f)
(define wrap-exit #f)
(define buffer-exit #f)

(define-process (generator n)
;;;	(let ((next-proc (list wrap buffer)))
	(let ((next-proc (list (make-wrap 5) buffer)))
	(do ((i 0 (+ i 1)))((= i n) (void))
		(wait 5.0)
		(wait (random-exponential 1.0))
		(schedule now (material-handle next-proc i)))))

(define-process (material-handle next-proc i)
	(with-resource (conv)
	        (printf"~a: unit ~a enters~n"
(current-simulation-time) i)		
		(set-insert! conv-set self)
		(work (random-flat 2.0 3.0))
		(with-resource (next-conv)
			(apply (car next-proc) (list (cdr next-proc) i))
		(set-remove! conv-set self)
;;simple wrapping sub-process, no internal state
(define (wrap next-proc i)
		(work 30.0)
	         (printf"~a: unit ~a wrapped~n"
(current-simulation-time) i)		
		(with-resource (wrap-exit)
			(apply (car next-proc) (list (cdr next-proc) i))

;;creates a wrapping sub-process with internal state
to model wrapping material
(define (make-wrap restock-level)
	(let ((inventory restock-level))
	(define (inv-adj n)
		(set! inventory (- inventory 1))
		(if (eq? inventory 0)
			(printf "restocking on unit ~a~n" n)
			(set! inventory restock-level)
			(wait 2.0)
		(lambda(next-proc i) (begin
		(work 30.0)
	         (printf"~a: unit ~a wrapped~n"
(current-simulation-time) i)
		 (inv-adj i)
		(with-resource (wrap-exit)
			(apply (car next-proc) (list (cdr next-proc) i))

;;sub-process for modelling end of the line
(define (buffer next-proc i)
	(work 1.0)
	(printf"~a: unit ~a buffered~n"
(current-simulation-time) i)		
		(with-resource (buffer-exit)
			(work 1.0)
(define (initialize)
	  (set! conv (make-resource conv-length))
	  (set! next-conv (make-resource 1))
	  (set! wrap-exit (make-resource 1))
	  (set! buffer-exit (make-resource 1))	  
	  (set! conv-set (make-set))
	   (accumulate (variable-history (set-variable-n
(define (run-sim n)
		(schedule (at 0.0) (generator n))
  (printf "~n-- Conveyor Utilization Statistics --~n")
  (printf "Mean No. of units    = ~a~n"
          (variable-mean (set-variable-n conv-set)))
  (printf "Variance              = ~a~n"
          (variable-variance (set-variable-n
  (printf "Maximum No. of units = ~a~n"
          (variable-maximum (set-variable-n
  (printf "Minimum No. of units = ~a~n"
          (variable-minimum (set-variable-n
		  (printf "~a~n"
          (history-plot (variable-history
(set-variable-n conv-set))
		  "Conveyor Utilization History"))))

Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 

Posted on the users mailing list.