A bi-level control architecture for the optimal operation of smart grids