0
I want to implement the following integer programming formulations for the machine scheduling problem but keeps getting the keyerror(0,9) in line 22Python-使用Python在Gurobi中實現TI的實現。 KeyError異常
procTime = [40,28,18,38]
dueDate = [38,23,17,25]
nJobs = len(procTime)
T = sum(procTime)
x = {}
for j in range(nJobs):
for t in range(nJobs):
x[j,t] = m.addVar(obj=1,vtype=GRB.BINARY,name='x%d%d' % (j,t))
c = {}
for j in range(nJobs):
c[j,t] = m.addVar(obj=1,vtype=GRB.INTEGER,name='c%d%d' % (j,t))
m.update()
for j in range(nJobs):
m.addConstr(quicksum([x[j,t] for t in range(T-procTime[j]+1)]) == 1, 'seq%d' % (j))
for t in range(T+1):
m.addConstr(quicksum([quicksum([x[j,s] for j in range(nJobs) for s in range(max(t-procTime[j]+1),min(T-procTime[j]+2))])]) <= 1, 'time%d' % t)
for j in range(nJobs):
for t in range(T+1):
m.addConstr(t+procTime[j]-1-dueDate[j] <= c[j,t], 'c%d' % (j,t))
m.optimize()
此代碼有多處錯誤。如果你使用7.0版本的'Model.addVars()'和'Model.addConstrs()'語法重寫它,將會容易得多。 –