The emergence of Internet of Things (IoT) and 5G networks has brought forth complex challenges in traffic engineering, driven by the unprecedented scale, heterogeneity, and dynamic behavior of data flows. Conventional traffic management approaches often lack the adaptability required to cope with such rapidly evolving environments. To address these limitations, this study proposes an intelligent hierarchical traffic engineering framework that synergistically combines Particle Swarm Optimization (PSO) and Machine Learning (ML) techniques to enhance resource allocation and traffic control in IoT and 5G infrastructures. The proposed architecture operates across two coordinated layers. The lower layer implements PSO for real-time optimization of network parameters, enabling dynamic adjustment of routing paths, bandwidth allocation, and load distribution. This reactive mechanism minimizes latency and packet loss while improving throughput under fluctuating traffic conditions. The upper layer, on the other hand, utilizes ML models to analyze historical traffic data and forecast future traffic trends, enabling proactive and predictive resource management. This dual-layer integration of adaptive optimization and predictive analytics facilitates intelligent decision-making, ensuring efficient resource utilization and sustained compliance with Quality of Service (QoS) constraints. Comprehensive simulation results validate the effectiveness of the proposed framework, demonstrating significant improvements over traditional methods in terms of reduced latency, increased throughput, and lower packet loss rates. By bridging real-time responsiveness with long-term foresight, the proposed solution presents a scalable and robust approach to traffic engineering in next-generation IoT and 5G networks.