# Modelling non-continuous piecewise linear functions

**URL:** https://discourse.julialang.org/t/modelling-non-continuous-piecewise-linear-functions/106743
**Category:** Optimization (Mathematical)
**Tags:** jump, piecewise
**Created:** [November 26, 2023, 1:50pm UTC](https://discourse.julialang.org/t/modelling-non-continuous-piecewise-linear-functions/106743 "2023-11-26T13:50:15Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Galois](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galois/32/51079_2.png) [@Galois](https://discourse.julialang.org/u/Galois)
#### Post date: [November 26, 2023, 1:50pm UTC](https://discourse.julialang.org/t/modelling-non-continuous-piecewise-linear-functions/106743/1 "2023-11-26T13:50:15Z")

</div>

I have an optimization problem where the objective (and some of the constraints) contain piecewise linear functions. The problem is relatively large and my attempts at solving it with pyomo and SCIP have not been very successful in terms on runtime. So I wanted to give it a shot using Julia after I learned about PiecewiseLinearOpt.jl

I am new to Julia and JuMP so maybe I have misunderstood things, but can I model a non-continuous piecewise linear function in PiecewiseLinearOpt.jl? If my understanding is correct, I need to pass in the breakpoints and the corresponding values. But with a non-coninuous function I don’t see how this can be done.

For example, consider the following function:

![Screenshot 2023-11-26 at 14.41.05](https://global.discourse-cdn.com/julialang/original/3X/2/7/2763d05d3133258b122f77344ba9bd0dfa22b9ec.png)

So at 2 and 4, the function is not continuous. And I was interested in trying PiecewiseLinearOpt.jl because it offers different formulations. I have modelled this using binary variables and big-M but the runtime is just horrendous (my problem involves summing millions of non continuous piecewise functions).

---

<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: [November 26, 2023, 7:35pm UTC](https://discourse.julialang.org/t/modelling-non-continuous-piecewise-linear-functions/106743/2 "2023-11-26T19:35:42Z")

</div>

I don’t think PiecewiseLinearOpt supports discontinuities. You likely do need to use a binary formulation that selects the piece.

> my problem involves summing millions of non continuous piecewise functions

This just sounds like a very difficult problem to solve. What is the application? Why are you modeling it like this?

---

<div class="post-metadata">

### Author: ![Galois](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galois/32/51079_2.png) [@Galois](https://discourse.julialang.org/u/Galois)
#### Post date: [November 26, 2023, 7:45pm UTC](https://discourse.julialang.org/t/modelling-non-continuous-piecewise-linear-functions/106743/3 "2023-11-26T19:45:26Z")

</div>

Ah, unfortunate but thanks for help!

The problem I am trying to solve is essentially a profit maximisation problem with churn. So i have a set of customers that have a probability of churning (depending on what price I offer them, call it x\_n). So something like this:

\sum\_{n}(1-prob\_{n})(1+x\_n)

Where prob\_{n} =

\begin{cases} 0.25, & x\_n \geq 2 \\ 0, & 0 \< x\_n \< 2 \\ 0.05, & x \leq 0 \\ \end{cases}

Where prob\_n is the probability of churning and instead of just (1+x\_n) I have x\_n with some additional parameters indexed by n. But basically I want to maximise expected profits.

The above is obviously not an interesting problem. In the real problem, I have additional constraints that turns this into a knapsack problem of sorts. But since the set of customers is very large, it is taking very long to solve.

---

<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: [November 26, 2023, 8:02pm UTC](https://discourse.julialang.org/t/modelling-non-continuous-piecewise-linear-functions/106743/4 "2023-11-26T20:02:46Z")

</div>

Is your churn probability really discontinuous like this? You can probably find a different functional form that leads to a nicer problem to solve.

But otherwise, yes, your binary formulation is likely pretty reasonable, and I would expect this to be very difficult to solve.

---

<div class="post-metadata">

### Author: ![Galois](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/galois/32/51079_2.png) [@Galois](https://discourse.julialang.org/u/Galois)
#### Post date: [November 26, 2023, 8:07pm UTC](https://discourse.julialang.org/t/modelling-non-continuous-piecewise-linear-functions/106743/5 "2023-11-26T20:07:34Z")

</div>

Given the industry I am trying to apply this for, this is surprisingly how we expect churn to look. I was thinking about removing the x\_n \leq 0 segment, and turning the remaining segments into a ReLU shaped function and see if that helps. In the worst case scenario I will simply have to group customers together reduce the size of the problem.
