Wireless Sensor Networks: Optimizing Energy Consumption and Minimizing Localization Error