Three state variable

I haven’t run this code, so there may be bugs, but this should get you on the right track. Your model is can be formulated as a mixed-integer linear program

using JuMP, Cbc
function main(data::Vector, data1::Vector, data2::Vector)
    N = length(data)
    @assert length(data1) == length(data2) == N
    # Make `M` sufficiently large so that it is bigger than |y|
    M = 10 * (maximum(abs.(data1)) + maximum(abs.(data2)))
    model = Model(Cbc.Optimizer)
    @variables(model, begin
        0 <= x_pos[1:2, [:p, :-]] <= 10
        z[1:N], Bin
    end)
    @expressions(model, begin
        x[i=1:2], x_pos[i, :p] - x_pos[i, :-]
        y[i=1:N], data1[i] * x[1] + data2[i] * x[2]
    end)
    @constraints(model, begin
        [i=1:2], x_pos[i, :p] + x_pos[i, :-] >= 0.000001
        [i=1:N], y[i] <= M * z[i]
        [i=1:N], y[i] >= -M * (1 - z[i])
    end)
    @objective(model, Max, sum(data[i] * (2 * z[i] - 1) for i = 1:N))
    return model
end

The Mosek modeling cookbook contains a number of these reformulations: 9 Mixed integer optimization — MOSEK Modeling Cookbook 3.3.0

2 Likes