How to deal with the sum of absolute functions in a JuMP model?

I would like to add a constraint that is the sum of absolute values, something like

using JuMP
model = Model(Ipopt.Optimizer)
@variable(model, w[1:n] >=0)
@constraint(model, sum(abs.(w - p)) <= alpha)   # not possible

I understand that the absolute function is not available (ERROR: LoadError: MethodError: no method matching abs(::JuMP.AffExpr). Also tried

@NLconstraint(model, sum(abs(w[i] - p[i]) for i in 1:n) <= alpha)

but got, as I was expecting, the same error. It is not clear to me how to proceed, for instance, using slack variables if that makes sense. How may I do it? Thanks in advance!

Use a linear reformulation

model = Model()
@variable(model, w >= 0)
p = rand()
@variable(model, t)
@constraint(model, t >= w - p)
@constraint(model, t >= p - w)
@constraint(model, t <= alpha)

Alternatively, you can use the MOI.NormOneCone.
If the solvers does not support it, it will be automatically reformulated by the MOI.Bridges.Constraint.NormOneBridge into the form given by @odow

model = Model()
@variable(model, w[1:n] >= 0)
p = rand(n)
@variable(model, t)
@constraint(model, [t; w - p] in MOI.NormOneCone(n + 1))

@odow and @blegat thank you very much!

1 Like