A bi-level approach for the management of microgrids