Skip to content

Instantly share code, notes, and snippets.

@BenLauwens
Created August 3, 2017 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenLauwens/15edb27cc0ac5cbea6e1bd79f5038a6a to your computer and use it in GitHub Desktop.
Save BenLauwens/15edb27cc0ac5cbea6e1bd79f5038a6a to your computer and use it in GitHub Desktop.
using SimJulia, Distributions
@resumable function people(sim::Simulation, λ::Float64, clerk::Resource)
i = 1
while true
Δt = rand(Exponential(1/λ))
@yield return Timeout(sim, Δt)
@coroutine customer(sim, i, clerk)
i += 1
end
end
@resumable function customer(sim::Simulation, n::Int64, clerk::Resource)
if length(clerk) > 5
println(@sprintf("%0.2f: Queue is full! Customer %d leaves", now(sim), n))
return
else
arrivaltime = now(sim)
println(@sprintf("%0.2f: Customer %d enqueues", now(sim), n))
@request clerk req begin
@yield return req
println(@sprintf("%0.2f: Customer %d being served", now(sim), n))
Δt = rand(DiscreteUniform(1, 5)) + randn()*0.2
@yield return Timeout(sim, Δt)
end
println(@sprintf("%0.2f: Customer %d leaves after %0.2f min", now(sim), n, now(sim)-arrivaltime))
end
end
sim = Simulation()
clerk = Resource(sim, 1)
@coroutine people(sim, 3.333, clerk)
run(sim, 60)
@pbayer
Copy link

pbayer commented Aug 3, 2017

Hi Ben,

it works as you said. I had to change my bug in function people to

function people(sim::Simulation, β::Float64, queue::Array{Int64,1}, clerk::Resource)
    i = 1
    while true
        Δt = rand(Exponential(β))
        yield(Timeout(sim, Δt))
        @process customer(sim, i, queue, clerk)
        i += 1
    end
end

then I get the same results as in my notebook.

You can close therefore the issue. But you should perhaps change accordingly the function customer2 in benchmarks\coroutines_MM1.jl, which I used as an example.

I cannot follow you yet with the queue management. For this example you are right. But for my development, I have to enqueue things like jobs::Job

mutable struct Job
  name::AbstractString
  wu::AbstractString
  op_time::Real
  status::Int64
  batch_size::Int64
  target::AbstractString     # name of target for transport jobs
end

which is not (yet) possible if I read your code well enough. One other aspect is a logical one: in my view, the clerk and the queue before the clerk are different entities, which should be treated separately. Your example would work if the clerk would work on different customers at a time (multitasking). But even then I needed a queue with separate distinguishable jobs or customers, on which the clerk could multitask.

BTW: are you interested, to integrate or to have integrated my functions from simlog.jl, which you can see working in the above notebook, into SimJulia?

All the best and thank you for your work,

Paul

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment