Help with JuMP

Hello
I’m trying to make the “for” compile all the variables in the model, but whenever I compile it just returns the variable “c”
Can anybody help me ?
Thank you!!

using JuMP, MathOptInterface, GLPK, GLPKMathProgInterface
ModeloD = Model(solver = GLPKSolverMIP())

a = 4
t = 4
w = 2

A = 1:a
T = 1:t
W = 1:w

NE = [14, 53]
WH = [2489 1447 3513 20914; 11073 5414 1523 16282]
CR = [11, 12]
CO = [50, 51]

var = @variable(ModeloD,c[A],Bin)
var = @variable(ModeloD,r[A,T] <=0)
var = @variable(ModeloD,wo[W,T]>=0)
var = @variable(ModeloD,wr[W,T]<=0)
var = @variable(ModeloD,er[W,T]<=0)
var = @variable(ModeloD,eo[W,T]<=0)
var = @variable(ModeloD,s[A,T]<=0)
var = @variable(ModeloD,x[A,T]<=0)

@objective(ModeloD,Min,sum(CR[w]*(wr[w,t]+wo[w,t])+CO[w]*wo[w,t] for w in W, t in T))

for w = 1:w, t= 1:t
    n= @constraint(ModeloD, er[w,t]<= NE[w])
    #println(n)
end

for w = 1:w,t= 1:t
    h = @constraint(ModeloD,eo[w,t]<=NE[w])
    #println(h)
end


for w = 1:w,t= 1:t
    ç = @constraint(ModeloD,wr[w,t]+wo[w,t]==WH[w,t])#25
    #println(ç)
end


print(ModeloD)
status = solve(ModeloD)
println("Objective value:  ", JuMP.getobjectivevalue(ModeloD))

#Compile all model variables
for w = 1:w, a = 1:a, t= 1:t
    pp = JuMP.getvalue(var)
    println(pp)
end

#The previous "for" should compile all of these variables
println("c =  ", JuMP.getvalue(c))
println("r =  ", JuMP.getvalue(r))
println("o = ", JuMP.getvalue(o))
println("wo = ", JuMP.getvalue(wo))
println("wr = ", JuMP.getvalue(wr))
println("er = ", JuMP.getvalue(er))
println("eo = ", JuMP.getvalue(eo))
println("s = ", JuMP.getvalue(s))
println("x = ", JuMP.getvalue(x))

Your code is nonsensical.

var is a name that points to some object. You create var initially pointing to variable c, then you change it to point to r, and then to wo, and so on. The value inside var at the end of your @variable block will always be the last variable x (not c) because it was the last thing you put inside var was x.

Your for loop iterates w = 1:w, a = 1:a, t= 1:t but none of these indexes are used to index var so they serve no purpose besides printing var over and over. Also, it is not good style to use the same variable as iterator and end of the range (even if it works).

Your syntax shows that you are using JuMP 0.18, as I already said before, if possible use latest version 0.21 instead.

What you seem to want is to save all variables in a structure to print later, and you can do so this way:


jump_vars = Dict{Symbol, Any}()
jump_vars[:c] = @variable(ModeloD,c[A],Bin)
jump_vars[:r] = @variable(ModeloD,r[A,T] <=0)
# ... and so on, do this for all @variable
# and then in your loop do:
for (curr_name, curr_var) in jump_vars
    println("$curr_name =  ", JuMP.getvalue(curr_var))
end

Probably there better ways to do this in Julia 0.21, maybe even in 0.18 but then I would need to re-read the old documentation.

1 Like

Hi, @Henrique_Becker
thank you so much for your help, I’m starting to program in Julia and once your suggestion worked. Thank you!

As you said, the idea really is this, to save all variables in a structure to print on a dataframe, which is another thing I’m trying to do.

I already tried to update JuMP to version 0.21 and I couldn’t.

I forgot to continue answering your question about JuMP version, I will return to it.

1 Like

Hello everybody
I would like the “for” to write the results of all variables in a DataFrame with extension for a .CSV, but when I compile the model, the “for” displays this error message
MethodError: no method matching setindex!

:resultvariablet is the name of the CSV field that I need to fill out.

for row in CSV.Rows ("file.csv")
     Result [c,: resultvariable] = JuMP.getvalue (jump_vars [:c])
     #println ("c =", JuMP.getvalue (jump_vars [: c])) #this attempt does not present an error but also does not write in the datafrema
     #print ("c = $ (JuMP.getvalue (c))") # this attempt does not present an error but also does not write in the datafrema
end

You don’t have a DataFrame, but a CSV.Rows iterator? Also, you don’t use the row which you take from the iterator in your loop?

There are also multiple syntax errors in your code - you can’t have whitespace between a function/constructor name and the opening brackets for the arguments when calling it, so CSV.Rows ("myfile.csv") errors, as you need CSV.Rows("myfile.csv")

Also, this question doesn’t have anything to do with JuMP from what I can tell, as it sounds like you’re able to extract the value you want from your model, you just don’t know how to save it into a DataFrame? In that case I would recommend opening a new topic with your specific question and a minimum reproducible example (using just CSV and DataFrames, as it appears JuMP is just a distraction here)

2 Likes