Modeling the Rate of Penetration (ROP) of the drill bit is essential for optimizing drilling operations, particularly in challenging deep well environments. While traditional engineering practices focus on optimizing controllable parameters to enhance operational efficiency, this study introduces a novel real-time predictive and optimization framework aimed at significantly improving ROP. The proposed framework integrates a stacked ensemble machine learning model-comprising Support Vector Regression (SVR), Random Forest (RF), Extremely Randomized Trees (ET), Gradient Boosting Machine (GB), LightGBM, and XGBoost-with Particle Swarm Optimization (PSO) to dynamically optimize drilling parameters, such as Weight on Bit (WOB), Revolutions per Minute (RPM), and mud flow rate (Q), in real time. The stacked ensemble model leverages the diversity of base learners and employs Linear Support Vector Regression (LinearSVR) as the meta-learner to synthesize predictions, achieving superior accuracy compared to individual models. To ensure robust generalization, the model was trained using data from one well and tested on data from a neighboring well, simulating real-world conditions characterized by geological variability. Results demonstrate that the stacked ensemble achieved an exceptional R2 value of 0.9335 on the test set and enabled an 18% improvement in ROP through real-time optimization. The PSO component dynamically adjusted parameters while maintaining operational constraints, such as Equivalent Circulation Density (ECD), ensuring both safety and performance. This research is the first to combine a stacked ensemble model with PSO for real-time drilling optimization, showcasing its ability to generalize across wells, enhance drilling efficiency, and reduce operational risks. By addressing key challenges in ROP prediction and parameter optimization, this study offers a transformative solution for the oil and gas industry. Future work will focus on validating the framework across diverse geological settings and optimizing computational performance to extend its applicability.