I don't know why there is 'gurobipy.LinExpr' object is not iterable

I'm making LP model with gurobipy but i keep getting the error. here is my code.

def cal_gl_1():
    m = gp.Model('Inventory')
    s = ['AUC','ADU','CF','LT']  #AUC: average unit cost, ADU: annual dollar usage, CF: critical factor, LT: lead time
    item_number = list(range(1, 11))

    def cal_s_4():
        s_lt = [7, 4, 3, 2, 7, 4, 4, 6, 4, 5]   
        list_key = [('LT', 1), ('LT', 2), ('LT', 3), ('LT', 4), ('LT', 5), 
                    ('LT', 6), ('LT', 7), ('LT', 8), ('LT', 9), ('LT', 10)]
        lt = dict(zip(list_key, s_lt))
        return lt

    A = cal_s_1()
    B = cal_s_2()
    C = cal_s_3()
    D = cal_s_4()
    merged = {**A, **B, **C, **D}
    Lj = m.addVars(item_number, lb=0, name="lamda")

    for r in s:
        m.addConstrs(gp.quicksum(Lj[j] * merged[r, j]) >= merged[r, 1] for j in item_number)

    obj = gp.quicksum(Lj[j] for j in item_number)
    m.setObjective(obj, GRB.MINIMIZE)
    m.optimize()
    data_all = []

    for v in m.getVars():    
        print('%s %g' % (v.varName, v.x))
    
    data_all.append(v.x) 
    # print('%s %g' % (v.varName, v.x))
    data_all.append(m.objVal)
    return data_all

In constraints what I want to express is this.

L1merge('AUC',1)+.....+L10merge('AUC',10)>=merge('AUC',1) L1merge('ADU',1)+.....+L10merge('ADU',10)>=merge('ADU',1)

I wanna express like this form. However i don't know where is fault in constraint


Solution 1:

You forgot [] in quicksum:

#   HERE ---v--------------------v
gp.quicksum([Lj[j] * merged[r, j]])

Tip: don't use a loop and a comprehension, use 2 comprehensions (and give a name to your constraints for debugging purpose:

constrX = m.addConstrs((gp.quicksum([Lj[j] * merged[r, j]]) >= merged[r, 1]
                           for j in item_number
                           for r in merged), name='MyConstrX')

And maybe you should call m.update() between the variable and constraint declaration.