[plt-scheme] Defining resource constrained producer/consumer in the simulation package

From: Doug Williams (m.douglas.williams at gmail.com)
Date: Mon Jun 23 15:36:06 EDT 2008

Here's a simple version using a single process and two resources.

;;; Noels' e-mail:
;;; I am trying to define the following producer/consumer process using
;;; the simulation package:
;;; There is a queue of resources of type A.
;;; Process X takes an A and produces a B (which contains the A).  The B
;;; is placed in a queue of fixed size
;;; Process Y takes a B, returns the A its queue and throws away the B.
;;; If any queue is empty a process that needs a resource of that type
;;; must wait.  If any queue is full a process that creates a resource of
;;; that type must wait.
;;; I can't work out how to model this.  If I model the As and Bs as
;;; resources, I have the problem that "It is an error to attempt to
;;; release more units than the process has allocated."
;;; (http://planet.plt-scheme.org/package-source/williams/simulation.plt/
;;; 2/2/html/simulation-Z-H-8.html#node_sec_7.2.2).
;;; Since X takes A and never releases them, and Y releases As but never
;;; takes them, this is a problem.
;;; If I schedule As and Bs as events how do I enforce the resource limits?
;;; This kind of simulation is simple in a message passing thread system
;;; but I don't see how it fits into the simulation package's model.
;;; Also: the documentation for the simulation package would benefit from
;;; an introductory section that lays out the model used by the package.
;;; Doug's Response:
;;; I'm not sure what A and B are in you example, but I assume A is
;;; that is required by both processes - like a work cell or jig - and that
;;; B is something only required by Y.  Also, I'm assuming that X and Y can
;;; be done at independent speeds.
;;; Let's assume a 'widget' represents the end product/result of X and Y
;;; an A and a B.

(require (planet "simulation-with-graphics.ss"
                 ("williams" "simulation.plt")))
(require (planet "random-distributions.ss"
                 ("williams" "science.plt")))

(define n 0)
(define A #f)
(define B #f)

(define (scheduler)
  (do ((i 1 (+ i 1)))
      ((> i n) ())
    (wait (random-exponential 1.5))
    (schedule now (widget i))))

(define-process (widget i)
  ;; Allocate an A for X and Y
  (with-resource (A)
    (printf "~a: Widget ~a acquires an A~n" (current-simulation-time) i)
    (work (random-flat 4.0 8.0)) ; X
    ;; Allocate a B for Y
    (with-resource (B)
      (printf "~a: Widget ~a acquires a B~n" (current-simulation-time) i)
      (work (random-flat 8.0 20.0)) ; Y
      (printf "~a: Widget ~a releases its B~n" (current-simulation-time) i))
    (printf "~a: Widget ~a releases its A~n" (current-simulation-time) i)))

(define (run-simulation n-widgets n-As n-Bs)
    (set! n n-widgets)
    (set! A (make-resource n-As))
    (set! B (make-resource n-Bs))
    (schedule (at 0.0) (scheduler))

(run-simulation 20 4 2)

On Mon, Jun 23, 2008 at 11:52 AM, Noel Welsh <noelwelsh at gmail.com> wrote:

> On Mon, Jun 23, 2008 at 4:53 PM, Doug Williams
> <m.douglas.williams at gmail.com> wrote:
> > I'll reload 3.72 on one of my machines and send you a quick solution.
>  I'll
> > still having problems with V4.0 (and V4.0.1) and haven't gotten the
> > simulation and inference collections ported yet.
> Don't worry about it too much.  My problem is so simple that it might
> be easier to just implement it directly.
> N.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20080623/12ae6d7a/attachment.html>

Posted on the users mailing list.