Help with exporting data to .CSV

Hello everybody!

I am trying to export only the numeric data of the result of the variable r to a .CSV. The model does not give an error, but I cannot export the data as I need it.

I would like the data to be written in .CSV this way


│ Row │ value │
│     │ Int64 │
├─────┼───────┤
│ 1   │ 0     │
│ 2   │ 0     │
│ 3   │ 0     │
│ 4   │ 0     │ 

However it is written this way for .CSV

│ Row │ value           │
│     │ String          │
├─────┼─────────────────┤
│ 1   │ r:2 dimensions: │

Why can’t I export the data the way I want?

As the model is, it may not make much sense because it is a cut out of a model that I am building.
Thank you!

using JuMP, GLPK, CSV, DataFrames, GLPKMathProgInterface
Modeltest = Model(solver = GLPKSolverMIP())

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

#Parameters
CR = [11, 12]
CO =[50, 51]
WA = [1, 2, 1, 2]
RHE = [150, 151]
#Variables
@variable(Modeltest,r[A,T]>=0)
@variable(Modeltest,o[A,T]>=0)
@variable(Modeltest,wr[W,T]>=0)
@variable(Modeltest,er[W,T]>=0)

@objective(Modeltest,Min,sum(CR[WA[a]]*(r[a,t]+o[a,t])+CO[WA[a]]*o[a,t] for a in A, t in T))

for w = 1:2, t= 1:4
    m = @constraint(Modeltest,sum(r[a,t] for a in A) +wr[w,t] ==RHE[w])
    #println(m)
end

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

println("r =  ", JuMP.getvalue(r))

valueresult = DataFrame(value=JuMP.getvalue(r))
CSV.write("C:\\Users\\raquel.santos\\Desktop\\testemodel.csv",valueresult)#CSV file path, file that has only one field called "value"

r is a 4x4 matrix, so it doesn’t make sense to try and store it as a single column in a data frame.

If you skip CSV, you can do something like:

open("test_model.csv", "w") do io
    for a in A
        println(io, join([getvalue(r[a, t]) for t in T], ", "))
    end
end

Again, I would encourage you to upgrade to the latest version of JuMP.

1 Like