-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation script.R
More file actions
156 lines (129 loc) · 5.57 KB
/
Copy pathsimulation script.R
File metadata and controls
156 lines (129 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#---
# title: "ED simulation project"
#author: "Eghorieta /"
#date: '2022-03-25'
#output:
# html_document: default
#word_document: default
#---
#install.packages("simmer")
library(simmer)
set.seed(123)
env <- simmer("emmergency department")
patient <- trajectory("patients' path") %>%
# probability that 20% of the patients are level 1 (NIA) and 80% are level 2 (CW)
branch(function() sample(1:2, size = 1, prob = c(0.20,0.80), replace = T), continue = c(T,T),
#### setting Triage priority level1 for stream 2 #####
trajectory("NIA Priority") %>%
set_attribute("priority", 3) %>% # patient is immediately admitted for treatment
set_prioritization(values = c(3,7,T)) %>%
seize("doctor", 1) %>% # adding resource engagement
timeout(function() runif(1, 3, 77)) %>% # patient receiving treatment uniform randomly distributed
release("doctor", 1) %>% # resource disengagement
#### Triage priority level1 reduced to wait for stream 3 #####
set_attribute("wait", 2) %>% # patient priority is reduced to wait
set_prioritization(c(2, 7, T), mod="+") %>%
seize("doctor", 1) %>%
timeout(function() runif(1, 5, 55)) %>%
release("doctor", 1),
#### setting Triage priority level2 for stream 4 #####
trajectory("CW Priority") %>%
set_attribute("priority", 1) %>% # on arrival patient is required to register and wait for admittance
set_prioritization(values = c(1,7,T)) %>%
seize("doctor", 1) %>% # adding resource engagement
timeout(function() runif(1, 1, 29)) %>%
release("doctor", 1) %>% # resource disengagement
#### Triage priority level2 increased to wait for stream 5 #####
set_attribute("wait", 2) %>%
set_prioritization(values = c(2,7,T), mod="+") %>%
seize("doctor", 1) %>%
timeout(function() runif(1, 2, 18)) %>%
release("doctor", 1)
)
library(simmer.plot)
plot(patient , verbose = F)
#get_palette<-scales::brewer_pal(type='qual',palette = 1)
#plot(patient , fill=get_palette, verbose = F)
envs <- lapply(1:20, function(i) {
simmer("emmergency room") %>%
add_resource("doctor", 2) %>%
add_generator("Need immediate attention", patient, at(c(0.20,0.80))) %>%
add_generator("patient", patient, function() runif(1, 10, 30), mon = 2) %>% # patient arrival to the ED is uniform randomly distributed
run(1440) # simulation time 20days continuous operation, 24hrs per day
})
# storing resource information and patient arrival times
resources <- get_mon_resources(envs)
arrivals <- get_mon_arrivals(envs)
# created plots to assess the system metrics
library(gridExtra)
p1 = plot(resources, metric = "utilization")
p2 = plot(resources, metric = "usage")
p3 = plot(arrivals, metric = "flow_time")
p4 = plot(arrivals, metric = "waiting_time")
grid.arrange(p1,p2,p3,p4)
#plot.resources()
#head(arrivals)
plot(resources, metric = "utilization")
plot(resources, metric = "usage")
plot(arrivals, metric = "flow_time")
plot(arrivals, metric = "waiting_time")
plot(get_mon_attributes(envs))
head(get_mon_attributes(envs))
head(get_mon_resources(envs))
head(get_mon_arrivals(envs))
arrivals
```
# the process was repeated again, but this time, we added more doctor resources to the system to analyze the changes
library(simmer)
set.seed(123)
env <- simmer("emmergency room")
patient <- trajectory("patients' path") %>%
branch(function() sample(1:2, size = 1, prob = c(0.20,0.80), replace = T), continue = c(T,T),
trajectory("NIA Priority") %>%
set_attribute("priority", 3) %>%
set_prioritization(values = c(3,7,T)) %>%
seize("doctor", 1) %>%
timeout(function() runif(1, 3, 77)) %>%
release("doctor", 1) %>%
set_attribute("wait", 2) %>%
set_prioritization(c(2, 7, T), mod="+") %>%
seize("doctor", 1) %>%
timeout(function() runif(1, 5, 55)) %>%
release("doctor", 1),
trajectory("CW Priority") %>%
set_attribute("priority", 1) %>%
set_prioritization(values = c(1,7,T)) %>%
seize("doctor", 1) %>%
timeout(function() runif(1, 1, 29)) %>%
release("doctor", 1) %>%
set_attribute("wait", 2) %>%
set_prioritization(values = c(2,7,T), mod="+") %>%
seize("doctor", 1) %>%
timeout(function() runif(1, 2, 18)) %>%
release("doctor", 1)
)
library(simmer.plot)
plot(patient , verbose = F)
```
envs2 <- lapply(1:20, function(i) {
simmer("emmergency room") %>%
add_resource("doctor", 3) %>% # Number of doctor resource increased from 1 to 3
add_generator("Need immediate attention", patient, at(c(0.20,0.80))) %>%
add_generator("patient", patient, function() runif(1, 10, 30), mon = 2) %>%
run(1440)
})
resources <- get_mon_resources(envs2)
arrivals <- get_mon_arrivals(envs2)
library(gridExtra)
p1 = plot(resources, metric = "utilization")
p2 = plot(resources, metric = "usage")
p3 = plot(arrivals, metric = "flow_time")
p4 = plot(arrivals, metric = "waiting_time")
grid.arrange(p1,p2,p3,p4)
plot(resources, metric = "utilization")
plot(resources, metric = "usage")
plot(arrivals, metric = "flow_time")
plot(arrivals, metric = "waiting_time")
plot(get_mon_attributes(envs2))
head(get_mon_attributes(envs2))
head(get_mon_resources(envs2))