bad operand type for abs(): 'gurobipy.LinExpr'
AnsweredHello,
I am trying to use my variables in a diagonal matrix and I have the following code:
import numpy as np
from gurobi import *
resources=2
product=6
A=np.array([[1,1,0,0,1,1],[0,0,1,1,1,1]])
price=np.array([150, 100, 120, 80, 250, 170])
c=np.array([90,90])
expected_demand=np.array([30,60,0,0,30,40])
P=np.identity(product)
epsilon=1
M=Model("Network Optimization")
y= M.addMVar(product, vtype = GRB.CONTINUOUS, lb = 0, ub = 1, name = 'y')
diag_y = [[M.addVar(lb=0, ub=1) if i == j else 0 for j in range(product)] for i in range(product)]
for i in range(resources):
M.addConstr(A[i]*diag_y*expected_demand +epsilon*np.linalg.norm(A[i]*diag_y*P,np.inf) <= c[i])
objFun=price*diag_y*expected_demand -epsilon*np.linalg.norm(A[i]*diag_y*P,np.inf)
M.setObjective(objFun, GRB.MAXIMIZE)
M.optimize()
When I run this, it gives the error "bad operand type for abs(): 'gurobipy.LinExpr' " in this line M.addConstr(A[i]*diag_y*expected_demand +epsilon*np.linalg.norm(A[i]*diag_y*P,np.inf) <= c[i])
I could not understand the reason why I am getting this error.
I appreciate your help.
Thanks
-
Hi İrem,
Thank you for reaching us.
If you don't mind, could you share the mathematical formulation of the constraint you want to add?
I mean, the following constraint.for i in range(resources):
M.addConstr(A[i]*diag_y*expected_demand +epsilon*np.linalg.norm(A[i]*diag_y*P,np.inf) <= c[i])When we fully understand your intention, we can guide and support you in a better way.
Additionally, we want to let you know about dealing with decision variables and simple general constraints such as MAX, ABS, and NORM constraints.
Gurobi's variables are not compatible with NumPy directly.
So, when you add a simple general expression, you need to use Gurobi's function.
The following is about the expression of the infinity norm of variable x.import gurobipy as gp
from gurobipy import *
m = gp.Model()
x = m.addMVar(5, name = 'x')
gp.norm(x, GRB.INFINITY)Best regards,
Chung-Kyun Han0 -
Hi Chung-Kyun,
I have the following mathematical model:
K is the set of resources, N is the set of products.I coded this but it is long, so to have more efficient code, I want to use matrix forms.
Thank you for your help,
İrem
0 -
Hi İrem,
Thank you for sharing your model.
We first share the entire codes representing your mathematical model and then we will add explanations.
import gurobipy as gp
import numpy as np
resources = 2
product = 6
a = np.array([[1,1,0,0,1,1],[0,0,1,1,1,1]])
r = np.array([150, 100, 120, 80, 250, 170])
c = np.array([90,90])
d = np.array([30,60,0,0,30,40])
P = np.identity(product)
epsilon = 1
m = gp.Model("Network Optimization")
diag_y = m.addMVar(product, vtype = gp.GRB.CONTINUOUS, lb = 0, ub = 1, name = 'diag_y')
a_diag_y = m.addMVar((resources, product), vtype = gp.GRB.CONTINUOUS, lb = 0, ub = 1, name = 'a_diag_y')
norm_ady = m.addMVar(resources, vtype = gp.GRB.CONTINUOUS, name = 'norm_ady')
r_diag_y = m.addMVar(product, vtype = gp.GRB.CONTINUOUS, lb = 0, ub = 1, name = 'r_diag_y')
norm_rdy = m.addVar(vtype = gp.GRB.CONTINUOUS, name = 'norm_rdy')
for i in range(resources):
m.addConstr(a_diag_y[i] == a[i] * diag_y)
m.addConstr(norm_ady[i] == gp.norm(a_diag_y[i], gp.GRB.INFINITY))
m.addConstr(a_diag_y[i] * d + epsilon * norm_ady[i] <= c[i])
m.addConstr(r_diag_y == r * diag_y)
m.addConstr(norm_rdy == gp.norm(r_diag_y, gp.GRB.INFINITY))
objFun = gp.quicksum(r * diag_y * d) - epsilon * norm_rdy
m.setObjective(objFun, gp.GRB.MAXIMIZE)
m.write("Network Optimization.lp")
m.optimize()Here, we define additional decision variables such as a_diag_y, r_diag_y, norm_ady, and norm_rdy.
To represent a norm expression including decision variables, you need to use Gurobi's norm().
The function cannot be included m.addConstr directly. Therefore we need additional variables such as norm_ady, and norm_rdy.
We also need a_diag_y and r_diag_y, because we only can pass 'vars' type to gp.norm().Please try our suggestion, and let us know if you have additional questions!
Best regards,
Chung-Kyun Han0
Please sign in to leave a comment.
Comments
3 comments