# L0 norm in JuMp?

**URL:** <https://discourse.julialang.org/t/l0-norm-in-jump/91222>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [December 4, 2022, 6:18pm UTC](https://discourse.julialang.org/t/l0-norm-in-jump/91222 "2022-12-04T18:18:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gaulex](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gaulex/32/44870_2.png) [@Gaulex](https://discourse.julialang.org/u/Gaulex)\
**Post date:** [December 4, 2022, 6:18pm UTC](https://discourse.julialang.org/t/l0-norm-in-jump/91222/1 "2022-12-04T18:18:08Z")

</div>

Hello,

I would like to minimize the L0-norm of a vector (i.e. the number of non-zero element) using JuMp, but I really struggle to achieve it. I tried to define a function to represent the L0-norm:

```julia
count_nonzero(x) = sum(x .!= 0)

```

Then I created an expression:

```julia
@expression(model, expr, count_nonzero(v))

```

And finally, I tried to formulate my objective:

```julia
@NLobjective(model, Min, expr)

```

But I get an error:

> The solver does not support nonlinear problems (i.e., NLobjective and NLconstraint).

Am I missing something? I’m really new to optimization (and to Julia as well) and I am a bit lost…

Thank you for your help!  
(I am using Gurobi as an optimizer, which is supposed to support nonlinear problems)

---

<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:** [December 4, 2022, 6:40pm UTC](https://discourse.julialang.org/t/l0-norm-in-jump/91222/2 "2022-12-04T18:40:32Z")

</div>

> [@Gaulex](#):
>
> I would like to minimize the L0-norm of a vector (i.e. the number of non-zero element) using JuMp

Realize that this is a very nasty discontinuous function to optimize; it is called a “cardinality optimization problem”, and is generally NP hard (to solve exactly).

A common approach is L1 relaxation: [replace the L0 norm with an L1 norm](https://web.stanford.edu/class/ee364b/lectures/l1_slides.pdf), which is often a [good approximation](https://www.cs.utep.edu/vladik/2013/tr13-18.pdf) and makes the problem _much_ easier to solve. There are lots of references on this subject, often under the topic of [compressed sensing](https://en.wikipedia.org/wiki/Compressed_sensing).

---

<div class="post-metadata">

**Author:** ![Gaulex](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gaulex/32/44870_2.png) [@Gaulex](https://discourse.julialang.org/u/Gaulex)\
**Post date:** [December 4, 2022, 6:45pm UTC](https://discourse.julialang.org/t/l0-norm-in-jump/91222/3 "2022-12-04T18:45:25Z")

</div>

Thank you so much! It is clearer now, I will try to use L1 instead.

---

<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:** [December 4, 2022, 7:45pm UTC](https://discourse.julialang.org/t/l0-norm-in-jump/91222/4 "2022-12-04T19:45:41Z")

</div>

> I’m really new to optimization (and to Julia as well)

Hi! Yes, getting used to JuMP takes a bit of time, especially if you don’t have a background in mathematical programming.

As a general rule (with some exceptions), we don’t support arbitrary Julia functions like:

> count\_nonzero(x) = sum(x .!= 0)  
> @expression(model, expr, count\_nonzero(v))

Instead, if you’re using a solver like Gurobi, you must formulate your problem as a mixed-integer linear or quadratic program using the syntax and functions provided by JuMP.

If you’re using Gurobi, and you want to use the L1-norm, then write:

```julia
model = Model()
@variable(model, x[1:4])
@variable(model, t)
@constraint(model, [t; x] in MOI.NormOneCone(length(x) + 1))
@objective(model, Min, t)

```

---

<div class="post-metadata">

**Author:** ![Gaulex](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gaulex/32/44870_2.png) [@Gaulex](https://discourse.julialang.org/u/Gaulex)\
**Post date:** [December 4, 2022, 9:05pm UTC](https://discourse.julialang.org/t/l0-norm-in-jump/91222/5 "2022-12-04T21:05:23Z")

</div>

Thank you so much! This helped me a lot, I have something that works now, thank you!! 😁
