# Energy Cost Minimization with Hybrid Energy Storage System Using Optimization Algorithm
## 2 Materials and Methods
### Implementation
The SLSQP optimizer is a sequential least squares programming algorithm, it applies the Han-Powell quasi-Newton method with a Broyden-Fletcher-Goldfarb-Shanno algorithm update of the B-matrix and an L1-test function in the step-length algorithm. Its implementation contains a modified version of [PERSON]'s nonlinear least-squares solver. The original source code was provided by [PERSON] in 1991, based on his work presented in [34]. The SLSQP was used in many publications in the domain of energy, e.g., [35; 36]. The convergence and properties of the SLSQP are described in [37] and [38]. The parameters of the solver in this implementation were: maximum 500 iterations and the precision goal for the value of the goal function in the stopping criterion equals 0.1. The optimization algorithm was called for each 24 h time window independently; only the final state of charge of each battery within HESS was passed to the next iteration. The outcome of the optimization was a time-series of the HESS operation--its discharge and charging power, change in the state of charge and amount of losses. Losses of the battery were defined as impacting the energy while charging--the program subtracts part of the energy that was equivalent to the \(I\)-\(RTE\). The order of calculating the energy storage was defined by the durability of the battery, which was the nominal number of cycles \(No\)C. The storage was ranked with this parameter, and the one with the highest value was considered first. This approach was chosen as a method to maximize overall durability of the HESS. When the first energy storage was calculated, the program recalculated the balances to include the energy storage impact on the installation. Then, the optimizer started with the next battery in line. Due to the nature of applied batteries it was assumed that priority would go to VRFB because of its high cycle life, low power and large capacity. The LFP battery was calculated in the second step to supplement operation of energy storage when higher power was needed. After each round, the details on the operation of each battery were saved and were also aggregated to the operation of the whole hybrid storage, which consisted of the sum of charging power, discharging power and losses. The last step was calculating the economic balance of the costs and revenues from the installation. This was calculated for two cases: (a) with HESS and (b) without HESS. On the side of the revenues was the energy that was sold to the grid, which was solely overproduction from the local energy sources. On the cost side was the energy that had to be bought from the grid, as well as the cost of the degradation of the batteries, which was calculated proportionally to the battery throughput. The analysis of the complexity was performed. We used the O-notation, as defined in [39], as it is a known standard in the defining of algorithm's complexity. In the balancing algorithm, the complexity depended only on the number of considered batteries (\(Nb\)) and number of days the calculation was performed for (\(n\)), the complexity is \(O(Nb\times n)\) as the time of calculation of the operation plan for a battery was constant. The optimization method was different--it used a solver and the number of iterations varied depending on the shape of the goal function. The worst case scenario was when there was no convergence and solver reached the maximum number of iterations, which was set to 500. Given that the maximum number of iterations was limited, this can be treated as a constant factor, and according to the O-notation this means the complexity of the operation was also \(O(Nb\times n)\). To estimate the mean time of calculation, we checked the distribution of the number of iterations--the histogram is presented in Figure 6. The number of iterations in time for both batteries is presented in Figure 7. On average, the simulation takes 186 iterations for each 24h window, per battery.