Norm 1 of a MVar as Constraint
AnsweredHello. I am trying to add a Constraint to my Matrix oriented program that checks if the norm 1 of the matrix variable is less than a p parameter. (With norm 1 beign the sum of the absolute values of each position of the MVar). I've been trying to use abs_() but then the GenExpr form makes it impossible to sum or iterate.
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
Hello José, unfortunately there isn't a shorthand for doing this in gurobipy yet, but I have taken note of your feature request in our development backlog.
For now you will have to model the absolute values per variable. For example, to compute a minimum 1-norm solution to an underdetermined linear system you could do:
import numpy as np
import gurobipy as gp
A = np.random.rand(30, 100)
x_true = np.random.rand(100)
b = A @ x_true
m = gp.Model()
x = m.addMVar(100, lb=-np.inf)
m.addConstr(A @ x == b)
# 'absx' takes absolute value of 'x'
absx = m.addMVar(100)
for v, absv in zip(x.tolist(), absx.tolist()):
m.addConstr(absv == gp.abs_(v))
# minimize 1-norm of x
m.setObjective(absx.sum())
m.optimize()
print("Solution 1-norm: {}".format(np.sum(np.abs(x.X))))0 -
Thank you so much Robert!
0 -
Hi José,
Gurobi 9.5 was recently released. Included in this release is the norm() general constraint helper function that can be used to set a decision variable equal to the norm of the other decision variables. The supported norms are 0-, 1-, 2-, and infinity.
We hope this new feature works well for you. Please let us know if you find any issues using this.
Best regards,
Maliheh
0
Post is closed for comments.
Comments
4 comments