File size: 2,275 Bytes
3375f70
 
 
 
a89c422
 
 
9285b6b
3375f70
 
 
9285b6b
3375f70
 
 
 
 
9285b6b
a89c422
3375f70
9285b6b
a89c422
3375f70
9285b6b
a89c422
3375f70
 
 
 
befe14f
9285b6b
 
a89c422
9285b6b
3375f70
9285b6b
a89c422
3375f70
 
9285b6b
 
 
 
 
 
 
 
3375f70
 
 
 
a89c422
 
 
 
3375f70
 
a89c422
9285b6b
3375f70
 
a89c422
 
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
import gradio as gr
from pyomo.environ import *

def optimize_dispatch(solar, wind, grid, load):
    total_capacity = solar + wind + grid
    if load > total_capacity:
        return {"error": "Load exceeds total available capacity!"}

    sources = {
        "solar": {"max": solar, "cost": 0},
        "wind": {"max": wind, "cost": 0},
        "grid": {"max": grid, "cost": 5}
    }

    model = ConcreteModel()
    model.sources = Set(initialize=sources.keys())
    model.P = Var(model.sources, bounds=(0, None))

    # Objective: minimize cost
    model.cost = Objective(expr=sum(sources[s]['cost'] * model.P[s] for s in model.sources), sense=minimize)

    # Load balance constraint
    model.balance = Constraint(expr=sum(model.P[s] for s in model.sources) == load)

    # Capacity constraints per source
    model.capacity = ConstraintList()
    for s in sources:
        model.capacity.add(model.P[s] <= sources[s]['max'])

    solver = SolverFactory('glpk')
    result = solver.solve(model)

    if (result.solver.status != SolverStatus.ok) or (result.solver.termination_condition != TerminationCondition.optimal):
        return {"error": "Solver failed to find an optimal solution."}

    dispatch = {s: round(model.P[s](), 2) for s in sources}
    return dispatch

def ui(solar, wind, grid, load):
    try:
        output = optimize_dispatch(solar, wind, grid, load)
        if isinstance(output, dict) and "error" in output:
            return f"Error: {output['error']}", ""
        explanation = f"Optimized dispatch: {output}. Priority given to renewables (zero cost), rest from grid."
        return str(output), explanation
    except Exception as e:
        return f"Unexpected Error: {str(e)}", ""

demo = gr.Interface(
    fn=ui,
    inputs=[
        gr.Slider(0, 100, step=1, label="Solar Capacity (MW)"),
        gr.Slider(0, 100, step=1, label="Wind Capacity (MW)"),
        gr.Slider(0, 200, step=1, label="Grid Capacity (MW)"),
        gr.Slider(0, 300, step=1, label="Total Load (MW)")
    ],
    outputs=["text", "text"],
    title="Renewable Integration Optimization Assistant",
    description="Enter available renewable sources, grid limits, and total load to receive optimized dispatch output."
)

if __name__ == "__main__":
    demo.launch()