# JuMP: Piecewise linear cost function

**URL:** <https://discourse.julialang.org/t/jump-piecewise-linear-cost-function/27269>\
**Category:** Optimization (Mathematical)\
**Created:** [August 7, 2019, 2:03pm UTC](https://discourse.julialang.org/t/jump-piecewise-linear-cost-function/27269 "2019-08-07T14:03:34Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![uwechsler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uwechsler/32/5223_2.png) [@uwechsler](https://discourse.julialang.org/u/uwechsler)\
**Post date:** [August 7, 2019, 2:03pm UTC](https://discourse.julialang.org/t/jump-piecewise-linear-cost-function/27269/1 "2019-08-07T14:03:34Z")

</div>

I am trying to solve an optimization problem with a piecewise linear cost function, which writes as:  
\ell^{max}(z) := \max\_{\omega\in\Omega} \ell(z+\omega)  
where the cost function is piecewise continous, e.g. \ell(z) = max(2z, -10z).

My minimal working example looks as follows:

```julia
using Plots
using JuMP
using Ipopt

function lmax(z)
    m = Model(with_optimizer(Ipopt.Optimizer, print_level=0))
    @variable(m, -1.0 <= ω <= 1.0)
    l(x) = max.(2*x, -10*x)
    JuMP.register(m, :l, 1, l, autodiff=true)
    @NLobjective(m, Max, l(z+ω))
    optimize!(m)
    return objective_value(m)
end

x = -5:0.1:5
y = max.(2*x, -10*x)
ymax= lmax.(x)
plot(x,y, label="l")
scatter!(x,ymax, label="l_max optmized")

```

The result for this optimization differs from the expected result, as shown in this plot.  
 ![fig](https://global.discourse-cdn.com/julialang/original/3X/7/0/7091c601fce239960afc41d5b473f0a7b6aa385c.png)

Should this work in general? If so, what did I do wrong? I have the feeling, that I messed up some basic optimization principles, but I cannot figure out what.

How could I implement a piecewise linear cost function in `julia` or `JuMP` otherwise?

---

<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:** [August 7, 2019, 2:39pm UTC](https://discourse.julialang.org/t/jump-piecewise-linear-cost-function/27269/2 "2019-08-07T14:39:15Z")

</div>

Your problem is non-convex, so Ipopt isn’t guaranteed to find the global optimum.

Generally, these kinds of problems are better solved using mixed integer solvers. Take a look at

[https://github.com/joehuchette/PiecewiseLinearOpt.jl](https://github.com/joehuchette/PiecewiseLinearOpt.jl)

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [August 8, 2019, 1:50pm UTC](https://discourse.julialang.org/t/jump-piecewise-linear-cost-function/27269/3 "2019-08-08T13:50:53Z")

</div>

In addition to being non-convex, it’s also non-smooth. Ipopt assumes that the objective and constraints are twice continuously differentiable: [https://www.coin-or.org/Ipopt/documentation/node3.html#eq:obj](https://www.coin-or.org/Ipopt/documentation/node3.html#eq:obj). Ipopt _may_ work in the presence of non-smoothness, but I’m not aware of any algorithmic guarantees.

---

<div class="post-metadata">

**Author:** ![uwechsler](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uwechsler/32/5223_2.png) [@uwechsler](https://discourse.julialang.org/u/uwechsler)\
**Post date:** [August 10, 2019, 9:43pm UTC](https://discourse.julialang.org/t/jump-piecewise-linear-cost-function/27269/4 "2019-08-10T21:43:58Z")

</div>

Thanks for the clarification and the references. I will try a mixed-integer solver.
