Adding a constraint on the norm
AnsweredHello,
I would like to add the following constraint :
Where :
- s is a size 2U vector
- beta is a size 1 variable
- A_r is a size 2Ux2B matrix
- l_0 is a float
- U is a size 2B x l variable
- L is size l float vector
- eta is a float
So only U and beta are variables.
I modeled U and beta as MVars. I used the following code snippet to add the constraint :
norm = gp.norm(s_r - beta*a_r@(l_0 + u @ l_array), 2)
m.addConstr(norm <= eta)
However, running this code leads to the following error message :
'<=' not supported between instances of 'GenExprNorm' and 'float'
I did not find a solution form the other questions related to this error message. Thank you in advance for the help !
-
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,
Matthias0 -
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)
10003From my understanding, 10003 is a gurobi error code, however, there is no error message along with this error code.
0 -
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.
Comments
3 comments