module 'gurobipy' has no attribute 'norm'
AnsweredHi everyone,
I'm trying to set up an optimisation where I have a variable x that is a threevector. I also have constants B (a 3x3 matrix) and u (a 3 vector). I want to constrain the values of x such that:
2norm(Bx)<transpose(u)x
The code im trying to use for this is:
import gurobipy as gp
from gurobipy import GRB
import numpy as np
m = gp.Model("lp")
B_i = np.array([[1, 0 ,0],
[0, 1, 0],
[0, 0, 0]])
u_i = np.array([0, 0, 0.6])
x_i = m.addMVar(shape=(3,), lb=25, ub=25, vtype=GRB.CONTINUOUS, name="x")
Bx_i = B_i @ x_i
ux_i = np.transpose(u_i) @ x_i
m.addConstr(gp.norm(Bx_i, 2)  gp.norm(ux_i, 2) <0, "constraint_1")
Unfortunately, this gives me the following error:
File "/home/.../minimal_problem_example.py", line 16, in <module>
m.addConstr(gp.norm(Bx_i, 2)  gp.norm(ux_i, 2) <0, "constraint_1")
AttributeError: module 'gurobipy' has no attribute 'norm'
I'm not sure why this is, as I found the norm() function here: https://www.gurobi.com/documentation/current/refman/py_norm.html
Could anyone provide some advice on possible solutions or alternative implementations? I'm using gurobi version 9.1.0 and Python 3.6.9

The norm general constraint was not added until Gurobi version 9.5. Please upgrade your Python version (3.6 is endoflife and not supported by recent versions of Gurobi), then upgrade to the latest version of Gurobi (11.0) by following the instructions in the How do I update my Gurobi installation to a newer version? article.
0 
Hi Eli,
Thanks for getting back to me. I'll try this, but I'm not sure if I can update my Python version because this project relies on libraries that are not compatible with later versions of Python.
In the event that I can't update my Python version, is there any way to do this in the version I have?
1 
Hi Eli,
While I try and determine if I can remove some dependencies to shift Python versions, I've created a separate Conda environment for Python 3.8 where I'm using Gurobi 10.0. When i try this code in that environment I get a new error:
Traceback (most recent call last):
File "/home/.../minimal_problem_example.py", line 16, in <module>
m.addConstr(gp.norm(Bx_i, 2)  gp.norm(ux_i, 2) < 0, "constraint_1")
TypeError: unsupported operand type(s) for : 'GenExprNorm' and 'GenExprNorm'Can you advise on what I'm doing wrong with this?
0 
The norm() function is used to set one variable equal to the norm of other variables. This means your constraint must be of the form
m.addConstr(y == gp.norm(x, 2))
where:
 \(\texttt{y}\) is a Var object or zerodimensional MVar object, and
 \(\texttt{x}\) is a 1dimensional MVar object, a tupledict of Var objects, or a list of Var objects
Consequently, you must introduce auxiliary variables for the arguments to the norm() function, as well as an auxiliary variable that will be set equal to the norm itself. For example:
v = m.addMVar(shape=(3,), lb=GRB.INFINITY, name="v")
w = m.addMVar(shape=(1,), lb=GRB.INFINITY, name="w")
y = m.addVars(2, name="y")
m.addConstr(v == B_i @ x_i, name="define_v")
m.addConstr(w == u_i @ x_i, name="define_w")
m.addConstr(y[0] == gp.norm(v, 2), name="define_y[0]")
m.addConstr(y[1] == gp.norm(w, 2), name="define_y[1]")
m.addConstr(y[0] <= y[1], name="constraint1")It is likely easier to model this constraint directly:
m.addConstr(Bx_i @ Bx_i <= ux_i * ux_i, name="constraint1")
Or even more simply:
x = m.addMVar(shape=(3,), lb=25, ub=25, name="x")
m.addConstr(x[:1] @ x[:1] <= 0.6**2 * x[1] * x[1], name="constraint1")Also, note that strict inequality constraints (<, >) are not supported by Gurobi or any other solver. You can read more about this in the article Why doesn't Gurobi support strict inequality constraints like x < a?.
0
Please sign in to leave a comment.
Comments
4 comments