# Help with JuMP variables

**URL:** <https://discourse.julialang.org/t/help-with-jump-variables/61733>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, optimization\
**Created:** [May 24, 2021, 4:40pm UTC](https://discourse.julialang.org/t/help-with-jump-variables/61733 "2021-05-24T16:40:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan.phi](https://avatars.discourse-cdn.com/v4/letter/d/c89c15/32.png) [@Dan.phi](https://discourse.julialang.org/u/Dan.phi)\
**Post date:** [May 24, 2021, 4:40pm UTC](https://discourse.julialang.org/t/help-with-jump-variables/61733/1 "2021-05-24T16:40:09Z")

</div>

I am trying to solve a constrained optimization problem using JuMP, but there is apparently a conflict between how I define the variables and one of the packages I call in the objective function. I just started with JuMP so it’s probably a very minor thing that I do not understand. The following is a MWE to illustrate the issue (the example itself makes no sense, but it is the same error I get in my code):

```julia
using QuantEcon # loads MarkovChain
using GLPK

function f_matrix(p_m)
    MC = MarkovChain(p_m)
    index_1 = stationary_distributions(MC)[1]
    return index_1[1]
end

model = Model(GLPK.Optimizer)
@variable(model, 0 <= p_matrix[1:3,1:3] <= 1)
@objective(model, Min, f_matrix(p_matrix))

```

When running `@objective(model, Min, f_matrix(p_matrix))` it gives the following error:

```julia
ERROR: MethodError: no method matching isless(::VariableRef, ::VariableRef)

```

---

<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:** [May 25, 2021, 11:34pm UTC](https://discourse.julialang.org/t/help-with-jump-variables/61733/2 "2021-05-25T23:34:40Z")

</div>

JuMP is a package for structured optimization and has its strengths and weaknesses. It isn’t well suited for black-box nonlinear optimization problems like you have there.

See, [“Should I use JuMP?”](https://jump.dev/JuMP.jl/stable/background/should_i_use/).

You may be better off with something like Optim.jl ([https://github.com/JuliaNLSolvers/Optim.jl](https://github.com/JuliaNLSolvers/Optim.jl)).

Note that JuMP _does_ support user-defined nonlinear functions, [Nonlinear Modeling · JuMP](https://jump.dev/JuMP.jl/stable/manual/nlp/#User-defined-Functions). That would look something like the following (I didn’t try, there might be typos, etc. It also needs `stationary_distributions` to be differentiable using ForwardDiff. Not sure if it is.).

```nohighlight
using QuantEcon
using JuMP
import Ipopt

function f_matrix(x...)
    p_m = reshape(collect(x), (3, 3))
    MC = MarkovChain(p_m)
    index_1 = stationary_distributions(MC)[1]
    return index_1[1]
end

model = Model(Ipopt.Optimizer)
@variable(model, 0 <= p_matrix[1:3, 1:3] <= 1)
register(model, :f_matrix, 9, f_matrix; autodiff = true)
@NLobjective(model, Min, f_matrix(p_matrix...))

```

(Note that you need Ipopt, a nonlinear optimizer, instead of GLPK, which is a linear optimizer.)

I would still start with Optim instead, however.
