Online R Compiler

rm(list=ls()) # Five patients comes every hour on average to the clinic, # and the single physicaina treats six patients every hour on average. # n=40 ; lambda=5/60 ; mu=6/60 MM1sim <- function(n=40,lambda=5/60,mu=6/60,seed=FALSE,Print=TRUE,Round=FALSE){ # service starc clock time(ssct) since 9:00 ssct=numeric(n) # waiting time(w8) w8=numeric(n) # service end clock time(sect) sect=numeric(n) # arrival clock time(act) if(seed) set.seed(1234) act=cumsum(rexp(n,lambda)) ; if(Round) act=round(act) # duration of service(ds) if(seed) set.seed(5678) ds=round(rexp(n,mu)) ; if(Round) ds=round(ds) # simulation assuming service starts at 9:00 head(act) # act : arrival clock time head(ds) # ds : duration of service # initial values ssct[1]=act[1] # 9:15 service start clock time for 1st guest sect[1]=act[1]+ds[1] # 9:25 sevice end clock time for 1st guest w8[1]=0 # simulation step by step # # act[2] # 9:16 arrival clock time of 2nd # max(sect[1]-act[2],0) # 9:25-9:16 vs 0 = ?sevice for 1st ends b4 2nd arrival # w8[2]=max(sect[1]-act[2],0) # 9 min : w8ing time of 2nd # ssct[2]=max(sect[1],act[2]) # 9:25 vs 9:16 = service start clock time for 2nd # sect[2]=ssct[2]+ds[2] # 9:25 + 8 = 9:33 service end clock time for 2nd # # act[3] # 9:17 arrival clock time of 3rd # max(sect[2]-act[3],0) # 9:33 - 9:17 vs 0 = ?serivce for 2nd ends b4 3rd arrival? # w8[3]=max(sect[2]-act[3],0) # 16 min : w8ting time of 3rd # ssct[3]=max(sect[2],act[3]) # 9:33 vs 9:17 = service start clock time for 3rd # sect[3]=ssct[3]+ds[3] # 9:33 + 11 = 9:44 service end clock time for 3rd # for(i in 2:n){ w8[i]=max(sect[i-1]-act[i],0) ssct[i]=max(sect[i-1],act[i]) sect[i]=ssct[i]+ds[i] } if(Print){ print(summary(w8)) hist(w8,freq=FALSE,col="lightblue",main="") } invisible(w8) } MM1sim(n=1e5) MM1sim(n=100) w8m=replicate(1e4,mean(MM1sim(n=40,P=F))) summary(w8m) hist(w8m,main='40 visits per day',freq=F,col='pink')