Skip to content

FAQ: norms in optimization problems behave weird

Joris Gillis edited this page Jan 15, 2025 · 3 revisions

In textbooks and papers, it is very common to encounter norms (L1, L2, Linf) in the objective of optimization problems.

While casadi implements operations for norms (Python: norm_1(.), norm_2(.), norm_inf(.), Matlab: norm(.,1),norm(.,2),norm(.,inf)`), you most likely do not want to use these directly in an optimization problem, since they are nonsmooth.

Instead, reformulate your problem. Instead of optimizing for || . ||_2, optimize for || . ||_2^2 (e.g. using sumqr(.)). Instead of optimizing with || . ||_1 or || . ||_inf, do a slack variable reformulation into a smooth problem.

Related, the derivative of the L2 norm is a fragile concept: when the argument reaches zero, the derivative is of the form 0/0 resulting in a NaN. Consider the 1-D case: L2 norm of x becomes sqrt(x), its derivative is 1/(2*sqrt(x)) which is problematic for x=0. See also https://github.com/casadi/casadi/wiki/FAQ:-Why-am-I-getting-%22NaN-detected%22in-my-optimization%3F

Clone this wiki locally