Software-defined networks (SDNs) are computer networks where parameters and devices are configured by software. Recently, artificial intelligence aspects have been used for SDN programs for various applications, including packet classification and forwarding according to the quality of service (QoS) requirements. The main problem is that when packets from different applications pass through computer networks, they have different QoS criteria. To meet the requirements of packets, routers classify these packets, add them to multiple weighting queue systems, and forward them according to their priorities. Multiple queue systems in routers usually use a class-based weighted round-robin (CBWRR) scheduling algorithm with pre-configured fixed weights for each priority queue. The problem is that the intensity of traffic in general and of each packet class occasionally changes. Therefore, in this work, we suggest using the particle swarm optimization algorithm to find the optimal weights for the weighted fair round-robin algorithm (WFRR) by considering the variable densities of the traffic. This work presents a framework to simulate router operations by determining the weights and schedule packets and forwarding them. The proposed algorithm to optimize the weights is compared with the conventional WFRR algorithm, and the results show that the particle swarm optimization for the weighted round-robin algorithm is more efficient than WFRR, especially in high-intensity traffic. Moreover, the average packet-loss ratio does not exceed 7%, and the proposed algorithms are better than the conventional CBWRR algorithm and the related work results.