## 3 Methodology
Firstly, the power flow to each EV must respect the technical limits of both the vehicle and the charging station, meaning that  
\[\text{U}_{\text{m}}[\text{k}]\in[0,1]\ \ \ \forall\text{m}\in\text{M},\ \forall\text{k} \tag{5}\]  
Additionally, each EV must reach its desired state of charge x\({}_{\text{ref}}\) by its specified departure time E\({}_{\text{m}}\):  
\[\sum_{\text{k}=1}^{\text{E}_{\text{m}}-1}\Delta\text{P}_{\text{m}}\cdot\text{U }_{\text{m}}[\text{k}]\cdot\Delta t\geq\text{x}_{\text{ref}}-\text{x}_{0} \tag{6}\]  
where x\({}_{\text{ref}}\) is the desired SoC at departure, x\({}_{0}\) is the initial SoC at the start of charging, and \(\Delta t\) is the duration of each time step. To further ensure grid stability, the total power demand must not exceed the local grid's capacity or deviate significantly from the reference power. \[\text{P}[\text{k}]\leq\text{P}_{\text{max}}\ \ \forall\text{k} \tag{7}\]  
where P\({}_{\text{max}}\) is the maximum allowable power demand. The optimization problem is solved iteratively using an MPC algorithm, which operates over a rolling horizon. The algorithm predicts future states of the system, including grid conditions and electricity prices, and optimizes control actions accordingly. The control actions U\({}_{\text{m}}\)[k] are computed to minimize the objective function J, and only the first control action is implemented, with the rest being recalculated at the next time step as new data become available. One of the distinguishing features of this methodology is its event-driven nature. The MPC framework is designed to respond to specific events, such as the arrival of a new EV, changes in electricity tariffs, or demand-response signals from the grid operator. This allows the system to adapt in real-time, recalculating the optimal charging schedule whenever an event occurs. This adaptability is crucial for managing the dynamic nature of electricity demand and supply, particularly in grids with high penetration of renewable energy sources. Given the computational complexity associated with solving the optimization problem at each time step, several strategies are employed to reduce the computational burden. First, the prediction horizon N is kept relatively short, typically within a few hours, to limit the number of variables and constraints in the optimization problem. Second, the optimization is triggered by specific events rather than being recalculated continuously,reducing the frequency of computations. Finally, the optimization problem is decomposed into smaller sub-problems that can be solved in parallel, leveraging modern multi-core processors to expedite computation. The performance of the proposed methodology was evaluated using a simulation framework designed to replicate real-world conditions. The simulations used historical data on electricity prices, grid load, and EV usage patterns to assess the effectiveness of the MPC framework in various scenarios. These scenarios included high-demand periods, where a large number of EVs arrive at charging stations during peak hours, and environments with high penetration of renewable energy sources, such as solar and wind power. The system's flexibility was also tested by simulating different user preferences for charging times and final SoC. To evaluate the effectiveness of the proposed system, several key performance indicators (KPIs) were used. These included the total electricity cost incurred by all users, the system's ability to maintain grid stability by minimizing deviations from the reference power signal, and the level of user satisfaction, which was measured by the system's ability to meet user preferences for charging without compromising on cost or grid stability. The simulation results were validated against a baseline scenario where EVs were charged using traditional methods without optimization. This comparison highlighted the advantages of the MPC framework in terms of cost reduction, peak load mitigation, and enhanced grid stability.