# Solving a piecewise linear system

**URL:** <https://discourse.julialang.org/t/solving-a-piecewise-linear-system/18856>\
**Category:** Optimization (Mathematical)\
**Tags:** question\
**Created:** [December 20, 2018, 4:18pm UTC](https://discourse.julialang.org/t/solving-a-piecewise-linear-system/18856 "2018-12-20T16:18:23Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [December 21, 2018, 4:32pm UTC](https://discourse.julialang.org/t/solving-a-piecewise-linear-system/18856/22 "2018-12-21T16:32:36Z")

</div>

Are you sure? If I solve an instance using PiecewiseLinearOpt with the following code:

```julia
using JuMP
using Random
using LinearAlgebra
using PiecewiseLinearOpt

Random.seed!(2)
n = 5
# problem data
a = rand()
b = rand()
κ = rand()
c₀ = rand()
c = randn(n)
f = randn(n)

# using Gurobi
# model = Model(solver=GurobiSolver())
using Cbc
model = Model(solver=CbcSolver())
@variable model s₀
@variable model s[1 : n]
s⁺ = piecewiselinear.(Ref(model), s, Ref([-100, 0, 100]), x -> max(0, x)) # 10 is arbitrary
δ = Matrix{Variable}(undef, n, n)
for i = 1 : n
    δ[:, i] = piecewiselinear.(Ref(model), s - s[i], Ref([-100, 0, 100]), x -> max(0, x)) # 10 is arbitrary
end
@constraint model a * s₀ == c₀ + κ * s⁺ ⋅ f
for i = 1 : n
    @constraint model a * s₀ + b * s[i] == c[i] + δ[:, i] ⋅ f
end
solve(model)

```

and then run the following tests:

```julia
using Test
@test getvalue.(s⁺) ≈ max.(getvalue.(s), 0) atol=1e-6 
for i = 1 : n
    @test getvalue.(δ[:, i]) ≈ max.(getvalue.(s) .- getvalue(s[i]), 0) atol=1e-6
end
@test a * getvalue(s₀) ≈ c₀ + κ * max.(getvalue.(s), 0) ⋅ f atol=1e-6
for i = 1 : n
    @test a * getvalue(s₀) + b * getvalue(s[i]) ≈ c[i] + max.(getvalue.(s) .- getvalue(s[i]), 0) ⋅ f atol=1e-6
end

I = sortperm(c)
@test issorted(c[I])
@test issorted(getvalue.(s)[I])

```

the last test fails, which seems to contradict your statement.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [December 22, 2018, 6:09am UTC](https://discourse.julialang.org/t/solving-a-piecewise-linear-system/18856/23 "2018-12-22T06:09:13Z")

</div>

Ah, I guess you’re additionally assuming that the elements of \mathbf{f} are \geq 0? In which case I agree.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 22, 2018, 8:18am UTC](https://discourse.julialang.org/t/solving-a-piecewise-linear-system/18856/24 "2018-12-22T08:18:30Z")

</div>

Yes. Should have mentioned that, sorry.

---

<div class="post-metadata">

**Author:** ![GUANCHI\_LIU](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/guanchi_liu/32/8100_2.png) [@GUANCHI\_LIU](https://discourse.julialang.org/u/GUANCHI_LIU)\
**Post date:** [May 12, 2019, 3:06am UTC](https://discourse.julialang.org/t/solving-a-piecewise-linear-system/18856/26 "2019-05-12T03:06:23Z")

</div>

Hi~

When I try to solve this model, it returns following error at line

`s⁺ = piecewiselinear.(Ref(model), s, Ref([-100, 0, 100]), x -> max(0, x))`

**LoadError: MethodError: Cannot `convert` an object of type Array{Int64,0} to an object of type Array{Float64,1}**  
This may have arisen from a call to the constructor Array{Float64,1}(…),  
since type constructors fall back to convert methods.  
while loading untitled-5fb4d9f69cd67be59e4a6dcf3032a728, in expression starting on line 19  
in broadcast at base/broadcast.jl:455  
in broadcast\_c at base/broadcast.jl:316  
in broadcast\_t at base/broadcast.jl:270  
in \_broadcast! at base/broadcast.jl:141  
in macro expansion at base/broadcast.jl:149  
in macro expansion at base/simdloop.jl:73  
in macro expansion at base/broadcast.jl:155  
in at base/  
in PiecewiseLinearOpt.PWLFunction{1} at PiecewiseLinearOpt/src/types.jl:21

Could you please help address why this error happen? Thanks very much

Guanchi

[Previous page](https://discourse.julialang.org/t/solving-a-piecewise-linear-system/18856.md?page=1)
