JuMP optimization

Hello all,

I am trying to solve an optimization problem in JUMP, however it is giving me some weird error (at least to me :slight_smile: ), please see my code below

using JuMP
using HiGHS

Q = [1 0;
    0 1.0]
A = zeros(0,2)
l1 = u1 = zeros(0)

M = ones(1,1)
N = -ones(1,1)
o = zeros(1)
l2 = zeros(1)
u2 = fill(Inf, 1)

x_init = [9.0, 8]
q = -[1.0; -2]

x = x_init
x_opt = []
n = length(x_init)
(zdim, wdim) = size(N)

items = 1:n

for k in 1:20  
    w = x[1:wdim] 
    z = solve_lmcp(M, N, o, l2, u2, w) 
    local_sols = find_local_solution_set(M,N,o,l2,u2,w,z)
    for sol in local_sols
        model = Model(HiGHS.Optimizer)
        @variable(model, f[1:n] >= 0)
        @objective(model, Min, 0.5*f'*Q*f+f'*q)
        @constraint(model,  l1 .<= A * f .<= u1)
        optimize!(model)
        x_opt = value.(f)
    end
    adf = []
    adf = [Vector{Float64}(vec(w)),Vector{Float64}(vec(z))]
    if all(isapprox(only(x), y; atol=1e-5) for (x,y) in zip(adf,x_opt))
        return (x_opt)
        break
    else
        x = 2.5.*[w, z] - 1.2*[w, z]  #issue is with this line, if i comment this line the code works, but if not then it says "x" not defined, however "x" is in the loop above and is defined
    end
end

it give me some weird error and says “x” not defined UndefVarError: x not defined in the “if and else” conditions . However “x” is already defined above? any idea what is going wrong here?

Thank you

I can’t reproduce your code because I don’t know what solve_lmcp and find_local_solution_set are.

But try:

    else
        global x = 2.5.*[w, z] - 1.2*[w, z]
    end

The global is a hint that you should restructure your code. The simplest way is to wrap everything in a function:

using JuMP
using HiGHS

function main()
    Q = [1 0;
        0 1.0]
    A = zeros(0,2)
    l1 = u1 = zeros(0)

    M = ones(1,1)
    N = -ones(1,1)
    o = zeros(1)
    l2 = zeros(1)
    u2 = fill(Inf, 1)

    x_init = [9.0, 8]
    q = -[1.0; -2]

    x = x_init
    x_opt = []
    n = length(x_init)
    (zdim, wdim) = size(N)

    items = 1:n

    for k in 1:20  
        w = x[1:wdim] 
        z = solve_lmcp(M, N, o, l2, u2, w) 
        local_sols = find_local_solution_set(M,N,o,l2,u2,w,z)
        for sol in local_sols
            model = Model(HiGHS.Optimizer)
            @variable(model, f[1:n] >= 0)
            @objective(model, Min, 0.5*f'*Q*f+f'*q)
            @constraint(model,  l1 .<= A * f .<= u1)
            optimize!(model)
            x_opt = value.(f)
        end
        adf = []
        adf = [Vector{Float64}(vec(w)),Vector{Float64}(vec(z))]
        if all(isapprox(only(x), y; atol=1e-5) for (x,y) in zip(adf,x_opt))
            return (x_opt)
            break
        else
            x = 2.5.*[w, z] - 1.2*[w, z]  #issue is with this line, if i comment this line the code works, but if not then it says "x" not defined, however "x" is in the loop above and is defined
        end
    end
    return x
end

See also: Performance tips · JuMP

2 Likes

Thank you :slight_smile: