A Multilevel Approach for the Optimal Control of Energy Systems Distributed over the Territory