# Trouble with nested summations in objective funtion

**URL:** https://discourse.julialang.org/t/trouble-with-nested-summations-in-objective-funtion/27215
**Category:** Optimization (Mathematical)
**Tags:** question
**Created:** [August 6, 2019, 12:12pm UTC](https://discourse.julialang.org/t/trouble-with-nested-summations-in-objective-funtion/27215 "2019-08-06T12:12:10Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Natedogg373](https://avatars.discourse-cdn.com/v4/letter/n/da6949/32.png) [@Natedogg373](https://discourse.julialang.org/u/Natedogg373)
#### Post date: [August 6, 2019, 12:12pm UTC](https://discourse.julialang.org/t/trouble-with-nested-summations-in-objective-funtion/27215/1 "2019-08-06T12:12:10Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [August 6, 2019, 12:43pm UTC](https://discourse.julialang.org/t/trouble-with-nested-summations-in-objective-funtion/27215/2 "2019-08-06T12:43:42Z")

</div>

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](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you))

```julia
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.

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [August 6, 2019, 12:56pm UTC](https://discourse.julialang.org/t/trouble-with-nested-summations-in-objective-funtion/27215/3 "2019-08-06T12:56:27Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

### Author: ![Natedogg373](https://avatars.discourse-cdn.com/v4/letter/n/da6949/32.png) [@Natedogg373](https://discourse.julialang.org/u/Natedogg373)
#### Post date: [August 6, 2019, 1:22pm UTC](https://discourse.julialang.org/t/trouble-with-nested-summations-in-objective-funtion/27215/4 "2019-08-06T13:22:02Z")

</div>

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.
