job = [[0,1,2],[3,2,5],[2,5,4]] machines = [[2,3,4],[8,5,6],[5,6,3]],def calculate_fitness(a): end_time = [0] * len(job) for i in a: machines_time = [0] * len(machines[i]) for j in range(len(job[i])): m_id = job[i][j] start_time = max(machines_time[m_id], end_time[i]) end_time[i] = start_time + machines[m_id][i] makespan = max(end_time) return 1 / makespan,就索引方面进行改错
时间: 2023-11-30 08:23:36 浏览: 59
Job-shop scheduling with multi-purpose machines.pdf
在这段代码中,变量`i`在`machines`列表中应该是索引而不是元素,因此需要将`machines[i]`改为`machines[a[i]]`。
下面是修改后的代码:
```python
def calculate_fitness(a):
end_time = [0] * len(job)
for i in range(len(a)):
machines_time = [0] * len(machines[a[i]])
for j in range(len(job[i])):
m_id = job[i][j]
start_time = max(machines_time[m_id], end_time[a[i]])
end_time[a[i]] = start_time + machines[m_id][a[i]]
makespan = max(end_time)
return 1.0 / makespan
```
在修改后的代码中,`i`被用作循环计数器,而`a[i]`表示第`i`个任务在哪台机器上进行加工,因此需要将`machines[i]`改为`machines[a[i]]`。
阅读全文