Optimal control of active power flows in Smart Microgrids