在 gurobipy 'Var' 对象不可迭代

In gurobipy 'Var' object is not iterable

我想用 gurobipy 解决一些线性规划问题,但总是出现错误。我是 Python 的新手,所以这可能很明显,但我似乎找不到我的错误。这是我的代码 ``` 将 gurobipy 导入为 gp 从 gurobipy 导入 GRB

    # In[4]:
    
    
    import numpy as np
    import pandas as pd
    
    time_shifts = list(range(1,11))
    num_turbine=list(range(1,10))
    I = ['t1','t2']
    NP=['t1']
    NC = ['t2']
    
    
    
    Wmax = 7 
    Qmax = 3 
    Vmax = 4 
    PPi = {'t1':80}
    Ut = 10
    Cteam = 1200000 
    TR = 3 
    
    HDT = [439517,530261,490784,530325,629710,556702,562887,493247,449940,479517]
    DT = [element * 24 * 7 for element in HDT]
    
    EH = {1:HDT[0], 2:HDT[1], 3:HDT[2], 4:HDT[3], 5:HDT[4], 6:HDT[5], 7:HDT[6], 8:HDT[7],
         9:HDT[8], 10:HDT[9]}
    
          
    ED = {1:DT[0], 2:DT[1], 3:DT[2], 4:DT[3], 5:DT[4], 6:DT[5], 7:DT[6], 8:DT[7],
         9:DT[8], 10:DT[9]}
    
    Ni = {'t1': 5,'t2': 18}
    At = {1:6,2:5,3:5,4:2,5:3,6:7,7:4,8:6,9:6,10:7}
    Wt= {1:4,2:4,3:4,4:4,5:4,6:4,7:4,8:4,9:4,10:4}
    
    
    Cvt = {}
    for i in range(1,11):
        Cvt[i] = At[i]*300*1000*TR
    
    
    
    
    Git = {('t2',1):7,('t2',2):7,('t2',3):7,('t2',4):7,('t2',5):8,('t2',6):8,('t2',7):8,
           ('t2',8):9,('t2',9):8,('t2',10):7}
    
    SGit = {('t2',1):7,('t2',2):14,('t2',3):21,('t2',4):28,('t2',5):36,('t2',6):44,('t2',7):52,
           ('t2',8):61,('t2',9):69,('t2',10):76}
    
    
    
    
    
    
    m = gp.Model('Offshore')#Xit 
    
    xitb = m.addVars(NP, time_shifts,num_turbine,lb=0, name = "preventive",vtype=GRB.BINARY)
    
    #Yitb
    yitb = m.addVars(NC, time_shifts,num_turbine,lb=0, name = "corrective",vtype=GRB.BINARY)
    
    #Wit 
    witb = m.addVars(NC, time_shifts,num_turbine,lb=0, name = "requiring",vtype=GRB.BINARY)
    
    #Zt 
    zt = m.addVars(time_shifts,lb=0,ub = Qmax, name = "vessel",vtype=GRB.INTEGER)
    
    #ut 
    ut = m.addVars(time_shifts,lb=0,ub = Ut, name = "team",vtype=GRB.INTEGER)
    
    m.addConstrs(gp.quicksum(witb[i,t,b])<=SGit[i,t] for i in NC for t in time_shifts for b in num_turbine)
    
    m.addConstrs(gp.quicksum(xitb[i,t,b]**Ni[i])+gp.quicksum(yitb[j,t,b]**Ni[j])<=Wt[t]*ut[t]*Wmax for i in NP for t in time_shifts for b in num_turbine for j in NC)
    
    m.addConstrs((gp.quicksum(xitb[i,t,b]**Ni[i] for i in NC for t in time_shifts for b in num_turbine)+gp.quicksum(yitb[j,t,b]**Ni[j])+ut[t]*Wt[t]*TR)<=ut[t]*40  for t in time_shifts for j in NC for b in num_turbine)
    
    time_shift2 = list(range(2,53))
    
    m.addConstrs(gp.quicksum(witb[j,t,b])==gp.quicksum(witb[j,t-1,b] for j in NC for t in time_shift2 for b in num_turbine )-gp.quicksum(yitb[j,t,b] )+Git[j,t] for j in NC for t in time_shift2 for b in num_turbine )
    
    m.addConstrs( Git[i,1] - yitb[i,1]==witb[i,1]   for i in NC )
    
    m.addConstrs(Git[i,1]-gp.quicksum(yitb[i,1,b])==gp.quicksum(witb[i,1,b]) for b in num_turbine)
    
    m.addConstrs(gp.quicksum(yitb[j,t,b] for j in NC for t in time_shift2 for b in num_turbine)<=gp.quicksum(witb[j,t,b])+Git[j,t] for j in NC for t in time_shift2 for b in num_turbine )
    
    m.addConstrs(gp.quicksum(xitb[i,t,b])==PPi[i] for i in NP for t in time_shifts for b in num_turbine)
    
    m.addConstr(gp.quicksum(xitb[i,t,b] for i in NP for b in num_turbine  for t in list(range(10,41))) >= 100*(2/3) ) 
    
    m.addConstrs((ut[t]/Vmax) <= zt[t] for t in time_shifts) 
    
    obj = gp.quicksum(Cvt[t]*zt[t] +  Cteam*ut[t] +
        EH[t]*Ni[P]*xitb[P,t,b] + ED[t]*witb[C,t,b] for P in NP  for C in NC for t in time_shifts for b in num_turbine )
    
    
    m.setObjective(obj, GRB.MINIMIZE)
    
    m.optimize()
    for v in m.getVars():
    
        
                
        print('%s %g' % (v.varName, v.x))
    
    
    
    
    ```
    
    The part that not working is:
    ```
    m.addConstrs(gp.quicksum(witb[i,t,b])<=SGit[i,t] for i in NC for t in time_shifts for b in num_turbine)
    ```

第一个约束是错误的。我认为存在根本性的错误。但是我不知道在哪里。 另外关于第一个约束我想这样表达 w(t2,1,1)+w(t2,1,2).....w(t2,1,9)<=SGit(t2,1), w(t2,2,1)+w(t2,2,2).....w(t2,2,9)<=SGit(t2,2),... w(t2,10,1)+w(t2,10,1)....w(t2,10,10)<=SGit(t2,10)

quicksum 方法需要生成器表达式或列表,而不是单个变量。同样,addConstrs 也需要一个生成器表达式。因此,你的第一个约束应该这样写(注意额外的括号)

from gurobipy import quicksum as qsum

# ... rest of your code ...

m.addConstrs((qsum(withb[i,t,b] for b in num_turbine) <= SGit[i,t]) for i in NC for t in time_shifts)

恕我直言,用循环和 addConstr 方法编写约束更不容易出错,并且更容易阅读代码:

from gurobipy import quicksum as qsum

for i in NC:
    for t in time_shifts:
        m.addConstr(qsum(withb[i,t,b] for b in num_turbine) <= SGit[i,t])