gens.cap = gens[:, :Pmax] .- gens[:, :Pmin]
gens_nr = sort!(gens[G, :], [:VarCost, order(:cap, rev=true)])
function plot_supply_curve(supply_curve, demand)
rectangle(w, h, x, y) = Shape(x .+ [0,w,w,0], y .+ [0,0,h,h])
p = plot(grid=:false, color_palette=:mk_15, size=(700, 400), titlefontsize=20, top_margin=10mm, right_margin=5mm)
plot!(legend=:outerright, legendcolumns=1)
marg_gen = 0
marg_price = 0
x = 0
plot!(p, rectangle(sum(supply_curve.Pmin), 0.5, x, 0), opacity=.5, label = "minimum", color="black")
x = x + sum(supply_curve.Pmin)
for i in 1:nrow(supply_curve)
if supply_curve[i, :VarCost] == 0
plot!(p, rectangle(supply_curve[i, :cap], 0.5,x,0), opacity=.5, label = supply_curve[i, :Plant])
else
plot!(p, rectangle(supply_curve[i, :cap], supply_curve[i, :VarCost],x,0), opacity=.5, label = supply_curve[i, :Plant])
end
if (x < demand) && (x + supply_curve[i,:cap] > demand)
marg_gen = i
marg_price = supply_curve[i,:VarCost]
end
x = x + supply_curve[i,:cap]
end
vline!([demand],linecolor="black",linewidth=3, linestyle=:dash, label = "demand")
title!("Dispatch Stack Supply Curve")
xlabel!("Capacity (MW)")
ylabel!("Marginal Cost (\$\$/MW)")
return (p, marg_price)
end
p, marg_price = plot_supply_curve(gens_nr, d)
plot!(p, size=(1300, 500))