TypeError: 'gurobipy.LinExpr' object is not subscriptable
AnsweredHi, I am trying to set up my code with constraints and running into some issues. I have a bus with 32 elements, and I am trying to set up multiple constraints for each of the busses. I am getting weird errors around unsupported operand type(s) for *: 'int' and 'generator' gurobi constraint or TypeError: 'gurobipy.LinExpr' object is not subscriptable. Any help would be appreciated. My code is below:
# user/literature defined input variables
v_nom = 12.66 * 1000
v_min = 0.95
v_max = 1.05
i_min = 0.95
i_max = 1.05 #***could also come back and calculate this based on V_max & P base
p_base = 1000
p_supply = 3715
q_supply = 2300
p_dg = 0
q_dg = 0
ke = 168
s = 4
y = 50
# input variables from data file
load_bus = load_data[:,0]
p_demand = load_data[:,1]
q_demand = load_data[:,2]
line_from = line_data[:,0]
line_to = line_data[:,1]
line_num = line_data[:,2]
r = line_data[:,3]
x = line_data[:,4]
z = r + 1j*x
model = gp.Model('MILP')
num_bus = 32
p = model.addVars(num_bus, vtype=GRB.CONTINUOUS, name="p")
q = model.addVars(num_bus, vtype=GRB.CONTINUOUS, name="q")
i = model.addVars(num_bus, vtype=GRB.CONTINUOUS, name="i")
b = model.addVars(num_bus, vtype=GRB.BINARY, name="b")
delta = (v_max**2 - v_min**2)/ (s+1)
# add 4 constraints
# 1. active power balance
p_bal = model.addConstr(gp.quicksum(p[type] for type in range(num_bus)) -
gp.quicksum((p[n+1] + r[n+1]*i[n+1] for n in range(num_bus-1))) +
p_supply +
p_dg
== np.sum(p_demand)) #removed for n in range() on the RHS of ==
# 2. reactive power balance
q_bal = model.addConstr(gp.quicksum(q[n] for n in range(num_bus-1)) -
gp.quicksum((q[n+1] + x[n+1]*i[n+1] for n in range(num_bus-1))) +
q_supply +
q_dg
== np.sum(p_demand)) #removed for n in range() on the RHS of ==
# 3. voltage drop
v_drop = model.addConstr((i[n]*(r[n]**2) -
(2*(r[n]*p[n] + x[n]*q[n])) -
((z[n]**2) *i[n]) -
(i[n+1]*(r[n]**2)) -
(b[n]) for n in range(num_bus-1))
== 0) #create multiple constraints, 1 for each bus of this formula
# 4. VI related to PQ
vi_pq = model.addConstrs(((p[type]*p[type]) + (q[type]*q[type]) for type in range (num_bus))
== (pow(v_min,2) + ((1/2)*delta * i[v]) for v in range(num_bus)) +
(s*gp.quicksum(delta*i[v] for v in range(num_bus))))
My error is the following:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/var/folders/s6/k3hg_1v51_d1t2bgqyr1ch4m0000gn/T/ipykernel_28908/3565018324.py in <module>
22 # 4. VI related to PQ
23 vi_pq = model.addConstrs(((p[type]*p[type]) + (q[type]*q[type]) for type in range (num_bus))
---> 24 == (pow(v_min,2) + ((1/2)*delta * i[v]) for v in range(num_bus)) +
25 (s*gp.quicksum(delta*i[v] for v in range(num_bus))))
26
src/gurobipy/linexpr.pxi in gurobipy.LinExpr.__add__()
src/gurobipy/linexpr.pxi in gurobipy.LinExpr.__init__()
TypeError: 'gurobipy.LinExpr' object is not subscriptable
Appreciate your help. Thanks!
0
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Anjali,
It seems you need to add a quicksum() call around those sums. Please always try writing a minimal reproducible example (MRE) instead of just copy-pasting your entire code.
Best regards,
Matthias0
Post is closed for comments.
Comments
2 comments