Radiological methodologies, such as chest x-rays and CT, are widely employed to help diagnose and monitor COVID-19 disease. COVID-19 displays certain radiological patterns easily detectable by X-rays of the chest. Therefore, radiologists can investigate these patterns for detecting coronavirus disease. However, this task is time-consuming and needs lots of trial and error. One of the main solutions to resolve this issue is to apply intelligent techniques such as deep learning (DL) models to automatically analyze the chest X-rays. Nevertheless, fine-tuning of architecture and hyperparameters of DL models is a complex and time-consuming procedure. In this paper, we propose an effective method to detect COVID-19 disease by applying convolutional neural network (CNN) to the chest X-ray images. To improve the accuracy of the proposed method, the last Softmax CNN layer is replaced with a -nearest neighbors (KNN) classifier which takes into account the agreement of the neighborhood labeling. Moreover, we develop a novel evolutionary algorithm by improving the basic version of competitive swarm optimizer. To this end, three powerful evolutionary operators: Cauchy Mutation (CM), Evolutionary Boundary Constraint Handling (EBCH), and tent chaotic map are incorporated into the search process of the proposed evolutionary algorithm to speed up its convergence and make an excellent balance between exploration and exploitation phases. Then, the proposed evolutionary algorithm is used to automatically achieve the optimal values of CNN’s hyperparameters leading to a significant improvement in the classification accuracy of the proposed method. Comprehensive comparative results reveal that compared with current models in the literature, the proposed method performs significantly more efficient.