-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoundRobin.py
67 lines (59 loc) · 2.22 KB
/
RoundRobin.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
class RoundRobin:
def __init__(self, arrival_times, service_times, m):
self.arrival_times = arrival_times
self.service_times = service_times
self.m = m
def update_queues(self, queues, arrival_time, departure_times):
max_queue = 0
average_queue_len = 0
for queue in queues.values():
while len(queue) > 0 and arrival_time > departure_times[queue[0]]:
queue.pop(0)
max_queue = max(max_queue, len(queue))
average_queue_len += len(queue)
average_queue_len /= len(queues)
return max_queue, average_queue_len
def run(self):
# Initialization
arrival_times = self.arrival_times
service_times = self.service_times
m = self.m
size = len(arrival_times)
# Servers to be chosen
selected = []
s = 0
for i in range(size):
selected.append(s)
s += 1
if s == m:
s = 0
# Statistics to be collected
wait_times = []
system_times = []
departure_times = []
max_len = 0
average_len = 0
# List of customers stored for each server
queues = {i: [] for i in range(m)}
for i in range(size):
arrival_time = arrival_times[i]
service_time = service_times[i]
server = selected[i]
queue = queues[server]
max_temp, average_temp = self.update_queues(queues, arrival_time, departure_times)
max_len = max(max_len, max_temp)
average_len += average_temp
if i == 0 or len(queue) == 0:
wait_time = 0
else:
prev = queue[-1]
wait_time = max(0, departure_times[prev] - arrival_time)
departure_time = arrival_time + wait_time + service_time
system_time = departure_time - arrival_time
# Update statistics
wait_times.append(wait_time)
departure_times.append(departure_time)
system_times.append(system_time)
queues[server].append(i)
average_len /= size
return wait_times, system_times, departure_times, selected, max_len, average_len