-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain_Rscript.R
More file actions
181 lines (129 loc) · 4.75 KB
/
Main_Rscript.R
File metadata and controls
181 lines (129 loc) · 4.75 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
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
# Clear your environment of variables
rm(list = ls())
#NPP intensity
lambdaf = function(t){
return(3 + (4/(t+1)))
}
#define DES function
time_next_arrival = function(s, lambda){
t = s
flag = 1
while(flag){
U1 = runif(1)
t = t - (1/lambda) * log(U1)
U2 = runif(1)
if (U2 <= lambdaf(t)/lambda){
T_s = t
flag = 0
}
}
return(T_s)
}
T = 4
lambda = 7
mu = 5
set.seed(1)
# Initialize
t= 0 # time
Na = 0 # number of arrivals at expert by time t
ND1 = 0 # number of departures from whisky expert by time t
ND2 = 0
# State variables # state of the system (number of customers in system) at time t
n1 = 0# ST will be a matrix that stores our (n,t) state-time pairs that we record
n2 =0 # every time there is an event: arrival or departure.
ST = matrix(c(n1,n2,t) , nrow = 1, ncol = 3)
# Event List variables
# use the nonhomogeneous Poisson to generate t_A = time of next arrival
t_A = time_next_arrival(t,lambda)
t_D1 = Inf # time of next departure from expert ; set to Inf since server is idle
t_D2 = Inf #time of next departure from cashier
event_list = matrix(c(t_A, t_D1, t_D2), nrow=1, ncol=3)
# Initialize output variables
Ae = c() # A(i) = time of arrival of ith customer at expert
D1 = c() # D(i) = time of departure of ith customer from expert
D2 = c() # time of departure of ith customer from cashier
T_p = 0 # time past T that the last customer departs
# Main loop
flag = 1
while(flag){
if (t_A == min(t_A,t_D1,t_D2,T)){ # Case 1: arrival at expert and within T
t = t_A # move to time t_A
Na = Na + 1 # count the arrival
n1=n1+1
t_A = time_next_arrival(t, lambda) # generate time of next arrival
if (n1 == 1){ # system had been empty so the arrival goes to the server
Y1 = -(1/mu) * log(runif(1)) # generate an exp(mu) RV
t_D1 = t + Y1 # set time of next departure from expert
}
Ae = c(Ae,t) # collect output data: A(N_A) = t
event_list = rbind(event_list, c(t_A, t_D1, t_D2))
} else if (t_D1==min(t_A,t_D1,t_D2,T)){ # Case 2: departure from expert and within T
t = t_D1 # move to time t_D
ND1 = ND1 + 1 # count the departure
n1=n1-1
n2=n2+1
if (n1==0){
t_D1=Inf
}
else {
Y1=-(1/mu) * log(runif(1))
t_D1=t+Y1
}
if (n2 == 1){ # no customers at the cashier
Y2 = -(1/mu) * log(runif(1)) # generate an exp(mu) RV
t_D2 = t + Y2 # set time of next departure from cashier
}
D1 = c(D1,t) # collect output data: D(N_D) = t
ST = rbind(ST, c(n1,n2,t)) # update ST
event_list = rbind(event_list, c(t_A, t_D1, t_D2))
} else if (t_D2<=min(t_D1,t_D2,t_A,T)) {#case 3: departure from cashier and within T
t=t_D2
ND2 = ND2 +1 #count departure from cashier
n2 = n2-1
if (n2 == 0){ # no customers at the cashier
t_D2 = Inf
} else { # new customer at the cashier
Y2 = -(1/mu) * log(runif(1)) # generate an exp(mu) RV
t_D2 = t + Y2 # set time of next departure
}
D2 = c(D2,t)
ST = rbind(ST, c(n1,n2,t)) # update ST
event_list = rbind(event_list, c(t_A, t_D1, t_D2))
} else if ((min(t_A,t_D1, t_D2) > T) & (n1 > 0)){
# Case 4: time ended, customers remain at expert, go to next queue
t = t_D1 # ignore arrival t_A since > T but still need to deal with
# customers in the system so we go to the next departure
ND1 = ND1 + 1 # count departure
n1=n1-1
n2 = n2+1
if (n1 > 0) {
# if we still have customers, they go to the server,
# if no customers then it is taken care of in the next iter under Case 4
Y1 = -(1/mu) * log(runif(1)) # generate an exp(mu) RV
t_D1 = t + Y1 # set time of next departure
}
D1 = c(D1,t) # collect output data: D(N_D) = t
ST = rbind(ST, c(n1,n2,t)) # update ST
event_list = rbind(event_list, c(t_A,t_D1,t_D2))
} else if ((min(t_A,t_D1, t_D2) > T) & (n1==0) & (n2>0)){
# Case 5: time ended, customers emptied from expert, customers remain at
#cashier, go to next departure
t = t_D2
ND2=ND2 +1
n2=n2-1
if (n2>0) {
Y2 = -(1/mu) * log(runif(1))
t_D2 = t+Y2
}
D2 = c(D2,t)
ST = rbind(ST, c(n1,n2,t)) # update ST
event_list = rbind(event_list, c(t_A,t_D1,t_D2))
} else if ((min(t_A,t_D1, t_D2) > T) & (n1==0) & (n2==0)){
# Case 6: time ended, customers gone, the end
flag = 0
T_p = max(t-T,0)
} else {
print(paste("I should not be here"))
}
}
print('Average time customers spend in the shop =' mean(D2-Ae)