I created a JuMP optimization model that reads input from CSV files. However, I want the program to go through more than one set of input files, so that I would have the objective values and solutions to all the models. Right now I’ve put the entire model in a for loop, with the exception of a few lines, so for each loop it reads from a different set of input files and I’m getting results. But I wanted to know if this is the best way to go about this, or is there a better and more efficient way?
If the loop works for you, that’s probably best. If you haven’t already, I would wrap the inner part of the loop in a function so that it looks like this:
function build_and_solve_model(filename)
# ...
end
for file_name in ["a.csv", "b.csv"]
build_and_solve_model(file_name)
end
Yes sorry I have read that already, but my code was really long so I wasn’t sure how to summarize it, but I managed to make an MWE out of it.
#add the 4 packages that will be used
using JuMP, GLPK, CSV, DataFrames
#CSV file containing file directories (in 2 columns because 2 files per loop)
Input_Files = CSV.File("C:\\Users\\Muadh\\.atom\\Input files.csv", header = false) |> DataFrame
#arrange the data from the input CSV files into dataframes
Df1 = DataFrame.(CSV.File.(Input_Files[!,:Column1]))
Df2 = DataFrame.(CSV.File.(Input_Files[!,:Column2]))
for i = 1:size(Input_Files, 2)
#create the model
m = Model(with_optimizer(GLPK.Optimizer))
#Assign input parameters from input files, create decision variables
g = Df1[i][1,2]
#...
@objective(m, Min, π)
#create constraints and objective function
con_rev, ρ == π * g
#....
optimize!(m)
#create solution dataframe of decision variables...
#send this dataframe to a .csv file
CSV.write("Solution Table$i.csv", df)
end
I tried to shorten it as much as possible, so I apologise if it is not exactly an MWE. But I’ll do what you suggested about putting it in a function. Forgive my ignorance, but I’m curious as to what exactly are the benefits of using a function instead of just a for loop?
Also, I think the for loop in your example uses only one file per loop, if I’m not mistaken? How would I do it for n files per loop? In my case n is equal to 2.
what exactly are the benefits of using a function instead of just a for loop?
It is generally regarded as good practice. If you haven’t already, you may want to read the performance tips.
How would I do it for n files per loop? In my case n is equal to 2.
There are multiple ways:
for (file_1, file_2) in [("a_1.csv", "a_2.csv"), ("b_1.csv", "b_2.csv")]
# ...
end
for (file_1, file_2) in zip(["a_1.csv", "b_1.csv"], ["a_2.csv", "b_2.csv"])
# ...
end