Cyber-Physical Systems (CPS), particularly in the form of the Internet of Medical Things (IoMT), are transforming modern healthcare through real-time data exchange, remote monitoring, and intelligent automation. However, their high interconnectivity makes them vulnerable to sophisticated cyberattacks, notably ransomware, which can encrypt sensitive patient data or disrupt critical medical services. This study proposes an intelligent attack detection framework combining Capsule Neural Networks (CapsNet) with a Dynamic Population Control based Polar Bear Optimization Algorithm (DPCPBOA). To enhance feature relevance and reduce dimensional complexity, Mutual Information-based Feature Selection (MIFS) is applied prior to the deep learning phase. This data mining technique quantifies the amount of information each feature contributes to ransomware detection, allowing the system to retain only the most significant input variables. CapsNet effectively captures spatial hierarchies and part-whole relationships within complex data, improving the identification of subtle ransomware behaviors. Meanwhile, APBO enhances learning by dynamically tuning model parameters based on convergence behavior, promoting faster optimization and adaptability to evolving threats. Evaluated on large-scale CPS dataset TON_IoT Train_Test Network the proposed framework demonstrates superior performance in ransomware detection accuracy, false positive reduction, and response time, making it well-suited for real-time deployment in IoMT environments.