Post · 7 min read· pp. 169–176

Quickstart 1 - Markets

Problem Description

Consider the following simple electricity market with two zones, each with a generator and a load. The demand in zone 1 is 500 MW and in zone 2 is 1500 MW. The generators have a nominal capacity of 2000 MW each, with cost functions defined as follows:

  • Zone 1: C1(g1) = 10 g1 + 0.005 g1²
  • Zone 2: C2(g2) = 13 g2 + 0.01 g2²

Find the least-cost dispatch of the generators to meet the load while respecting the transmission capacity. Identify the marginal prices at each bus and the flow on the transmission line. Calculate the congestion rent.

PyPSA Solution

For installation instructions, consult the Installation section first.

The first step is always to import the pypsa module:

In [2]:

Copied! diagram

ipynb
import pypsa

import pypsa

A new PyPSA network instance can be created with the pypsa.Network constructor.

In [3]:

Copied! diagram

ipynb
n = pypsa.Network()

n = pypsa.Network()

text
/tmp/ipykernel_3200/3563396155.py:1: PyPSA2Warning: pandas reads strings as `str` dtype since 3.0. PyPSA converts them back to object dtype on import, but keeps `str` from PyPSA 2.0 on. Set `pypsa.options.future.string_dtype` to True to opt in now or to False to keep the current behaviour.
  n = pypsa.Network()

Components like buses can be added with n.add() and registered under an arbitrary name, e.g. "zone_1":

In [4]:

Copied! diagram

ipynb
n.add("Bus", "zone_1")
n.add("Bus", "zone_2")
n.buses

n.add("Bus", "zone_1") n.add("Bus", "zone_2") n.buses

Out[4]:

v_nomtypexycarrierunitscalinglocationv_mag_pu_setv_mag_pu_minv_mag_pu_maxcontrolgeneratorsub_network
name
zone_11.00.00.0AC1.01.00.0infPQ
zone_21.00.00.0AC1.01.00.0infPQ

Next, add the loads to the network, where p_set specifies the power demand in MW at the corresponding bus:

In [5]:

Copied! diagram

ipynb
n.add("Load", "load_1", bus="zone_1", p_set=500)
n.add("Load", "load_2", bus="zone_2", p_set=1500)
n.loads

n.add("Load", "load_1", bus="zone_1", p_set=500) n.add("Load", "load_2", bus="zone_2", p_set=1500) n.loads

Out[5]:

buscarriertypep_setq_setsignactive
name
load_1zone_1500.00.0-1.0True
load_2zone_21500.00.0-1.0True

Generators are added in a similar way, where p_nom specifies the nominal capacity, and marginal_cost and marginal_cost_quadratic specify the linear and quadratic coefficients of the cost function:

In [6]:

Copied! diagram

ipynb
n.add(
    "Generator",
    "gen_1",
    bus="zone_1",
    p_nom=2000,
    marginal_cost=10,
    marginal_cost_quadratic=0.005,
)
n.add(
    "Generator",
    "gen_2",
    bus="zone_2",
    p_nom=2000,
    marginal_cost=13,
    marginal_cost_quadratic=0.01,
)
n.generators

n.add( "Generator", "gen_1", bus="zone_1", p_nom=2000, marginal_cost=10, marginal_cost_quadratic=0.005, ) n.add( "Generator", "gen_2", bus="zone_2", p_nom=2000, marginal_cost=13, marginal_cost_quadratic=0.01, ) n.generators

Out[6]:

buscontroltypep_nomp_nom_modp_nom_extendablep_nom_minp_nom_maxp_nom_setp_min_pu...down_time_beforeramp_limit_upramp_limit_downramp_limit_start_upramp_limit_shut_downramp_up_costramp_down_costweightp_nom_optcapital_cost_piecewise_opt
name
gen_1zone_1PQ2000.00.0False0.0infNaN0.0...0NaNNaNNaNNaN0.00.01.00.00.0
gen_2zone_2PQ2000.00.0False0.0infNaN0.0...0NaNNaNNaNNaN0.00.01.00.00.0

2 rows × 49 columns

Lines connecting two buses bus0 and bus1 are added with a nominal capacity s_nom in MW and a reactance x in Ohm (which is required for modelling power flow):

In [7]:

Copied! diagram

ipynb
n.add("Line", "line_1", bus0="zone_1", bus1="zone_2", x=0.01, s_nom=400)
n.lines

n.add("Line", "line_1", bus0="zone_1", bus1="zone_2", x=0.01, s_nom=400) n.lines

Out[7]:

bus0bus1typexrgbs_noms_nom_mods_nom_extendable...v_ang_maxsub_networkx_pur_pug_pub_pux_pu_effr_pu_effs_nom_optcapital_cost_piecewise_opt
name
line_1zone_1zone_20.010.00.00.0400.00.0False...inf0.00.00.00.00.00.00.00.0

1 rows × 36 columns

With all components added, the network can be optimised with the n.optimize() method. This function constructs the optimisation problem with the linopy library, solves it with a selected solver , and stores the results in the network instance n.

In [8]:

Copied! diagram

ipynb
n.optimize()

n.optimize()

text
/tmp/ipykernel_3200/1261279110.py:1: PyPSA2Warning: From PyPSA 2.0 on, the objective leaves out the capital costs of existing capacity on extendable assets, which improves LP conditioning. Pass `include_objective_constant` to `n.optimize` to choose per call. Set `pypsa.options.future.exclude_objective_constant` to True to opt in now or to False to keep the current behaviour.
  n.optimize()
text
WARNING:pypsa.consistency:The following buses have carriers which are not defined. Run n.sanitize() to add them. Components with undefined carriers:
Index(['zone_1', 'zone_2'], dtype='object', name='name')
text
WARNING:pypsa.consistency:The following lines have carriers which are not defined. Run n.sanitize() to add them. Components with undefined carriers:
Index(['line_1'], dtype='object', name='name')
text
WARNING:pypsa.consistency:The following lines have zero r, which could break the linear load flow:
Index(['line_1'], dtype='object', name='name')
text
INFO:linopy.model: Solve problem using Gurobi solver
text
INFO:linopy.model:Solver options:
 - log_to_console: False
text
INFO:linopy.io: Writing time: 0.04s
text
Set parameter WLSAccessID
text
Set parameter WLSSecret
text
Set parameter LicenseID to value 2537914
text
Academic license 2537914 - for non-commercial use only - registered to l.___@tu-berlin.de
text
Read LP format model from file /tmp/linopy-problem-msjufik7.lp
text
Reading time = 0.00 seconds
text
obj: 8 rows, 3 columns, 10 nonzeros
text
Set parameter LogToConsole to value 0
text
INFO:linopy.constants: Optimization successful: 
Status: ok
Termination condition: optimal
Solution: 3 primals, 8 duals
Objective: 3.95e+04
Solver: gurobi
Runtime: 0.00s
Dual bound: 3.94e+04
Solver model: available
Solver message: 2
text
INFO:pypsa.optimization.optimize:The shadow-prices of the constraints Generator-fix-p-lower, Generator-fix-p-upper, Line-fix-s-lower, Line-fix-s-upper were not assigned to the network.

Out[8]:

text
('ok', 'optimal')

The optimised generators dispatch can be accessed with:

In [9]:

Copied! diagram

ipynb
n.generators_t.p

n.generators_t.p

Out[9]:

namegen_1gen_2
snapshot
now900.01100.0

The market clearing prices per bus can be accessed with:

In [10]:

Copied! diagram

ipynb
n.buses_t.marginal_price

n.buses_t.marginal_price

Out[10]:

namezone_1zone_2
snapshot
now19.035.0

The optimised flows on transmission lines can be accessed with:

In [11]:

Copied! diagram

ipynb
n.lines_t.p1

n.lines_t.p1

Out[11]:

nameline_1
snapshot
now-400.0

Here, n.lines_t.p0 denotes flow from bus0 to bus1 if values are positive. The related attribute n.lines_t.p1 denotes flow from bus1 to bus0 if values are positive.

The congestion rent can be calculated as the product of the flow on the line and the price difference between the two buses:

In [12]:

Copied! diagram

ipynb
n.buses_t.marginal_price.eval("zone_2 - zone_1") * n.lines_t.p0["line_1"]

n.buses_t.marginal_price.eval("zone_2 - zone_1") * n.lines_t.p0["line_1"]

Out[12]:

text
snapshot
now    6400.0
dtype: float64

This example is based on Tom Brown's Energy Systems course, taken from the lecture on Complex Markets, slides 37ff.

diagram 2026-11-05

diagram 2025-10-09