Hi I am migrating a lot of code over from PuLP to JuMP and just wanted to suggest that Julia put in a splitDict() function which is useful for Network Flow optimization work. I’ve added something that works below.
function splitDict(Data::Dict)
maxitems = maximum([length(values) for values in values(stuff)])
output = [Dict() for i in range(start=1,stop=maxitems,step=1)]
for (key, values) in stuff
for (i, val) in enumerate(values)
output[i][key] = val
end
end
return Tuple(output)
end
The general approach in Julia (and JuMP) has been not to add ‘simple’ functions that work for a particular niche use-case. Instead, you are encouraged to write (and use) your own helper functions.
Here’s how I would write your function:
julia> function split_dict(data::Dict{K,V}) where {K,V<:AbstractVector}
return [
Dict(k => v[i] for (k, v) in data if length(v) >= i)
for i in 1:maximum(length.(values(data)))
]
end
split_dict (generic function with 2 methods)
julia> data = Dict(
:a => [1, 2, 3],
:b => [4, 5]
)
Dict{Symbol, Vector{Int64}} with 2 entries:
:a => [1, 2, 3]
:b => [4, 5]
julia> split_dict(data)
3-element Vector{Dict{Symbol, Int64}}:
Dict(:a => 1, :b => 4)
Dict(:a => 2, :b => 5)
Dict(:a => 3)
Another thing to keep in mind is that the data structures you used in Python and in PuLP are not necessarily the best data structures to use for JuMP. If you find yourself having to reshape the data like this, it suggests that there might be a better data structure to use in the first place.
If you get stuck along the way, you can post a reproducible example of your PuLP model and what you’ve tried in JuMP, and we can hopefully point you in the right direction for improvements.