-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDPS_Simulation.py
299 lines (229 loc) · 9.87 KB
/
DPS_Simulation.py
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import numpy as np
from queue import Queue
import random
import math
class Customer:
def __init__(self, id, arrival_time, waiting_time, service_time, cl):
self.arrival_time = arrival_time
self.waiting_time = waiting_time
self.service_time = service_time
self.id = id
self.cl = cl
def __str__(self):
return "id:" + str(self.id) + "\t arrival time:" + str(self.arrival_time) + "\t waiting time:" + str(
self.waiting_time) + "\t service time:" + str(self.service_time)
def __lt__(self, other):
return self.arrival_time < other.arrival_time
class Event:
def __init__(self, customer_id, type_idx, occ_time):
self.types = ['arrival', 'deadline', 'done']
self.type_idx = type_idx
self.occ_time = occ_time
self.customer_id = customer_id
def __str__(self):
return "customer id:" + str(self.customer_id) + "\t occurence time:" + str(self.occ_time) + "\t type:" + str(
self.types[self.type_idx])
def __lt__(self, other):
return self.occ_time < other.occ_time
def __le__(self, other):
return self.occ_time <= other.occ_time
def __sub__(self, other):
if isinstance(other, Event):
return self.occ_time - other.occ_time
else:
return self.occ_time - other
def generate_exp_rv(rate):
return -1.0 * np.log(1.0 - random.random()) / rate
def binary_search(arr, x):
low = 0
high = len(arr) - 1
mid = 0
while low <= high:
mid = (high + low) // 2
# If x is greater, ignore left half
if arr[mid] < x:
low = mid + 1
# If x is smaller, ignore right half
elif arr[mid] > x:
high = mid - 1
# means x is present at mid
else:
return mid
# If we reach here, then the element was not present
return low
def insert_arr(arr, value):
# idx = np.argmin(np.abs(value - np.array(arr)))
idx = binary_search(arr, value)
if idx >= len(arr):
idx = len(arr)
arr.insert(idx, value)
else:
if value <= arr[idx]:
arr.insert(idx, value)
else:
while idx <= len(arr) - 1 and value > arr[idx]:
idx += 1
arr.insert(idx, value)
return arr
def make_customer(pre_cus_time, id, cl):
if const_theta:
waiting_time = theta
else:
waiting_time = generate_exp_rv(1.0 / theta)
service_time = generate_exp_rv(mu)
arrival_time = pre_cus_time + generate_exp_rv(lamb)
customer = Customer(id, arrival_time, waiting_time, service_time, cl)
return customer
def find_first_job(queue):
if len(queue) == 0:
return None
min = queue[0]
for p in queue:
if (p.service_time / p.cl) < (min.service_time / min.cl):
min = p
return min
if __name__ == "__main__":
f = open("parameters.conf", "r")
theta = float(f.readline())
mu = float(f.readline())
# theta = 2.0
# mu = 1.0
const_theta = False
num_of_customers = 1_000_000_0
for lamba in [5.0,10.0,15.0]:
lamb = lamba
# ============================= Experiment =============================
arrival_time = 0
pre_cus_time = 0
len_c1, len_c2 = 0, 0
cl = 1 if random.random() < 0.5 else 2
cus = make_customer(pre_cus_time, id=0, cl=cl)
queue = []
num_blocked_users = 0
num_deadlined_users = 0
num_deadlined_users1 = 0
num_deadlined_users2 = 0
events = []
time = 0
cnt = 0
cnt1 = 0
cnt2 = 0
cnt1 = cnt1 + 1 if cl == 1 else cnt1
cnt2 = cnt2 + 1 if cl == 2 else cnt2
events.append(Event(cnt, type_idx=0, occ_time=cus.arrival_time))
pre_cus = cus
cnt += 1
cl = 1 if random.random() < 0.5 else 2
nxt_cus = make_customer(cus.arrival_time, 1, cl=cl)
done = False
# main loop
while len(events) > 0 or cnt < num_of_customers or len(queue) > 0:
pre_task_service1 = 0
pre_task_service2 = 0
if len(queue) > 0:
pre_task_service1 = (mu * 1 / (1 * len_c1 + 2 * len_c2))
pre_task_service2 = (mu * 2 / (1 * len_c1 + 2 * len_c2))
first_job = find_first_job(queue)
tmp_per_task_service = 0
if first_job:
tmp_per_task_service = pre_task_service1 if first_job.cl == 1 else pre_task_service2
if first_job and (
((first_job.service_time / tmp_per_task_service) + time) < nxt_cus.arrival_time or
cnt >= num_of_customers):
if events[0].occ_time >= ((first_job.service_time / tmp_per_task_service) + time):
############ CALCULATE SERVICE ###########
# a job should be execute before next event
# remove customer from server
queue.remove(first_job)
if first_job.cl == 1:
len_c1 -= 1
else:
len_c2 -= 1
# remove deadline event
for event in events:
if event.customer_id == first_job.id and event.type_idx == 1:
events.remove(event)
break
# calculate current time
time += (first_job.service_time / tmp_per_task_service)
# calculate the time should subtract for each customer
for customer in queue:
customer.service_time -= (customer.cl * first_job.service_time / first_job.cl)
continue
###########################################
if nxt_cus:
################ ADDING CUSTOMER ################
if cnt < num_of_customers:
if len(events) == 0:
events.append(Event(cnt, type_idx=0, occ_time=nxt_cus.arrival_time))
cnt += 1
pre_cus = nxt_cus
cl = 1 if random.random() < 0.5 else 2
cnt1 = cnt1 + 1 if cl == 1 else cnt1
cnt2 = cnt2 + 1 if cl == 2 else cnt2
nxt_cus = make_customer(pre_cus.arrival_time, cnt, cl=cl)
elif nxt_cus.arrival_time < events[0].occ_time:
events.insert(0, Event(cnt, type_idx=0, occ_time=nxt_cus.arrival_time))
cnt += 1
pre_cus = nxt_cus
cl = 1 if random.random() < 0.5 else 2
cnt1 = cnt1 + 1 if cl == 1 else cnt1
cnt2 = cnt2 + 1 if cl == 2 else cnt2
nxt_cus = make_customer(pre_cus.arrival_time, cnt, cl=cl)
#################################################
# get first event of list of events (sorted)
event = events.pop(0)
# update time
pre_time = time
time = event.occ_time
# calculate each per_task_services
pre_task_service1 = 0
pre_task_service2 = 0
if len(queue) > 0:
pre_task_service1 = (mu * 1 / (1 * len_c1 + 2 * len_c2))
pre_task_service2 = (mu * 2 / (1 * len_c1 + 2 * len_c2))
# calculate service time of each service
for customer in queue:
customer.service_time -= ((time - pre_time) * pre_task_service1 * customer.cl)
# arrival customer event
if event.type_idx == 0:
if len(queue) >= 12:
cnt += 1
num_blocked_users += 1
pre_cus = nxt_cus
cl = 1 if random.random() < 0.5 else 2
cnt1 = cnt1 + 1 if cl == 1 else cnt1
cnt2 = cnt2 + 1 if cl == 2 else cnt2
nxt_cus = make_customer(pre_cus.arrival_time, cnt, cl=cl)
insert_arr(events, Event(pre_cus.id, 0, pre_cus.arrival_time))
else:
customer = pre_cus
queue.append(customer)
if customer.cl == 1:
len_c1 += 1
else:
len_c2 += 1
# adding deadline by the way
insert_arr(events, Event(customer.id, 1, time + customer.waiting_time))
# dead line event
if event.type_idx == 1:
customer = next((x for x in queue if x.id == event.customer_id), None)
queue.remove(customer)
if customer.cl == 1:
len_c1 -= 1
else:
len_c2 -= 1
num_deadlined_users += 1
if customer.cl == 1:
num_deadlined_users1 += 1
elif customer.cl == 2:
num_deadlined_users2 += 1
pb_experiment = num_blocked_users / num_of_customers
pd_experiment = num_deadlined_users / num_of_customers
pd1_experiment = num_deadlined_users1 / num_of_customers
pd2_experiment = num_deadlined_users2 / num_of_customers
print("lambda : ", lamba)
print(pb_experiment, "\t\t", pd_experiment, "\t\t", pd1_experiment, "\t\t", pd2_experiment, "\n")
with open('exp.txt', 'a') as f:
f.writelines(str(pb_experiment) + "\t" + str(pd_experiment) + "\t" + str(pd1_experiment) + "\t" + str(
pd2_experiment) + "\n")