-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtasks_scheduler.py
More file actions
58 lines (44 loc) · 1.84 KB
/
tasks_scheduler.py
File metadata and controls
58 lines (44 loc) · 1.84 KB
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
"""Tasks scheduler. The goal is to minimize the weighted sum of completion times.
We can achieve this by ordering tasks by their ratios (weight / length)."""
import operator
# [job_weight, job_length]
jobs = [[3, 6], [54, 2], [1, 500], [1000, 1], [3, 555], [27, 1111], [2, 1000]]
def schedule(tasks, alg=None):
"""Takes list of tasks (weight, length). Algorithm 'diff' or 'ratio'
can be passed to determine the way of task comparison"""
# Defines operator dynamically. Subtraction when algorithm
# is based on weight and length difference, otherwise division is used
# what reflects scheduling by ratio (optimal).
operation = operator.sub if alg == 'diff' else operator.truediv
# schedules jobs in decreasing order of the difference (weight - length)
# or ratio (weight / length)
tasks = sorted(tasks, key=lambda x: operation(x[0], x[1]), reverse=True)
# handle ties so that bigger weights go first.
difference = operation(tasks[0][0], tasks[0][1])
temp = []
for idx, i in enumerate(tasks):
diff = operation(i[0], i[1])
if diff == difference:
temp.append(i)
else:
difference = diff
if len(temp) > 1:
temp.sort(reverse=True)
tasks[idx - len(temp):idx] = temp
temp = [i]
if len(temp) > 1:
temp.sort(reverse=True)
tasks[len(tasks) - len(temp):len(tasks)] = temp
return tasks
def weighted_completion_time(scheduled):
comp_time = 0
weighted_comp_time = 0
for i in scheduled:
comp_time += i[1]
weighted_comp_time += i[0] * comp_time
return weighted_comp_time
if __name__ == "__main__":
ordered = schedule(jobs)
print(ordered)
print("Before scheduling:", weighted_completion_time(jobs))
print("After scheduling:", weighted_completion_time(ordered))