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:

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

Then I created an expression:

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

And finally, I tried to formulate my objective:

@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)

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).

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:

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