Modeling, Simulation and Optimization of Residential and Commercial Energy Systems