We aim to simulate an optimal optical biosensor based on one-dimensional crystal photonics, for measuring blood and urine glucose concentration. By optimizing the sensor structure through metaheuristic optimization algorithms, sensitivity was increased. To measure blood and urine glucose concentration, these materials are used as a defect layer in one-dimensional crystal photonics, consisting of three materials: magnesium fluoride (MgF2), borosilicate glass (BK7), and orphan iodide (LiI) with refractive indices of 37/ 1, 1/5, and 1/99. By changing the concentration of glucose, the refractive index of the defect layer changes, changing the optical properties of the defect layer in the photonic crystal and the spectrum of transmitted and reflected light. According to the amount of light absorption by glucose, a wavelength range of 900-2200 nm (near infrared) was used as the input light. The transfer matrix method was used to calculate multi-layer systems. This method is based on the definition of two matrices, the boundary matrix and the diffusion matrix, which can be used to directly apply the boundary conditions. By plotting the spectrum passing through the crystal using the transfer matrix method and determining the location of the peak in the spectrum, the sensitivity of the sensor was calculated for different concentrations of glucose in blood and urine. The sensitivity obtained before optimization was 530 nm/RIU, while after optimization it reached 842 nm/RIU