Economic Dispatch - Examples in Python

Dispatch Model

This example solves the economic dispatch problem with Pyomo and HiGHS. The reusable implementation lives in examples/python/dispatch_model.py; this section imports that script, defines a small generator fleet, and reports the least-cost dispatch.

from pathlib import Path
import sys

for project_root in [Path.cwd(), *Path.cwd().parents]:
    examples_dir = project_root / "examples" / "python"
    if (examples_dir / "dispatch_model.py").exists():
        sys.path.insert(0, str(examples_dir))
        break
else:
    raise FileNotFoundError("Could not find examples/python/dispatch_model.py")

from dispatch_model import Generator, merit_order_dispatch, total_cost

The generator data include a zero-marginal-cost wind unit, a low-cost solar unit, and a gas unit that covers the remaining demand.

generators = [
    Generator("wind", marginal_cost=0.0, capacity=35.0),
    Generator("solar", marginal_cost=3.0, capacity=25.0),
    Generator("gas", marginal_cost=75.0, capacity=60.0),
]

demand = 80.0

The solver chooses generation from the cheapest available units first, while enforcing the demand balance and each unit’s capacity limit.

dispatch = merit_order_dispatch(demand, generators)
cost = total_cost(dispatch, generators)

dispatch, cost
([('wind', 35.0), ('solar', 25.0), ('gas', 20.0)], 1575.0)

The solution uses all available wind and solar output, then dispatches gas for the remaining demand.

for generator_name, output in dispatch:
    print(f"{generator_name}: {output:.1f} MW")

print(f"Total cost: {cost:.2f}")
wind: 35.0 MW
solar: 25.0 MW
gas: 20.0 MW
Total cost: 1575.00

If demand exceeds total available capacity, the model is infeasible and the example raises a clear error.

try:
    merit_order_dispatch(200.0, generators)
except ValueError as error:
    print(error)
demand exceeds available capacity