# JuMP optimization

**URL:** <https://discourse.julialang.org/t/jump-optimization/89528>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [October 30, 2022, 4:37pm UTC](https://discourse.julialang.org/t/jump-optimization/89528 "2022-10-30T16:37:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Muhammad\_Nadeem](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Muhammad\_Nadeem](https://discourse.julialang.org/u/Muhammad_Nadeem)\
**Post date:** [October 30, 2022, 4:37pm UTC](https://discourse.julialang.org/t/jump-optimization/89528/1 "2022-10-30T16:37:23Z")

</div>

Hello all,

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

```julia
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

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [October 30, 2022, 7:16pm UTC](https://discourse.julialang.org/t/jump-optimization/89528/2 "2022-10-30T19:16:38Z")

</div>

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

But try:

```julia
    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:

```nohighlight
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](https://jump.dev/JuMP.jl/stable/tutorials/getting_started/performance_tips/#Read-the-Julia-performance-tips)

---

<div class="post-metadata">

**Author:** ![Muhammad\_Nadeem](https://avatars.discourse-cdn.com/v4/letter/m/bc79bd/32.png) [@Muhammad\_Nadeem](https://discourse.julialang.org/u/Muhammad_Nadeem)\
**Post date:** [October 30, 2022, 7:25pm UTC](https://discourse.julialang.org/t/jump-optimization/89528/3 "2022-10-30T19:25:36Z")

</div>

Thank you 🙂
