Application of Optimization to Sizing Renewable Energy Systems and Energy Management in Microgrids