Trouble with nested summations in objective funtion

Hello,

I’m a beginner with julia and working on a MIP optimization problem and I’ve been having trouble finishing my objective. Below is the portion of the objective I have traced the error to.

sum(sum(3*t[i,j,l]+v1[i,j,l] for l in 1:days)*from_site_dist[i,j] for i in 1:sites for j in 1:stores)

The goal is to sum all the t and v1 int variables over 1:7 days, with respect to the route from site i to store j. Julia is telling me I’m indexing wrong but I can’t seem to figure out what the proper way to go about this is. Any help would be greatly appreciated.

According to your line of code I tried the following MWE (minimal working example, see also here: https://discourse.julialang.org/t/psa-make-it-easier-to-help-you)

julia> days=7
julia> sites=5
julia> stores=12
julia> t=rand(Int,sites,stores,days);
julia> v1=rand(Int,sites,stores,days);
julia> from_site_dist=rand(Int,sites,stores);
julia> sum(sum(3*t[i,j,l]+v1[i,j,l] for l in 1:days)*from_site_dist[i,j] for i in 1:sites for j in 1:stores)
2869535901568287053

It seems to work, so your arrays are not of the dimensions you are trying to index.

1 Like

You can check for your arrays, the following should all give true:

julia> size(t)[3]==size(v1)[3]==days
true

julia> size(t)[2]==size(v1)[2]==size(from_site_dist)[2]==stores
true

julia> size(t)[1]==size(v1)[1]==size(from_site_dist)[1]==sites
true

Should have used this MWE strategy from the start, I’ll make sure to start implementing it in my debugging. This helped me fix the issue, thanks.