import%20marimo%0A%0A__generated_with%20%3D%20%220.23.16%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Renewable%20Capacity%20Planning%20with%20German%20Grid%20Data%0A%0A%20%20%20%20We%20consider%20renewable%20capacity%20planning%20in%20a%20power%20system%2C%20where%20a%0A%20%20%20%20generation%20planner%20must%20choose%20renewable%20capacity%20before%20an%20electricity%0A%20%20%20%20dispatcher%20sees%20the%20resulting%20operating%20limits%20and%20optimizes%20the%20dispatch%0A%20%20%20%20accordingly.%20The%20planner%20anticipates%20that%20response%20and%20trades%20investment%0A%20%20%20%20cost%20against%20the%20thermal%20generation%20that%20remains%20in%20the%20dispatch.%0A%0A%20%20%20%20The%20hourly%20demand%20and%20renewable-availability%20profile%20comes%20from%20observed%0A%20%20%20%20German%20grid%20data%20for%2021%20June%202019.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20csv%0A%20%20%20%20from%20datetime%20import%20datetime%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20cvxpy%20as%20cp%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20import%20numpy%20as%20np%0A%0A%20%20%20%20from%20blvpy%20import%20BilevelProblem%2C%20LowerProblem%0A%0A%20%20%20%20plt.style.use(Path(__file__).resolve().parents%5B1%5D%20%2F%20%22_shared%22%20%2F%20%22zhlatex.mplstyle%22)%0A%20%20%20%20return%20BilevelProblem%2C%20LowerProblem%2C%20Path%2C%20cp%2C%20csv%2C%20datetime%2C%20mo%2C%20np%2C%20plt%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Bilevel%20formulation%0A%0A%20%20%20%20Let%20%24%5Calpha%5Cin%5Cmathbf%7BR%7D%24%20denote%20renewable%20capacity%20in%2010-GW%20blocks%2C%20and%0A%20%20%20%20let%20%24L%5Cin%5Cmathbf%7BR%7D%5Em%24%20denote%20the%20observed%20aggregate%20renewable%20capacity%0A%20%20%20%20factor%20over%20%24m%24%20time%20periods.%20Thus%2C%20%24%5Calpha%20L%5Cin%5Cmathbf%7BR%7D%5Em%24%20gives%20the%0A%20%20%20%20renewable-generation%20limits%2C%20also%20in%2010-GW%20blocks.%20Let%0A%20%20%20%20%24D%5Cin%5Cmathbf%7BR%7D%5Em%24%20denote%20electricity%20demand%20in%20the%20same%20units%2C%20and%20let%0A%20%20%20%20%24r%2Cg%5Cin%5Cmathbf%7BR%7D%5Em%24%20denote%20renewable%20and%20thermal%20dispatch.%20The%20generation%0A%20%20%20%20planner%20solves%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%5Cbegin%7Barray%7D%7Bll%7D%0A%20%20%20%20%5Cmathop%7B%5Cmathrm%7Bminimize%7D%7D_%7B%5Calpha%2Cr%2Cg%7D%0A%20%20%20%20%20%20%20%20%26%20p_r%5Calpha%5E2%2Bp_g%5Cmathbf%7B1%7D%5ETg%20%5C%5C%0A%20%20%20%20%5Cmathop%7B%5Cmathrm%7Bsubject%5C%20to%7D%7D%0A%20%20%20%20%20%20%20%20%26%200%5Cleq%5Calpha%5Cleq%5Calpha_%7B%5Cmathrm%7Bmax%7D%7D%2C%5C%5C%0A%20%20%20%20%20%20%20%20%26%20(r%2Cg)%5Cin%20S(%5Calpha).%0A%20%20%20%20%5Cend%7Barray%7D%0A%20%20%20%20%5C%5D%0A%0A%20%20%20%20For%20a%20fixed%20%24%5Calpha%24%2C%20the%20grid%20dispatcher%20solves%20the%20lower%20problem%0A%0A%20%20%20%20%5C%5B%0A%20%20%20%20%5Cbegin%7Barray%7D%7Bll%7D%0A%20%20%20%20S(%5Calpha)%3D%5Cmathop%7B%5Cmathrm%7Bargmin%7D%7D_%7Br%2Cg%7D%0A%20%20%20%20%20%20%20%20%26%20c_r%5Cmathbf%7B1%7D%5ETr%2Bc_g%5Cmathbf%7B1%7D%5ETg%0A%20%20%20%20%20%20%20%20%20%20%2B%5Cdelta%5Cleft(%5ClVert%20r%5CrVert_2%5E2%2B%5ClVert%20g%5CrVert_2%5E2%5Cright)%20%5C%5C%0A%20%20%20%20%5Cmathop%7B%5Cmathrm%7Bsubject%5C%20to%7D%7D%0A%20%20%20%20%20%20%20%20%26%200%5Cpreceq%20r%5Cpreceq%5Calpha%20L%2C%5Cquad%20g%5Csucceq0%2C%5C%5C%0A%20%20%20%20%20%20%20%20%26%20r%2Bg%5Csucceq%20D.%0A%20%20%20%20%5Cend%7Barray%7D%0A%20%20%20%20%5C%5D%0A%0A%20%20%20%20The%20upper%20variable%20is%20%24%5Calpha%24%2C%20while%20%24r%24%20and%20%24g%24%20are%20the%20lower%20variables.%0A%20%20%20%20The%20planning%20and%20dispatch%20cost%20coefficients%20%24p_r%2Cp_g%2Cc_r%2Cc_g%24%2C%20maximum%0A%20%20%20%20capacity%20%24%5Calpha_%7B%5Cmathrm%7Bmax%7D%7D%24%2C%20regularization%20parameter%20%24%5Cdelta%24%2C%20and%0A%20%20%20%20vectors%20%24L%2CD%24%20are%20given.%20The%20small%20quadratic%20term%20makes%20the%20lower%20response%0A%20%20%20%20stable.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Observed%20German%20operating%20data%0A%0A%20%20%20%20The%2024%20rows%20cover%20the%20local%20day%2021%20June%202019%20in%20Central%20European%20Summer%0A%20%20%20%20Time%20and%20include%20grid%20load%2C%20onshore%20and%20offshore%20wind%20generation%2C%0A%20%20%20%20photovoltaics%2C%20and%20their%20installed%20capacities.%20Source%3A%0A%20%20%20%20**Bundesnetzagentur%20%7C%20SMARD.de**.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Path%2C%20csv%2C%20datetime%2C%20np)%3A%0A%20%20%20%20data_path%20%3D%20Path(__file__).resolve().parents%5B1%5D%20%2F%20%22_shared%22%20%2F%20%22data%22%20%2F%20%22smard_germany_2019-06-21.csv%22%0A%20%20%20%20with%20data_path.open(encoding%3D%22utf-8%22%2C%20newline%3D%22%22)%20as%20data_file%3A%0A%20%20%20%20%20%20%20%20data_rows%20%3D%20list(csv.DictReader(data_file))%0A%0A%20%20%20%20local_times%20%3D%20%5Brow%5B%22local_time_cest%22%5D%20for%20row%20in%20data_rows%5D%0A%20%20%20%20utc_timestamps%20%3D%20%5Brow%5B%22utc_timestamp%22%5D%20for%20row%20in%20data_rows%5D%0A%20%20%20%20grid_load_mwh%20%3D%20np.array(%5Bfloat(row%5B%22grid_load_mwh%22%5D)%20for%20row%20in%20data_rows%5D)%0A%20%20%20%20renewable_generation_mwh%20%3D%20np.array(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20float(row%5B%22wind_onshore_mwh%22%5D)%20%2B%20float(row%5B%22wind_offshore_mwh%22%5D)%20%2B%20float(row%5B%22photovoltaics_mwh%22%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20row%20in%20data_rows%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20installed_capacity_mw%20%3D%20np.array(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20float(row%5B%22wind_onshore_capacity_mw%22%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20float(row%5B%22wind_offshore_capacity_mw%22%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%2B%20float(row%5B%22photovoltaics_capacity_mw%22%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20row%20in%20data_rows%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%0A%20%20%20%20hours%20%3D%20np.arange(len(data_rows))%0A%20%20%20%20m%20%3D%20hours.size%0A%20%20%20%20D%20%3D%20grid_load_mwh%20%2F%2010_000.0%0A%20%20%20%20L%20%3D%20renewable_generation_mwh%20%2F%20installed_capacity_mw%0A%0A%20%20%20%20assert%20m%20%3D%3D%2024%0A%20%20%20%20assert%20len(set(local_times))%20%3D%3D%20m%20%3D%3D%20len(set(utc_timestamps))%0A%20%20%20%20local_datetimes%20%3D%20%5Bdatetime.fromisoformat(timestamp)%20for%20timestamp%20in%20local_times%5D%0A%20%20%20%20utc_datetimes%20%3D%20%5Bdatetime.fromisoformat(timestamp.replace(%22Z%22%2C%20%22%2B00%3A00%22))%20for%20timestamp%20in%20utc_timestamps%5D%0A%20%20%20%20for%20timestamps%20in%20(local_datetimes%2C%20utc_datetimes)%3A%0A%20%20%20%20%20%20%20%20assert%20all(%0A%20%20%20%20%20%20%20%20%20%20%20%20(later%20-%20earlier).total_seconds()%20%3D%3D%203_600.0%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20earlier%2C%20later%20in%20zip(timestamps%5B%3A-1%5D%2C%20timestamps%5B1%3A%5D%2C%20strict%3DTrue)%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20assert%20np.all(np.isfinite(np.column_stack(%5Bgrid_load_mwh%2C%20renewable_generation_mwh%2C%20installed_capacity_mw%5D)))%0A%20%20%20%20assert%20np.all(grid_load_mwh%20%3E%200.0)%0A%20%20%20%20assert%20np.all(renewable_generation_mwh%20%3E%3D%200.0)%0A%20%20%20%20assert%20np.all(installed_capacity_mw%20%3E%200.0)%0A%20%20%20%20assert%20np.all((L%20%3E%3D%200.0)%20%26%20(L%20%3C%3D%201.0))%0A%0A%20%20%20%20p_r%20%3D%200.08%0A%20%20%20%20p_g%20%3D%200.6%0A%20%20%20%20c_r%20%3D%200.4%0A%20%20%20%20c_g%20%3D%202.5%0A%20%20%20%20alpha_max%20%3D%2012.0%0A%20%20%20%20delta%20%3D%201e-3%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20D%2C%0A%20%20%20%20%20%20%20%20L%2C%0A%20%20%20%20%20%20%20%20alpha_max%2C%0A%20%20%20%20%20%20%20%20c_g%2C%0A%20%20%20%20%20%20%20%20c_r%2C%0A%20%20%20%20%20%20%20%20delta%2C%0A%20%20%20%20%20%20%20%20grid_load_mwh%2C%0A%20%20%20%20%20%20%20%20hours%2C%0A%20%20%20%20%20%20%20%20installed_capacity_mw%2C%0A%20%20%20%20%20%20%20%20m%2C%0A%20%20%20%20%20%20%20%20p_g%2C%0A%20%20%20%20%20%20%20%20p_r%2C%0A%20%20%20%20%20%20%20%20renewable_generation_mwh%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Specify%20and%20solve%20the%20model%0A%0A%20%20%20%20%60alpha%60%20is%20listed%20as%20a%20%60LowerProblem%60%20parameter%2C%20so%20BLVPY%20treats%20it%20as%0A%20%20%20%20fixed%20inside%20dispatch%20while%20retaining%20it%20as%20the%20planner's%20variable.%20Its%0A%20%20%20%20planning%20limits%20are%20written%20as%20explicit%20CVXPY%20constraints.%20The%20initial%0A%20%20%20%20%60.value%60%20provides%20a%20starting%20point%20for%20the%20nonlinear%20solve.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(%0A%20%20%20%20BilevelProblem%2C%0A%20%20%20%20D%2C%0A%20%20%20%20L%2C%0A%20%20%20%20LowerProblem%2C%0A%20%20%20%20alpha_max%2C%0A%20%20%20%20c_g%2C%0A%20%20%20%20c_r%2C%0A%20%20%20%20cp%2C%0A%20%20%20%20delta%2C%0A%20%20%20%20m%2C%0A%20%20%20%20p_g%2C%0A%20%20%20%20p_r%2C%0A)%3A%0A%20%20%20%20alpha%20%3D%20cp.Variable(name%3D%22alpha%22)%0A%20%20%20%20r%20%3D%20cp.Variable(m%2C%20name%3D%22r%22)%0A%20%20%20%20g%20%3D%20cp.Variable(m%2C%20name%3D%22g%22)%0A%20%20%20%20alpha.value%20%3D%208.5%0A%0A%20%20%20%20dispatch%20%3D%20LowerProblem(%0A%20%20%20%20%20%20%20%20cp.Minimize(c_r%20*%20cp.sum(r)%20%2B%20c_g%20*%20cp.sum(g)%20%2B%20delta%20*%20(cp.sum_squares(r)%20%2B%20cp.sum_squares(g)))%2C%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20r%20%3E%3D%200.0%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20g%20%3E%3D%200.0%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20r%20%3C%3D%20cp.multiply(L%2C%20alpha)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20r%20%2B%20g%20%3E%3D%20D%2C%0A%20%20%20%20%20%20%20%20%5D%2C%0A%20%20%20%20%20%20%20%20parameters%3D%5Balpha%5D%2C%0A%20%20%20%20)%0A%20%20%20%20problem%20%3D%20BilevelProblem(%0A%20%20%20%20%20%20%20%20cp.Minimize(p_r%20*%20cp.square(alpha)%20%2B%20p_g%20*%20cp.sum(g))%2C%0A%20%20%20%20%20%20%20%20dispatch%2C%0A%20%20%20%20%20%20%20%20upper_constraints%3D%5Balpha%20%3E%3D%200.0%2C%20alpha%20%3C%3D%20alpha_max%5D%2C%0A%20%20%20%20)%0A%20%20%20%20return%20alpha%2C%20g%2C%20problem%2C%20r%0A%0A%0A%40app.cell%0Adef%20_(problem)%3A%0A%20%20%20%20result%20%3D%20problem.solve(%0A%20%20%20%20%20%20%20%20epsilon_initial%3D1e-2%2C%0A%20%20%20%20%20%20%20%20epsilon_target%3D1e-5%2C%0A%20%20%20%20%20%20%20%20conic_solver_options%3D%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22tol_feas%22%3A%201e-10%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22tol_gap_abs%22%3A%201e-10%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22tol_gap_rel%22%3A%201e-10%2C%0A%20%20%20%20%20%20%20%20%7D%2C%0A%20%20%20%20%20%20%20%20verbose%3DFalse%2C%0A%20%20%20%20)%0A%20%20%20%20diagnostics%20%3D%20problem.gap_diagnostics(result)%0A%20%20%20%20return%20diagnostics%2C%20result%0A%0A%0A%40app.cell%0Adef%20_(D%2C%20L%2C%20alpha%2C%20alpha_max%2C%20g%2C%20np%2C%20p_g%2C%20p_r%2C%20r%2C%20result)%3A%0A%20%20%20%20assert%20result.succeeded%2C%20result.message%0A%0A%20%20%20%20selected_alpha%20%3D%20float(np.asarray(result.variable_values%5Balpha%5D))%0A%20%20%20%20selected_r%20%3D%20np.asarray(result.variable_values%5Br%5D%2C%20dtype%3Dfloat)%0A%20%20%20%20selected_g%20%3D%20np.asarray(result.variable_values%5Bg%5D%2C%20dtype%3Dfloat)%0A%20%20%20%20closed_form_r%20%3D%20np.minimum(D%2C%20selected_alpha%20*%20L)%0A%20%20%20%20closed_form_g%20%3D%20D%20-%20closed_form_r%0A%0A%20%20%20%20np.testing.assert_allclose(%0A%20%20%20%20%20%20%20%20np.concatenate((selected_r%2C%20selected_g))%2C%0A%20%20%20%20%20%20%20%20np.concatenate((closed_form_r%2C%20closed_form_g))%2C%0A%20%20%20%20%20%20%20%20atol%3D1e-6%2C%0A%20%20%20%20%20%20%20%20rtol%3D0.0%2C%0A%20%20%20%20)%0A%0A%20%20%20%20alpha_grid%20%3D%20np.linspace(0.0%2C%20alpha_max%2C%202_001)%0A%20%20%20%20r_grid%20%3D%20np.minimum(D%5BNone%2C%20%3A%5D%2C%20alpha_grid%5B%3A%2C%20None%5D%20*%20L%5BNone%2C%20%3A%5D)%0A%20%20%20%20g_grid%20%3D%20D%5BNone%2C%20%3A%5D%20-%20r_grid%0A%20%20%20%20investment_costs%20%3D%20p_r%20*%20np.square(alpha_grid)%0A%20%20%20%20thermal_penalties%20%3D%20p_g%20*%20np.sum(g_grid%2C%20axis%3D1)%0A%20%20%20%20planner_objectives%20%3D%20investment_costs%20%2B%20thermal_penalties%0A%20%20%20%20grid_minimum_index%20%3D%20int(np.argmin(planner_objectives))%0A%20%20%20%20grid_minimizer%20%3D%20float(alpha_grid%5Bgrid_minimum_index%5D)%0A%20%20%20%20grid_spacing%20%3D%20float(alpha_grid%5B1%5D%20-%20alpha_grid%5B0%5D)%0A%0A%20%20%20%20assert%20abs(selected_alpha%20-%20grid_minimizer)%20%3C%3D%202.0%20*%20grid_spacing%0A%0A%20%20%20%20baseline_g%20%3D%20float(np.sum(D))%0A%20%20%20%20optimized_g%20%3D%20float(np.sum(selected_g))%0A%20%20%20%20return%20(%0A%20%20%20%20%20%20%20%20alpha_grid%2C%0A%20%20%20%20%20%20%20%20baseline_g%2C%0A%20%20%20%20%20%20%20%20closed_form_g%2C%0A%20%20%20%20%20%20%20%20closed_form_r%2C%0A%20%20%20%20%20%20%20%20grid_minimizer%2C%0A%20%20%20%20%20%20%20%20investment_costs%2C%0A%20%20%20%20%20%20%20%20optimized_g%2C%0A%20%20%20%20%20%20%20%20planner_objectives%2C%0A%20%20%20%20%20%20%20%20selected_alpha%2C%0A%20%20%20%20%20%20%20%20thermal_penalties%2C%0A%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(%0A%20%20%20%20baseline_g%2C%0A%20%20%20%20diagnostics%2C%0A%20%20%20%20grid_load_mwh%2C%0A%20%20%20%20grid_minimizer%2C%0A%20%20%20%20installed_capacity_mw%2C%0A%20%20%20%20mo%2C%0A%20%20%20%20optimized_g%2C%0A%20%20%20%20result%2C%0A%20%20%20%20selected_alpha%2C%0A)%3A%0A%20%20%20%20mo.md(rf%22%22%22%0A%20%20%20%20%23%23%20Result%0A%0A%20%20%20%20%7C%20quantity%20%7C%20value%20%7C%0A%20%20%20%20%7C%20---%20%7C%20---%3A%20%7C%0A%20%20%20%20%7C%20status%20%7C%20%60%7Bresult.status%7D%60%20%7C%0A%20%20%20%20%7C%20observed%20daily%20grid%20load%20%7C%20%7Bfloat(grid_load_mwh.sum()%20%2F%201_000)%3A.3f%7D%20GWh%20%7C%0A%20%20%20%20%7C%20observed%20wind%20and%20solar%20capacity%20%7C%20%7Bfloat(installed_capacity_mw%5B0%5D%20%2F%201_000)%3A.3f%7D%20GW%20%7C%0A%20%20%20%20%7C%20selected%20renewable%20capacity%20%2410%5Calpha%24%20%7C%20%7B10.0%20*%20selected_alpha%3A.3f%7D%20GW%20%7C%0A%20%20%20%20%7C%202%2C001-point%20grid%20reference%20%2410%5Calpha%24%20%7C%20%7B10.0%20*%20grid_minimizer%3A.3f%7D%20GW%20%7C%0A%20%20%20%20%7C%20thermal%20production%20without%20renewables%20%7C%20%7B10.0%20*%20baseline_g%3A.3f%7D%20GWh%20%7C%0A%20%20%20%20%7C%20optimized%20thermal%20production%20%2410%5Cmathbf%7B%7B1%7D%7D%5ETg%24%20%7C%20%7B10.0%20*%20optimized_g%3A.3f%7D%20GWh%20%7C%0A%20%20%20%20%7C%20upper%20objective%20%7C%20%7Bfloat(result.objective)%3A.6f%7D%20%7C%0A%20%20%20%20%7C%20final%20epsilon%20%7C%20%7Bresult.final_epsilon%3A.3e%7D%20%7C%0A%20%20%20%20%7C%20maximum%20lifted%20residual%20%7C%20%7Bresult.residuals.max_violation%3A.3e%7D%20%7C%0A%20%20%20%20%7C%20complementarity%20%7C%20%7Bresult.complementarity%3A.3e%7D%20%7C%0A%20%20%20%20%7C%20signed%20source%20gap%20%7C%20%7Bdiagnostics.source_gap%3A.3e%7D%20%7C%0A%0A%20%20%20%20With%20the%20observed%20hourly%20profile%20held%20fixed%2C%20the%20illustrative%20model%20selects%0A%20%20%20%20a%20little%20more%20renewable%20capacity%20than%20the%20102.823%20GW%20installed%20in%20the%0A%20%20%20%20source%20data.%20The%20independent%20grid%20search%20places%20its%20minimum%20at%20nearly%20the%0A%20%20%20%20same%20capacity.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Interpreting%20the%20capacity%20and%20dispatch%0A%0A%20%20%20%20The%20top%20panel%20stacks%20the%0A%20%20%20%20modeled%20renewable%20and%20thermal%20dispatch%20against%20grid%20load%3B%20the%20dotted%20line%0A%20%20%20%20is%20the%20renewable%20output%20physically%20available%20from%20the%20selected%20capacity.%0A%20%20%20%20Thermal%20generation%20fills%20the%20shortfall%20when%20renewable%20potential%20is%20below%0A%20%20%20%20demand.%0A%0A%20%20%20%20The%20bottom%20panel%20explains%20the%20planner's%20choice%3A%20investment%20cost%20rises%0A%20%20%20%20quadratically%2C%20while%20the%20thermal-production%20penalty%20falls.%20Their%20sum%20is%0A%20%20%20%20minimized%20close%20to%20the%20capacity%20returned%20by%20BLVPY.%20These%20grid%20curves%20use%0A%20%20%20%20the%20hand-derived%20dispatch%20response%0A%20%20%20%20%24r_t(%5Calpha)%3D%5Cmin%5C%7BD_t%2C%5Calpha%20L_t%5C%7D%24%20and%0A%20%20%20%20%24g_t(%5Calpha)%3DD_t-r_t(%5Calpha)%24%20for%20this%20example.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(%0A%20%20%20%20D%2C%0A%20%20%20%20L%2C%0A%20%20%20%20Path%2C%0A%20%20%20%20alpha_grid%2C%0A%20%20%20%20closed_form_g%2C%0A%20%20%20%20closed_form_r%2C%0A%20%20%20%20hours%2C%0A%20%20%20%20investment_costs%2C%0A%20%20%20%20np%2C%0A%20%20%20%20planner_objectives%2C%0A%20%20%20%20plt%2C%0A%20%20%20%20renewable_generation_mwh%2C%0A%20%20%20%20selected_alpha%2C%0A%20%20%20%20thermal_penalties%2C%0A)%3A%0A%20%20%20%20figure_dir%20%3D%20Path(__file__).resolve().parent%20%2F%20%22figures%22%0A%20%20%20%20figure_dir.mkdir(parents%3DTrue%2C%20exist_ok%3DTrue)%0A%0A%20%20%20%20fig%2C%20(dispatch_axis%2C%20objective_axis)%20%3D%20plt.subplots(2%2C%201%2C%20figsize%3D(6.5%2C%207))%0A%0A%20%20%20%20dispatch_axis.stackplot(%0A%20%20%20%20%20%20%20%20hours%2C%0A%20%20%20%20%20%20%20%20closed_form_r%2C%0A%20%20%20%20%20%20%20%20closed_form_g%2C%0A%20%20%20%20%20%20%20%20labels%3D%5Br%22%24r%24%22%2C%20r%22%24g%24%22%5D%2C%0A%20%20%20%20%20%20%20%20colors%3D%5B%22C2%22%2C%20%22grey%22%5D%2C%0A%20%20%20%20%20%20%20%20alpha%3D0.85%2C%0A%20%20%20%20%20%20%20%20step%3D%22mid%22%2C%0A%20%20%20%20)%0A%20%20%20%20dispatch_axis.plot(%0A%20%20%20%20%20%20%20%20hours%2C%0A%20%20%20%20%20%20%20%20D%2C%0A%20%20%20%20%20%20%20%20color%3D%22k%22%2C%0A%20%20%20%20%20%20%20%20linestyle%3D%22--%22%2C%0A%20%20%20%20%20%20%20%20marker%3D%22o%22%2C%0A%20%20%20%20%20%20%20%20markersize%3D2.5%2C%0A%20%20%20%20%20%20%20%20drawstyle%3D%22steps-mid%22%2C%0A%20%20%20%20%20%20%20%20label%3Dr%22%24D%24%22%2C%0A%20%20%20%20%20%20%20%20zorder%3D10%2C%0A%20%20%20%20)%0A%20%20%20%20dispatch_axis.plot(%0A%20%20%20%20%20%20%20%20hours%2C%0A%20%20%20%20%20%20%20%20selected_alpha%20*%20L%2C%0A%20%20%20%20%20%20%20%20color%3D%22C2%22%2C%0A%20%20%20%20%20%20%20%20linestyle%3D%22%3A%22%2C%0A%20%20%20%20%20%20%20%20marker%3D%22o%22%2C%0A%20%20%20%20%20%20%20%20markersize%3D2.5%2C%0A%20%20%20%20%20%20%20%20drawstyle%3D%22steps-mid%22%2C%0A%20%20%20%20%20%20%20%20label%3Dr%22%24%5Calpha%20L%24%22%2C%0A%20%20%20%20)%0A%20%20%20%20dispatch_axis.plot(%0A%20%20%20%20%20%20%20%20hours%2C%0A%20%20%20%20%20%20%20%20renewable_generation_mwh%20%2F%2010_000.0%2C%0A%20%20%20%20%20%20%20%20color%3D%22C0%22%2C%0A%20%20%20%20%20%20%20%20linewidth%3D1.0%2C%0A%20%20%20%20%20%20%20%20marker%3D%22.%22%2C%0A%20%20%20%20%20%20%20%20drawstyle%3D%22steps-mid%22%2C%0A%20%20%20%20%20%20%20%20label%3D%22observed%20renewable%20output%22%2C%0A%20%20%20%20)%0A%20%20%20%20dispatch_axis.set(%0A%20%20%20%20%20%20%20%20xlabel%3D%22Hour%20(CEST)%22%2C%0A%20%20%20%20%20%20%20%20ylabel%3D%22Power%20(10%20GW)%22%2C%0A%20%20%20%20%20%20%20%20xticks%3Dnp.arange(0%2C%2024%2C%204)%2C%0A%20%20%20%20%20%20%20%20xlim%3D(-0.5%2C%2023.5)%2C%0A%20%20%20%20)%0A%20%20%20%20dispatch_axis.legend(loc%3D%22upper%20left%22%2C%20frameon%3DFalse%2C%20fontsize%3D13)%0A%0A%20%20%20%20objective_axis.plot(alpha_grid%2C%20investment_costs%2C%20label%3Dr%22%24p_r%5Calpha%5E2%24%22%2C%20color%3D%22C2%22)%0A%20%20%20%20objective_axis.plot(alpha_grid%2C%20thermal_penalties%2C%20label%3Dr%22%24p_g%5Cmathbf%7B1%7D%5ETg%24%22%2C%20color%3D%22grey%22)%0A%20%20%20%20objective_axis.plot(%0A%20%20%20%20%20%20%20%20alpha_grid%2C%0A%20%20%20%20%20%20%20%20planner_objectives%2C%0A%20%20%20%20%20%20%20%20label%3Dr%22%24p_r%5Calpha%5E2%2Bp_g%5Cmathbf%7B1%7D%5ETg%24%22%2C%0A%20%20%20%20%20%20%20%20color%3D%22k%22%2C%0A%20%20%20%20)%0A%20%20%20%20objective_axis.axvline(selected_alpha%2C%20color%3D%22C3%22%2C%20linestyle%3D%22--%22)%0A%20%20%20%20objective_axis.set(%0A%20%20%20%20%20%20%20%20xlabel%3Dr%22%24%5Calpha%24%22%2C%0A%20%20%20%20%20%20%20%20ylabel%3D%22Planning%20cost%22%2C%0A%20%20%20%20)%0A%20%20%20%20objective_axis.legend(loc%3D(0%2C%200.1)%2C%20frameon%3DFalse%2C%20fontsize%3D13)%0A%0A%20%20%20%20fig.tight_layout()%0A%20%20%20%20figure_path%20%3D%20figure_dir%20%2F%20%22renewable_capacity_planning.pdf%22%0A%20%20%20%20fig.savefig(figure_path%2C%20bbox_inches%3D%22tight%22)%0A%20%20%20%20plt.show()%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
bf6d73157f1f90c374faa10d1bc1db07