Pushing into a dictionary by accumulating the values of JumpVariables

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

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

where :

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 :

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:

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

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.

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

You probably want to use this instead:

@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.,

@constraint(model, prod[y1, site1] >= 100)
1 Like

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