多机调度问题——python实现
- 问题描述:
有N个独立作业,每个作业处理时间为time[i],有M个相同的机器加工处理,约定每个作业可以在任何一台机器上加工处理,未完工前不允许中断处理,作业不能拆分成更小的子作业。要求在最短时间内完成,求最短时
- 解决方案
最理想的方法是平均分配,每台机器处理的时间相同,最后同时处理完任务。实际情况中不一定能完全分配,我们应尽量缩小各个机器处理时间的差距,用贪 心算法可以比较好的解决:先将作业处理时间降序排列,依次选择时间往机器上安排,每次安排在当前工作量总时间最小的机器上,最后求得时间差距最小
- 代码实现
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
def main():
Machine = 4
time = []
for i in range(100):
time.append(random.randint(1,100))
time.sort()
time.reverse()
print time
total = [0,0,0,0]
for i in time:
min_time = total[0]
k = 0
for j in range(1,4):
if min_time > total[j]:
k = j
min_time = total[j]
total[k] += i
print total
return 0
if __name__ == '__main__':
main()
# -*- coding: utf-8 -*-
import random
def main():
Machine = 4
time = []
for i in range(100):
time.append(random.randint(1,100))
time.sort()
time.reverse()
print time
total = [0,0,0,0]
for i in time:
min_time = total[0]
k = 0
for j in range(1,4):
if min_time > total[j]:
k = j
min_time = total[j]
total[k] += i
print total
return 0
if __name__ == '__main__':
main()
- 运行结果
[100, 99, 97, 95, 95, 95, 94, 94, 92, 91, 91, 90, 89, 89, 89, 89, 88, 88, 87, 87, 86, 85, 84, 84, 83, 82, 82, 82, 82, 81, 79, 79, 78, 77, 76, 76, 75, 73, 72, 72, 68, 67, 61, 60, 60, 58, 58, 56, 54, 53, 53, 52, 51, 50, 50, 49, 49, 47, 47, 47, 46, 45, 44, 44, 43, 43, 43, 43, 42, 41, 41, 40, 38, 38, 36, 36, 34, 32, 31, 29, 28, 27, 27, 25, 25, 23, 22, 22, 19, 18, 18, 15, 14, 14, 12, 10, 8, 5, 4, 2]
[1410, 1411, 1411, 1412]
博主ma6174对本博客文章(除转载的)享有版权,未经许可不得用于商业用途。转载请注明出处http://www.cnblogs.com/ma6174/
对文章有啥看法或建议,可以评论或发电子邮件到ma6174@163.com