A Multilevel Approach for the Optimal Control of Distributed Energy Resources and Storage