using SimJulia
using Distributions
using RandomStreams
using Distributions
const SEED = 12345
rand_dist(rng::MRG32k3a, Dist::Distribution) = quantile(Dist, rand(rng))
seeds = [SEED, SEED, SEED, SEED, SEED, SEED]
gen = MRG32k3aGen(seeds)
include("tally.jl")
include("tallystore.jl")
println("M/M/1 with processes")
#rates
λ = 1.9
μ = 2.0
ρ = λ/μ
warmup = 500 # this should be set with a formal procedure
type System
W
arrival
service
counter
arrgen
servgen
function System(text::String)
s = new()
s.W = Tally(text)
s.arrival = Exponential(1.0/λ)
s.service = Exponential(1.0/μ)
s.arrgen = next_stream(gen)
s.servgen = next_stream(gen)
return s;
end
end
function restart(s::System)
init(s.W)
next_substream!(s.arrgen)
next_substream!(s.servgen)
end
s = System("Waiting Times")
meanWaits = Tally("Mean waiting times")
k = 30 # number of batches
batchSize = 10000 # number of clients per batch
# Allow a simulation with a fixed horizon
function source(env::Environment, s::System, limit::Float64)
i = 0
while (true)
yield(Timeout(env, rand_dist(s.arrgen, s.arrival)))
if (now(env) > limit) break end
i += 1
Process(env, customer, s, i, s.counter)
end
end
# Allow a simulation with a fixed number of clients
function source(env::Environment, s::System, nCusts::Int64)
for i = 1:nCusts
yield(Timeout(env, rand_dist(s.arrgen, s.arrival)))
Process(env, customer, s, i, s.counter)
end
end
function customer(env::Environment, s::System, idx::Int, counter::Resource)
# Record the arrival time in the system
arrive = now(env)
yield(Request(counter))
# The simulation clock now contains the time when the client goes to the server.
# Record the waiting time
if (idx > warmup)
wait = now(env) - arrive
add(s.W, wait)
# Check if we have a new batch
if ((idx - warmup) % batchSize == 0)
# Collect the information of the elapsed batch
println("Batch. Average waiting time: ", average(s.W),
"Number of observations: ", nobs(s.W))
add(meanWaits, average(s.W))
init(s.W)
end
end
yield(Timeout(env, rand_dist(s.servgen, s.service)))
yield(Release(counter))
end
function onesim(s::System)
env = Environment()
s.counter = Resource(env, 1)
Process(env, source, s, warmup+k*batchSize)
run(env)
end
onesim(s)
average(meanWaits)
nobs(meanWaits)
function ci_normal(n::Int64, mean::Float64, stdev::Float64, α::Float64)
z = quantile(Normal(), 1-α/2)
w = z*stdev/sqrt(n)
# Lower bound
l = mean - w
# Upper bound
u = mean + w
return l, u
end
ci_normal(nobs(meanWaits), average(meanWaits), stdev(meanWaits), 0.05)
function ci_tdist(n::Int64, mean::Float64, stdev::Float64, α::Float64)
z = quantile(TDist(n-1), 1-α/2)
w = z*stdev/sqrt(n)
# Lower bound
l = mean - w
# Upper bound
u = mean + w
return l, u
end
ci_tdist(nobs(meanWaits)-1, average(meanWaits), stdev(meanWaits), 0.05)
Average waiting time according to M/M/1 formulas. In stationnary regime, the mean waiting time is $$ \overline{W} = \frac{\frac{\rho}{\mu}}{1-\rho} = \frac{\rho}{\mu-\lambda}. $$
w = (ρ/μ)/(1-ρ)