JuMP in julia

Link:
https://gist.githubusercontent.com/thiagoselias/65dd09e675493443493b394bcc136784/raw/c81339907844c6bfb296353ed22269706d46b497/minimizacao.jl

I have this problem

But specifically in the constraint

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

When I run with the constraint I have this error:

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

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

You declare y as

@variable(model, y)

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

Did you mean

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

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

@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:

@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:

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

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.6DBla + 0.6DBO))
@constraint(model, y[1:length(PTV68)] .≥ 1)

optimize!(model)

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)

1 Like

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.6DBla + 0.6DBO))

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.

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

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?

1 Like

The formulation you want to solve is this:

image

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

image

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

1 Like

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

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


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)
1 Like

Thanks, it was what I was looking for

1 Like