Optimal Planning Of Advanced Microgrids With An Energy Management System