Assigning duties to pharmacies to serve the public on weekends, nights, and public holidays is known as the pharmacy duty scheduling problem (PDSP). In this study, a p-median model is proposed with a case-specific constraint that provides the distance restriction between on-duty pharmacies. We adapted Binary and Random-Key versions of the most recent and popular Swarm Intelligence (SI) algorithms, which are Grey Wolf Optimizer (GWO), Particle Swarm Optimization (PSO), Dragonfly Algorithm (DA), and Harris Hawks Optimization (HHO) to solve it in a reasonable amount of time since the PDSP is known to be an NP-Hard problem. We also proposed several enhancements to the algorithms and conducted computational tests on real cases generated instances using Geographic Information System (GIS) tools to compare the performances of the proposed algorithms with the state-of-the-art general-purpose solver. Two real-world datasets, called DS1 and DS2, are built considering the demand points. To validate the proposed algorithms, the CPLEX results, obtained with the exact solution for the DS1 dataset, are used. Compared to the MIP results for 800 meters distance constraint as an instance, maximum relative errors for Binary and Random-Key GWO, PSO, DA, and HHO are 0.07, 0.08, 0.08, and 0.08, respectively. The success of the algorithms is similar to the other distance criteria as well. Moreover, the results for the large DS2 dataset show that the proposed Binary GWO is more effective and offers efficient solutions compared to the other swarm intelligence algorithms and CPLEX solvers in terms of convergence to optimization and solution time.