SKILL.md
Economic Dispatch
Economic dispatch minimizes total generation cost while meeting load demand and respecting generator limits.
Generator Data Indices
MATPOWER generator array columns (0-indexed):
| Index | Field | Description |
|---|---|---|
| 0 | GEN_BUS | Bus number (1-indexed) |
| 8 | PMAX | Maximum real power (MW) |
| 9 | PMIN | Minimum real power (MW) |
# Use bus number mapping (handles non-contiguous bus numbers)
bus_num_to_idx = {int(buses[i, 0]): i for i in range(n_bus)}
gen_bus = [bus_num_to_idx[int(g[0])] for g in gens]
pmax_MW = gen[8]
pmin_MW = gen[9]
Cost Function Format
MATPOWER gencost array (polynomial type 2):
| Index | Field | Description |
|---|---|---|
| 0 | MODEL | 2 = polynomial |
| 1 | STARTUP | Startup cost ($) |
| 2 | SHUTDOWN | Shutdown cost ($) |
| 3 | NCOST | Number of coefficients |
| 4+ | coeffs | Cost coefficients (highest order first) |
For quadratic (NCOST=3): coefficients are [c2, c1, c0] at indices 4, 5, 6 For linear (NCOST=2): coefficients are [c1, c0] at indices 4, 5
Cost = c₂·P² + c₁·P + c₀ ($/hr) where P is in MW.
Optimization Formulation
import cvxpy as cp
Pg = cp.Variable(n_gen) # Generator outputs in per-unit
# Objective: minimize total cost (handles variable NCOST)
cost = 0
for i in range(n_gen):
ncost = int(gencost[i, 3])
Pg_MW = Pg[i] * baseMVA
if ncost >= 3:
# Quadratic: c2*P^2 + c1*P + c0
c2, c1, c0 = gencost[i, 4], gencost[i, 5], gencost[i, 6]
cost += c2 * cp.square(Pg_MW) + c1 * Pg_MW + c0
elif ncost == 2:
# Linear: c1*P + c0
c1, c0 = gencost[i, 4], gencost[i, 5]
cost += c1 * Pg_MW + c0
else:
# Constant cost
cost += gencost[i, 4] if ncost >= 1 else 0
# Generator limits (convert MW to per-unit)
constraints = []
for i in range(n_gen):
pmin = gens[i, 9] / baseMVA
pmax = gens[i, 8] / baseMVA
constraints.append(Pg[i] >= pmin)
constraints.append(Pg[i] <= pmax)
