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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
|
import numpy as np import matplotlib import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec from matplotlib import cm from scipy.optimize import minimize, LinearConstraint, Bounds from mpl_toolkits.mplot3d import Axes3D import warnings warnings.filterwarnings("ignore")
plt.style.use("dark_background") DARK_BG = "#0d1117" PANEL_BG = "#161b22" ACCENT = "#58a6ff" ACCENT2 = "#3fb950" ACCENT3 = "#f78166" ACCENT4 = "#d2a8ff" GOLD = "#e3b341" TEXT_CLR = "#c9d1d9" GRID_CLR = "#21262d"
plt.rcParams.update({ "figure.facecolor": DARK_BG, "axes.facecolor": PANEL_BG, "axes.edgecolor": "#30363d", "axes.labelcolor": TEXT_CLR, "xtick.color": TEXT_CLR, "ytick.color": TEXT_CLR, "text.color": TEXT_CLR, "grid.color": GRID_CLR, "grid.linestyle": "--", "grid.alpha": 0.5, "legend.framealpha": 0.2, "legend.edgecolor": "#30363d", "font.size": 11, })
N_BUS = 5 N_GEN = 3 SLACK = 0
gen_a = np.array([0.004, 0.006, 0.009]) gen_b = np.array([2.0, 1.8, 2.2 ]) gen_c = np.array([80.0, 60.0, 40.0 ])
gen_pmin = np.array([10.0, 10.0, 10.0]) gen_pmax = np.array([200.0, 150.0, 100.0])
gen_bus = np.array([0, 1, 4])
load_bus = np.zeros(N_BUS) load_bus[2] = 90.0 load_bus[3] = 80.0 load_bus[4] = 50.0 P_demand = load_bus.sum()
lines = [ (0, 1, 0.10, 150.0), (0, 3, 0.15, 100.0), (1, 2, 0.12, 100.0), (1, 3, 0.18, 80.0), (2, 4, 0.20, 60.0), (3, 4, 0.25, 60.0), ] N_LINE = len(lines) line_labels = ["L12", "L14", "L23", "L24", "L35", "L45"]
def build_B_matrix(n_bus, lines): """Build full susceptance matrix B (n_bus x n_bus).""" B = np.zeros((n_bus, n_bus)) for (fr, to, x, _) in lines: b = 1.0 / x B[fr, fr] += b B[to, to] += b B[fr, to] -= b B[to, fr] -= b return B
def dc_power_flow(P_gen, gen_bus, load_bus, lines, n_bus, slack=0): """ Solve DC OPF power flow. Returns voltage angles (rad) and line flows (MW). """ P_inj = np.zeros(n_bus) for i, g in enumerate(gen_bus): P_inj[g] += P_gen[i] P_inj -= load_bus
B = build_B_matrix(n_bus, lines)
non_slack = [i for i in range(n_bus) if i != slack] B_red = B[np.ix_(non_slack, non_slack)] P_red = P_inj[non_slack]
theta_red = np.linalg.solve(B_red, P_red)
theta = np.zeros(n_bus) for k, idx in enumerate(non_slack): theta[idx] = theta_red[k]
flows = np.zeros(len(lines)) for k, (fr, to, x, _) in enumerate(lines): flows[k] = (theta[fr] - theta[to]) / x
return theta, flows
def total_cost(P_gen): """Quadratic total generation cost [$/h].""" return np.sum(gen_a * P_gen**2 + gen_b * P_gen + gen_c)
def total_cost_grad(P_gen): """Analytic gradient of cost.""" return 2.0 * gen_a * P_gen + gen_b
def power_balance(P_gen): """Equality: sum of generation = total demand.""" return P_gen.sum() - P_demand
def line_flow_constraints(P_gen): """ Inequality constraints: -P_max <= flow <= P_max Returns array that must be >= 0 for scipy (feasible side). """ _, flows = dc_power_flow(P_gen, gen_bus, load_bus, lines, N_BUS, SLACK) c = [] for k, (fr, to, x, fmax) in enumerate(lines): c.append(fmax - flows[k]) c.append(fmax + flows[k]) return np.array(c)
constraints = [ {"type": "eq", "fun": power_balance}, {"type": "ineq", "fun": line_flow_constraints}, ]
bounds = Bounds(gen_pmin, gen_pmax)
P_total_cap = gen_pmax.sum() P0 = gen_pmin + (gen_pmax - gen_pmin) * (P_demand - gen_pmin.sum()) / ( (gen_pmax - gen_pmin).sum()) P0 = np.clip(P0, gen_pmin, gen_pmax)
result = minimize( total_cost, P0, jac=total_cost_grad, method="SLSQP", bounds=bounds, constraints=constraints, options={"ftol": 1e-9, "maxiter": 500, "disp": False}, )
P_opt = result.x cost_opt = result.fun theta_opt, flows_opt = dc_power_flow(P_opt, gen_bus, load_bus, lines, N_BUS, SLACK)
print("=" * 55) print(" OPF Solution (SLSQP)") print("=" * 55) print(f" Converged : {result.success} ({result.message})") print(f" Total demand : {P_demand:.1f} MW") print(f" Total gen : {P_opt.sum():.4f} MW") print() print(" Generator Dispatch:") for i in range(N_GEN): pct = (P_opt[i] - gen_pmin[i]) / (gen_pmax[i] - gen_pmin[i]) * 100 ci = gen_a[i]*P_opt[i]**2 + gen_b[i]*P_opt[i] + gen_c[i] print(f" G{i+1} (Bus {gen_bus[i]+1}): {P_opt[i]:7.3f} MW " f"[{gen_pmin[i]:.0f}–{gen_pmax[i]:.0f}] " f"Load: {pct:.1f}% Cost: {ci:.2f} $/h") print() print(f" Total cost : {cost_opt:.4f} $/h") print() print(" Line Flows:") for k, (fr, to, x, fmax) in enumerate(lines): util = abs(flows_opt[k]) / fmax * 100 flag = " *** CONGESTED ***" if util > 99.0 else "" print(f" {line_labels[k]} ({fr+1}→{to+1}): {flows_opt[k]:+8.3f} MW" f" / {fmax:.0f} MW ({util:.1f}%){flag}") print() print(" Voltage Angles (degrees):") for i in range(N_BUS): print(f" Bus {i+1}: {np.degrees(theta_opt[i]):+8.4f}°") print("=" * 55)
fig = plt.figure(figsize=(18, 14), facecolor=DARK_BG) fig.suptitle("Optimal Power Flow — 5-Bus DC OPF Results", fontsize=16, color=TEXT_CLR, y=0.98, fontweight="bold")
gs = gridspec.GridSpec(3, 3, figure=fig, hspace=0.48, wspace=0.38, left=0.06, right=0.97, top=0.93, bottom=0.05)
gen_colors = [ACCENT, ACCENT2, ACCENT3] gen_names = ["G1 (Bus 1)", "G2 (Bus 2)", "G3 (Bus 5)"]
ax1 = fig.add_subplot(gs[0, 0]) x_pos = np.arange(N_GEN) bars = ax1.bar(x_pos, P_opt, color=gen_colors, width=0.5, edgecolor="#30363d", linewidth=0.8, zorder=3)
for i in range(N_GEN): ax1.hlines(gen_pmax[i], i - 0.35, i + 0.35, colors=GOLD, linewidths=1.5, linestyles="--", zorder=4) ax1.hlines(gen_pmin[i], i - 0.35, i + 0.35, colors=TEXT_CLR, linewidths=1.0, linestyles=":", alpha=0.6, zorder=4) ax1.text(i, P_opt[i] + 3, f"{P_opt[i]:.1f}", ha="center", fontsize=10, color=TEXT_CLR, fontweight="bold") ax1.set_xticks(x_pos) ax1.set_xticklabels(gen_names, fontsize=9) ax1.set_ylabel("Power (MW)") ax1.set_title("Generator Dispatch", color=TEXT_CLR) ax1.set_ylim(0, 220) ax1.grid(True, axis="y") ax1.legend(["Optimal output", "P_max limit", "P_min limit"], loc="upper right", fontsize=8, handles=[ plt.Rectangle((0,0),1,1, color=ACCENT, alpha=0.8), plt.Line2D([0],[0], color=GOLD, lw=1.5, ls="--"), plt.Line2D([0],[0], color=TEXT_CLR, lw=1.0, ls=":", alpha=0.6), ])
ax2 = fig.add_subplot(gs[0, 1]) costs_ind = [gen_a[i]*P_opt[i]**2 + gen_b[i]*P_opt[i] + gen_c[i] for i in range(N_GEN)] wedges, texts, autotexts = ax2.pie( costs_ind, labels=gen_names, autopct="%1.1f%%", colors=gen_colors, startangle=90, wedgeprops={"edgecolor": DARK_BG, "linewidth": 1.5}, ) for at in autotexts: at.set_fontsize(9) at.set_color(DARK_BG) ax2.set_title(f"Cost Share (Total: {cost_opt:.1f} $/h)", color=TEXT_CLR)
ax3 = fig.add_subplot(gs[0, 2]) util_pct = np.abs(flows_opt) / np.array([l[3] for l in lines]) * 100 bar_colors = [ACCENT3 if u > 90 else ACCENT2 if u > 60 else ACCENT for u in util_pct] ax3.barh(line_labels, util_pct, color=bar_colors, edgecolor="#30363d", linewidth=0.8, zorder=3) ax3.axvline(100, color=GOLD, lw=1.5, ls="--", label="Limit (100%)") ax3.axvline(90, color=ACCENT3, lw=1.0, ls=":", alpha=0.7, label="90% warning") ax3.set_xlabel("Utilization (%)") ax3.set_title("Line Flow Utilization", color=TEXT_CLR) ax3.set_xlim(0, 115) ax3.grid(True, axis="x") ax3.legend(fontsize=8) for i, (u, f) in enumerate(zip(util_pct, flows_opt)): ax3.text(u + 0.5, i, f"{f:+.1f} MW", va="center", fontsize=8, color=TEXT_CLR)
ax4 = fig.add_subplot(gs[1, 0:2]) ax4.set_facecolor(PANEL_BG) ax4.set_xlim(-0.3, 4.3) ax4.set_ylim(-0.5, 2.2) ax4.axis("off") ax4.set_title("Network Topology & Power Flows", color=TEXT_CLR, pad=8)
bus_pos = { 0: (0.0, 1.5), 1: (2.0, 2.0), 2: (4.0, 1.8), 3: (0.5, 0.2), 4: (3.5, 0.2), }
line_colors_topo = [] for k, (fr, to, x, fmax) in enumerate(lines): u = abs(flows_opt[k]) / fmax c = ACCENT3 if u > 0.9 else ACCENT2 if u > 0.6 else ACCENT line_colors_topo.append(c) xf, yf = bus_pos[fr] xt, yt = bus_pos[to] ax4.plot([xf, xt], [yf, yt], color=c, lw=2.5 + u * 2.5, alpha=0.8, zorder=1) mx, my = (xf + xt) / 2, (yf + yt) / 2 ax4.text(mx, my + 0.08, f"{flows_opt[k]:+.1f}", ha="center", fontsize=8, color=c, fontweight="bold", bbox={"boxstyle": "round,pad=0.2", "facecolor": PANEL_BG, "edgecolor": c, "alpha": 0.85})
for bus_idx, (bx, by) in bus_pos.items(): is_gen = bus_idx in gen_bus is_load = load_bus[bus_idx] > 0 radius = 0.22 if is_gen else 0.16 fc = ACCENT if is_gen else ACCENT3 if is_load else TEXT_CLR
circ = plt.Circle((bx, by), radius, color=fc, zorder=3, alpha=0.9) ax4.add_patch(circ) ax4.text(bx, by, f"B{bus_idx+1}", ha="center", va="center", fontsize=9, fontweight="bold", color=DARK_BG, zorder=4)
lines_ann = [f"Bus {bus_idx+1}"] gen_idx = np.where(gen_bus == bus_idx)[0] if len(gen_idx) > 0: gi = gen_idx[0] lines_ann.append(f"G{gi+1}: {P_opt[gi]:.1f} MW") if load_bus[bus_idx] > 0: lines_ann.append(f"Load: {load_bus[bus_idx]:.0f} MW") offset = (0.0, 0.32) if by > 1.0 else (0.0, -0.32) ax4.text(bx + offset[0], by + offset[1], "\n".join(lines_ann), ha="center", va="center", fontsize=8, color=TEXT_CLR, bbox={"boxstyle": "round,pad=0.3", "facecolor": PANEL_BG, "edgecolor": "#30363d", "alpha": 0.85})
ax4.legend( handles=[ plt.Line2D([0],[0], color=ACCENT3, lw=3, label=">90% util."), plt.Line2D([0],[0], color=ACCENT2, lw=3, label="60–90% util."), plt.Line2D([0],[0], color=ACCENT, lw=3, label="<60% util."), plt.Circle((0,0), 0.1, color=ACCENT, label="Generator bus"), plt.Circle((0,0), 0.1, color=ACCENT3, label="Load bus"), ], loc="lower right", fontsize=8, framealpha=0.3, )
ax5 = fig.add_subplot(gs[1, 2]) P_range = [np.linspace(gen_pmin[i], gen_pmax[i], 200) for i in range(N_GEN)] for i in range(N_GEN): c_curve = gen_a[i] * P_range[i]**2 + gen_b[i] * P_range[i] + gen_c[i] ax5.plot(P_range[i], c_curve, color=gen_colors[i], lw=2, label=gen_names[i]) c_opt_i = gen_a[i] * P_opt[i]**2 + gen_b[i] * P_opt[i] + gen_c[i] ax5.scatter(P_opt[i], c_opt_i, color=gen_colors[i], s=90, zorder=5, edgecolors=GOLD, linewidths=1.5) ax5.set_xlabel("Output P (MW)") ax5.set_ylabel("Cost ($/h)") ax5.set_title("Generator Cost Curves", color=TEXT_CLR) ax5.legend(fontsize=8) ax5.grid(True)
ax6 = fig.add_subplot(gs[2, 0]) lam = 2.0 * gen_a * P_opt + gen_b bars_lam = ax6.bar(x_pos, lam, color=gen_colors, width=0.5, edgecolor="#30363d", linewidth=0.8, zorder=3) lam_mean = np.average(lam, weights=P_opt) ax6.axhline(lam_mean, color=GOLD, lw=2, ls="--", label=f"Avg λ = {lam_mean:.3f} $/MWh") for i in range(N_GEN): ax6.text(i, lam[i] + 0.01, f"{lam[i]:.3f}", ha="center", fontsize=10, color=TEXT_CLR, fontweight="bold") ax6.set_xticks(x_pos) ax6.set_xticklabels(gen_names, fontsize=9) ax6.set_ylabel("Marginal Cost ($/MWh)") ax6.set_title("Incremental Cost (λ)", color=TEXT_CLR) ax6.legend(fontsize=9) ax6.grid(True, axis="y")
ax7 = fig.add_subplot(gs[2, 1]) demands = np.linspace(40, 420, 120) costs_sweep = [] g1_sweep = [] g2_sweep = [] g3_sweep = []
for pd in demands: def pb_sweep(Pg): return Pg.sum() - pd def lfc_sweep(Pg): _, fl = dc_power_flow(Pg, gen_bus, load_bus * pd / P_demand, lines, N_BUS, SLACK) c = [] for kk, (_, _, _, fm) in enumerate(lines): c.append(fm - fl[kk]) c.append(fm + fl[kk]) return np.array(c) con_s = [ {"type": "eq", "fun": pb_sweep}, {"type": "ineq", "fun": lfc_sweep}, ] p0_s = np.clip(gen_pmin + (gen_pmax - gen_pmin) * pd / P_total_cap, gen_pmin, gen_pmax) r_s = minimize(total_cost, p0_s, jac=total_cost_grad, method="SLSQP", bounds=bounds, constraints=con_s, options={"ftol": 1e-8, "maxiter": 300, "disp": False}) if r_s.success: costs_sweep.append(r_s.fun) g1_sweep.append(r_s.x[0]) g2_sweep.append(r_s.x[1]) g3_sweep.append(r_s.x[2]) else: costs_sweep.append(np.nan) g1_sweep.append(np.nan) g2_sweep.append(np.nan) g3_sweep.append(np.nan)
costs_sweep = np.array(costs_sweep) ax7.plot(demands, costs_sweep / 1000, color=GOLD, lw=2.5, label="Total cost") ax7.axvline(P_demand, color=ACCENT, lw=1.5, ls="--", label=f"Operating point ({P_demand} MW)") ax7.set_xlabel("Total Demand (MW)") ax7.set_ylabel("Optimal Cost (k$/h)") ax7.set_title("Cost vs. Total Demand", color=TEXT_CLR) ax7.legend(fontsize=9) ax7.grid(True)
ax8 = fig.add_subplot(gs[2, 2], projection="3d")
P1_grid = np.linspace(gen_pmin[0], gen_pmax[0], 60) P2_grid = np.linspace(gen_pmin[1], gen_pmax[1], 60) PP1, PP2 = np.meshgrid(P1_grid, P2_grid)
PP3 = np.clip(P_demand - PP1 - PP2, gen_pmin[2], gen_pmax[2]) CTOTAL = (gen_a[0]*PP1**2 + gen_b[0]*PP1 + gen_c[0] + gen_a[1]*PP2**2 + gen_b[1]*PP2 + gen_c[1] + gen_a[2]*PP3**2 + gen_b[2]*PP3 + gen_c[2])
mask = np.abs(PP1 + PP2 + PP3 - P_demand) > 5.0 CTOTAL[mask] = np.nan
surf = ax8.plot_surface(PP1, PP2, CTOTAL, cmap="plasma", alpha=0.85, linewidth=0, antialiased=True)
ax8.scatter([P_opt[0]], [P_opt[1]], [cost_opt], color=GOLD, s=120, zorder=10, depthshade=False, edgecolors="white", linewidths=1.5, label="Optimal")
ax8.set_xlabel("G1 (MW)", labelpad=6) ax8.set_ylabel("G2 (MW)", labelpad=6) ax8.set_zlabel("Total Cost ($/h)", labelpad=6) ax8.set_title("Cost Surface\n(G1 vs G2, G3 residual)", color=TEXT_CLR, pad=4) ax8.legend(fontsize=9, loc="upper left") ax8.tick_params(axis="both", which="major", labelsize=8) ax8.set_facecolor(PANEL_BG) fig.colorbar(surf, ax=ax8, shrink=0.45, pad=0.1, label="Cost ($/h)")
plt.savefig("opf_results.png", dpi=150, bbox_inches="tight", facecolor=DARK_BG) plt.show() print("\nFigure saved as opf_results.png")
|