-
Notifications
You must be signed in to change notification settings - Fork 65
Fix interaction between survival curves and early retirement #294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ParticularlyPythonicBS
merged 3 commits into
TemoaProject:unstable
from
idelder:fix/survival_curve_retirements
Apr 8, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reject zero survival fractions before end-of-life.
This check only validates the endpoints, so a curve that reaches
0beforeeol_yearand stays there still passes.adjusted_capacity_constraint()later dividesv_retired_capacitybylifetime_survival_curve[r, S_p, t, v], so any eligible early-retirement period at or after that first zero still hits the divide-by-zero path this PR is trying to eliminate.🛠️ Suggested validation hardening
if ( p_first != v or p_last != eol_year or value(model.lifetime_survival_curve[r, v, t, v]) != 1 or value(model.lifetime_survival_curve[r, eol_year, t, v]) != 0 ): msg = ( 'lifetime_survival_curve must be defined as 1 at start and 0 at end of life. Must ' f'define ({r}, >{v}<, {t}, {v}) = 1 and ({r}, >{eol_year}<, {t}, {v}) = 0.' ) logger.error(msg) raise ValueError(msg) + + for p_chk in periods_rtv[:-1]: + if value(model.lifetime_survival_curve[r, p_chk, t, v]) <= 0: + msg = ( + 'lifetime_survival_curve must stay strictly positive before end of life. ' + f'Found non-positive value at {(r, p_chk, t, v)}.' + ) + logger.error(msg) + raise ValueError(msg)🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lifetime_survival_curve has a default value of zero so it will always be zero outside the lifetime. I don't believe this case would ever come up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh wait I see okay there is a check that logs as info but should be an error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Let me look at the actual constraint to verify whether it already guards against zero survival fractions.
🏁 Script executed:
Length of output: 2771
@idelder, I understand the argument about the default — years outside the lifetime will always be 0 by default, and the constraint conditions (S_p + period_length ≤ eol_year) correctly exclude those. My concern is about years inside the lifetime (strictly betweenvandeol_year) where the curve could legally reach 0 beforeeol_yearand still pass every current validation check.The two relevant division sites found in
capacity.pyare:Line 322 (inside
annual_retirement_constraint):Line 557 (inside
adjusted_capacity_constraint):with the guard
v < S_p <= p and S_p + period_length[S_p] <= eol_year.A concrete scenario that passes all current validation but triggers the divide-by-zero:
lifetime_survival_curve[r, year, t, v]vv+3v+5(=eol_year)p_first == v✓,p_last == eol_year✓, starts at 1 ✓, ends at 0 ✓, monotonically non-increasing ✓ — all checks pass.v+3withperiod_length = 2, thenS_p + period_length = v+5 = eol_year ≤ eol_year— the loop condition is satisfied.v_retired_capacity[r, v+3, t, v]bylifetime_survival_curve[r, v+3, t, v] = 0.0→ ZeroDivisionError.The proposed fix (rejecting any non-positive value in
periods_rtv[:-1]) is the minimal safeguard:This runs after interpolation has filled in all intermediate years, so it catches both explicitly-set zeros and interpolated-to-zero values before the model ever runs.
🧠 Learnings used
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@idelder, yes — that's exactly the spot. The existing block in the monotonicity loop:needs to become a hard error when
p != p_last, because silentlycontinue-ing allows the near-zero value to persist inlifetime_survival_curvefor an intermediate year, which is then used as a divisor in the constraints:The
continuebranch forp == p_lastis correct and can stay as-is — reaching zero exactly ateol_yearis the expected terminal condition and is already excluded by the constraint guards. Only the intermediate near-zero case needs to be aValueError.🧠 Learnings used