This code reads a CSV file into df (which is where you get the error mesage — it may not be a valid CSV file), and then overwrites this value with JuMP.value.(N[1]).
If you want to export data to CSV, you probably need CSV.write.
Yes, JuMP.value returns the value that was found by the solver.
I know it’s not in MWE format, but maybe with the full code it gets better.
I would like the N build response to be exported to the CSV I created.
using JuMP, Cbc, DataFrames, CSV
model = Model(with_optimizer(Cbc.Optimizer))
P = [12;60]
M = [0.25 0.1 0.1; 0.5 0.75 0.4]
D = [36; 22; 15]
@variable(model,N[1:2], lower_bound=0)
@objective(model, Max, sum(N[i]*P[i] for i ∈ 1:2))
Pp= transpose(P)
for i ∈ 1:3
@constraint(model, Pp*M[2*i-2+1:2*i]<= D[i])
end
print(model)
optimize!(model) #model status
println("Objective value: ", JuMP.objective_value(model))
println("N = ", JuMP.value.(N))
Result= CSV.write("\\File.csv")
for i ∈ 1:2
Result = JuMP.value.(N[1])
end
In general, you can’t appned columns to a CSV, only rows. If you are tryign to write rows to a CSV you can do
CSV.write("filename.csv", df, append=true)
I believe that for this to work df needs to be a dataframe (or other table) with the same format as what already exists in the CSV. You could for example do DataFrame(value.(N)') but this would give you the default column names :x1, :x2.
I of course don’t know the context of what you’re doing, but I suggest considering a more appropriate format for numerical data such as HDF5.jl.
I followed your suggestion, and did not reach my goal.
With this representation maybe I can better explain what I need. First I created an empty DataFrame, just with the field called QtdProduction then I made a for so that the DataFrame was only filled in the QtdProduction field with the compilation results of my model, but it’s giving error.
Results = CSV.read ("file.csv")
for i ∈ 1: 3
Results [1,: QtdAProduction] = JuMP.value. (N [i])
end
CSV.write ("file.csv", Results, append = true)
Please post the error when you post example code. it makes it easier to help you.
It seems like you aren’t using your loop variable i in your loop. Additionally, : QtdAProduction is not valid Julia code. You need :QtdAProduction instead.
Results = CSV.read ("file.csv")
for i ∈ 1: 3
Results [i,:QtdAProduction] = JuMP.value. (N[i])
end
CSV.write ("file.csv", Results, append = true)
: QtdAProduction is the name of the field in CSV that I need to fill in.
This is the error that appears in the for.
ArgumentError: CSV.Column is read-only; to get a mutable vector from copy (col) or to make all columns mutable from CSV.read (file; copycols = true) or CSV.File (file) |> DataFrame
You should make the change suggested by the error message. For speed, CSV.jl chooses not to make mutable copies of the vectors in a DataFrame.
Once you make that change, also remember to fix the spaces in your Symbol commands. The way you are using spaces in your code is not valid Julia syntax and will cause your code to break.
I made the adjustments and the code was like this:
println("N = ",JuMP.value.(N))
Results = CSV.read("file.csv";copycols=true)#I would like .CSV were filled with the answer that println()
for i ∈ 1:3
copy(Results)[i,:QtdAProduzir] = JuMP.value.(N[i])
end
CSV.write("file.csv",Results,append = true)
But it is not yet possible to populate .CSV with Julia’s compile result
The code is adding to the .CSV the fields that were already filled, that is, duplicating the information, when it should include the empty fields of the results obtained with println().
println("N = ",JuMP.value.(N))
Results = CSV.read("file.csv";copycols=true)
for i ∈ 1:3
copy(JuMP.value.(N[i]))
end
CSV.write("file.csv",Results,append=true)