## 3 Methodology  
This study proposes a methodology for optimizing the charging of electric vehicles (EVs) within a demand-side management (DSM) framework, leveraging model predictive control (MPC) to dynamically adjust the charging process based on real-time grid conditions, electricity prices, and user preferences. The methodology is structured around a centralized control system, the charging station control center (CSCC), which acts as the central hub for data collection, optimization, and control distribution. The CSCC is designed to manage a network of charging stations (CSs), each capable of communicating with the CSCC to provide real-time data on the state of charge (SoC) of connected EVs, power draw, and user preferences such as desired departure time and final SoC. Additionally, the CSCC interfaces with the grid operator, receiving critical data on current grid load, electricity prices, and any demand response (DR) signals. This information is used to predict future grid conditions and optimize the charging schedule for all connected EVs, aiming to minimize the total cost of charging for users while maintaining grid stability. The optimization problem at the heart of this methodology is formulated as a discrete-time optimal control problem. The objective function J that the system seeks to minimize is a combination of the electricity cost and a regulation term that penalizes deviations from a desired power reference. This function is mathematically expressed as  
\[\mathrm{J}=\mathrm{J}_{\mathrm{cost}}+\mathrm{\mu J}_{\mathrm{reg}} \tag{1}\]  
where \(\mathrm{J}_{\mathrm{cost}}\) represents the total cost of electricity for charging the EVs, \(\mathrm{J}_{\mathrm{reg}}\) represents the regulation term to maintain grid stability, and \(\mathrm{\mu}\) is a weighting factor that balances the trade-off between cost minimization and grid stability. The cost component \(\mathrm{J}_{\mathrm{cost}}\) is calculated based on the power flow to each EV and the electricity price at each time step, defined as:  
\[\mathrm{J}_{\mathrm{cost}}=\sum_{\mathrm{m}\in\mathrm{M}}\sum_{k=1}^{\mathrm{ E}_{\mathrm{m}}-1}\Delta\mathrm{P}_{\mathrm{m}}\cdot\mathrm{C}[k]\cdot\mathrm{U}_{ \mathrm{m}}[k] \tag{2}\]In this equation, M denotes the set of all EVs connected to the charging stations, k represents the time step, E\({}_{\text{m}}\) is the departure time of EV m, \(\Delta\text{P}_{\text{m}}\) is the maximum power flow to EV m, C[k] is the electricity price at time step k, and U\({}_{\text{m}}\)[k] is the control variable, representing the normalized power flow to EV m at time step k.  
To maintain grid stability, the regulation term J\({}_{\text{reg}}\) penalizes deviations from a power reference P\({}_{\text{ref}}\)[k], which is set according to grid stability requirements:  
\[\text{J}_{\text{reg}}=\sum_{\text{k}=1}^{\text{K}}\Lambda[\text{k}]\cdot\| \text{P}[\text{k}]-\text{P}_{\text{ref}}[\text{k}]\|_{\infty} \tag{3}\]  
Here, \(\Lambda[\text{k}]\) is a diagonal matrix that weights the tracking error at time step k, P[k] is the total power demand at time step k, and \(\|\text{P}[\text{k}]-\text{P}_{\text{ref}}[\text{k}]\|_{\infty}\) denotes the infinity norm, capturing the maximum deviation across all time steps. The total power demand P[k] at any time step k is determined by the sum of power consumed by non-flexible loads P\({}_{\text{s}}\)[k] and the power drawn by the flexible EV loads:  
\[\text{P}[\text{k}]=\text{P}_{\text{s}}[\text{k}]\ -\sum_{\text{m}\in\text{M}_{ \text{k}}}\Delta\text{P}_{\text{m}}\cdot\text{U}_{\text{m}}[\text{k}] \tag{4}\]  
This setup imposes several constraints to ensure the feasibility of the solution and the stability of the system.