Skip to main content

Adding a constraint on the norm

Answered

Comments

3 comments

  • Matthias Miltenberger
    • Gurobi Staff Gurobi Staff

    Hi Nicolas,

    You need to introduce an auxiliary variable that takes the value of the norm and construct the constraint using that new variable:

    norm = gp.norm(s_r - beta*a_r@(l_0 + u @ l_array), 2)
    aux_norm = m.addVar(ub=eta, name="norm")
    m.addConstr(norm == aux_norm)

    That should do the trick.

    Cheers,
    Matthias

    0
  • Nicolas Schlegel
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you for the quick answer.

    I changed the constraint with the code snipped you gave. However, I now receive the following error (model here refers to a class from another library) : 

    m.addConstr(mse_norm == aux_norm)
      File "src\gurobipy\genexpr.pxi", line 24, in gurobipy.GenExpr.__eq__
    RuntimeError: Exception encountered when calling layer 'model' (type Model) 
    10003

    From my understanding, 10003 is a gurobi error code, however, there is no error message along with this error code.

     

    0
  • Eli Towle
    • Gurobi Staff Gurobi Staff

    Try introducing auxiliary variables equal to \(\texttt{s_r - beta * a_r @ (l_0 + u @ l_array)}\). Then, introduce one additional auxiliary variable equal to the L2 norm of those variables. For example:

    aux = m.addMVar(shape=(2 * U,), lb=-GRB.INFINITY, name="aux")
    aux_norm = m.addMVar(shape=(1,), name="aux_norm")

    m.addConstr(aux == s_r - beta * a_r @ (l_0 + u @ l_array), name="set_aux")
    m.addConstr(aux_norm == gp.norm(aux, 2), name="set_aux_norm")
    m.addConstr(aux_norm <= eta, name="norm_ub")

    Alternatively, you could model your norm constraint without the use of gurobipy's norm() function:

    aux = m.addMVar(shape=(2 * U,), lb=-GRB.INFINITY, name="aux")

    m.addConstr(aux == s_r - beta * a_r @ (l_0 + u @ l_array), name="set_aux")
    m.addConstr(aux @ aux <= eta**2, "norm_ub")
    0

Please sign in to leave a comment.