hareshchander/Renewable-Integration-Assistant
0
1import gradio as gr2from scipy.optimize import linprog3import matplotlib.pyplot as plt4 5def optimize_dispatch(solar_max, wind_max, grid_max, load):6 # Cost vector: prioritize solar and wind (zero cost), grid cost = 5 (example)7 c = [0, 0, 5]8 9 # Inequality constraints: x_i <= max capacity10 A_ub = [11 [1, 0, 0], # solar <= solar_max12 [0, 1, 0], # wind <= wind_max13 [0, 0, 1], # grid <= grid_max14 ]15 b_ub = [solar_max, wind_max, grid_max]16 17 # Equality constraint: total supply = load18 A_eq = [[1, 1, 1]]19 b_eq = [load]20 21 bounds = [(0, None), (0, None), (0, None)]22 23 res = linprog(c, A_ub=A_ub, b_ub=b_ub, A_eq=A_eq, b_eq=b_eq, bounds=bounds, method='highs')24 25 if res.success:26 solar, wind, grid = [round(x, 2) for x in res.x]27 dispatch = {"Solar Power": solar, "Wind Power": wind, "Grid Power": grid}28 29 plt.figure(figsize=(6, 4))30 plt.bar(dispatch.keys(), dispatch.values(), color=['orange', 'green', 'blue'])31 plt.title("Optimized Power Dispatch (MW)")32 plt.ylabel("Power (MW)")33 plt.ylim(0, max(solar_max, wind_max, grid_max, load) * 1.1)34 plt.grid(axis='y')35 plt.tight_layout()36 plt.savefig("dispatch_plot.png")37 plt.close()38 39 # Format output as string with units, avoid percentage display40 result_str = "\n".join([f"{k}: {v} MW" for k, v in dispatch.items()])41 return result_str, "dispatch_plot.png"42 else:43 return "Error: Optimization failed. Please check inputs.", None44 45inputs = [46 gr.Number(label="Solar Max Capacity (MW)", value=50, minimum=0),47 gr.Number(label="Wind Max Capacity (MW)", value=60, minimum=0),48 gr.Number(label="Grid Max Capacity (MW)", value=100, minimum=0),49 gr.Number(label="Load Demand (MW)", value=120, minimum=0),50]51 52outputs = [53 gr.Label(label="Optimized Dispatch (MW)"),54 gr.Image(type="filepath", label="Dispatch Visualization")55]56 57demo = gr.Interface(58 fn=optimize_dispatch,59 inputs=inputs,60 outputs=outputs,61 title="Renewable Integration Optimization Assistant",62 description="Input max capacities of renewable sources and grid, along with load demand, to get optimized power dispatch minimizing grid usage."63)64 65if __name__ == "__main__":66 demo.launch()67 