C/ sum absolute value in objective
AnsweredHi,
-
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?. -
Hi,
You can use the addGenConstrAbs function. Note that, since the addGenConstrAbs function accepts only exactly 1 in- and output variable, you have to introduce auxiliary variables to model the sum you have in mind, i.e.,
\[\begin{align}
\min\, &\sum_i z_i \\
\text{s.t. } z_i &= \text{abs}(y_i) \quad \forall i\\
y_i &= x_i - f_i \quad \forall i
\end{align}\]Best regards,
Jaromił0 -
Dear Jaromił,
is there a way to set up the problem without creating auxiliary variables/constraints. For example, I tried
m.setObjective(gp.abs_(z).vars.sum()),
with z being an MVar object. The problem can be compiled without error message and it also solves but the result will be wrong. Any suggestions? The issue is that i have a rather large problem and creating constraints and variables is costly. I understand that internally you probably going to linearize the problem and therefore split x into a positive and negative part anyways, but i thought that creating variables internally is probably faster than through the python interface. Any thoughts on this are very much appreciated.
Thank you.
C.
0 -
Dear C.,
Unfortunately, this is currently not possible and the introduction of auxiliary variable is required. You can still work with MVars by using the list version of them, e.g.,
import gurobipy as gp
model = gp.Model("test")
zsize = 4
# z is MVar
z = model.addMVar(zsize)
y = model.addVars(zsize)
# list version of z for abs function
z_list = z.tolist()
# construct y_i = abs(z_i) constraints
model.addConstrs(y[i] == gp.abs_(z_list[i]) for i in range(zsize))
# objective = sum y_i
model.setObjective(gp.quicksum(y[i] for i in range(zsize)))This situation will very likely be improved in a future release.
Best regards,
Jaromił0 -
Thank you very much for your reply, Jaromił.
I am looking forward to future releases. I'd think that making Gurobi more efficient for (matrix) norm minimization problems would help many people.
C.
0
Post is closed for comments.
Comments
5 comments