Python-使用Python在Gurobi中实现TI实现。 KeyError异常

时间:2017-02-11 00:55:28

标签: python gurobi

I want to implement the following integer programming formulations for the machine scheduling problem but keeps getting the keyerror(0,9) in line 22

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()

1 个答案:

答案 0 :(得分: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]。