# JuMP in julia

**URL:** <https://discourse.julialang.org/t/jump-in-julia/89945>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, optimization, ipopt, nonlinearsolve\
**Created:** [November 8, 2022, 5:22pm UTC](https://discourse.julialang.org/t/jump-in-julia/89945 "2022-11-08T17:22:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![thiagoselias](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thiagoselias/32/43916_2.png) [@thiagoselias](https://discourse.julialang.org/u/thiagoselias)\
**Post date:** [November 8, 2022, 5:22pm UTC](https://discourse.julialang.org/t/jump-in-julia/89945/1 "2022-11-08T17:22:22Z")

</div>

Link:  
[https://gist.githubusercontent.com/thiagoselias/65dd09e675493443493b394bcc136784/raw/c81339907844c6bfb296353ed22269706d46b497/minimizacao.jl](https://gist.githubusercontent.com/thiagoselias/65dd09e675493443493b394bcc136784/raw/c81339907844c6bfb296353ed22269706d46b497/minimizacao.jl)

I have this problem

But specifically in the constraint

[@constraint](https://web.telegram.org/?legacy=1#/im?p=%40constraint)(model, y[1:length(PTV68)] ≥ 1)

When I run with the constraint I have this error:

MethodError: no method matching getindex(::VariableRef, ::UnitRange{Int64})

---

<div class="post-metadata">

**Author:** ![thiagoselias](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thiagoselias/32/43916_2.png) [@thiagoselias](https://discourse.julialang.org/u/thiagoselias)\
**Post date:** [November 8, 2022, 5:25pm UTC](https://discourse.julialang.org/t/jump-in-julia/89945/2 "2022-11-08T17:25:37Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/8/4/841b02520bd783346648218f4a9b04d7447ce53e.png)

This is the constraint for:  
@constraint(model, y[1:length(PTV68)] ≥ 1)

this is the error:  
MethodError: no method matching getindex(::VariableRef, ::UnitRange{Int64})

Stacktrace:  
[1] macro expansion  
@ C:\Users\Elias.julia\packages\MutableArithmetics\maUDe\src\rewrite.jl:294 [inlined]  
[2] macro expansion  
@ C:\Users\Elias.julia\packages\JuMP\Z1pVn\src\macros.jl:819 [inlined]  
[3] top-level scope  
@ In[29]:11  
[4] eval  
@ .\boot.jl:368 [inlined]  
[5] include\_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)  
@ Base .\loading.jl:1428

---

<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:** [November 8, 2022, 7:40pm UTC](https://discourse.julialang.org/t/jump-in-julia/89945/3 "2022-11-08T19:40:10Z")

</div>

You declare `y` as

```julia
@variable(model, y)

```

This is single variable. It is not a vector, so you cannot index it with `[]`.

Did you mean

```julia
@variable(model, y[1:length(PTV68)])

```

Even if you fix the definition of `y`, you’ll still have a problem with:

```julia
@constraint(model, y[1:length(PTV68)] ≥ 1)

```

The left-hand side is a vector. The right-hand side is a constant.

Did you mean one of:

```julia
@constraint(model, y[1:length(PTV68)] .>= 1)
# or
@constraint(model, sum(y[1:length(PTV68)]) >= 1)

```

Note that variable bounds can be added in `@variable`:

```julia
@variable(model, y[1:length(PTV68)] >= 1)

```

---

<div class="post-metadata">

**Author:** ![thiagoselias](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thiagoselias/32/43916_2.png) [@thiagoselias](https://discourse.julialang.org/u/thiagoselias)\
**Post date:** [November 8, 2022, 9:56pm UTC](https://discourse.julialang.org/t/jump-in-julia/89945/4 "2022-11-08T21:56:16Z")

</div>

Would it be this way?

model = Model(Ipopt.Optimizer)  
obj = length(DRe) #Quantidade de variaveis da Matriz

@variable(model, x[1:obj] ≥ 0)  
@variable(model, y[1:length(PTV68)] ≥ 1)

@objective(model, Min, dot(x, DRe + 0.6_DBla + 0.6_DBO))  
@constraint(model, y[1:length(PTV68)] .≥ 1)

optimize!(model)

---

<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:** [November 8, 2022, 11:24pm UTC](https://discourse.julialang.org/t/jump-in-julia/89945/5 "2022-11-08T23:24:27Z")

</div>

If you have a variable bound like

`@variable(model, y[1:length(PTV68)] ≥ 1)`

then you don’t also need the constraint

`@constraint(model, y[1:length(PTV68)] .≥ 1)`

---

<div class="post-metadata">

**Author:** ![thiagoselias](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thiagoselias/32/43916_2.png) [@thiagoselias](https://discourse.julialang.org/u/thiagoselias)\
**Post date:** [November 9, 2022, 12:31am UTC](https://discourse.julialang.org/t/jump-in-julia/89945/6 "2022-11-09T00:31:47Z")

</div>

model = Model(Ipopt.Optimizer)  
obj = length(DRe) #Quantidade de variaveis da Matriz

@variable(model, x[1:obj] ≥ 0)  
@variable(model, y[1:length(PTV68)] ≥ 1)

@objective(model, Min, dot(x, DRe + 0.6_DBla + 0.6_DBO))

optimize!(model)

When I put the restriction, it executes, but everything crashes. this img below is the constraint, it needs to be greater than 1.

you will need to create an array of 1.

 ![image](https://global.discourse-cdn.com/julialang/original/3X/8/4/841b02520bd783346648218f4a9b04d7447ce53e.png)

---

<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:** [November 9, 2022, 12:46am UTC](https://discourse.julialang.org/t/jump-in-julia/89945/7 "2022-11-09T00:46:57Z")

</div>

I’m going to gently remind you to step back and write down the formulation you want to achieve:

> [@The objective function sparse is not supported by JuMP in julia](https://discourse.julialang.org/t/the-objective-function-sparse-is-not-supported-by-jump-in-julia/89726/8):
>
> I think you need to step back and think about what you’re trying to achieve. Do you have, on a piece of paper, a mathematical formulation of your problem as a (mixed-integer) linear program? What are the decision variables? What are their bounds? What are the constraints? What is the objective function? It doesn’t make sense to minimize a vector of numbers, which is what @objective(model, min, c) is trying to do.

Have you formulated a constraint like the following?

\sum\limits\_{i \in 1 \ldots I} a\_{ij} x\_i \le b\_j \quad \forall j\in 1\ldots J

What are the sets 1\ldots I and 1\ldots J? What are the decision variables x\_i? What are the data a\_{ij} and b\_i?

> this img below is the constraint, it needs to be greater than 1.

`PTV68` is a matrix of data. It does not contain any decision variables. It is not a constraint. Forcing a matrix of data to be greater than 1 does not have a mathematical meaning.

If you’re translating this problem from MATLAB, perhaps you could show the MATLAB code you’re trying to reproduce?

---

<div class="post-metadata">

**Author:** ![thiagoselias](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thiagoselias/32/43916_2.png) [@thiagoselias](https://discourse.julialang.org/u/thiagoselias)\
**Post date:** [November 9, 2022, 10:41am UTC](https://discourse.julialang.org/t/jump-in-julia/89945/8 "2022-11-09T10:41:46Z")

</div>

The formulation you want to solve is this:

![image](https://global.discourse-cdn.com/julialang/original/3X/2/3/2347b2bf31866530944e6f37f1a0173496d5d647.png)

The MATLAB code I’m trying to reproduce for Julia is this:

![image](https://global.discourse-cdn.com/julialang/original/3X/d/9/d938856ef5a34c7833db53dd1cd2b2f04e7a43c8.png)

[x, fval, eflag] = linprog(c, PTV¨68, b,, , lb, ub, , opt);

---

<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:** [November 9, 2022, 6:41pm UTC](https://discourse.julialang.org/t/jump-in-julia/89945/9 "2022-11-09T18:41:55Z")

</div>

I see your confusion. JuMP and MATLAB are very different in how they model linear programs.

```julia
x_sol = linprog(c, A, b, [], [], lb, ub, [], opt)

# is equivalent to

using JuMP, HiGHS
model = Model(HiGHS.Optimizer)
@variable(model, lb[i] <= x[i=1:length(c)] <= ub[I])
@objective(model, Min, c' * x)
@constraint(model, A * x .<= b)
optimize!(model)
x_sol = value.(x)

```

I think you’re looking for something like

```nohighlight

using JuMP, HiGHS, LinearAlgebra
c = DRe + 0.6DBla + 0.6DBO
model = Model(HiGHS.Optimizer)
@variable(model, 0 <= x[i=1:length(c)] <= 50)
@objective(model, Min, dot(c, x))
@constraint(model, PTV68 * x .<= -1)
optimize!(model)
x_sol = value.(x)

```

---

<div class="post-metadata">

**Author:** ![thiagoselias](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thiagoselias/32/43916_2.png) [@thiagoselias](https://discourse.julialang.org/u/thiagoselias)\
**Post date:** [November 9, 2022, 9:05pm UTC](https://discourse.julialang.org/t/jump-in-julia/89945/10 "2022-11-09T21:05:42Z")

</div>

> [@odow](#):
>
> ```julia
> using JuMP, HiGHS, LinearAlgebra
> c = DRe + 0.6DBla + 0.6DBO
> model = Model(HiGHS.Optimizer)
> @variable(model, 0 <= x[i=1:length(c)] <= 50)
> @objective(model, Min, dot(c, x))
> @constraint(model, PTV68 * x .<= -1)
> optimize!(model)
> x_sol = value.(x)
> 
> ```

Thanks, it was what I was looking for
