Error when using addConstrs to construct vector constraints
AnsweredHello
I am in the process of constructing my model, but I run into problems. The problem occurs when I try to declare the constraint below

Here C is a 2x2 matrix, gamma[b,t,i,:] is a vector with two elements (variable), and x^L an x^S are also variables. Finally, normPsi[b,t,:] is also an array of two elements (variable).
When running the provided code below, I get the error:
model.addConstrs((np.transpose(C) @ gamma[b,t,i,:].tolist() - [x_L[b,t], x_S[b,t]] <= normPsi[b,t,:] for b in B for t in T for i in range(N[b,t])), name = 'Dual_Norm')
File "src\gurobipy\mvar.pxi", line 656, in gurobipy.MVar.__ge__
File "src\gurobipy\tempconstr.pxi", line 22, in gurobipy.TempConstr.__init__
File "src\gurobipy\mvar.pxi", line 353, in gurobipy.MVar.__sub__
File "src\gurobipy\mlinexpr.pxi", line 1385, in gurobipy.MLinExpr.__sub__
File "src\gurobipy\mlinexpr.pxi", line 896, in gurobipy.MLinExpr.__add__
File "src\gurobipy\mlinexpr.pxi", line 240, in gurobipy.MLinExpr._from_compact_affine_expr
ValueError: setting an array element with a sequence.
I hope that anyone can help - thank you in advance :)
Also, bare in mind that I do not consider the norm here.
import gurobipy as gp
from gurobipy import GRB
import pandas as pd
import numpy as np
num_bidding_zones = 12
num_time_zones = 8
num_scenarios = 500
T = range(num_time_zones)
B = range(num_bidding_zones)
N = num_scenarios*np.ones((len(B),len(T))).astype('int')
# C matrix
C = np.matrix([[1,0],
[-1,0]])
model = gp.Model("Revenue")
# Decision variables
x_L = model.addMVar((num_bidding_zones, num_time_zones), name="x_L", vtype=GRB.CONTINUOUS, lb = 0) # x_L >= 0
x_S = model.addMVar((num_bidding_zones, num_time_zones), name="x_S", vtype=GRB.CONTINUOUS, ub = 0) # x_S <= 0
# Dual variables from reformulation
psi = model.addVars(B, T, name="psi", vtype=GRB.CONTINUOUS, lb = 0) # psi >= 0
gamma = model.addMVar((num_bidding_zones, num_time_zones, num_scenarios, 2), name="gamma", vtype=GRB.CONTINUOUS, lb = 0) # gamma >= 0
s = model.addVars(B, T, num_scenarios, name="s", vtype=GRB.CONTINUOUS, lb = 0) # s >= 0 (auxiliary variable??)
# Auxiliary variables
v = model.addVars(B, T, name="v", vtype=GRB.CONTINUOUS)
normPsi = model.addMVar((num_bidding_zones, num_time_zones, 2), name="normPsi", vtype=GRB.CONTINUOUS, lb = 0)
# The problem
model.addConstrs((np.transpose(C) @ gamma[b,t,i,:].tolist() - [x_L[b,t], x_S[b,t]] <= normPsi[b,t,:] for b in B for t in T for i in range(N[b,t])), name = 'Dual_Norm')
-
Hi Magnus,
The best way to debug this is to set intermediate variables to some values:
b = B[0]
t = T[0]
i = 0Then play around with the expression:
np.transpose(C) @ gamma[b,t,i,:].tolist() - [x_L[b,t], x_S[b,t]] <= normPsi[b,t,:]
Where does it fail? What's the smallest piece of it that fails? What are the types of these things? What are their shapes? Which pieces are not compatible with each other?
Do this, and you will find there are incompatibilities between gurobipy.MVar and numpy.matrix. You can't simply use them interchangeably. If you use the following then you will not have errors.
np.transpose(C) @ gamma[b,t,i,:] - gp.hstack([x_L[b,t], x_S[b,t]]) <= normPsi[b,t,:]
Note that gurobipy.hstack was introduced in v11.
- Riley
0
Please sign in to leave a comment.
Comments
1 comment