-
Notifications
You must be signed in to change notification settings - Fork 0
/
6plot_parallel.txt
277 lines (245 loc) · 9.4 KB
/
6plot_parallel.txt
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# Load required libraries
library(ggplot2)
library(patchwork)
library(parallel)
library(foreach)
library(doParallel)
# Simulation parameters
k <- 3 # number of servers
lambda <- 2 # arrival rate
mu <- 1 # service rate per server
T <- 100 # simulation time
n_sims <- 20 # number of simulations
initial_n <- 100 # initial queue length
dt <- 0.05 # time step
wait_thresholds <- c(5, 10, 15, 20, 25) # wait time thresholds
# Calculate number of steps
n_steps <- ceiling(T/dt)
# Set up parallel processing
n_cores <- detectCores() - 1 # Leave one core free
cl <- makeCluster(n_cores)
registerDoParallel(cl)
# Enhanced simulation function
simulate_mmk <- function(params) {
k <- params$k
lambda <- params$lambda
mu <- params$mu
T <- params$T
dt <- params$dt
initial_n <- params$initial_n
sim_number <- params$sim_number
wait_thresholds <- params$wait_thresholds
n_steps <- ceiling(T/dt)
# Pre-allocate vectors
queue_length <- numeric(n_steps)
processed <- numeric(n_steps)
waiting_times <- numeric(n_steps)
estimated_wait <- numeric(n_steps)
# Create matrices for threshold tracking
wait_counts <- matrix(0, nrow = n_steps, ncol = length(wait_thresholds))
wait_pcts <- matrix(0, nrow = n_steps, ncol = length(wait_thresholds))
colnames(wait_counts) <- paste0("wait_", wait_thresholds)
colnames(wait_pcts) <- paste0("pct_", wait_thresholds)
# Initialize
queue_length[1] <- initial_n
processed[1] <- 0
waiting_times[1] <- 0
estimated_wait[1] <- 0
# Customer tracking matrix
customer_matrix <- matrix(
c(rep(0, initial_n), 1:initial_n),
ncol = 2,
dimnames = list(NULL, c("arrival_time", "position"))
)
# Pre-generate random numbers
arrivals <- rpois(n_steps, lambda * dt)
departures <- rpois(n_steps, k * mu * dt)
# Main simulation loop
for(i in 2:n_steps) {
current_n <- queue_length[i-1]
current_time <- (i-1) * dt
# Handle arrivals
arrival <- arrivals[i]
if(arrival > 0) {
new_customers <- matrix(
c(rep(current_time, arrival),
(nrow(customer_matrix) + 1):(nrow(customer_matrix) + arrival)),
ncol = 2
)
customer_matrix <- rbind(customer_matrix, new_customers)
}
# Handle departures
departure <- min(departures[i], min(current_n, nrow(customer_matrix)))
if(departure > 0 && nrow(customer_matrix) > 0) {
waiting_times[i] <- mean(current_time - customer_matrix[1:departure, 1])
customer_matrix <- customer_matrix[-(1:departure), , drop = FALSE]
} else {
waiting_times[i] <- waiting_times[i-1]
}
# Calculate threshold metrics
if(nrow(customer_matrix) > 0) {
current_wait_times <- current_time - customer_matrix[,1]
for(j in seq_along(wait_thresholds)) {
wait_counts[i, j] <- sum(current_wait_times > wait_thresholds[j])
wait_pcts[i, j] <- (wait_counts[i, j] / nrow(customer_matrix)) * 100
}
}
# Estimate waiting time
estimated_wait[i] <- if(current_n > k) (current_n - k) / (k * mu) else 0
# Update queue length and processed count
queue_length[i] <- current_n + arrival - departure
processed[i] <- processed[i-1] + departure
}
data.frame(
time = seq(0, T-dt, length.out=n_steps),
queue_length = queue_length,
processed = processed,
waiting_time = waiting_times,
estimated_wait = estimated_wait,
wait_counts,
wait_pcts,
simulation = factor(sim_number)
)
}
# Create parameter list for simulations
params_list <- lapply(1:n_sims, function(sim_id) {
list(k=k, lambda=lambda, mu=mu, T=T, dt=dt,
initial_n=initial_n, sim_number=sim_id,
wait_thresholds=wait_thresholds)
})
# Run parallel simulations
sim_data <- foreach(params = params_list,
.packages = c("stats"),
.combine = rbind) %dopar% {
set.seed(123 + params$sim_number)
simulate_mmk(params)
}
# Stop cluster
stopCluster(cl)
# Calculate mean trajectories for basic metrics
time_points <- unique(sim_data$time)
mean_trajectories <- data.frame(
time = time_points,
mean_length = tapply(sim_data$queue_length, sim_data$time, mean),
mean_processed = tapply(sim_data$processed, sim_data$time, mean),
mean_waiting = tapply(sim_data$waiting_time, sim_data$time, mean),
mean_estimated = tapply(sim_data$estimated_wait, sim_data$time, mean)
)
# Calculate threshold trajectories
# First for counts
wait_cols <- grep("^wait_", colnames(sim_data), value = TRUE)
mean_counts_data <- lapply(wait_cols, function(col) {
means <- tapply(sim_data[[col]], sim_data$time, mean)
data.frame(
time = time_points,
count = means,
threshold = sub("wait_", "", col)
)
})
mean_counts <- do.call(rbind, mean_counts_data)
mean_counts$threshold <- factor(mean_counts$threshold,
levels = as.character(wait_thresholds),
labels = paste(wait_thresholds, "iterations"))
# Then for percentages
pct_cols <- grep("^pct_", colnames(sim_data), value = TRUE)
mean_pcts_data <- lapply(pct_cols, function(col) {
means <- tapply(sim_data[[col]], sim_data$time, mean)
data.frame(
time = time_points,
percentage = means,
threshold = sub("pct_", "", col)
)
})
mean_pcts <- do.call(rbind, mean_pcts_data)
mean_pcts$threshold <- factor(mean_pcts$threshold,
levels = as.character(wait_thresholds),
labels = paste(wait_thresholds, "iterations"))
# Calculate theoretical mean
rho <- lambda/(k*mu)
theoretical_mean <- if(rho < 1) {
(lambda^2)/(mu*(mu-lambda)) + lambda/mu
} else {
NA
}
# Create all six plots
p1 <- ggplot() +
geom_line(data = sim_data,
aes(x = time, y = queue_length, group = simulation),
color = "black", alpha = 0.1) +
geom_line(data = mean_trajectories,
aes(x = time, y = mean_length),
color = "red", size = 1) +
{if(!is.na(theoretical_mean))
geom_hline(yintercept = theoretical_mean,
linetype = "dashed",
color = "red",
alpha = 0.5)} +
labs(title = "Queue Length Over Time",
x = "Time", y = "Queue Length") +
theme_minimal()
p2 <- ggplot() +
geom_line(data = sim_data,
aes(x = time, y = processed, group = simulation),
color = "black", alpha = 0.1) +
geom_line(data = mean_trajectories,
aes(x = time, y = mean_processed),
color = "red", size = 1) +
labs(title = "Cumulative Processed Customers",
x = "Time", y = "Number of Processed Customers") +
theme_minimal()
p3 <- ggplot() +
geom_line(data = sim_data,
aes(x = time, y = waiting_time, group = simulation),
color = "black", alpha = 0.1) +
geom_line(data = mean_trajectories,
aes(x = time, y = mean_waiting),
color = "red", size = 1) +
labs(title = "Current Customer Waiting Time",
x = "Time", y = "Waiting Time") +
theme_minimal()
p4 <- ggplot() +
geom_line(data = sim_data,
aes(x = time, y = estimated_wait, group = simulation),
color = "black", alpha = 0.1) +
geom_line(data = mean_trajectories,
aes(x = time, y = mean_estimated),
color = "red", size = 1) +
labs(title = "Estimated Waiting Time for New Arrivals",
x = "Time", y = "Estimated Wait Time") +
theme_minimal()
p5 <- ggplot(mean_counts,
aes(x = time, y = count, color = threshold)) +
geom_line(size = 1) +
scale_color_viridis_d(end = 0.9) +
labs(title = "Number of Customers Exceeding Wait Time Thresholds",
x = "Time",
y = "Number of Customers",
color = "Wait Time\nThreshold") +
theme_minimal() +
theme(legend.position = "right")
p6 <- ggplot(mean_pcts,
aes(x = time, y = percentage, color = threshold)) +
geom_line(size = 1) +
scale_color_viridis_d(end = 0.9) +
labs(title = "Percentage of Customers Exceeding Wait Time Thresholds",
x = "Time",
y = "Percentage of Customers",
color = "Wait Time\nThreshold") +
theme_minimal() +
theme(legend.position = "right")
# Combine all plots
final_plot <- (p1 + p2 + p3) / (p4 + p5 + p6) +
plot_layout(guides = "collect") +
plot_annotation(
title = "Queue Simulation Results",
subtitle = sprintf("Servers: %d, Arrival rate: %.1f/day, Service rate: %.1f/day, Initial queue: %d, Time: %d days, Sims: %d",
k, lambda, mu, initial_n, T, n_sims),
theme = theme(
plot.title = element_text(size = 16, face = "bold"),
plot.subtitle = element_text(size = 10)
)
)
# Display plot
print(final_plot)
# Save plot
ggsave("queue_simulation_results.png", final_plot, width = 15, height = 10, dpi = 300)