# Pushing into a dictionary by accumulating the values of JumpVariables

**URL:** https://discourse.julialang.org/t/pushing-into-a-dictionary-by-accumulating-the-values-of-jumpvariables/10952
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [May 17, 2018, 10:59am UTC](https://discourse.julialang.org/t/pushing-into-a-dictionary-by-accumulating-the-values-of-jumpvariables/10952 "2018-05-17T10:59:49Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jayce\_ram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayce_ram/32/3977_2.png) [@jayce\_ram](https://discourse.julialang.org/u/jayce_ram)
#### Post date: [May 17, 2018, 10:59am UTC](https://discourse.julialang.org/t/pushing-into-a-dictionary-by-accumulating-the-values-of-jumpvariables/10952/1 "2018-05-17T10:59:49Z")

</div>

I have been using Jump to model a LP. The variable in my problem can be represented as follow:

`x_prod[year, hour, site, product] `

In my obj function I need something like that, for all the sites existing

```julia
sum(annual_prod[year, site] * cost[year,site] )

```

where :

```julia
annual_prod[year, site] = sum(x_prod[year, hour, site, product])

```

That means, I need to get the annual production by site. In order to get this value,  
I’ve tried to use the object Accumulator from DataStrucutre like that :

```julia
annual_prod= Accumulator(Array,Float64)
for y in years  
        for t in timeslice
            for site in list_available_site[y]
                for p in prod_by_site[site]
                    push!(annual_prod,[y,site],x_prod[y,t,site,p])
                end
            end
        end
    end
end

```

That didn’t work at all because it isn’t defined for JumpVariables. I tried to get the values of the variables( that are float) it doesn’t work neither because the model has to be solved before using the values.  
So, I’ve tried to create a Dictionary to accumulate these values but I haven’t found a way to push them by accumulating themselves into their respective key. Anyone knows if it is possible to do it ? This dumb code shows what I would like to be able to do:

```julia
for y in years 
        for t in timeslice
            for site in list_available_site[y]
               for p in prod_by_site[site]
                    annual_prod[y,site]= accumulate(+,x_prod[y,t,site,p])
                end
            end
        end
end

```

---

<div class="post-metadata">

### Author: ![jayce\_ram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayce_ram/32/3977_2.png) [@jayce\_ram](https://discourse.julialang.org/u/jayce_ram)
#### Post date: [May 17, 2018, 2:02pm UTC](https://discourse.julialang.org/t/pushing-into-a-dictionary-by-accumulating-the-values-of-jumpvariables/10952/2 "2018-05-17T14:02:59Z")

</div>

I have solved the problem. If it matters for anyone, inside my nested loops, I created an expression to receive the accumulation of my variables.

```julia
exp[y,site] = @expression(model,x_prod[y,t,site,p] + exp[y,site])

```

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [May 17, 2018, 9:34pm UTC](https://discourse.julialang.org/t/pushing-into-a-dictionary-by-accumulating-the-values-of-jumpvariables/10952/3 "2018-05-17T21:34:13Z")

</div>

You probably want to use this instead:

```julia
@expression(model, annual_prod[y=years, site=list_available_site[y] ],
    sum(x_prod[y, t, site, p] for t in timeslice, p in prob_by_site[site])
)

```

This will create some JuMP expressions that you can used in other constraints/objective, e.g.,

```julia
@constraint(model, prod[y1, site1] >= 100)

```

---

<div class="post-metadata">

### Author: ![jayce\_ram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jayce_ram/32/3977_2.png) [@jayce\_ram](https://discourse.julialang.org/u/jayce_ram)
#### Post date: [May 18, 2018, 9:03am UTC](https://discourse.julialang.org/t/pushing-into-a-dictionary-by-accumulating-the-values-of-jumpvariables/10952/4 "2018-05-18T09:03:29Z")

</div>

that’s true! it is much more elegant! thanks.
