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