# How to define JuMP expressions using a loop

**URL:** <https://discourse.julialang.org/t/how-to-define-jump-expressions-using-a-loop/54079>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [January 27, 2021, 8:51pm UTC](https://discourse.julialang.org/t/how-to-define-jump-expressions-using-a-loop/54079 "2021-01-27T20:51:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Micah\_Mungal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/micah_mungal/32/14660_2.png) [@Micah\_Mungal](https://discourse.julialang.org/u/Micah_Mungal)\
**Post date:** [January 27, 2021, 8:51pm UTC](https://discourse.julialang.org/t/how-to-define-jump-expressions-using-a-loop/54079/1 "2021-01-27T20:51:15Z")

</div>

Hi, I am trying to incorporate an expression into Julia using JuMP. Now I have been able to do this using MATLAB, but decided to switch to Julia for specific purposes. However based on the documentation provided for using JuMP, I am having some issues with the expressions.

I am dealing with a problem over a 24 hour period with 1hr intervals for some n battery devices. However, I need to initialize its capacity associated with this device in the first hour and then update it using and optimization variable (amount it charges and discharges by) after every hour.

So I have come up with something like this and just want to know if this makes sense:

```julia
BatteryCap = Array{Float64}(undef, nHours, n)
for j = 1:n
	for k = 1:nHours
		if k = 1
			BatteryCap[k, j] = initialCapacity[j]
		else
			BatteryCap[k, j] = @expression(model, BatteryCap[(k-1), j] + Charging[k, j] - Discharging[k, j])
		end
	end
end

```

Where Charging and Discharging are optimization variables which the solver would solve for.  
For reference purposes, this is what I used in MATLAB:

```julia
%Update the Energy Capacity of the Storage
Capacity = optimexpr(nHours,num_of_storage); 

for jj = 1:n
    for kk = 1:nHours

        if kk == 1
            
            %Initialise the capacity at Hour = 1 to 50% of Storage Capacity
            Capacity(kk,jj) = InitStorage_Capacity(1, jj);
            
        else
            
            Capacity(kk,jj) = Capacity((kk-1), jj) - (Discharging(kk, jj)) +Charging(kk, jj);
        
        end
        
    end  
end

```

---

<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:** [January 27, 2021, 10:03pm UTC](https://discourse.julialang.org/t/how-to-define-jump-expressions-using-a-loop/54079/2 "2021-01-27T22:03:48Z")

</div>

This is a reasonable way to initialize this. The only thing to change is that you should initialize `BatteryCap = Array{AffExpr,2}(undef, nHours, n)`. (Linear JuMP expressions are type `AffExpr`, not `Float64`.)

The other option is to write it as something like

```nohighlight
@expression(
    model,
    BatteryCap[k = 1:nHours, j = 1:n],
    initialCapacity[j] + sum(Charging[i, j] - Discharging[i, j] for i = 2:k)
)

```
