forked from drvinceknight/Simulating_Queues
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMM1Q.py
executable file
·99 lines (82 loc) · 2.74 KB
/
MM1Q.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
__metaclass__=type
import random
import csv
#define a class called 'Customer'
class Customer:
def __init__(self,arrival_date,service_start_date,service_time):
self.arrival_date=arrival_date
self.service_start_date=service_start_date
self.service_time=service_time
self.service_end_date=self.service_start_date+self.service_time
self.wait=self.service_start_date-self.arrival_date
#a simple function to sample from negative exponential
def neg_exp(lambd):
return random.expovariate(lambd)
def QSim(lambd=False,mu=False,simulation_time=False):
"""
This is the main function to call to simulate an MM1 queue.
"""
#If parameters are not input prompt
if not lambd:
lambd=input('Inter arrival rate: ')
if not mu:
mu=input('Service rate: ')
if not simulation_time:
simulation_time=input('Total simulation time: ')
#Initialise clock
t=0
#Initialise empty list to hold all data
Customers=[]
#----------------------------------
#The actual simulation happens here:
while t<simulation_time:
#calculate arrival date and service time for new customer
if len(Customers)==0:
arrival_date=neg_exp(lambd)
service_start_date=arrival_date
else:
arrival_date+=neg_exp(lambd)
service_start_date=max(arrival_date,Customers[-1].service_end_date)
service_time=neg_exp(mu)
#create new customer
Customers.append(Customer(arrival_date,service_start_date,service_time))
#increment clock till next end of service
t=arrival_date
#----------------------------------
#calculate summary statistics
Waits=[a.wait for a in Customers]
Mean_Wait=sum(Waits)/len(Waits)
Total_Times=[a.wait+a.service_time for a in Customers]
Mean_Time=sum(Total_Times)/len(Total_Times)
Service_Times=[a.service_time for a in Customers]
Mean_Service_Time=sum(Service_Times)/len(Service_Times)
Utilisation=sum(Service_Times)/t
#output summary statistics to screen
print ""
print "Summary results:"
print ""
print "Number of customers: ",len(Customers)
print "Mean Service Time: ",Mean_Service_Time
print "Mean Wait: ",Mean_Wait
print "Mean Time in System: ",Mean_Time
print "Utilisation: ",Utilisation
print ""
#prompt user to output full data set to csv
if input("Output data to csv (True/False)? "):
outfile=open('MM1Q-output-(%s,%s,%s).csv' %(lambd,mu,simulation_time),'wb')
output=csv.writer(outfile)
output.writerow(['Customer','Arrival_Date','Wait','Service_Start_Date','Service_Time','Service_End_Date'])
i=0
for customer in Customers:
i=i+1
outrow=[]
outrow.append(i)
outrow.append(customer.arrival_date)
outrow.append(customer.wait)
outrow.append(customer.service_start_date)
outrow.append(customer.service_time)
outrow.append(customer.service_end_date)
output.writerow(outrow)
outfile.close()
print ""
return