/
SRPTE_Catastrophic.py
1037 lines (871 loc) · 34.1 KB
/
SRPTE_Catastrophic.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#----------------------------------------------------------------------#
# SRPTE.py
#
# This application simulates a single server with Poisson arrivals
# and processing times of a general distribution. There are errors in
# time estimates within a range. Jobs are serviced in order of shortest
# remaining processing time.
#
# Rachel Mailach
#----------------------------------------------------------------------#
from tkinter import *
#from tkinter import messagebox
from tkinter import ttk
from tkinter import filedialog
from datetime import datetime
from math import log
import plotly.plotly as py
from plotly.graph_objs import Scatter
import plotly.graph_objs as go
#from scipy import integrate as integrate
import copy
import random
import csv
import operator
import sqlite3
import pandas
conn=sqlite3.connect('SingleServerDatabase_SRPTE.db')
NumJobs = []
AvgNumJobs = []
NumJobsTime = []
TimeSys = []
ProcTime = []
PercError = []
#----------------------------------------------------------------------#
# Class: GUI
#
# This class is used as a graphical user interface for the application.
#
#----------------------------------------------------------------------#
class GUI(Tk):
def __init__(self, master):
Tk.__init__(self, master)
self.master = master # reference to parent
self.statusText = StringVar()
global SEED
#SEED = random.randint(0, 1000000000)
SEED = 994863731
random.seed(SEED)
# Create the input frame
self.frameIn = Input(self)
self.frameIn.pack(side=TOP, fill=BOTH, padx = 5, pady =5, ipadx = 5, ipady = 5)
# Create the output frame
self.frameOut = Output(self)
self.frameOut.pack(side=TOP, fill=BOTH, padx = 5, pady =5, ipadx = 5, ipady = 5)
# Bind simulate button
self.bind("<<input_simulate>>", self.submit)
# Bind save button
self.bind("<<output_save>>", self.saveData)
# Bind clear button
self.bind("<<output_clear>>", self.clearConsole)
# Bind stop button
self.bind("<<stop_sim>>", self.stopSimulation)
# Status Bar
status = Label(self.master, textvariable=self.statusText, bd=1, relief=SUNKEN, anchor=W)
status.pack(side=BOTTOM, anchor=W, fill=X)
# Initialize console
self.consoleFrame = Frame(self.frameOut)
self.console = Text(self.consoleFrame, wrap = WORD)
self.makeConsole()
self.printIntro()
self.updateStatusBar("Waiting for submit...")
def makeConsole(self):
#self.consoleFrame = Frame(self.frameOut)
self.consoleFrame.pack(side=TOP, padx=5, pady=5)
#self.console = Text(self.consoleFrame, wrap = WORD)
self.console.config(state=DISABLED) # start with console as disabled (non-editable)
self.scrollbar = Scrollbar(self.consoleFrame)
self.scrollbar.config(command = self.console.yview)
self.console.config(yscrollcommand=self.scrollbar.set)
self.console.grid(column=0, row=0)
self.scrollbar.grid(column=1, row=0, sticky='NS')
def writeToConsole(self, text = ' '):
self.console.config(state=NORMAL) # make console editable
self.console.insert(END, '%s\n'%text)
self.update()
self.console.yview(END) # auto-scroll
self.console.config(state=DISABLED) # disable (non-editable) console
def saveData(self, event):
# Get filename
filename = fileDialog.asksaveasfilename(title="Save as...", defaultextension='.txt')
if filename:
file = open(filename, mode='w')
data = self.console.get(1.0, END)
encodedData = data.encode('utf-8')
text = str(encodedData)
file.write(text)
file.close()
# Empty arrivals file at the begining of each simulation
def clearSavedArrivals(self):
with open("Arrivals.txt", "w") as myFile:
myFile.write('Job Name, Arrival Time, RPT, ERPT' + '\n')
myFile.close()
def clearConsole(self, event):
self.console.config(state=NORMAL) # make console editable
self.console.delete('1.0', END)
self.console.config(state=DISABLED) # disable (non-editable) console
def updateStatusBar(self, text=' '):
self.statusText.set(text)
def printIntro(self):
self.writeToConsole("SRPTE \n\n This application simulates a single server with Poisson arrivals and processing times of a general distribution. Each arrival has an estimation error within a percent error taken as input. Jobs are serviced in order of shortest remaining processing time.")
def saveParams(self, load, arrRate, arrDist, procRate, procDist, percErrorMin, percErrorMax, simLength, alpha, lower, upper):
##params = pandas.DataFrame(columns=('seed', 'numServers', 'load', 'arrRate', 'arrDist', 'procRate', 'procDist', 'alpha', 'lower', 'upper', 'percErrorMin', 'percErrorMax', 'simLength'))
print (SEED)
params = pandas.DataFrame({ 'seed' : [SEED],
'load' : [load],
'arrRate' : [arrRate],
'arrDist' : [arrDist],
'procRate' : [procRate],
'procDist' : [procDist],
'alpha' : [alpha],
'lower' : [lower],
'upper' : [upper],
'percErrorMin' : [percErrorMin],
'percErrorMax' : [percErrorMax],
'simLength' : [simLength],
'avgNumJobs' : [MachineClass.AvgNumJobs]
})
params.to_sql(name='parameters', con=conn, if_exists='append')
print (params)
def printParams(self, load, arrDist, procRate, procDist, percErrorMin, percErrorMax, simLength):
self.writeToConsole("--------------------------------------------------------------------------------")
self.writeToConsole("PARAMETERS:")
self.writeToConsole("Load = %.4f"%load)
#self.writeToConsole("Arrival Rate = %.4f"%arrRate)
self.writeToConsole("Arrival Distribution = %s"%arrDist)
self.writeToConsole("Processing Rate = %.4f, Processing Distribution = %s"%(procRate, str(procDist)))
self.writeToConsole("% Error = " + u"\u00B1" + " %.4f, %.4f"%(percErrorMin, percErrorMax))
self.writeToConsole("Simulation Length = %.4f\n\n"%simLength)
def plotNumJobsInSys(self):
py.sign_in('mailacrs','wowbsbc0qo')
trace0 = Scatter(x=NumJobsTime, y=NumJobs)
data = [trace0]
layout = go.Layout(
title='Number of Jobs Over Time',
xaxis=dict(
title='Time',
titlefont=dict(
family='Courier New, monospace',
size=18,
color='#7f7f7f'
)
),
yaxis=dict(
title='Number of Jobs',
titlefont=dict(
family='Courier New, monospace',
size=18,
color='#7f7f7f'
)
)
)
fig = go.Figure(data=data, layout=layout)
unique_url = py.plot(fig, filename = 'SRPT_NumJobs')
def plotAvgNumJobsInSys(self):
py.sign_in('mailacrs','wowbsbc0qo')
trace0 = Scatter(x=NumJobsTime, y=AvgNumJobs)
data = [trace0]
layout = go.Layout(
title='Average Number of Jobs Over Time',
xaxis=dict(
title='Time',
titlefont=dict(
family='Courier New, monospace',
size=18,
color='#7f7f7f'
)
),
yaxis=dict(
title='Number of Jobs',
titlefont=dict(
family='Courier New, monospace',
size=18,
color='#7f7f7f'
)
)
)
fig = go.Figure(data=data, layout=layout)
unique_url = py.plot(fig, filename = 'SRPT_AvgNumJobs')
def calcVariance(self, List, avg):
var = 0
for i in List:
var += (avg - i)**2
return var/len(List)
def displayAverageData(self):
##AvgNumJobs = int(float(sum(NumJobs))/len(NumJobs))
AvgNumJobs = MachineClass.AvgNumJobs
AvgTimeSys = float(sum(TimeSys))/len(TimeSys)
AvgProcTime = float(sum(ProcTime))/len(ProcTime)
VarProcTime = self.calcVariance(ProcTime, AvgProcTime)
AvgPercError = float(sum(PercError))/len(PercError)
self.writeToConsole('\n\nAverage number of jobs in the system %s' %AvgNumJobs)
self.writeToConsole('Average time in system, from start to completion is %s' %AvgTimeSys)
self.writeToConsole('Average processing time, based on generated service times is %s' %AvgProcTime)
self.writeToConsole('Variance of processing time %s' %VarProcTime)
self.writeToConsole('Average percent error %.4f\n' %AvgPercError)
#self.writeToConsole('Request order: %s' % ArrivalClass.JobOrderIn)
#self.writeToConsole('Service order: %s\n\n' % MachineClass.JobOrderOut)
def stopSimulation(self, event):
MachineClass.StopSim = True
def submit(self, event):
self.updateStatusBar("Simulating...")
self.clearSavedArrivals()
I = Input(self)
self.printParams(I.valuesList[0], #load
'Exponential', #arrival dist
#I.valuesList[1], # arrival rate
I.valuesList[2], I.distList[1], #processing rate
I.valuesList[3], #error min
I.valuesList[4], #error max
I.valuesList[5]) #sim time
main.timesClicked = 0
# Start process
MC = MachineClass(self)
MC.run( I.valuesList[0], # load
'Exponential', # arrival
#I.valuesList[1], # arrival rate
I.valuesList[2], I.distList[1], # processing
I.valuesList[3], # error min
I.valuesList[4], # error max
I.valuesList[5]) # sim time
self.saveParams(I.valuesList[0], #load
'?', # arrival rate
'Exponential', # arrival dist
'?', I.distList[1], # processing
I.valuesList[3], # error min
I.valuesList[4], # error max
I.valuesList[5], # sim time
JobClass.BPArray[0], # alpha
JobClass.BPArray[1], # lower
JobClass.BPArray[2]) # upper
self.displayAverageData()
self.plotNumJobsInSys()
self.plotAvgNumJobsInSys()
#self.saveData()
self.updateStatusBar("Simulation complete.")
#----------------------------------------------------------------------#
# Class: Input
#
# This class is used as a graphical user interface for a larger
# application.
#
#----------------------------------------------------------------------#
class Input(LabelFrame):
def __init__(self, master):
LabelFrame.__init__(self, master, text = "Input")
self.master = master
self.loadInput = DoubleVar()
self.arrivalRateInput = DoubleVar()
self.processingRateInput = DoubleVar()
self.percentErrorMinInput = DoubleVar()
self.percentErrorMaxInput = DoubleVar()
self.simLengthInput = DoubleVar()
self.errorMessage = StringVar()
self.comboboxVal = StringVar()
self.loadInput.set(0.95) ##################################CHANGE LATER
#self.arrivalRateInput.set(1.0) ##################################CHANGE LATER
self.processingRateInput.set(0.5) ##################################CHANGE LATER
self.percentErrorMinInput.set(-50) ##################################CHANGE LATER
self.percentErrorMaxInput.set(0) ##################################CHANGE LATER
self.simLengthInput.set(5000000.0)
self.grid_columnconfigure(0, weight=2)
self.grid_columnconfigure(1, weight=2)
self.grid_columnconfigure(2, weight=1)
self.grid_columnconfigure(3, weight=1)
self.grid_columnconfigure(4, weight=1)
self.grid_columnconfigure(5, weight=2)
self.grid_rowconfigure(0, weight=1)
# Labels
labels = ['System Load', 'Interarrival Rate (' + u'\u03bb' + ')', 'Processing Rate (' + u'\u03bc' + ')', '% Error' , 'Simulation Length']
r=0
c=0
for elem in labels:
Label(self, text=elem).grid(row=r, column=c)
r=r+1
Label(self, textvariable=self.errorMessage, fg="red", font=14).grid(row=6, columnspan=4) #error message, invalid input
#Label(self, text=u"\u00B1").grid(row=3, column=1) # +/-
Label(self, text="Min").grid(row=3, column=1, sticky = E)
Label(self, text="Max").grid(row=3, column=3, sticky = W)
# Entry Boxes
self.entry_0 = Entry(self, textvariable = self.loadInput)
self.entry_1 = Entry(self, textvariable = self.arrivalRateInput)
self.entry_2 = Entry(self, textvariable = self.processingRateInput)
self.entry_3a = Entry(self, textvariable = self.percentErrorMinInput, width = 5)
self.entry_3b = Entry(self, textvariable = self.percentErrorMaxInput, width = 5)
self.entry_4 = Entry(self, textvariable = self.simLengthInput)
self.entry_0.grid(row = 0, column = 1)
self.entry_1.grid(row = 1, column = 1)
self.entry_2.grid(row = 2, column = 1)
self.entry_3a.grid(row = 3, column = 2, sticky = E)
self.entry_3b.grid(row = 3, column = 4, sticky = W)
self.entry_4.grid(row = 4, column = 1)
# Disable interarrival time
self.entry_1.delete(0, 'end')
self.entry_1.configure(state = 'disabled')
# Distribution Dropdowns
self.distributions = ('Select Distribution', 'Poisson', 'Exponential', 'Uniform', 'Bounded Pareto', 'Custom')
self.comboBox_1 = ttk.Combobox(self, values = self.distributions, state = 'disabled')
self.comboBox_1.current(2) # set selection
self.comboBox_1.grid(row = 1, column = 5)
self.comboBox_2 = ttk.Combobox(self, textvariable = self.comboboxVal, values = self.distributions, state = 'readonly')
self.comboBox_2.current(4) # set default selection #####################CHANGE LATER
self.comboBox_2.grid(row = 2, column = 5)
self.comboboxVal.trace("w", self.selectionChange) # refresh on change
self.refreshComboboxes()
# Simulate Button
self.simulateButton = Button(self, text = "SIMULATE", command = self.onButtonClick)
self.simulateButton.grid(row = 7, columnspan = 6)
def entryBoxChange(self, name, index, mode):
self.refreshLoad()
def refreshLoad(self):
if len(self.entry_0.get()) > 0:
self.entry_1.delete(0, 'end')
self.entry_1.configure(state = 'disabled')
else:
self.entry_1.configure(state = 'normal')
self.arrivalRateInput.set(self.arrRateDefault)
if len(self.entry_1.get()) > 0:
self.entry_0.delete(0, 'end')
self.entry_0.configure(state = 'disabled')
else:
self.entry_0.configure(state = 'normal')
self.loadInput.set(self.loadDefault)
def selectionChange(self, name, index, mode):
self.refreshComboboxes()
def refreshComboboxes(self):
selection = self.comboBox_2.get()
if selection == 'Bounded Pareto':
#self.entry_2.delete(0, 'end')
self.entry_2.configure(state = 'disabled')
else:
self.entry_2.configure(state = 'normal')
#self.processingRateInput.set(self.procRateDefault)
def onButtonClick(self):
if (self.getNumericValues() == 0) and (self.getDropDownValues() == 0):
# Send to submit button in main
self.simulateButton.event_generate("<<input_simulate>>")
def getNumericValues(self):
try:
load = self.loadInput.get()
#arrivalRate = self.arrivalRateInput.get()
processingRate = self.processingRateInput.get()
percentErrorMin = self.percentErrorMinInput.get()
percentErrorMax = self.percentErrorMaxInput.get()
maxSimLength = self.simLengthInput.get()
except ValueError:
self.errorMessage.set("One of your inputs is an incorrect type, try again.")
return 1
#try:
# arrRate = float(self.arrivalRateInput.get())
#except ValueError:
# arrRate = 0.0
#try:
# procRate = float(self.processingRateInput.get())
#except ValueError:
# procRate = 0.0
if load <= 0.0:
self.errorMessage.set("System load must be a non-zero value!")
return 1
#if arrivalRate <= 0.0:
# self.errorMessage.set("Arrival rate must be a non-zero value!")
# return 1
#if processingRate <= 0.0:
# self.errorMessage.set("Processing rate must be a non-zero value!")
# return 1
if maxSimLength <= 0.0:
self.errorMessage.set("Simulation length must be a non-zero value!")
return 1
else:
self.errorMessage.set("")
Input.valuesList = [load, 0.0, processingRate, percentErrorMin, percentErrorMax, maxSimLength]
return 0
def getDropDownValues(self):
comboBox1Value = self.comboBox_1.get()
comboBox2Value = self.comboBox_2.get()
if comboBox2Value == 'Select Distribution':
self.errorMessage.set("You must select a distribution for the processing rate")
return 1
else:
self.errorMessage.set("")
Input.distList = [comboBox1Value, comboBox2Value]
return 0
#----------------------------------------------------------------------#
# Class: Output
#
# This class is used as a graphical user interface for a larger
# application.
#
#----------------------------------------------------------------------#
class Output(LabelFrame):
def __init__(self, master):
LabelFrame.__init__(self, master, text = "Output")
self.grid_columnconfigure(0, weight=1)
self.grid_rowconfigure(0, weight=1)
buttonFrame = Frame(self)
buttonFrame.pack(side=BOTTOM, padx=5, pady=5)
# Clear Button
self.clearButton = Button(buttonFrame, text = "CLEAR DATA", command = self.onClearButtonClick)
self.clearButton.grid(row = 2, column = 0)
# Save Button
self.saveButton = Button(buttonFrame, text = "SAVE DATA", command = self.onSaveButtonClick)
self.saveButton.grid(row=2, column=1)
# Stop Button
self.stopButton = Button(buttonFrame, text = "STOP SIMULATION", command = self.onStopButtonClick)
self.stopButton.grid(row = 2, column = 2)
def onClearButtonClick(self):
# Clear console
self.clearButton.event_generate("<<output_clear>>")
def onSaveButtonClick(self):
# Save data
self.saveButton.event_generate("<<output_save>>")
def onStopButtonClick(self):
# Stop simulation
self.stopButton.event_generate("<<stop_sim>>")
#----------------------------------------------------------------------#
# Class: CustomDist
#
# This class is used to allow users to enter a custom distribution.
#
#----------------------------------------------------------------------#
class CustomDist(object):
def __init__(self, master):
top = self.top = Toplevel(master)
top.geometry("500x200") # set window size
top.resizable(0,0)
self.function = StringVar()
# Label frame
frame1 = Frame(top)
frame1.pack(side=TOP, padx=5, pady=5)
self.l=Label(frame1, text="Please enter the functional inverse of the distribution of your choice. \nExponential distribution is provided as an example. \nNote: x " + u"\u2265" + " 0", font=("Helvetica", 12), justify=LEFT)
self.l.pack()
# Button frame
frame2 = Frame(top)
frame2.pack(side=TOP, padx=5, pady=5)
self.mu=Button(frame2, text=u'\u03bc', command=self.insertMu)
self.mu.pack(side=LEFT)
self.x=Button(frame2, text="x", command=self.insertX)
self.x.pack(side=LEFT)
self.ln=Button(frame2, text="ln", command=self.insertLn)
self.ln.pack(side=LEFT)
# Input frame
frame3 = Frame(top)
frame3.pack(side=TOP, padx=5, pady=5)
self.e = Entry(frame3, textvariable = self.function)
self.e.insert(0, "-ln(1 - x)/" + u'\u03bc')
self.e.pack(fill="both", expand=True)
frame4 = Frame(top)
frame4.pack(side=TOP, pady=10)
self.b=Button(frame4,text='Ok',command=self.cleanup)
self.b.pack()
def cleanup(self):
self.stringEquation=self.convertFunction()
self.top.destroy()
def insertMu(self):
self.e.insert(END, u'\u03bc')
def insertX(self):
self.e.insert(END, "x")
def insertLn(self):
self.e.insert(END, "ln")
def convertFunction(self):
self.stringList = list(self.e.get())
for i in range(len(self.stringList)):
if self.stringList[i] == u'\u03bc':
self.stringList[i] = "procRate"
elif self.stringList[i] == "x":
self.stringList[i] = "random.uniform(0.0, 1.0)"
elif self.stringList[i] == "l" and self.stringList[i+1] == "n":
self.stringList[i] = "log"
self.stringList[i+1] = ""
print ("".join(self.stringList))
return "".join(self.stringList)
#----------------------------------------------------------------------#
# Class: BoundedParetoDist
#
# This class is used to allow users to enter parameters to
# Bounded Pareto distribution.
#
#----------------------------------------------------------------------#
class BoundedParetoDist(object):
Array = []
def __init__(self, master):
top = self.top = Toplevel(master)
top.geometry("500x200") # set window size
top.resizable(0,0)
self.errorMessage = StringVar()
self.alpha = DoubleVar()
self.L = DoubleVar()
self.U = DoubleVar()
# Set default parameters
self.alpha.set(1.1)
self.L.set(1)
self.U.set(10**(6))
# Label frame
frame1 = Frame(top)
frame1.pack(side=TOP, padx=5, pady=5)
self.l=Label(frame1, text="Please enter the parameters you would like.", font=("Helvetica", 12), justify=LEFT)
self.l.pack()
self.error = Label(frame1, textvariable=self.errorMessage, fg="red", font=14)
self.error.pack()
# Input frame
frame2 = Frame(top)
frame2.pack(side=TOP, padx=5, pady=5)
frame2.grid_columnconfigure(0, weight=1)
frame2.grid_rowconfigure(0, weight=1)
self.l1 = Label(frame2, text = "alpha (shape)")
self.l2 = Label(frame2, text = "L (smallest job size)")
self.l3 = Label(frame2, text = "U (largest job size)")
self.l1.grid(row = 0, column = 0)
self.l2.grid(row = 1, column = 0)
self.l3.grid(row = 2, column = 0)
self.e1 = Entry(frame2, textvariable = self.alpha)
self.e2 = Entry(frame2, textvariable = self.L)
self.e3 = Entry(frame2, textvariable = self.U)
self.e1.grid(row = 0, column = 1)
self.e2.grid(row = 1, column = 1)
self.e3.grid(row = 2, column = 1)
frame3 = Frame(top)
frame3.pack(side=TOP, pady=10)
self.b=Button(frame3,text='Ok',command=self.cleanup)
self.b.pack()
def cleanup(self):
if(self.checkParams() == 0):
self.paramArray=BoundedParetoDist.Array
self.top.destroy()
def checkParams(self):
self.a = float(self.e1.get())
self.l = float(self.e2.get())
self.u = float(self.e3.get())
if (self.a <= 0) or (self.u < self.l) or (self.l <= 0):
print ("ERROR: Bounded pareto paramater error")
self.errorMessage.set("Bounded pareto paramater error")
return 1
else:
self.errorMessage.set("")
BoundedParetoDist.Array = [self.a, self.l, self.u]
return 0
#----------------------------------------------------------------------#
# Class: Node
#
# This class is used to define the linked list nodes.
#
#----------------------------------------------------------------------#
class Node():
def __init__(self, job, nextNode = None):
self.job = job
self.nextNode = nextNode
#----------------------------------------------------------------------#
# Class: LinkedList
#
# This class is used to make the linked list data structure used to
# store jobs.
#
#----------------------------------------------------------------------#
class LinkedList(object):
Size = 0
def __init__(self, head = None):
self.head = head
# Insert job into queue (sorted by ERPT)
def insert(self, job):
current = self.head # node iterator, starts at head
previous = None
if (current == None): # if queue is empty, set current job as head
self.head = Node(job, None)
else:
while (current != None) and (job.ERPT > current.job.ERPT):
previous = current # prev = node[i]
current = current.nextNode # current = node[i+1]
# Insert new node after previous before current
if (previous == None):
self.head = Node(job, current)
else:
previous.nextNode = Node(job, current)
LinkedList.Size += 1
# Remove first item in queue
def removeHead(self):
if (LinkedList.Size > 0):
self.head = self.head.nextNode # move head forward one node
LinkedList.Size -= 1
else:
GUI.writeToConsole(self.master, "ERROR: The linked list is already empty!")
def clear(self):
self.head = None
def printList(self):
current = self.head
while (current != None):
print (current.job.name, current.job.ERPT)
current = current.nextNode
#----------------------------------------------------------------------#
# Class: JobClass
#
# This class is used to define jobs.
#
# Attributes: arrival time, processing time, remaining processing
# time, estimated remaining processing time, percent error
#----------------------------------------------------------------------#
class JobClass(object):
BPArray = []
def __init__(self, master):
self.master = master
self.arrivalTime = 0
self.procTime = 0
self.RPT = 0 # Real Remaining Processing Time
self.ERPT = 0 # Estimated Remaining Processing Time
self.percentError = 0
self.processRate = 0
self.arrivalRate = 0
#JobClass.BPArray = []
def setArrProcRates(self, load, procRate, procDist):
if procDist == 'Bounded Pareto':
alpha = JobClass.BPArray[0]
L = JobClass.BPArray[1]
U = JobClass.BPArray[2]
if alpha > 1 and L > 0:
procMean = (L**alpha/(1 - (L/U)**alpha))*(alpha/(alpha - 1))*((1/(L**(alpha - 1)))-(1/(U**(alpha - 1))))
self.processRate = 1/float(procMean)
else:
self.processRate = procRate
self.arrivalRate = float(load) * self.processRate
# Dictionary of service distributions
def setServiceDist(self, procRate, procDist):
ServiceDistributions = {
'Poisson': random.expovariate(1.0/procRate),
'Exponential': random.expovariate(procRate),
'Uniform': random.uniform(0.0, procRate),
'Bounded Pareto': self.setBoundedPareto,
'Custom': self.setCustomDist
}
if(procDist == 'Custom'):
return ServiceDistributions[procDist](procRate)
elif(procDist == 'Bounded Pareto'):
return ServiceDistributions[procDist]()
else:
return ServiceDistributions[procDist]
def setCustomDist(self, procRate):
if main.timesClicked == 0:
main.timesClicked += 1
self.popup = CustomDist(self.master)
self.master.wait_window(self.popup.top)
main.customEquation = self.popup.stringEquation
return eval(main.customEquation)
def setBoundedPareto(self):
# Get and set parameters (in job class array)
if main.timesClicked == 0:
main.timesClicked += 1
self.popup = BoundedParetoDist(self.master)
self.master.wait_window(self.popup.top)
self.alpha = float(self.popup.paramArray[0]) # Shape, power of tail, alpha = 2 is approx Expon., alpha = 1 gives higher variance
self.L = float(self.popup.paramArray[1]) # Smallest job size
self.U = float(self.popup.paramArray[2]) # Largest job size
JobClass.BPArray = [self.alpha, self.L, self.U]
x = random.uniform(0.0, 1.0)
# reassigning
alpha = JobClass.BPArray[0]
L = JobClass.BPArray[1]
U = JobClass.BPArray[2]
paretoNumerator = float(-(x*(U**alpha) - x*(L**alpha) - (U**alpha)))
paretoDenominator = float((U**alpha) * (L**alpha))
main.customEquation = (paretoNumerator/paretoDenominator)**(-1/alpha)
return main.customEquation
# Generates a percent error for processing time
def generateError(self, percErrorMin, percErrorMax):
self.percentError = random.uniform(percErrorMin, percErrorMax)
return self.percentError
# Sets all processing times for job
def setJobAttributes(self, load, procRate, procDist, percErrorMin, percErrorMax, jobArrival):
if(procDist == 'Bounded Pareto'):
self.procTime = self.setServiceDist(procRate, procDist) #use updated proc rate
self.setArrProcRates(load, procRate, procDist)
else:
self.setArrProcRates(load, procRate, procDist)
self.procTime = self.setServiceDist(procRate, procDist) #use updated proc rate
self.estimatedProcTime = (1 + (self.generateError(percErrorMin, percErrorMax)/100.0))*self.procTime
self.RPT = self.procTime
self.ERPT = self.estimatedProcTime
self.arrivalTime = jobArrival
#----------------------------------------------------------------------#
# Class: MachineClass
#
# This class is used to generate Jobs at random and process them.
#
# Entities: jobs, server
# Events: job arrives, job completes
# Activities: processing job, waiting for new job
#
#----------------------------------------------------------------------#
class MachineClass(object):
Queue = LinkedList()
JobOrderOut = []
CurrentTime = 0.0
TimeUntilArrival = 0.0
ServiceStartTime = 0
AvgNumJobs = 0
PrevTime = 0
PrevNumJobs = 0
ServerBusy = False
StopSim = False
def __init__(self, master):
self.master = master
MachineClass.ServerBusy = False
MachineClass.StopSim = False
MachineClass.Queue.clear()
LinkedList.Size = 0
MachineClass.CurrentTime = 0.0
MachineClass.TimeUntilArrival = 0.0
MachineClass.ServiceStartTime = 0
MachineClass.AvgNumJobs = 0
MachineClass.PrevTime = 0
MachineClass.PrevNumJobs = 0
NumJobs[:] = []
AvgNumJobs[:] = []
NumJobsTime[:] = []
TimeSys[:] = []
ProcTime[:] = []
PercError[:] = []
MachineClass.JobOrderOut[:] = []
self.ctr = 0
# Dictionary of arrival distributions
def setArrivalDist(self, arrRate, arrDist):
ArrivalDistributions = {
'Poisson': random.expovariate(1.0/arrRate),
'Exponential': random.expovariate(arrRate)
#'Normal': Rnd.normalvariate(self.inputInstance.valuesList[0])
#'Custom':
}
return ArrivalDistributions[arrDist]
def getProcessingJob(self):
currentJob = MachineClass.Queue.head.job
return currentJob
#update data
def updateJob(self):
currentJob = self.getProcessingJob()
serviceTime = MachineClass.CurrentTime - MachineClass.ServiceStartTime
currentJob.RPT -= serviceTime
currentJob.ERPT -= serviceTime
def calcNumJobs(self, jobID, load):
self.currentNumJobs = MachineClass.Queue.Size #NOTE: This includes job in service
self.t = MachineClass.CurrentTime
self.delta_t = self.t - MachineClass.PrevTime
# If one job in system
if(jobID == 0):
MachineClass.AvgNumJobs = 1 # First event is always create new job
# UPDATE
else:
MachineClass.AvgNumJobs = (MachineClass.PrevTime/(self.t))*float(MachineClass.AvgNumJobs) + float(MachineClass.PrevNumJobs)*(float(self.delta_t)/self.t)
# PrevTime becomes "old" t
MachineClass.PrevTime = self.t
# PrevNum jobs becomes current num jobs
MachineClass.PrevNumJobs = self.currentNumJobs
NumJobs.append(self.currentNumJobs) # y axis of plot
AvgNumJobs.append(MachineClass.AvgNumJobs) # y axis of plot
NumJobsTime.append(MachineClass.CurrentTime) # x axis of plot
self.saveNumJobs(load, MachineClass.CurrentTime, self.currentNumJobs)
self.saveAvgNumJobs(load, MachineClass.CurrentTime, MachineClass.AvgNumJobs)
def saveNumJobs(self, load, numJobs, time):
text = "%f,%f"%(numJobs, time) + "\n"
scaledLoad = int(load * 100)
path = "./SINGLE_SERVER_RESULTS/Catastrophic/SRPT_Num_load=%s_alpha=%s_servers=1_catastrophic.txt"%(scaledLoad, JobClass.BPArray[0])
with open(path, "a") as myFile:
myFile.write(text)
myFile.close()
def saveAvgNumJobs(self, load, avgNumJobs, time):
text = "%f,%f"%(avgNumJobs, time) + "\n"
scaledLoad = int(load * 100)
path = "./SINGLE_SERVER_RESULTS/Catastrophic/SRPT_Avg_load=%s_alpha=%s_servers=1_catastrophic.txt"%(scaledLoad, JobClass.BPArray[0])
with open(path, "a") as myFile:
myFile.write(text)
myFile.close()
# Job arriving
def arrivalEvent(self, load, arrDist, procRate, procDist, percErrorMin, percErrorMax):
J = JobClass(self.master)
J.setJobAttributes(load, procRate, procDist, percErrorMin, percErrorMax, MachineClass.CurrentTime)
J.name = "Job%02d"%self.ctr
GUI.writeToConsole(self.master, "%.6f | %s arrived, ERPT = %.5f"%(MachineClass.CurrentTime, J.name, J.ERPT))
self.calcNumJobs(self.ctr, load)
#self.saveArrivals(J) # save to list of arrivals, for testing
if(MachineClass.Queue.Size > 0):
self.updateJob() # update data in queue
MachineClass.Queue.insert(J) # add job to queue
self.processJob() # process first job in queue
# Generate next arrival
MachineClass.TimeUntilArrival = self.setArrivalDist(J.arrivalRate, arrDist)
self.ctr += 1
def insertLargeJob(self, counter, procDist):
J = JobClass(self.master)
J.setJobAttributes(1, 1, procDist, 0, 0, MachineClass.CurrentTime)
J.name = "JobXXXXX" + str(counter)
J.RPT = 100000
J.ERPT = 50000
GUI.writeToConsole(self.master, "%.6f | %s arrived, ERPT = %.5f"%(MachineClass.CurrentTime, J.name, J.ERPT))
self.calcNumJobs(self.ctr)
#self.saveArrivals(J) # save to list of arrivals, for testing
if(MachineClass.Queue.Size > 0):
self.updateJob() # update data in queue
MachineClass.Queue.insert(J) # add job to queue
self.processJob() # process first job in queue
# Generate next arrival
MachineClass.TimeUntilArrival = self.setArrivalDist(J.arrivalRate, 'Exponential')
#def saveArrivals(self, job):
# text = "%s, %.4f, %.4f, %.4f"%(job.name, job.arrivalTime, job.RPT, job.ERPT) + "\n"
#
# with open("Arrivals.txt", "a") as myFile:
# myFile.write(text)
# myFile.close()
# Processing first job in queue
def processJob(self):
MachineClass.ServiceStartTime = MachineClass.CurrentTime
currentJob = self.getProcessingJob()
GUI.writeToConsole(self.master, "%.6f | %s processing, ERPT = %.5f"%(MachineClass.CurrentTime, currentJob.name, currentJob.ERPT))
MachineClass.ServerBusy = True
# Job completed
def completionEvent(self, load):
currentJob = self.getProcessingJob()
MachineClass.ServerBusy = False
MachineClass.JobOrderOut.append(currentJob.name)
self.calcNumJobs(self.ctr, load)
# NumJobs.append(MachineClass.AvgNumJobs) # y axis of plot
# NumJobsTime.append(MachineClass.CurrentTime) # x axis of plot
TimeSys.append(MachineClass.CurrentTime - currentJob.arrivalTime)
ProcTime.append(currentJob.procTime)
PercError.append(abs(currentJob.percentError))
GUI.writeToConsole(self.master, "%.6f | %s COMPLTED"%(MachineClass.CurrentTime, currentJob.name))
MachineClass.Queue.removeHead() # remove job from queue
def run(self, load, arrDist, procRate, procDist, percErrorMin, percErrorMax, simLength):
counter = 1;
while 1:
if(self.ctr == 0): # set time of first job arrival
arrRate = float(load) / procRate
MachineClass.TimeUntilArrival = self.setArrivalDist(arrRate, arrDist) # generate next arrival
#Inject large jobs
if(MachineClass.CurrentTime >= 2000000.0 and counter == 1):
self.insertLargeJob(counter, procDist);
counter += 1;
print ("FIRST LARGE JOB INJECTED");
elif(MachineClass.CurrentTime >= 2000500.0 and counter == 2):
self.insertLargeJob(counter, procDist);