Skip to content

Walkthrough PyPSA Unit Commitment

James Dunkerley edited this page Nov 8, 2026 · 9 revisions

Unit commitment: coal and gas on one bus

Built with dmg/20261108 (455f4f3f6): #1586, #1587, #1617, #1642–#1649

All walkthroughs

What it models

PyPSA's Unit Commitment (notebook at v1.4.0). One bus, one load, and two generators, coal and gas. Each case adds one operating limit and solves a mixed-integer problem: each committable generator is on or off in each timestep.

Case What it shows Timesteps
Minimum part load Coal cannot run below 30%, so gas covers the 800 MW hour 4
Minimum up time Gas, once on, stays on 3 timesteps 4
Minimum down time Coal, once off, stays off 2 timesteps 4
Start-up and shut-down costs 5,000 to start coal, 25 to stop gas 4
Time-varying start-up and shut-down costs The costs are series, so the cheap unit starts early 5
Ramp limits Coal ramps 10% up and 20% down per timestep 6
Ramp limits with expansion The same, with coal's size optimised 6
Ramp limits at start-up and shut-down Coal reaches only 10% in its first timestep on 7
Rolling horizon 30 timesteps, solved upstream in six overlapping windows 30

All nine cases are built, as ten networks in one project. None is folded away. Four networks are copies of an earlier one with a few cells changed (step 8). The tenth network is a copy of the rolling horizon case, run with a MIP gap (step 16).

The Python version is docs/examples/pypsa/unit-commitment/commit_generators_with_operating_limits.ipynb.

Data. Every case has a demand series, and one case has two cost series. A series cannot be typed in (see Hard to do), so each was written as a small CSV file. Each file has a snapshot column with hourly times from 2026-01-01 00:00:

File Columns
part-load.csv load: 4000, 6000, 5000, 800
time-varying.csv load: 0, 0.1, 10, 0.1, 0.1; start_up_cost: 0, 0, 100, 100, 100; shut_down_cost: 100, 100, 100, 100, 0
ramp.csv load: 4000, 7000, 7000, 7000, 7000, 3000
ramp-start-up.csv load: 0, 200, 7000, 7000, 7000, 2000, 0
rolling.csv load: 4000, 5000, 700, 800, 4000, six times

Upstream's timesteps are the integers 0, 1, 2 and so on. Here they are hours from 1 January 2026. The answer does not change.

Steps

1. Create the project

Projects → New project. Name: Unit commitment. Press Enter.

New project

2. Minimum part load: create the network

New network. Name: Minimum part load. Framework: PyPSA. Under Time axis, choose Define: Starts 01/01/2026 00:00, for 4 hours, Step Hourly. The form shows "4 steps · 2026-01-01 00:00 to 2026-01-01 03:00". Click Create network.

New network

3. Add the components

On the network page, Add component:

Type Name Bus Values
Bus bus
Load load bus (series in step 5)
Generator coal bus P Nom 10000, Marginal Cost 20, P Min Pu 0.3, Committable
Generator gas bus P Nom 1000, Marginal Cost 70, P Min Pu 0.1, Committable

Committable and every other unit commitment field (Start Up Cost, Shut Down Cost, Stand By Cost, Min Up Time, Min Down Time, Up Time Before, Down Time Before, Ramp Limit Start Up, Ramp Limit Shut Down) are under Show all 45 fields, near the bottom.

Add coal

The draft bar reads "4 unsaved changes"; the page still says "No components yet." Click Save version… and save as Minimum part load.

Draft

4. Why the load needs a file

Open Load, and click Add series on the P Set cell. The panel says "No series held" and offers only Load from CSV and Copy from another series. There is no way to type four values.

5. Load the demand series

Load from CSV, choose part-load.csv. Time column snapshot, Value column load. The fit box says "Same resolution, so nothing is filled". Click Load 4 values. This saves a version at once.

Load from CSV

6. Run

Run… → Run, with Solver highs and Method Default. The run succeeds with objective 356,000. The solver output ends with "MIP gap: 0.00e+00".

Run succeeded

7. Read the commitment

Open Generator. The outputs Status, Start Up and Shut Down are not shown. Click Columns, and under Unit commitment tick Status, Start Up and Shut Down. Each cell is a "Series · 4" chip. Click it to see the values.

coal · status reads 1, 1, 1, 0: coal is off in the last hour, where the 800 MW load is below its 3,000 MW part load. gas · status reads 0, 0, 0, 1.

Coal status

coal · start_up reads 0, 1, 1, 1, though coal starts no time. Start Up and Shut Down mean start-ups and shut-downs only when they carry a cost; with no cost, the solver may set them to 1. PyPSA returns the same values.

Coal start-up

8. Minimum up time: copy the network

On the network page, Network actions (the … button) → Save as new network…. Name: Minimum up time. Click Save. The copy has the components and the load series, and no results.

Save as new network

In Generator, edit the gas row: Stand By Cost 50, Up Time Before 0, Min Up Time 3. Save version… Gas minimum up time.

Now that the load has a series, its panel can be edited. Open Load, click the P Set chip, and double-click a value to change it. Set the rows to 4000, 800, 5000, 3000.

Edit the series

Click Done. A second draft bar, "Series draft", appears. Click Save as version…, then Save as a new version. Save it before you leave the browser: like the component draft, it is kept only in the browser's storage, and a fresh browser loses it.

Save the series draft

Run. Objective 306,150.

9. Minimum down time

Save as new network… from Minimum part load, name Minimum down time. Edit: coal Min Down Time 2, Down Time Before 1; gas P Nom 4000. Load series 3000, 800, 3000, 8000. Run. Objective 486,000. Coal is off for the first two timesteps.

10. Start-up and shut-down costs

Save as new network… from Minimum down time, name Start-up and shut-down costs. Edit: coal Down Time Before 0, Start Up Cost 5000; gas Shut Down Cost 25. Run. Objective 491,025.

Now Start Up and Shut Down are the events: coal start_up 0, 0, 1, 0; gas shut_down 0, 0, 1, 0. A custom statistic counts them. Statistics → Add custom statistic, Name coal_start_ups, Expression sum(Generator["coal"].start_up). The preview shows 1.

Start-ups statistic

The built-in opex is 225 k currency for coal: 220,000 of fuel plus the 5,000 start-up. It does not split them.

11. Time-varying start-up and shut-down costs

New network Time-varying start-up costs, 5 hours. Bus, load, and:

Name Values
cheap-committable Committable, P Nom 10, Marginal Cost 0, P Min Pu 0.01, Stand By Cost 10, Up Time Before 0, Down Time Before 0
expensive-fallback Committable, P Nom 10, Marginal Cost 100, Start Up Cost 0.1, Shut Down Cost 0.1

Add cheap-committable

Save the version. From time-varying.csv, load three series: load P Set from column load, and cheap-committable Start Up Cost and Shut Down Cost from the columns of the same names. Start Up Cost has an Add series button in its cell, as P Set does.

Start-up cost series

Run. Objective 40. cheap-committable starts in timestep 1, where starting is free, not in timestep 2, where the load peaks and starting costs 100. It stays on to the end.

12. Ramp limits

New network Ramp limits, 6 hours. No generator is committable:

Name Values
coal P Nom 10000, Marginal Cost 20, Ramp Limit Up 0.1, Ramp Limit Down 0.2
gas P Nom 4000, Marginal Cost 70

Ramp Limit Up and Ramp Limit Down are under Show all 45 fields too.

Add coal with ramp limits

Load P Set from ramp.csv. Run. Objective 950,000. coal · p climbs 1,000 MW a timestep (10% of 10,000) and falls 2,000 MW.

Coal dispatch with ramp limits

13. Ramp limits with expansion

Save as new network… from Ramp limits, name Ramp limits with expansion. On the coal row, tick P Nom Extendable, set Capital Cost 100 and P Nom 0 (upstream gives no P Nom).

Coal extendable

Save and run. Objective 1,675,000. Coal P Nom Opt is 5,000 MW, so it ramps 500 MW up and 1,000 MW down per timestep.

14. Ramp limits at start-up and shut-down

New network Ramp limits at start-up and shut-down, 7 hours:

Name Values
coal Committable, P Nom 10000, Marginal Cost 20, P Min Pu 0.05, Ramp Limit Up 0.2, Ramp Limit Down 0.25, Ramp Limit Start Up 0.1, Ramp Limit Shut Down 0.15
gas P Nom 10000, Marginal Cost 70

Upstream also sets initial_status=0 on coal. The sheet has no such field. PyPSA 1.4.0 does not read it either, so the answer is the same without it; the Python version checks this.

Load P Set from ramp-start-up.csv. Run. Objective 1,149,000. coal · p: 0, 0, 1000, 3000, 4000, 1500, 0. It starts at 1,000 MW (10%), and its last 1,500 MW (15%) is the most it may hold before shutting down.

Coal dispatch with start-up ramps

15. Rolling horizon

New network Rolling horizon, 30 hours:

Name Values
coal Committable, P Nom 10000, Marginal Cost 20, P Min Pu 0.3, Min Up Time 3, Min Down Time 2, Up Time Before 1, Ramp Limit Up 1, Ramp Limit Down 1, Ramp Limit Start Up 1, Ramp Limit Shut Down 1, Start Up Cost 200, Shut Down Cost 150
gas Committable, P Nom 1000, Marginal Cost 70, P Min Pu 0.1, Min Up Time 3, Up Time Before 2, Start Up Cost 50, Shut Down Cost 20

Add coal for the rolling horizon

Load P Set from rolling.csv. Run. Objective 2,227,450, in 0.4 s.

Upstream solves this in six windows of 5 timesteps plus 2 of overlap, each window starting from the last one's state. The UI has no rolling horizon; it solves all 30 timesteps in one problem. Here both give the same cost (see the results).

16. Set the MIP gap

The run dialog's Advanced section has Mip gap, "The relative MIP gap at which HiGHS stops (mip_rel_gap)", and Linearized unit commitment, which relaxes the problem to a linear one.

To set them on a network that has been run, you must change it first. On the solved Rolling horizon, Run… is greyed out; its tooltip says "This version is already solved."

Run is greyed out

So: Save as new network… Rolling horizon, MIP gap 0.5, then Run… → Advanced, Mip gap 0.5, Run.

Mip gap

The log shows solver_options={'mip_rel_gap': 0.5} and "Gap 0% (tolerance: 50%)". The problem is small enough that HiGHS still proves the optimum: objective 2,227,450.

Results compared with PyPSA

PyPSA 1.4.0 with HiGHS 1.15.1, upstream's code for each case, run from the backend environment. "Status" is each committable generator's on/off series.

Case Quantity UI PyPSA Match
Minimum part load Objective 356,000 356,000 Yes
coal p / status 4000, 6000, 5000, 0 / 1, 1, 1, 0 same Yes
gas p / status 0, 0, 0, 800 / 0, 0, 0, 1 same Yes
Minimum up time Objective 306,150 306,150 Yes
coal p / status 3900, 0, 4900, 3000 / 1, 0, 1, 1 same Yes
gas p / status 100, 800, 100, 0 / 1, 1, 1, 0 same Yes
Minimum down time Objective 486,000 486,000 Yes
coal p / status 0, 0, 3000, 8000 / 0, 0, 1, 1 same Yes
gas p / status 3000, 800, 0, 0 / 1, 1, 0, 0 same Yes
Start-up and shut-down costs Objective 491,025 491,025 Yes
coal start_up / shut_down 0, 0, 1, 0 / 1, 0, 0, 0 same Yes
gas shut_down 0, 0, 1, 0 same Yes
Time-varying costs Objective 40 40 Yes
cheap-committable p / status 0, 0.1, 10, 0.1, 0.1 / 0, 1, 1, 1, 1 same Yes
cheap-committable start_up / shut_down 1, 1, 0, 0, 0 / 0, 0, 0, 0, 1 same Yes
Ramp limits Objective 950,000 950,000 Yes
coal p 4000, 5000, 6000, 7000, 5000, 3000 same Yes
gas p 0, 2000, 1000, 0, 2000, 0 same Yes
Ramp limits with expansion Objective 1,675,000 1,675,000 Yes
coal P Nom Opt 5,000 MW 5,000 MW Yes
coal p 4000, 4500, 5000, 5000, 4000, 3000 same Yes
Ramp limits at start-up and shut-down Objective 1,149,000 1,149,000 Yes
coal p / status 0, 0, 1000, 3000, 4000, 1500, 0 / 0, 0, 1, 1, 1, 1, 0 same Yes
gas p 0, 200, 6000, 4000, 3000, 500, 0 same Yes
Rolling horizon, one window Objective 2,227,450 2,227,450 Yes
coal status 1, 1, 0, 0, then 1, 1, 1, 0, 0 five times, then 1 same Yes
gas p, all 30 values as PyPSA called as the UI calls it same Yes
Rolling horizon, upstream's six windows Total cost of the joined result 2,227,450 2,225,000 fuel + 1,450 start-up + 1,000 shut-down = 2,227,450 Same cost

Every number matches, including the Start Up and Shut Down series where they carry no cost (step 7).

Two notes on the rolling horizon:

  • Gas's 100 MW can sit in different hours at the same cost. PyPSA's default call runs gas at 100 MW in hours 9, 16 and 21; the UI in hours 6, 19 and 24. Called with io_api='direct' and set_names=False, as the UI calls it, PyPSA gives the UI's 30 values exactly.
  • Upstream's six windows reach the same cost here as one 30-timestep solve. With a longer load profile they need not. The UI cannot do the windowed solve.

Missing features

  • Rolling horizon. Upstream solves in overlapping windows, each starting from the last window's state. The UI solves the whole time axis at once. For these 30 timesteps the cost is the same; for a year of hourly commitment it is the difference between solving and not.
  • A split of opex. Built-in opex adds start-up and shut-down costs to fuel cost (coal 225 k currency = 220,000 + 5,000). There is no start-up cost column.
  • Initial Status. Upstream sets initial_status=0. The sheet has no field for it. PyPSA 1.4.0 ignores it, so nothing is lost today.

Hard to do

  • A series cannot be typed in. "No series held" offers Load from CSV and Copy from another series only. A 4-value demand needs a CSV file. Once a series exists, its values can be edited in the panel.
  • Run is greyed out on a solved version. To try a MIP gap or Linearized unit commitment on a network you have run, copy the network or change it first. The reason is only in a tooltip: "This version is already solved."
  • The commitment is hidden. Status, Start Up and Shut Down are off in Columns, and each shows as a "Series · 4" chip. Reading on/off for two generators means opening four panels.
  • Start Up and Shut Down are not events unless they cost something. With no start-up cost, coal reads start_up 0, 1, 1, 1 while it never starts. PyPSA does the same, but the UI shows them with no note, and a custom statistic that counts them is wrong.
  • The unit commitment fields are under "Show all 45 fields", below Fom Cost and E Sum Max: Committable, Start Up Cost, Min Up Time and the rest, and Ramp Limit Up and Down.
  • The series draft is kept in the browser. An edited series that is not saved is gone in a fresh browser, as the component draft is.
  • Series charts join the points with a slope. A 0/1 status reads as a ramp from 2:00 to 3:00, and the axis ticks every 15 minutes for hourly data.
  • The sidebar cuts network names to about ten characters. Three networks read "Minimum …", two "Ramp limit…" and two "Rolling hor…".
  • Min Up Time and the like are in "snapshots", where the rest of the UI says timesteps.
  • Recurring: the network page says "No components yet." with 4 unsaved changes; each CSV series load and each custom statistic saves its own version; output columns come after every input column; Add component is only on the network page; statistics read GW and k currency; the run dialog says "The solver linopy hands the model to."

Clone this wiki locally