The structural optimization of wireless sensor networks is a critical issue because it impacts energy consumption and hence the network's lifetime. Many studies have been conducted for homogeneous networks, but few have been performed for heterogeneous wireless sensor networks. This paper utilizes Rao algorithms to optimize the structure of heterogeneous wireless sensor networks according to node locations and their initial energies. The proposed algorithms lack algorithm-specific parameters and metaphorical connotations. The proposed algorithms examine the search space based on the relations of the population with the best, worst, and randomly assigned solutions. The proposed algorithms can be evaluated using any routing protocol, however, we have chosen the well-known routing protocols in the literature: Low Energy Adaptive Clustering Hierarchy (LEACH), Power-Efficient Gathering in Sensor Information Systems (PEAGSIS), Partitioned-based Energy-efficient LEACH (PE-LEACH), and the Power-Efficient Gathering in Sensor Information Systems Neural Network (PEAGSIS-NN) recent routing protocol. We compare our optimized method with the Jaya, the Particle Swarm Optimization-based Energy Efficient Clustering (PSO-EEC) protocol, and the hybrid Harmony Search Algorithm and PSO (HSA-PSO) algorithms. The efficiencies of our proposed algorithms are evaluated by conducting experiments in terms of the network lifetime (first dead node, half dead nodes, and last dead node), energy consumption, packets to cluster head, and packets to the base station. The experimental results were compared with those obtained using the Jaya optimization algorithm. The proposed algorithms exhibited the best performance. The proposed approach successfully prolongs the network lifetime by 71% for the PEAGSIS protocol, 51% for the LEACH protocol, 10% for the PE-LEACH protocol, and 73% for the PEGSIS-NN protocol; Moreover, it enhances other criteria such as energy conservation, fitness convergence, packets to cluster head, and packets to the base station.