# Energy Cost Minimization with Hybrid Energy Storage System Using Optimization Algorithm
## 2 Materials and Methods
### Implementation  
The model was implemented in Python [28] as it is one of the popular languages with many well-implemented libraries. The libraries used were: pandas (which contains the very useful DataFrame structure that allows for database-like operation on data), numpy [29] (a package that contains a wide number of data structures and functions for data analysis and scientific methods), scipy [30] (which is equipped with a set of well-known optimization methods and also methods for interpolation and statistical analysis) and datetime (library for advanced operations on date-time formats). The scheme of the software developed by the authors to run the technical and economic models, to calculate the optimization algorithm and to analyze and plot the results is illustrated in Figure 5. The model was designed to analyze the long-term (yearly) operation of the installation, and for that purpose a 15 min time-step was chosen, as it is a standard resolution of energy meters in Poland. This strictly binds the model to the data gathered in real systems. The input data for the program were:  
* The initial setup parameters, which included the general description of the microgrid parameters and date range for the simulation--the program allowed us to calculate the optimization for any data from a database or csv files. * The information regarding energy prices--for the calculation of costs and revenues, it was necessary to have the full information regarding the zones, which can change monthly, and the prices of tariffs. The program has the ability to read the prices from a _csv_ file in case there are dynamic tariffs; for the purpose of the project, the most typical Polish tariffs were implemented. * The setup of the HESS--the parameters relevant for cost calculation and optimization of each battery that constitutes HESS had to be defined. The parameters were: the capacity, the maximum power of charging and of discharging, depth-of-discharge (_DoD_), number of cycles limit (_NoC_), round-trip efficiency (_RTE_), the capex cost and the cost _ReC_ of replacing the battery unit when it reaches the end of its life. * Time series of load and generation values for the installation--the required format consisted of separate files with a timestamp and average power in a row of csv files. * The tariff profile file that consisted of a timestamp, the price for purchasing energy from the grid (or other entity in future, e.g., an aggregator) in PLN per kWh, the price  
Figure 5: The general scheme of the operation of the optimization method. for selling energy to the grid (or other) in PLN per kWh. It can represent dynamic tariffs [31] related to market or fixed peak hours tariffs. We assumed that changes can occur after a 15 min interval. The data flow of the implemented model is schematically presented in Figure 5. The first stage is the initialization of a single DataFrame type structure with all the data indexed by a timestamp. Thanks to this, it is possible to make fast operations on columns and rows without the danger of missing the time alignment of the data. The program calculates initial balances of the energy \(p_{grid}\) that result from local load \(p_{load}\) and generation \(p_{PV}\) profiles and initializes structures for energy storage operation. The output data are the input to the optimization algorithm, which sequentially calculates the battery levels. The program divides the data into 1-day-long chunks (starting from midnight) as daily patterns in usage are very often present and the production from PV modules has, by default, strong daily patterns. Many different solvers were tested, the COBYLA [32] solver was the fastest in execution, but there were small fluctuations in the result that were not explainable in context of the test case. We chose SLSQP [33] as it is based on verified methods; for the considered problem, it should be effective in finding an optimal solution--if there is one--in reasonable time, without the need for extensive adjustments of the solver's parameters.