Abs value constraint for MLinExpr
AnsweredI am trying to Minimize the following function using the Python API of Gurobi 9.0.0
min ||A*x||_1 - ||B*x||_1
for x in [-1,1]^n
This cost function is not convex and thererfore not as easy to compute as in https://support.gurobi.com/hc/en-us/community/posts/360056614871-L1-norm-in-objective-function
Depending on the values of the two matrices, the minimal value of the loss may be 0 (with x=0) or negative. I previosly tried to solve the optimization problem in Matlab using the Yalmip wrapper and a nonconvex solver (bmibnb), which works as expected. Now i have problems solving it in Python.
Since the 1-Norm is not supported in the API, I try to model it as a sum of absolute values. My idea was to introduce new variables (MLinVar) with the following constraints
yA = A*x
yB = B*x
zA = abs(yA)
zB = abs(yB)
But unfortunately, the absolute value does not work for the MVar nor for the individual elements of it (zA[0] = abs(yA[0]).
Is there another way how to optimize this function?
-
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?. -
You can simply change your variable declaration from using MVar to regular Gurobi variables; the abs-value should work there without any issues. Have you tried that by any chance?
0 -
Hi Christian,Gurobi 10.0 was recently released. Included in this release is the extension of Gurobi Matrix API which enables natural model building using matrix based expressions relying on NumPy concepts such as vectorization and broadcasting.The new capabilities of modelling classes MVar, MLinExpr, and MQuadExpr are not still fully integrated with general constraints. The general constraints should be added by indexing the Matrix API classes. See the snippet below.
m = gp.Model()
A = np.random.rand(3, 3)
b = np.random.rand(3, 2)
x = m.addMVar(shape=(3, 2), name="x")
y = m.addMVar(shape=(3, 2), name="y")
m.addConstr(A @ x == b)
# We still do not support vectorization of the general constraints. The
# constraints should be added via indexing
for i in range(3):
for j in range(2):
m.addConstr(y[i, j] == gp.abs_(x[i, j]))Checkout the Matrix-friendly Modeling with Gurobipy webinar if you would like to learn more about this new functionality.Please continue submitting new community posts for any bugs you might encounter in the future or for any comments/questions you might have. Users like you help to make Gurobi better!Best regards,Maliheh0
Post is closed for comments.
Comments
3 comments