Clearing Electricity Markets Using PyPSA
In The Supply and Demand of Electricity, we discussed the electricity market and the economic dispatch (ED) optimization problem. In this blog post, we will show how to use PyPSA to recreate the results of Example 2.4. Example 2.4 consists of an electrical grid with two market nodes, and , connected by a lossy transmission line. Node has a generator, , and node has another generator and two loads and . The generators and loads have made offers and bids in the electricity market, with price-quantity pairs shown in Figure 3.

Figure 1:Network of Example 2.4.
We start by defining a PyPSA Network:
import pypsa
network = pypsa.Network()We define the market nodes and as two electrical Bus components.
network.add("Bus", name="A")
network.add("Bus", name="B")Transmission lines are modeled by the Line component, but because the transmission line of Example 2.4 has a fixed efficiency for simplicity, we model it as a Link component:
network.add(
"Link",
name="line",
bus0="A",
bus1="B",
efficiency=0.75,
p_nom_extendable=True,
p_min_pu=-1,
p_max_pu=1,
)The generators and loads have piecewise linear dispatch curves, but PyPSA supports only linear or quadratic dispatch curves (using the marginal_cost and marginal_cost_quadratic parameters). PyPSA does this because it is designed for planning and research purposes. It is designed to work with the typical marginal cost of each generator, rather than the exact price-quantity pairs that each generator has submitted in the past for each market hour.
Following the approach discussed in Higher-Dimensional Optimization Problems, we will model each resource (generator or load) as multiple resources—one for each price-quantity pair submitted by the resource.
For example, because generator submitted two nonzero price-quantity pairs, we will model the generator in PyPSA as two generators. According to its price-quantity pairs, generator is willing to produce up to at , or up to at . We model this as a generator that has a marginal cost of , plus a generator that has a marginal cost of .
Thus, we define the Generator components as follows:
network.add("Generator", name="G1-1", bus="A", p_nom=6, marginal_cost=2)
network.add("Generator", name="G1-2", bus="A", p_nom=3, marginal_cost=7)
network.add("Generator", name="G2-1", bus="B", p_nom=7, marginal_cost=4)
network.add("Generator", name="G2-2", bus="B", p_nom=3, marginal_cost=10)We will use the same approach for the loads. PyPSA’s Load component does not support the marginal_cost parameter, but PyPSA’s documentation explicitly states to use the Generator component with a negative sign and negative marginal_cost for this purpose:
network.add("Generator", name="L1-1", bus="B", p_nom=4, marginal_cost=-8, sign=-1)
network.add("Generator", name="L1-2", bus="B", p_nom=4, marginal_cost=-5, sign=-1)
network.add("Generator", name="L2-1", bus="B", p_nom=3, marginal_cost=-9, sign=-1)
network.add("Generator", name="L2-2", bus="B", p_nom=6, marginal_cost=-3, sign=-1)Network.optimize() solves the optimal power flow (OPF) problem, which extends economic dispatch with linearized (i.e., approximate) power flow constraints. In this sense, optimize() acts also to simulate the grid and the market clearing process.
network.optimize()By inspecting network.generators_t and network.buses_t, we can see the market-clearing quantities of the generators and loads, as well as the locational marginal prices (LMPs) of the two nodes:
network.generators_t["p"].loc["now"].groupby(
lambda name: name.split("-")[0]
).sum().to_frame(r"$Q^*$ [MW]").rename_axis(None).Tnetwork.buses_t["marginal_price"].loc["now"].to_frame("$P^*$ [$/MWh]").rename_axis(
"Node"
).T