Solving Two-Level Optimization Problems with Applications to Robust Design and Energy Markets