# Problems with a picewise quadratic problem

**URL:** https://discourse.julialang.org/t/problems-with-a-picewise-quadratic-problem/7728
**Category:** Optimization (Mathematical)
**Created:** [December 13, 2017, 3:52am UTC](https://discourse.julialang.org/t/problems-with-a-picewise-quadratic-problem/7728 "2017-12-13T03:52:18Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![gcabrera](https://avatars.discourse-cdn.com/v4/letter/g/6f9a4e/32.png) [@gcabrera](https://discourse.julialang.org/u/gcabrera)
#### Post date: [December 13, 2017, 3:52am UTC](https://discourse.julialang.org/t/problems-with-a-picewise-quadratic-problem/7728/1 "2017-12-13T03:52:18Z")

</div>

Hi Guys,

I need to model the following optimization problem

\sum\_{j=1}^{N} (\alpha \* w\_j (d\_j(x)-p\_j)^2)

where \alpha = 1 when (d\_j(x)-p\_j) \> 0 and \alpha = 0 otherwise

I’ve been through the JuMP’s documentation without much success. Could anyone tell me whether JuMP supports this kind of mathematical expressions?

Thanks!

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [December 14, 2017, 10:28pm UTC](https://discourse.julialang.org/t/problems-with-a-picewise-quadratic-problem/7728/2 "2017-12-14T22:28:26Z")

</div>

[ConditionalJuMP.jl](https://github.com/rdeits/ConditionalJuMP.jl) is a tool that I wrote to make things like your problem easier to formulate in JuMP. For example, if we introduce a new variable `z` and constrain that `z_j = d_j - p_j` when `d_j - p_j >= 0` and `z_j = 0` otherwise, then we have the same problem you wrote, but with `z = alpha * (d_j - p_j)`.

```julia
using JuMP, ConditionalJuMP, Gurobi

N = 10
w = randn(N)
p = randn(N)

model = Model(solver=GurobiSolver())
@variable model d[1:N] lowerbound=-10 upperbound=10
@variable model z[1:N] lowerbound=0 upperbound=100
for i in 1:N
    @implies(model,
        (d[i] - p[i] >= 0) => z[i] == d[i] - p[i],
        (d[i] - p[i] <= 0) => z[i] == 0)
end
@objective model Min sum(w[i] * z[i]^2 for i in 1:N)
solve(model)
@show getvalue(d) getvalue(z);

```

gives:

```julia
Optimize a model with 80 rows, 30 columns and 190 nonzeros
Model has 10 quadratic objective terms
Variable types: 20 continuous, 10 integer (10 binary)
Coefficient statistics:
  Matrix range [1e+00, 1e+02]
  Objective range [0e+00, 0e+00]
  QObjective range [1e-01, 5e+00]
  Bounds range [1e+00, 1e+02]
  RHS range [1e+01, 1e+02]
Found heuristic solution: objective 0
Presolve removed 80 rows and 30 columns
Presolve time: 0.00s
Presolve: All rows and columns removed

Explored 0 nodes (0 simplex iterations) in 0.00 seconds
Thread count was 1 (of 12 available processors)

Solution count 1: -732.788 
Pool objective bound -732.788

Optimal solution found (tolerance 1.00e-04)
Best objective -7.327877477104e+02, best bound -7.327877477104e+02, gap 0.0000%
getvalue(d) = [10.0, 1.46984, 10.0, 10.0, -2.17514, 0.285866, 10.0, 10.0, -0.522088, 10.0]
getvalue(z) = [10.8604, 0.0, 10.827, 9.46254, 0.0, 0.0, 9.21451, 11.013, 0.0, 9.64365]

```

or, equivalently:

```julia
model = Model(solver=GurobiSolver())
@variable model d[1:N] lowerbound=-10 upperbound=10
@variable model z[1:N] lowerbound=0 upperbound=100
for i in 1:N
    @disjunction(model,
        (d[i] - p[i] >= 0) & (z[i] == d[i] - p[i]),
        (z[i] == 0))
end
@objective model Min sum(w[i] * z[i]^2 for i in 1:N)
solve(model)
@show getvalue(d) getvalue(z);

```

Note that ConditionalJuMP requires that all your decision variables have bounds.

There’s also [GitHub - joehuchette/PiecewiseLinearOpt.jl: Solve optimization problems containing piecewise linear functions](https://github.com/joehuchette/PiecewiseLinearOpt.jl) which will probably give you a more efficient formulation, but I’m not as familiar with its interface.

---

<div class="post-metadata">

### Author: ![gcabrera](https://avatars.discourse-cdn.com/v4/letter/g/6f9a4e/32.png) [@gcabrera](https://discourse.julialang.org/u/gcabrera)
#### Post date: [December 15, 2017, 6:44pm UTC](https://discourse.julialang.org/t/problems-with-a-picewise-quadratic-problem/7728/3 "2017-12-15T18:44:16Z")

</div>

Thank you very much @rdeits!!

Cheers

Guillermo

---

<div class="post-metadata">

### Author: ![opt](https://avatars.discourse-cdn.com/v4/letter/o/e47774/32.png) [@opt](https://discourse.julialang.org/u/opt)
#### Post date: [August 1, 2018, 12:26pm UTC](https://discourse.julialang.org/t/problems-with-a-picewise-quadratic-problem/7728/4 "2018-08-01T12:26:57Z")

</div>

Hi @rdeits.  
It seems that ConditionalJuMP.jl is not supported with Julia 0.7.  
Do you plan to fix it, or do you have any advice to do other?  
Thanks in advance,  
Arnaud.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [August 1, 2018, 5:26pm UTC](https://discourse.julialang.org/t/problems-with-a-picewise-quadratic-problem/7728/5 "2018-08-01T17:26:50Z")

</div>

Someday? Yes, definitely. But probably not in the next month at least. Porting it to 0.7 would really mean porting it to use the new MathOptInterface, which would probably involve rewriting the package to some extent. I think that’s definitely worth doing, but it’s not my top priority right now.

---

<div class="post-metadata">

### Author: ![opt](https://avatars.discourse-cdn.com/v4/letter/o/e47774/32.png) [@opt](https://discourse.julialang.org/u/opt)
#### Post date: [August 2, 2018, 6:58am UTC](https://discourse.julialang.org/t/problems-with-a-picewise-quadratic-problem/7728/6 "2018-08-02T06:58:03Z")

</div>

Understood. Thanks Robin for your prompt answer.  
I’ll carefully check the availability as I’m a strong user of your awesome package.

---

<div class="post-metadata">

### Author: ![opt](https://avatars.discourse-cdn.com/v4/letter/o/e47774/32.png) [@opt](https://discourse.julialang.org/u/opt)
#### Post date: [October 7, 2018, 8:51pm UTC](https://discourse.julialang.org/t/problems-with-a-picewise-quadratic-problem/7728/7 "2018-10-07T20:51:58Z")

</div>

Hi guys,

Does anyone knows how to formulate a JuMP Variable comparison?  
I was wondering how to fomulate this, just with a if else statement, like:

```julia
@variable(model, 0 <= loc_pn_compat_free[loc = LOCATIONS, pn = PN_ALL, time = TIMES] <= 1, Bin)
@variable(model, 0 <= IsInLocked[loc = LOCATIONS, pn = PN_ALL, time = TIMES] <= 1, Bin)

for loc in LOCATIONS, pn in PN_ALL, time in TIMES
	if loc_pn_compat_free[loc,pn,time] >= 0.5
		@constraint(model,IsInLocked[loc,pn,time] == 0)
	end
end

```

By doing this, I obtain:

ERROR: MethodError: no method matching isless(::JuMP.Variable, ::Float64)

(MILP - Cbc Solver)

The @implies macro does the job great, but only for 1 statement to compare.  
Basically, I’d like to simulate something like: @implies(model, x \>= 0.5 && y \<= 1 =\> …), which is impossible with ConditionalJuMP package, but maybe possible by another way …?

Thanks in advance,  
Arnaud.
