Solving a piecewise linear system

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

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:

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.

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

Yes. Should have mentioned that, sorry.

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