Skip to content

Bug Fix: rolling horizon with linearized UC and ramp limits#1489

Merged
lkstrp merged 5 commits intomasterfrom
fix-rh
Dec 11, 2025
Merged

Bug Fix: rolling horizon with linearized UC and ramp limits#1489
lkstrp merged 5 commits intomasterfrom
fix-rh

Conversation

@Irieo
Copy link
Copy Markdown
Contributor

@Irieo Irieo commented Dec 10, 2025

Follows discussion in #1454

Changes proposed in this Pull Request

When using optimize_with_rolling_horizon with linearized_unit_commitment and ramp limits defined for committable generators, the optimization fails with "KeyError: "not all values found in index 'snapshot'". Here is a minimalistic example to reproduce h/t @thomgeo

import pypsa
n = pypsa.examples.scigrid_de()

disp = ["Gas", "Hard Coal", "Waste", "Brown Coal", "Oil", "Nuclear"]
n.generators.loc[n.generators.carrier.isin(disp), "committable"] = True
n.generators.loc[n.generators.carrier.isin(disp), "ramp_limit_up"] = 0.5

n.optimize.optimize_with_rolling_horizon(
    solver_name="gurobi", linearized_unit_commitment=True, horizon=12
)

(FYI @thomgeo though it is not related to ramp values >1 per se)

The error occurs in the ramp limit constraints for committable components when trying to adjust the RHS for the first snapshot of each rolling horizon window. The problematic code is here:

if is_rolling_horizon:
status_start = c.dynamic["status"].iloc[start_i].loc[original_com_i]
limit_diff = (limit_up - limit_start).isel(snapshot=0)
rhs.loc[{"snapshot": rhs.coords["snapshot"].item(0)}] += (
limit_diff * status_start
)

If we step in with debugger in this line for an example above: the .item(0) method returns the snapshot coordinate's int64 representation instead of a datetime object.

{"snapshot": rhs.coords["snapshot"].item(0)}
{'snapshot': 1293883200000000000}

Later this integer is used to index into a DatetimeIndex, and pandas complains.

For the fix I use xarray position-based .isel() indexing instead for two places in the code (ramp up/down). I'll add some tests too later tonight.

Checklist

  • Code changes are sufficiently documented; i.e. new functions contain docstrings and further explanations may be given in docs.
  • Unit tests for new features were added (if applicable).
  • A note for the release notes docs/release-notes.md of the upcoming release is included.
  • I consent to the release of this PR's code under the MIT license.

@lkstrp lkstrp merged commit 6c97817 into master Dec 11, 2025
22 checks passed
@lkstrp lkstrp deleted the fix-rh branch December 11, 2025 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants