工作 | 成本函数 | 工作时间 |
---|---|---|
1 | 1+c | 2 |
2 | 1.2c | 3 |
3 | 10 | 5 |
有以上三个工作,c表示当前工作的完成时间,工作不可同时进行,不可中断
目标:使具有最大成本的工作的值最小化
算法
计算出所有工作完成的时间,对每个工作,计算出完成时间时的成本,将成本最小的工作安排在调度表的最后(后向调度)
删除调度过的工作,然后重复此过程,直到所有工作被安排。
python代码实现如下:
def min_biggest_cost(tasks):
schedule = list()
biggest_cost = 0
while tasks:
scope = dict()
scope['time'] = 0
for task in tasks:
scope['time'] += task['time']
tasks = sorted(tasks, key=lambda x: eval(x['cost'], scope))
schedule.insert(0, tasks[0])
cost = eval(tasks[0]['cost'], scope)
if cost > biggest_cost:
biggest_cost = cost
del tasks[0]
return schedule, biggest_cost
def main():
tasks = [dict(cost='1 + time', time=2, index=1),
dict(cost='1.2 * time', time=3, index=2),
dict(cost='10', time=5, index=3)]
schedule, cost = min_biggest_cost(tasks)
print(cost)
for task in schedule:
print(task)
if __name__ == '__main__':
main()
输出:
10
{'cost': '1.2 * time', 'time': 3, 'index': 2}
{'cost': '1 + time', 'time': 2, 'index': 1}
{'cost': '10', 'time': 5, 'index': 3}