# How to impose absolute value greater than constraint

**URL:** <https://discourse.julialang.org/t/how-to-impose-absolute-value-greater-than-constraint/100631>\
**Category:** Optimization (Mathematical)\
**Created:** [June 21, 2023, 1:03am UTC](https://discourse.julialang.org/t/how-to-impose-absolute-value-greater-than-constraint/100631 "2023-06-21T01:03:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![horvetz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/horvetz/32/31993_2.png) [@horvetz](https://discourse.julialang.org/u/horvetz)\
**Post date:** [June 21, 2023, 1:03am UTC](https://discourse.julialang.org/t/how-to-impose-absolute-value-greater-than-constraint/100631/1 "2023-06-21T01:03:56Z")

</div>

The following gives me an error and I don’t know how to fix it. I want entries of the matrix X to be bounded by -1 and 1, and in addition I wand |X(i,j)| \>= 0.01 for all i and j. What am I doing wrong?

> using JuMP  
> model = Model( )
> 
> n = 8
> 
> @variable(model, -1.0 \<= X[1:n, 1:n] \<= 1.0, Symmetric)  
> @constraint(model, minimum(abs.(X)) .\>= 0.01)

```julia

MethodError: no method matching abs(::LinearAlgebra.Symmetric{VariableRef, Matrix{VariableRef}})
Closest candidates are:
  abs(::ForwardDiff.Dual{T}) where T at ~/.julia/packages/ForwardDiff/pDtsf/src/dual.jl:238
  abs(::Signed) at /Volumes/Julia-1.7.2/Julia-1.7.app/Contents/Resources/julia/share/julia/base/int.jl:180
  abs(::Unsigned) at /Volumes/Julia-1.7.2/Julia-1.7.app/Contents/Resources/julia/share/julia/base/int.jl:179
  ...

```

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [June 21, 2023, 1:22am UTC](https://discourse.julialang.org/t/how-to-impose-absolute-value-greater-than-constraint/100631/2 "2023-06-21T01:22:36Z")

</div>

I guess that `abs` isn’t supported in constraints. Maybe try replacing `min(abs(X)) .>= 0.01` with something like these two constraints: `X[1:n, 1:n] ≤ -0.01` and `0.01 ≤ X[1:n, 1:n]`.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 21, 2023, 1:37am UTC](https://discourse.julialang.org/t/how-to-impose-absolute-value-greater-than-constraint/100631/3 "2023-06-21T01:37:50Z")

</div>

> [@horvetz](#):
>
> min(abs(X))

`minimum(abs.(X))`?

---

<div class="post-metadata">

**Author:** ![horvetz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/horvetz/32/31993_2.png) [@horvetz](https://discourse.julialang.org/u/horvetz)\
**Post date:** [June 21, 2023, 1:45am UTC](https://discourse.julialang.org/t/how-to-impose-absolute-value-greater-than-constraint/100631/4 "2023-06-21T01:45:12Z")

</div>

I need OR not AND which I don’t know how to code

---

<div class="post-metadata">

**Author:** ![horvetz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/horvetz/32/31993_2.png) [@horvetz](https://discourse.julialang.org/u/horvetz)\
**Post date:** [June 21, 2023, 1:46am UTC](https://discourse.julialang.org/t/how-to-impose-absolute-value-greater-than-constraint/100631/5 "2023-06-21T01:46:56Z")

</div>

Yes that’s the correct syntax but still I get the error

```julia
MethodError: no method matching abs(::VariableRef)
Closest candidates are:
  abs(::ForwardDiff.Dual{T}) where T at ~/.julia/packages/ForwardDiff/pDtsf/src/dual.jl:238
  abs(::Signed) at /Volumes/Julia-1.7.2/Julia-1.7.app/Contents/Resources/julia/share/julia/base/int.jl:180
  abs(::Unsigned) at /Volumes/Julia-1.7.2/Julia-1.7.app/Contents/Resources/julia/share/julia/base/int.jl:179

```

…

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 21, 2023, 1:54am UTC](https://discourse.julialang.org/t/how-to-impose-absolute-value-greater-than-constraint/100631/6 "2023-06-21T01:54:18Z")

</div>

> [@horvetz](#):
>
> @variable(model, -1.0 \<= X[1:n, 1:n] \<= 1.0, Symmetric)  
> @constraint(model, minimum(abs.(X)) .\>= 0.01)

Realize that even if you can formulate this for JuMP, it’s likely to be a pretty terrible combinatorial optimization problem — your feasible set is not only non-convex, it is actually disconnected, with an exponentially large number of disjoint sign choices. It may be pretty hard (probably NP hard) for any algorithm to solve such a problem. Are you sure you can’t formulate a different problem for your application? Why do you need the absolute-value constraint?

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [June 21, 2023, 2:37am UTC](https://discourse.julialang.org/t/how-to-impose-absolute-value-greater-than-constraint/100631/7 "2023-06-21T02:37:09Z")

</div>

As @stevengj alludes to, you can model this using binary variables:

```julia
using JuMP

function add_abs_greater_than_constraint(x::VariableRef, abs_min::Float64)
    lb, ub = lower_bound(x), upper_bound(x)
    x_neg = @variable(model, lower_bound = 0, upper_bound = -lb)
    x_pos = @variable(model, lower_bound = 0, upper_bound = ub)
    z = @variable(model, [1:2], Bin)
    @constraints(model, begin
        x == x_pos - x_neg
        sum(z) == 1
        x_pos <= z[1]
        x_pos >= abs_min * z[1]
        x_neg <= z[2]
        x_neg >= abs_min * z[2]
    end)
    return
end

n = 8
model = Model()
@variable(model, -1.0 <= X[1:n, 1:n] <= 1.0, Symmetric)
for i in 1:n, j in i:n
    add_abs_greater_than_constraint(X[i, j], 0.01)
end

```

But an absolute value greater than constraint is a little unusual. What’s wrong with zero elements in `X`?
