# Energy Cost Minimization with Hybrid Energy Storage System Using Optimization Algorithm
## 2 Materials and Methods
### Economic Optimization  
The operation of the HESS is described by its average power in a time period \(t\) and denoted as \(p_{HESS}(t)=\sum_{i\in\{LFP,\ VRFB\}}p_{i}(t)\). The optimization happened in a time window, which had \(T\) periods. Any length of time window can be chosen, but the size of this time window correlated with the duration of the computation: the longer the total time, the longer the computation will be. As we assumed that energy comes from PV and, in general, daily cycles are observed in usage patterns, it made sense to choose the total duration of \(T\) as 24 h. To speed up the calculation of the solver, an initial solution was calculated and passed to the optimizer; this was a vector of length \(T\) of values \(p_{i}(t)\), where \(i\in\ \{LFP,\ VRFB\}\). This initial solution suggested discharging at times of high-price tariff and charging in the low-price tariff. The gain in the time of calculation was small and neglectable. The grid balance at time \(t\) was denoted by \(p_{grid}(t)\). The general rule was that the energy sent from the installation to the grid and the energy from the installation to the battery was positive; the energy taken from grid and the energy taken from the battery was negative.  
The aim was to minimize the total balance that is influenced by the battery operation; therefore, the goal function \(f_{i}\) was:  
\[f_{i}=\sum_{t=0}^{T}\left\{\begin{array}{l}p_{grid}(t)+p_{i}(t)\big{|}*c_{buy }(t)+deg_{ri}(t),\ if\ \Big{(}p_{grid}(t)+p_{i}(t)\Big{)}>0\\ p_{grid}(t)+p_{i}(t)\big{|}*c_{sell}(t)+deg_{ri}(t),\ if\ \Big{(}p_{grid}(t)+p_{i}(t) \Big{)}\leq 0\end{array}\right. \tag{3}\]  
\[degr(t)=\left\{\begin{array}{l}p_{i}(t)*DCF_{i},\ if\ x_{i}(t)<0\\ 0,\ \ \ \ \ \ if\ x_{i}(t)>0\end{array}\right. \tag{4}\]  
The goal function includes the tariffs: \(c_{buy}(t)\) is the unit cost for buying energy from the grid for the given time frame \(t\), and \(c_{sell}(t)\) is the unit profit for selling energy to the grid. As we assumed that the tariff can change in each \(t\), the main difference with peak shaving was that now it was not just the amount of energy that mattered at a time \(t\), but this amount was linked via the tariff with the time at which it occurred. This combined energy with price, allowing an optimization for the general idea of reducing the amount of energy bought during price peaks with the aim of reducing the cost.  
The constraints included the limitation for charging power:  
\[\forall t:p_{i}(t)+p_{clg,i}>0 \tag{5}\]  
where \(p_{clg,i}\) is the maximum power allowed for charging the battery.  
Symmetrically there is a limitation for discharging power:  
\[\forall t:p_{clg,i}-p_{i}(t)>0 \tag{6}\]  
where \(p_{clg,i}\) is the maximum power allowed for discharging the battery. In the examples, \(p_{clg,i}=p_{cldgb,i}=P_{max,i}\).  
Constraints of the state-of-charge (SOC) values were defined for each of the consecutive power values for energy storage at each time \(t\). For not exceeding maximum state of charge, constraints were defined as follows:  
\[\forall t:(E_{bol,i}-e_{i}(t))-\sum_{j=0}^{t}e_{i}(j)>0 \tag{7}\]  
There are T-1 such constraints for charging the energy storage as power had to be recalculated to the equivalent energy \(e(t)\) that was inserted into the battery. The charging energy was multiplied by the RTE value.  
For not exceeding minimum state of charge, constraints were defined as follows:  
\[\forall t:\bigg{(}e_{i}(t)-E_{bol,i}*\bigg{(}\frac{SOC_{min,i}}{100}\bigg{)} \bigg{)}+\sum_{j=0}^{t}e_{i}(j)>0 \tag{8}\]In this equation \(e(t)\) is the energy that is discharged or charged, in case the charging of the RTE value is considered.