Optimal Design And Regulation of Residential Distributed Energy Systems