# A K-nearest Neighbours Inspired Direct MPC for SOC Balancing in Smart Batteries

Francesco Simonetti, Roberta Di Fonso, Remus Teodorescu

► To cite this version:

Francesco Simonetti, Roberta Di Fonso, Remus Teodorescu. A K-nearest Neighbours Inspired Direct MPC for SOC Balancing in Smart Batteries. The 26th European Conference on Power Electronics and Applications, GDR SEEDS France & EPE Association, Mar 2025, Paris, France. 10.34746/epe2025-0209. [hal-05100715](https://HAL Id: hal-05100715)

Submitted on 6 Jun 2025

HAL is a multi-disciplinary open access
archive for the deposit and dissemination of sci-
entific research documents, whether they are pub-
lished or not. The documents may come from
teaching and research institutions in France or
abroad, or from public or private research centers.

L'archive ouverte pluridisciplinaire HAL, est
destinée au dépôt et à la diffusion de documents
scientifiques de niveau recherche, publiés ou non,
émanant des établissements d'enseignement et de
recherche français ou étrangers, des laboratoires
publics ou privés.

<figure><img src="image_1.png" /></figure>

Distributed under a Creative Commons Attribution 4.0 International License
# A K-nearest Neighbours Inspired Direct MPC for SOC Balancing in Smart Batteries

Francesco Simonetti

Roberta Di Fonso

Remus Teodorescu

Aalborg University

Aalborg University

Aalborg University

Pontoppidanstræde 111

Pontoppidanstræde 111

Pontoppidanstræde 111

Aalborg, Denmark

Aalborg, Denmark

Aalborg, Denmark

Tel.: +45 99409940.

Tel.: +45 99409940.

Tel.: +45 99409940.

E-Mail: frsi@energy.aau.dk

E-Mail: rdf@energy.aau.dk

E-Mail: ret@energy.aau.dk

URL: https://www.aau.dk

URL: https://www.aau.dk

URL: https://www.aau.dk

*Index Terms*—Battery energy storage systems, Battery Management Systems (BMS), MPC (Model-based Predictive Control), Optimization, Machine Learning.

*Abstract*—Lithium-ion batteries dominate energy storage systems. Working circumstances and parameter variations in single cells can result in state of charge imbalances that reduce the battery's lifetime. Modular batteries have been proven to be capable of actively reducing the imbalance among the cells by connecting or bypassing the individual batteries, sharing the work load in real-time. This paper presents a machine learning-inspired direct model predictive control for reconfigurable batteries to balance the cells that solves the underlying optimization problem in polynomial time.

## I. INTRODUCTION

As the reliance on battery operated devices increases, there is a need for battery systems that are high-voltage, highly efficient, and have a long lifespan [1], [2], requiring optimized battery monitoring algorithms [3]. Battery packs are constituted by the connection of single cells. These cells can differ in their capacities for many different factors, including the impact of aging mechanisms and differences in the manufacturing process [4]. Additionally, the cells may be affected by leakage currents and varying working conditions, which may result in state of charge (SOC) imbalances and varying depth of discharge (DOD) [5]. The battery lifetime is heavily affected by the SOC imbalance. Furthermore, the performance of a series connected battery pack is determined by the cell with lowest SOC. To avoid these issues, methods for balancing SOC are required [6]. The battery management system (BMS) is responsible for the functional safety of the battery pack and performs tasks such as battery monitoring, protection, operational state

estimation and optimal operation. The operation of the battery outside of the charging or discharge limits could affect both the electrochemical and thermal behavior, leading to internal soft short circuit, capacity loss, and accelerated aging [7], [8].

In order to accomplish the active balancing of the battery pack, reconfigurable batteries are an attractive and effective solution. These topologies allow for the insertion or bypass of individual cells or a group of cells in the battery pack. In this way, the battery topology can be dynamically changed and this feature can be exploited to achieve SOC balancing without the need for additional converters [9]–[12]. Model predictive control (MPC) is a feedback control algorithm that uses a model to make predictions of future output of a process. The main strengths of MPC are: (i) the ability to handle multi-input multi-output; (ii) the ability to handle constraints. The drawback is a high computational complexity, exponential in the number of control variables. Authors in [13]–[15] implemented a direct MPC strategy to control the currents and voltage of a cascaded h-bridge static synchronous compensator, applying an efficient technique to balance the DC-link voltages of the capacitors, which is a problem similar to the SOC balance from a mathematical point of view, given the modularity nature of the system under study [16]. Machine learning techniques have become extremely popular over the last several decades. Among them, the k-nearest neighbor (KNN) was effectively used for the SOC balancing of a modular battery [6], [17]. The main idea of the KNN is to find the k nearest points to a given data point by minimizing a distance metric [17]. Usually, local search is based on Euclidean distance. In [6], the authors propose to balance the state of charge by computing the Euclidean distance between the SOC reference and the individual SOC, producing an effective algorithm with low compu-
tational complexity. This work takes inspiration from the aforementioned algorithm and adapts it in a direct MPC framework approach. By changing the distance metrics of the KNN in order to encompass the prediction of the SOC in a dynamical model, it is possible to find an algorithm that solves the SOC balancing problem with direct MPC in polynomial computational time. This work presents the SOC balance problem in reconfigurable batteries. The main contributions of this paper are:

1- Development of a model of a reconfigurable battery, where the switching variables are discrete variables;

2- Formulation of a direct MPC for SOC balancing for the proposed battery model;

3- Development of an efficient algorithm for solving the direct MPC inspired by the KNN balancing algorithm present in the literature. Despite the high computational complexity of the derived discrete MPC problem, the proposed KNN inspired algorithm computes the solution in polynomial time, enabling run-time implementation of the proposed controller, making it an attractive solution for BMS, since they have limited computing capabilities. Simulations are carried out to prove the effectiveness of the proposed approach.

## II. SMART BATTERIES

Smart battery topologies are an emerging class of dynamic battery topologies. The idea behind smart battery topologies is to change dynamically the topology of the battery through a network of reconfigurable switches that connect the single battery cells of battery modules. By adding this additional degree of freedom, the active balancing capability is embedded in the battery topology. Therefore, by changing the topology while the battery is working, it is possible to achieve balancing [18]. Furthermore this class of batteries can offer additional advantages, like increasing the safety of the battery by disconnecting faulty modules for instance. Many topologies have been proposed in literature, with a variable number of switches [19]. In this work, we focus on a smart battery topology constituted by the series connection of single battery cells. Fig. 1 depicts the chosen battery structure, which is made of a switching device, and a battery cell. The switching device is a half-bridge MOSFET circuit, which connects the cell to the battery string and offers two modes of operation: inserted and bypassed. Cell-level load management is therefore enabled. To enable active balancing, the battery contains a redundant number of N cells, therefore only a subset K is needed for operation. The slave controller has the ability to track the cell's voltage, current, and temperature in addition to estimating the SOC of the cell.

<figure><img src="image_2.png" /><figcaption>Fig. 1. Modular battery schematics.</figcaption></figure>

All slaves are connected to a master controller, which handles higher level tasks like lifetime control, SOC balancing, and AI-based SOH estimation and prediction. One cell at a time is bypassed during the balancing operation to avoid impacting the load current. Additionally, the switching device adds a fault-tolerant operation mode by permanently bypassing malfunctioning cells, enhancing system safety and reliability at system level.

## III. THE BATTERY MODEL

The evolution of the state of charge of the j-th cell is given by the Coulomb Counting equation, i.e.,

$$SOC_{j}(t) = SOC_{j}(0) - \frac{1}{Q} \int_{0}^{t} i_{j}(\tau) d\tau \quad (1)$$

where Q is the maximum battery capacity and $i_j$ is the current flowing from the j-th cell. In order to have a model useful for the control, the Coulomb counting model is discretized via the forward Euler method:

$$SOC_{j}(k + 1) = SOC_{j}(k) - \frac{T_s}{Q} \cdot i_{j}(k), \quad (2)$$

where $T_s$ is the sampling time.

We can notice that the output current of the individual cell is equal to the output current of the battery when the cell is inserted and null when it is bypassed. Hence,
we can write:

$$i_j(k) = i(k) \cdot s_j(k), \qquad (3)$$

where $i(k)$ is the output current of the battery and $s_j(k)$ is the switching signal of the j-th cell at time $k$, which can either be equal to 1 (inserted) or 0 (bypassed), i.e., $s_j(k) \in \{0, 1\}$.

Therefore, the discretized model of the state of charge of the j-th cell becomes:

$$SOC_j(k + 1) = SOC_j(k) - \frac{T_s}{Q} i(k) \cdot s_j(k). \quad (4)$$

Analogously, the output voltage of the j-th cell is equal to the voltage of the battery $v_j(k)$ if the cell is inserted and equal to zero if the cell is bypassed. Hence, since the overall output voltage $v^{out}$ applied by the modular battery is given by the sum of the output voltages of the individual cells, then we can write:

$$v^{out}(k) = \sum_{j=1}^{N} v_j(k) \cdot s_j(k).$$

The dynamical model of the modular battery can be written in matrix form as:

$$\mathbf{SOC}(k + 1) = \mathbf{SOC}(k) - \frac{T_s}{Q} i(k) \cdot \mathbf{s}(k) \quad (5)$$

where $\mathbf{SOC}(k)$, $\mathbf{v}(k)$ and $\mathbf{s}(k)$ are the $N \times 1$ vectors of the state of charge, battery voltage and switching variables of the modular battery cells, i.e.,

$$\mathbf{SOC}(k) = [SOC_1(k), SOC_2(k), \dots, SOC_N(k)]^T$$

$$\mathbf{v}(k) = [v_1(k), v_2(k), \dots, v_N(k)]^T$$

$$\mathbf{s}(k) = [s_1(k), s_2(k), \dots, s_N(k)]^T,$$

and the output voltage can be expressed as:

$$v^{out}(k) = \mathbf{v}(k)^T \mathbf{s}(k).$$

## IV. DIRECT MODEL PREDICTIVE CONTROL

This work proposes a direct MPC to balance the state of charge of the battery cells, which aims to select the switching vector $\mathbf{s}(k)$ to keep the predicted values within $\mathbf{SOC}(k)$ as close as possible to its mean value while guaranteeing the overall voltage at the output terminals $v^{out}$ is equal to the desired voltage $v^{ref}$.

The mathematical formulation of the optimization problem for the SOC balance is the following:

$$\mathbf{s}^*(k) = \min_{\mathbf{s}(k)} \left\| \mathbf{SOC}^{\text{ref}} - \mathbf{SOC}(k+1) \right\|^2 \quad (9)$$

$$\text{s. t. } \mathbf{SOC}(k+1) = \mathbf{SOC}(k) \quad (10)$$

$$-\frac{T_s}{Q}i(k) \cdot \mathbf{s}(k)$$

$$\mathbf{v}(k)^T \mathbf{s}(k) = v^{ref} \quad (11)$$

$$\mathbf{s}(k) \in \{0, 1\}^N, \quad (12)$$

where $\mathbf{SOC}^{\text{ref}}$ is the $N \times 1$ vector containing the state of charge reference $\mathbf{SOC}^{\text{ref}}$ in each row, which is set equal to the mean value of the $\mathbf{SOC}$ vector.

The dynamical model in constraint (10) is employed to predict the future state of charge. The constraint (11) is added to the optimization problem to ensure that the total output voltage of the modular battery is equal to the desired output voltage $v^{ref}$.

The constraint (12) takes into account the discrete nature of the switching states of the half-bridges and the so-defined problem is an integer optimization problem. Since each switching variable $s_j(k)$ can have two different values in $\{0, 1\}$, the number of possible switching combinations is $2^N$, which makes an exhaustive search algorithm not scalable for a large number of cells because of its combinatorial explosion.

## V. PROPOSED POLYNOMIAL ALGORITHM

This paper proposes an efficient way to solve the problem in (9) inspired by the K-nearest neighbour algorithm, which is summarized by the flowchart in Fig. 2. The original algorithm in [6] was revised by substituting the Euler distance with a metric that embeds the SOC predictions of the system.

According to the described modular battery concept, each cell j can only be inserted or bypassed by setting the switching variable $s_j(k)$ either to 1 or 0. The key idea is to compute the cost $||\mathbf{SOC}^{\text{ref}} - \mathbf{SOC}(k+1)||^2$ when setting $s_j(k) = 1$ in the SOC prediction, and the cost when setting $s_j(k) = 0$. The cost increment $\Delta C_j(k)$ is defined as the difference between the two costs, which is the new metric to use in the KNN algorithm.

By computing the cost increments of each cell, it is possible to define the array of costs increments $\Delta \mathbf{C}(k)$. By scanning the array, we can evaluate which are the cells that lead to a higher increment in the overall cost function and which are the ones that provides a minimum increment.

Hence, the array is sorted in ascending order. By selecting the first K elements of the sorted array such that the
**Algorithm 1 SOC balancing.**

1: **for** j ← 1 to N **do**

$$SOC_j^1(k+1) \leftarrow SOC_j(k) - \frac{T_s}{Q} (i(k) \cdot 1) \qquad (6)$$

$$SOC_j^0(k+1) \leftarrow SOC_j(k) - \frac{T_s}{Q} (i(k) \cdot 0) = SOC_j(k) \qquad (7)$$

2: **end for**

3: **for** j ← 1 to N **do**

$$\Delta C_j(k) \leftarrow \left\| SOC^{ref} - SOC_j^1(k+1) \right\|^2 - \left\| SOC^{ref} - SOC_j^0(k+1) \right\|^2 \qquad (8)$$

4: **end for**

5: **I** ← sort($\Delta C(k)$)

6: j ← 1

7: **while** $v^{tot} < v^{ref}$ & j < N **do**

8: $v^{tot} \leftarrow v^{tot} + v_j(k)$

9: $s_j(k) \leftarrow 1$

10: j ← j + 1

11: **end while**

overall output voltage $v^{out}$ is equal to the desired voltage $v^{ref}$, the constraint in (11) is fulfilled and the solution of the optimization problem is computed. The procedure is summarized in Algorithm 1, and the corresponding flowchart is shown in Fig. 2, where the modification with respect to the KNN approach is highlighted. The

dimension array, and performing a sorting algorithm, which is $O(N \cdot log(N))$ for algorithms such as the merge-sort and bubble-sort, or, in the worst case, $O(N^2)$ for simpler algorithms such as the selection sort. Hence, the overall computational complexity is given by the chosen sorting strategy, which is, in general, a polynomial complexity algorithm. In this work, for simplicity, we propose using of a selection sort, which makes the implementation straightforward and the overall procedure complexity $O(N^2)$.

## VI. RESULTS

### A. Simulation Model

In this work, we used a battery model from the Matlab Simulink SimPowerSystems library. This model can describe the electrochemical behaviour the battery in terms of dynamic voltage, open circuit voltage, internal resistance, charge and discharge current and SOC.

The battery discharge voltage is given by:

$$v_j = E_0 - K \frac{Q}{Q - i_j \cdot t} \cdot i_j \cdot t + K \frac{Q}{Q - i_j \cdot t} \cdot i_j^* + A e^{-B \cdot i_j \cdot t} \qquad (13)$$

where:

$v_j$ = battery voltage (V)

$E_0$ = battery constant voltage (V)

$K$ =polarization resistance (Ω)

$Q$ = battery capacity (Ah)

$i_j \cdot t = \int i_j dt$ = actual battery charge (Ah)

<figure><img src="image_3.png" /><figcaption>Fig. 2. Flowchart of the MPC control.</figcaption></figure>

algorithm requires computing N costs, scanning a N-
<figure><img src="image_4.png" /><figcaption>Fig. 3. 48 Ah LiNiO2 Battery cell discharge characteristics.</figcaption></figure>

<figure><img src="image_5.png" /><figcaption>Fig. 4. 48 Ah Zoom of the Battery exponential area.</figcaption></figure>

$A$= exponential zone amplitude (V)

$B$= exponential zone time current inverse (Ah)$^{-1}$

$i_j$= battery current (A)

$i_j^*$= current flowing through K (A)

The model parameters are derived from the discharge characteristics of a 48 Ah LiNiO2 batteries. Table I summarizes the battery parameters.

<table><caption>TABLE I
48 Ah LiNiO2 BATTERY CELL PARAMETERS</caption><thead><tr><th colspan="2">Battery cell parameters</th></tr></thead><tbody><tr><td>E<sub>0</sub></td><td>3.95 V</td></tr><tr><td>R</td><td>0.6 mΩ</td></tr><tr><td>K</td><td>1.37 mΩ</td></tr><tr><td>A</td><td>0.06 Ah</td></tr><tr><td>B</td><td>0.789 (Ah)<sup>-1</sup></td></tr></tbody></table>

Fig. 3 shows the discharge characteristics used to derive the battery parameters. Fig. 4 shows a zoomed in view of the battery exponential area.

The battery pack is simulated by 25 lithium-ion cell modules connected in series, obtaining a 4.32 kWh battery.

## B. Simulation Results

To prove the effectiveness of the proposed control, a simulation with a load of 1.8 Ω was carried out. The control algorithm connected the appropriate number of cells in series at each sample period so that the average output voltage was $v^{out} = 60$ V. The initial SOC of the cells was set in a range between 70 % and 90 %. Fig.

5 shows the SOC of the cells with the proposed direct MPC. The cell selection policy of the optimal controller led to a SOC balance of the overall pack.

<figure><img src="image_6.png" /><figcaption>Fig. 5. SOC balance of the cells with direct MPC.</figcaption></figure>

## VII. CONCLUSIONS

Smart battery topologies offer modularity and re-configurability, allowing to independently insert/bypass single cells in a battery string. This work proposes a direct MPC control to achieve optimal SOC balancing. Given the high computational complexity of the derived discrete optimization problem, an efficient algorithm for solving the direct MPC inspired by the K-nearest neighbours balancing algorithm present in the literature is developed. By exploiting the features of the K-nearest neighbours solution, it is possible to derive an algorithm of polynomial complexity. This is an attractive feature for a balancing algorithm that has to be implemented on BMS, since they have limited computing capabilities. Simulation results show that the cell selection strategy can lead to SOC balance of the battery pack. This operation has the potential to extend the battery lifetime. Comparison analysis with literature balancing algorithm will be provided in the final version.
REFERENCES

[1] G. Bossi, A. Floris, N. Campagna, R. Miceli, and A. Damiano, "Design of a battery pack for the power system of an all-electric aircraft by cell characterization," in *2023 International Conference on Clean Electrical Power (ICCEP)*, 2023, pp. 76-82.

[2] G. Bossi, A. Damiano, N. Campagna, V. Castiglia, R. Miceli, and A. O. Di Tommaso, "A hybrid storage systems for all electric aircraft," in *2021 IEEE 15th International Conference on Compatibility, Power Electronics and Power Engineering (CPE-POWERENG)*, 2021, pp. 1-6.

[3] R. D. Fonso, R. Teodorescu, C. Cecati, and P. Bharadwaj, "A battery digital twin from laboratory data using wavelet analysis and neural networks," *IEEE Transactions on Industrial Informatics*, vol. 20, no. 4, pp. 6889-6899, 2024.

[4] M. Ricco, J. Meng, T. Gherman, G. Grandi, and R. Teodorescu, "Smart battery pack for electric vehicles based on active balancing with wireless communication feedback," *Energies*, vol. 12, no. 20, 2019.

[5] F. Altaf, "Thermal and state-of-charge balancing of batteries using multilevel converters," Ph.D. dissertation, Mar. 2014.

[6] R. Di Fonso, X. Sui, A. B. Acharya, R. Teodorescu, and C. Cecati, "Multidimensional machine learning balancing in smart battery packs," in *IECON 2021 – 47th Annual Conference of the IEEE Industrial Electronics Society*, 2021, pp. 1-6.

[7] C. F. Lopez, J. A. Jeevarajan, and P. P. Mukherjee, "Experimental analysis of thermal runaway and propagation in lithium-ion battery modules," *Journal of The Electrochemical Society*, vol. 162, no. 9, p. A1905, Jul 2015. [Online]. Available: https://dx.doi.org/10.1149/2.0921509jes

[8] L. B. Diaz et al., "Meta-review of fire safety of lithium-ion batteries: Industry challenges and research contributions," *Optica*, vol. 167, 2020.

[9] M. Dezhbord, M. Babalou, V. Marzang, P. Alavi, S. H. Hosseini, and S. Mohammadi, "A new high step-up three-port dc-dc structure for hybrid pv/battery energy systems," in *2019 10th International Power Electronics, Drive Systems and Technologies Conference (PEDSTC)*, 2019, pp. 509-514.

[10] M. Babalou, H. Torkaman, E. Pouresmaeil, and M. Dezhbord, "Dual-transformer-based dab converter with asymmetrical power transmission and high-power density for electric vehicles," *IEEE Journal of Emerging and Selected Topics in Power Electronics*, vol. 12, no. 2, pp. 1742-1753, 2024.

[11] M. Eskandarpour Azizkandi, F. Sedaghati, H. Shayeghi, and M. Dezhbord, "A single-switch high step-up dc-dc converter based on integrating coupled inductor and voltage multiplier cell for renewable energy applications," in *2019 10th International Power Electronics, Drive Systems and Technologies Conference (PEDSTC)*, 2019, pp. 527-532.

[12] R. Di Fonso and C. Cecati, "Test cycle simulation of an electric car with regenerative braking," in *2020 AEIT International Conference of Electrical and Electronic Technologies for Automotive (AEIT AUTOMOTIVE)*, 2020, pp. 1-5.

[13] F. Simonetti, A. D'Innocenzo, and C. Cecati, "Neural network model-predictive control for chb converters with fpga implementation," *IEEE Transactions on Industrial Informatics*, vol. 19, no. 9, pp. 9691-9702, 2023.

[14] F. Simonetti, A. D'Innocenzo, and C. Cecati, "Simple explicit solution of finite control set model predictive control for cascaded h-bridge inverters," *IEEE Transactions on Industrial Electronics*, pp. 1-11, 2023.

[15] F. Simonetti, S. Mohamadian, C. Buccella, and C. Cecati, "Dc current suppression in chb-statcom with model predictive control employing current transformers," *IEEE Transactions on Industrial Electronics*, pp. 1-11, 2024.

[16] S. Mohamadian, M. Modarres, F. Simonetti, and C. Cecati, "Modeling of switching power losses in cascaded h-bridges with unipolar pwm," *IEEE Journal of Emerging and Selected Topics in Power Electronics*, vol. 11, no. 3, pp. 3270-3280, 2023.

[17] M. Papanikolaou, G. Evangelidis, and S. Ougiaroglou, "Dynamic k determination in k-nn classifier: A literature review," in *2021 12th International Conference on Information, Intelligence, Systems Applications (IISA)*, 2021, pp. 1-8.

[18] Y. Weng and C. Ababei, "Ai-assisted reconfiguration of battery packs for cell balancing to extend driving runtime," *Journal of Energy Storage*, vol. 84, p. 110853, 2024.

[19] W. Han, T. Wik, A. Kersten, G. Dong, and C. Zou, "Next-generation battery management systems: Dynamic reconfiguration," *IEEE Industrial Electronics Magazine*, vol. 14, no. 4, pp. 20-31, 2020.