-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscheduling.py
More file actions
executable file
·75 lines (58 loc) · 1.52 KB
/
scheduling.py
File metadata and controls
executable file
·75 lines (58 loc) · 1.52 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/python
#-*- coding:utf-8 -*-
from heapq import *
__metaclass__ = type
class JobNode:
def __init__(self):
self.ID = None
self.time = None
def __lt__(self, other):
return self.time < other.time
def __gt__(self, other):
return self.time > other.time
def __le__(self, other):
return self.time <= other.time
def __ge__(self, other):
return self.time >= other.time
def __eq__(self, other):
return self.time == other.time
__metaclass__ = type
class MachineNode:
def __init__(self):
self.ID = None
self.avail = None
def __lt__(self, other):
return self.avail < other.avail
def __gt__(self, other):
return self.avail > other.avail
def __le__(self, other):
return self.avail <= other.avail
def __ge__(self, other):
return self.avail >= other.avail
def __eq__(self, other):
return self.avail == other.avail
def Greedy(a, n, m):
if n <= m:
print "为每个作业分配一台机器。"
return
jobnode = a.pop(0)
a.sort()
a.insert(0, jobnode)
MachHeap = []
heapify(MachHeap)
for i in range(1, m+1):
x = MachineNode()
x.avail = 0
x.ID = i
heappush(MachHeap, x)
for i in range(n, 0, -1):
x = heappop(MachHeap)
print "将机器", x.ID, "从", x.avail, "到", (x.avail + a[i].time), "的时间段分配给作业", a[i].ID
x.avail += a[i].time
heappush(MachHeap, x)
if __name__ == "__main__":
a = [JobNode() for i in range(6)]
for i in range(1, len(a)):
a[i].ID = i
a[1].time, a[2].time, a[3].time, a[4].time, a[5].time = 300, 120, 55, 70, 90
Greedy(a, 5, 3)