Skip to main content

module 'gurobipy' has no attribute 'norm'

Answered

Comments

4 comments

  • Daniel Johnson
    Gurobi-versary
    First Comment
    First Question

    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
  • Eli Towle
    Gurobi Staff Gurobi Staff

    The norm general constraint was not added until Gurobi version 9.5. Please upgrade your Python version (3.6 is end-of-life 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
  • Daniel Johnson
    Gurobi-versary
    First Comment
    First Question

    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
  • Eli Towle
    Gurobi Staff Gurobi Staff

    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 zero-dimensional MVar object, and
    • \(\texttt{x}\) is a 1-dimensional 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.