2017-02-11 340 views
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() 
+0

此代碼有多處錯誤。如果你使用7.0版本的'Model.addVars()'和'Model.addConstrs()'語法重寫它,將會容易得多。 –

回答

0

您有上quicksum的範圍內的問題([X [J,T]對於t在範圍(T-procTime [j]的+1)])。因爲對於x [j,t],j在[0,3]中,t也在[0,3]或T = 124中,則T-procTime [j] +1在[124-0 + 1,124-3 + 1]。