-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
458 lines (421 loc) · 17.6 KB
/
main.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
from operator import attrgetter
class Food:
def __init__(self, name, cook_time, deadline, period):
self.name = name
self.cookTime = int(cook_time) # original data that user input
self.deadline = int(deadline) # original data that user input
self.period = int(period) # original data that user input
self.tempDeadline = int(deadline) # change this without original data changed
self.tempCook = int(cook_time) # change this without original data changed
self.missFood = int(deadline) # for check deadline is missed or not
self.waitingTime = 0 # use this for calculate waiting time
self.lastEnter = 0 # use this (and tempCook) for calculate change between foods
self.priority = 0 # set food priority
self.predict = False # for predict deadline ---> if true means we predict for this food
def __repr__(self):
return f"{self.name}"
# initializer (for each scheduling function we should use this)
def set_value(self):
"""
set initial value like __init__
use this function at start of each scheduling algorithm.
:return: nothing return ---> only set value to class attributes
"""
self.predict = False
self.priority = 0
self.lastEnter = 0
self.waitingTime = 0
self.missFood = int(self.deadline)
self.tempCook = int(self.cookTime)
self.tempDeadline = int(self.deadline)
def set_priority_for_llf(self):
"""
set priority for least laxity first
:return: nothing return ---> set priority of each food
"""
self.priority = self.tempDeadline - self.tempCook
def set_priority_for_edf(self):
"""
set priority for earliest deadline first
:return: nothing return ---> set priority of each food
"""
self.priority = self.tempDeadline
def set_priority_for_rate_monotonic(self):
"""
set priority for rate monotonic
:return: nothing return ---> set priority of each food
"""
self.priority = self.period
def lcm(x, y):
"""
calculate the LCM between 2 number
:param x: first number
:param y: second number
:return: the LCM between x and y
"""
if x > y:
greater = x
else:
greater = y
while (True):
if (greater % x == 0) and (greater % y == 0):
lcm = greater
break
greater += 1
return lcm
def calculate_chef_time(list_of_food):
"""
calculate the LCM between foods
:param list_of_food: list of foods
:return: LCM between foods period
"""
time = 1
for food in list_of_food:
time = lcm(food.period, time)
return time
def check_do_order(list_of_food, chef_time):
"""
check that chef can do this order or not
:param list_of_food: list of foods
:param chef_time: the time that chef spend in kitchen(use calculate chef time function to calculate this param)
:return: if chef can do this job return true else return false
"""
result = 0
for food in list_of_food:
count = chef_time / food.period
result += food.cookTime * count
if result <= chef_time:
return True
else:
return False
def predict_miss_deadline(list_temp_foods, round):
"""
this function predict the food that miss the deadline.
first we calculate how many food have better priority than the food we choose.
then we use that number and tempDeadline for that food and compare it with tempCook.
with this compare we can understand that our food will miss the deadline or not.
:param list_temp_foods: list of foods that are in come and wait for cooked.
:param round: the round that we try to predict for find the miss food.
:return: nothing return ---> print in terminal.
"""
for food in list_temp_foods:
food_with_better_priority = 0
for otherfood in list_temp_foods:
if otherfood == food:
continue
elif otherfood.priority < food.priority:
food_with_better_priority += 1
else:
continue
if food.tempDeadline - food_with_better_priority < food.tempCook and food.predict is False:
print(f"{round} {food.name} will miss the deadline.")
food.predict = True
def least_laxity_first(list_of_foods, chef_time):
"""
least laxity first scheduling algorithm.
this algorithm priority is tempDeadline - tempCook that use set_priority_for_llf function in Food class.
in first check if list is empty the chef is idle and increase round and check new foods must come in new round.
next get food with minimum priority and decrease tempCook and if tempCook is 0 that means the food is done so
remove the food from list.
each round decrease tempDeadline and miss food for find use them in predict miss food and find miss food.
save food in last_food to find changes between foods if last_food is changed and tempCook is not equal to original
cook time.
at the end calculate the new foods that must come in new round.
:param list_of_foods: list of food that chef must cook.
:param chef_time: maximum time that chef in kitchen.
:return: nothing return ---> print in terminal
"""
print(60 * "*")
print("Least laxity first")
# set value
for food in list_of_foods:
food.set_value()
i = 0
idle_time = 0
change_between_foods = 0
temp_list = list_of_foods.copy()
last_food = None
while i < chef_time:
# check if list is empty print idle and add new foods
if len(temp_list) == 0:
print(f"{i} Idle.")
idle_time += 1
i += 1
for food in list_of_foods:
if i % food.period == 0:
# print(f"{i} {food.name} come.")
temp_list.append(food)
continue
for food in list_of_foods:
food.set_priority_for_llf()
# find food with minimum priority
min_food = min(temp_list, key=attrgetter('priority'))
# calculate change food
if i == 0:
pass
elif min_food != last_food and last_food.tempCook != last_food.cookTime:
print(f"Chef change food in round {i}")
change_between_foods += 1
# cook that food with minimum priority
print(f"{i} {min_food.name}.")
min_food.tempCook -= 1
# remove food if it's done
if min_food.tempCook == 0:
min_food.predict = False
min_food.priority = 0
min_food.tempDeadline = min_food.deadline
min_food.tempCook = min_food.cookTime
min_food.missFood = min_food.deadline
min_food.waitingTime += ((i + 1) - min_food.lastEnter) - min_food.cookTime
temp_list.remove(min_food)
for food in temp_list:
# use this for predict miss deadline food
food.tempDeadline -= 1
food.missFood -= 1
if food.missFood == 0:
print(f"{i} {food.name} miss the deadline.")
# set min_food as last_food that we use in calculate change between foods
last_food = min_food
# predict miss foods
predict_miss_deadline(temp_list, i)
i += 1
# add food that must come every period
for food in list_of_foods:
if i % food.period == 0:
# print(f"{i} {food.name} come.")
food.lastEnter = i
# print(f"{food.name} enter {food.lastEnter}")
temp_list.append(food)
print(f"idle time = {idle_time}.")
for food in list_of_foods:
print(f"{food.name} waiting time = {food.waitingTime}.")
print(f"change between foods is {change_between_foods}.")
def rate_monotonic(list_of_foods, chef_time):
"""
rate monotonic scheduling algorithm.
this algorithm priority is minimum period between foods.
in first check if list is empty the chef is idle and increase round and check new foods must come in new round.
next get food with minimum period and decrease tempCook and if tempCook is 0 that means the food is done so
remove the food from list.
each round decrease tempDeadline and miss food for find use them in predict miss food and find miss food.
save food in last_food to find changes between foods if last_food is changed and tempCook is not equal to original
cook time.
at the end calculate the new foods that must come in new round.
:param list_of_foods: list of food that chef must cook.
:param chef_time: maximum time that chef in kitchen.
:return: nothing return ---> print in terminal
"""
print(60 * "*")
print("Rate monotonic")
# set value
for food in list_of_foods:
food.set_value()
i = 0
idle_time = 0
change_between_foods = 0
temp_list = list_of_foods.copy()
last_food = None
while i < chef_time:
# check if list is empty print idle and add new foods
if len(temp_list) == 0:
print(f"{i} Idle.")
idle_time += 1
i += 1
for food in list_of_foods:
if i % food.period == 0:
# print(f"{i} {food.name} come.")
temp_list.append(food)
continue
for food in list_of_foods:
food.set_priority_for_rate_monotonic()
# find minimum period between foods
min_food = min(temp_list, key=attrgetter('period'))
# calculate change food
if i == 0:
pass
elif min_food != last_food and last_food.tempCook != last_food.cookTime:
print(f"Chef change food in round {i}")
change_between_foods += 1
# cook that food with minimum period
print(f"{i} {min_food.name}.")
min_food.tempCook -= 1
# remove food if it's done
if min_food.tempCook == 0:
min_food.predict = False
min_food.priority = 0
min_food.tempDeadline = min_food.deadline
min_food.tempCook = min_food.cookTime
min_food.missFood = min_food.deadline
min_food.waitingTime += ((i + 1) - min_food.lastEnter) - min_food.cookTime
temp_list.remove(min_food)
for food in temp_list:
# use this for predict miss deadline food
food.tempDeadline -= 1
food.missFood -= 1
if food.missFood == 0:
print(f"{i} {food.name} miss the deadline.")
# set min_food as last_food that we use in calculate change between foods
last_food = min_food
# predict miss foods
predict_miss_deadline(temp_list, i)
i += 1
# add food that must come every period
for food in list_of_foods:
if i % food.period == 0:
# print(f"{i} {food.name} come.")
food.lastEnter = i
# print(f"{food.name} enter {food.lastEnter}")
temp_list.append(food)
print(f"idle time = {idle_time}.")
for food in list_of_foods:
print(f"{food.name} waiting time = {food.waitingTime}.")
print(f"change between foods is {change_between_foods}.")
def earliest_deadline_first(list_of_foods, chef_time):
"""
earliest deadline first scheduling algorithm.
this algorithm priority is minimum deadline between foods.
in first check if list is empty the chef is idle and increase round and check new foods must come in new round.
next get food with minimum priority and decrease tempCook and if tempCook is 0 that means the food is done so
remove the food from list.
each round decrease tempDeadline and miss food for find use them in predict miss food and find miss food.
save food in last_food to find changes between foods if last_food is changed and tempCook is not equal to original
cook time.
at the end calculate the new foods that must come in new round.
:param list_of_foods: list of food that chef must cook.
:param chef_time: maximum time that chef in kitchen.
:return: nothing return ---> print in terminal
"""
print(60 * "*")
print("Earliest deadline first")
# set value
for food in list_of_foods:
food.set_value()
i = 0
idle_time = 0
change_between_foods = 0
temp_list = list_of_foods.copy()
last_food = None
while i < chef_time:
# check if list is empty print idle and add new foods
if len(temp_list) == 0:
print(f"{i} Idle.")
idle_time += 1
i += 1
for food in list_of_foods:
if i % food.period == 0:
# print(f"{i} {food.name} come.")
temp_list.append(food)
continue
for food in list_of_foods:
food.set_priority_for_edf()
# find minimum deadline between foods
min_food = min(temp_list, key=attrgetter('tempDeadline'))
# calculate change food
if i == 0:
pass
elif min_food != last_food and last_food.tempCook != last_food.cookTime:
print(f"Chef change food in round {i}")
change_between_foods += 1
# cook that food with minimum deadline
print(f"{i} {min_food.name}.")
min_food.tempCook -= 1
# remove food if it's done
if min_food.tempCook == 0:
min_food.predict = False
min_food.priority = 0
min_food.tempDeadline = min_food.deadline
min_food.tempCook = min_food.cookTime
min_food.missFood = min_food.deadline
min_food.waitingTime += ((i + 1) - min_food.lastEnter) - min_food.cookTime
temp_list.remove(min_food)
# like ageing increase priority each round with -1 deadline
for food in temp_list:
# if want to remove ageing ---> only need to comment below line
food.tempDeadline -= 1
food.missFood -= 1
if food.missFood == 0:
print(f"{i} {food.name} miss the deadline.")
# set min_food as last_food that we use in calculate change between foods
last_food = min_food
# predict miss foods
predict_miss_deadline(temp_list, i)
i += 1
# add food that must come every period
for food in list_of_foods:
if i % food.period == 0:
# print(f"{i} {food.name} come.")
food.lastEnter = i
# print(f"{food.name} enter {food.lastEnter}")
temp_list.append(food)
print(f"idle time = {idle_time}.")
for food in list_of_foods:
print(f"{food.name} waiting time = {food.waitingTime}.")
print(f"change between foods is {change_between_foods}.")
def get_user_input(list_of_food):
n = int(input("Enter number of foods:"))
for num in range(n):
user_input = input(f"{num + 1} Enter food name, cook time, deadline and period in order: ")
user_input.replace(",", "")
user_input = user_input.split(" ")
user_food = Food(user_input[0], user_input[1], user_input[2], user_input[3])
list_of_food.append(user_food)
print(list_of_food)
# ----------------------------------------------------------------------------------------------------------------------
# food1 = Food("Food1", 2, 2, 8)
# food2 = Food("Food2", 1, 3, 4)
# food3 = Food("Food3", 3, 6, 8)
#
# # list for store foods
# arr_of_foods = [food1, food2, food3]
# ----------------------------------------------------------------------------------------------------------------------
# # other input
# food1 = Food("Food1", 1, 4, 8)
# food2 = Food("Food2", 2, 2, 5)
# food3 = Food("Food3", 4, 5, 10)
# food4 = Food("Food4", 2, 3, 40)
# # list for store foods
# arr_of_foods = [food1, food2, food3, food4]
# ----------------------------------------------------------------------------------------------------------------------
# food1 = Food("Food1", 3, 2, 20)
# food2 = Food("Food2", 2, 3, 5)
# food3 = Food("Food3", 2, 6, 10)
#
# # list for store foods
# arr_of_foods = [food1, food2, food3]
# ----------------------------------------------------------------------------------------------------------------------
# food1 = Food("Food1", 3, 7, 20)
# food2 = Food("Food2", 2, 4, 5)
# food3 = Food("Food3", 2, 8, 10)
#
# # list for store foods
# arr_of_foods = [food1, food2, food3]
# ----------------------------------------------------------------------------------------------------------------------
# food1 = Food("Food1", 3, 7, 20)
# food2 = Food("Food2", 2, 4, 5)
# food3 = Food("Food3", 2, 8, 10)
#
# # list for store foods
# arr_of_foods = [food1, food2, food3]
# ----------------------------------------------------------------------------------------------------------------------
# food1 = Food("Food1", 2, 6, 6)
# food2 = Food("Food2", 2, 8, 8)
# food3 = Food("Food3", 3, 10, 10)
#
# # list for store foods
# arr_of_foods = [food1, food2, food3]
# ----------------------------------------------------------------------------------------------------------------------
# get user input
arr_of_foods = []
get_user_input(arr_of_foods)
# chef time that spend in kitchen
chef_time_spend = calculate_chef_time(arr_of_foods)
for food in arr_of_foods:
print(f"{food.name} cook in {food.cookTime}, deadline each period is {food.deadline}, come every {food.period}")
if check_do_order(arr_of_foods, chef_time_spend) is False:
print("Can't handle orders!")
exit()
print(f"the chef must be in kitchen for {chef_time_spend} period")
print(f"chef can do this order? {check_do_order(arr_of_foods, chef_time_spend)}")
earliest_deadline_first(arr_of_foods, chef_time_spend)
rate_monotonic(arr_of_foods, chef_time_spend)
least_laxity_first(arr_of_foods, chef_time_spend)