-
Notifications
You must be signed in to change notification settings - Fork 28
add Secant-based methods for ELCC and EFC calculations #99
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
Open
qian-harvard
wants to merge
14
commits into
NREL:main
Choose a base branch
from
qian-harvard:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e8dc466
add Secant-based methods for ELCC and EFC calculations
qian-harvard 5211e61
Update docs/src/quickstart.md
qian-harvard 1067e68
Update docs/src/quickstart.md
qian-harvard cc42813
Update PRASCapacityCredits.jl/src/EFC_Secant.jl
qian-harvard 46165bf
Update PRASCapacityCredits.jl/src/EFC_Secant.jl
qian-harvard d52649a
Update docs/src/quickstart.md
qian-harvard c91d0e3
Update PRASCapacityCredits.jl/src/ELCC_Secant.jl
qian-harvard f0fd736
Update PRASCapacityCredits.jl/src/ELCC_Secant.jl
qian-harvard cafc0ca
Update PRASCapacityCredits.jl/src/ELCC_Secant.jl
qian-harvard 5c4eeab
Update PRASCapacityCredits.jl/src/EFC_Secant.jl
qian-harvard 5a54b97
Update PRASCapacityCredits.jl/src/EFC_Secant.jl
qian-harvard f2b0084
Update PRASCapacityCredits.jl/src/ELCC_Secant.jl
qian-harvard 18e08f9
Add Secant-based EFC and ELCC methods with tests
qian-harvard 7c42177
Refactor secant method bracketing in EFC and ELCC
qian-harvard 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| # Secant-based Equivalent Firm Capacity (EFC) Method | ||
| # | ||
| # This implementation uses a Secant-based root-finding approach to find the capacity credit. | ||
| # | ||
| # Key Advantages: | ||
| # - Speed: On benchmarking with the RTS system, the Secant method is generally 2-3 times | ||
| # faster than bisection as it uses metric gradients to approach the root more directly. | ||
| # - Informed Stepping: Unlike bisection, which reduces search space by a fixed amount, | ||
| # the Secant method takes informed steps, reducing the number of costly system assessments. | ||
| # - Robustness: More robust to different perturbation step sizes and provides a | ||
| # converged point estimate while strictly honoring user-specified gap tolerances. | ||
|
|
||
| struct EFC_Secant{M} <: CapacityValuationMethod{M} | ||
|
|
||
| capacity_max::Int | ||
| capacity_gap::Int | ||
| p_value::Float64 | ||
| regions::Vector{Tuple{String,Float64}} | ||
| verbose::Bool | ||
|
|
||
| function EFC_Secant{M}( | ||
| capacity_max::Int, regions::Vector{Pair{String,Float64}}; | ||
| capacity_gap::Int=1, p_value::Float64=0.05, verbose::Bool=false) where M | ||
|
|
||
| @assert capacity_max > 0 | ||
| @assert capacity_gap > 0 | ||
| @assert 0 < p_value < 1 | ||
| @assert sum(x.second for x in regions) ≈ 1.0 | ||
|
|
||
| return new{M}(capacity_max, capacity_gap, p_value, Tuple.(regions), verbose) | ||
|
|
||
| end | ||
|
|
||
| end | ||
|
|
||
| function EFC_Secant{M}( | ||
| capacity_max::Int, region::String; kwargs... | ||
| ) where M | ||
| return EFC_Secant{M}(capacity_max, [region=>1.0]; kwargs...) | ||
| end | ||
|
|
||
| function assess(sys_baseline::S, sys_augmented::S, | ||
| params::EFC_Secant{M}, simulationspec::SequentialMonteCarlo | ||
| ) where {N, L, T, P, S <: SystemModel{N,L,T,P}, M <: ReliabilityMetric} | ||
|
|
||
| _, powerunit, _ = unitsymbol(sys_baseline) | ||
|
|
||
| regionnames = sys_baseline.regions.names | ||
| regionnames != sys_augmented.regions.names && | ||
| error("Systems provided do not have matching regions") | ||
|
|
||
| # Add firm capacity generators to the relevant regions | ||
| efc_gens, sys_variable, sys_target = | ||
| add_firmcapacity(sys_baseline, sys_augmented, params.regions) | ||
|
|
||
| target_metric = M(first(assess(sys_target, simulationspec, Shortfall()))) | ||
|
|
||
| capacities = Int[] | ||
| metrics = typeof(target_metric)[] | ||
|
|
||
| # Initial points for Secant method | ||
| # L = 0 MW | ||
| c_low = 0 | ||
| update_firmcapacity!(sys_variable, efc_gens, c_low) | ||
| m_low = M(first(assess(sys_variable, simulationspec, Shortfall()))) | ||
| f_low = val(m_low) - val(target_metric) | ||
| push!(capacities, c_low) | ||
| push!(metrics, m_low) | ||
|
|
||
| # U = Max Capacity | ||
| c_high = params.capacity_max | ||
| update_firmcapacity!(sys_variable, efc_gens, c_high) | ||
| m_high = M(first(assess(sys_variable, simulationspec, Shortfall()))) | ||
| f_high = val(m_high) - val(target_metric) | ||
| push!(capacities, c_high) | ||
| push!(metrics, m_high) | ||
|
|
||
| # Bracketing check/search | ||
| if f_low * f_high > 0 | ||
| # If they don't bracket, we scan to find a bracket | ||
| found_bracket = false | ||
| step = max(params.capacity_gap, 1) | ||
|
|
||
| # We assume f is decreasing (EFC). If both are negative, we are already above target? | ||
| # If f_low < 0, then even at 0 MW firm capacity, augmented is better than baseline. EFC = 0. | ||
| if f_low < 0 | ||
| return CapacityCreditResult{typeof(params), typeof(target_metric), P}( | ||
| target_metric, 0, 0, capacities, metrics) | ||
| end | ||
|
|
||
| # If both are positive, scan upwards | ||
| c_prev = c_low | ||
| f_prev = f_low | ||
| for c in step:step:params.capacity_max | ||
| update_firmcapacity!(sys_variable, efc_gens, c) | ||
| m_c = M(first(assess(sys_variable, simulationspec, Shortfall()))) | ||
| f_c = val(m_c) - val(target_metric) | ||
| push!(capacities, c) | ||
| push!(metrics, m_c) | ||
|
|
||
| if f_c * f_prev <= 0 | ||
| c_low, c_high = c_prev, c | ||
| f_low, f_high = f_prev, f_c | ||
| found_bracket = true | ||
| break | ||
| end | ||
| c_prev, f_prev = c, f_c | ||
| end | ||
|
|
||
| if !found_bracket | ||
| # If still not found, return the best we have (either 0 or max) | ||
| final_val = f_high > 0 ? c_high : c_low | ||
| return CapacityCreditResult{typeof(params), typeof(target_metric), P}( | ||
| target_metric, final_val, final_val, capacities, metrics) | ||
| end | ||
| end | ||
|
|
||
| # Robust Secant (False Position / Regula Falsi) loop | ||
| iter = 0 | ||
| max_iter = 100 | ||
|
Comment on lines
+119
to
+120
|
||
|
|
||
| while (c_high - c_low) > params.capacity_gap && iter < max_iter | ||
| iter += 1 | ||
|
|
||
| # Secant/Interpolation guess | ||
| if abs(f_high - f_low) < 1e-12 | ||
| c_mid = div(c_low + c_high, 2) | ||
| else | ||
| c_mid_float = c_high - f_high * (c_high - c_low) / (f_high - f_low) | ||
| c_mid = round(Int, c_mid_float) | ||
|
|
||
| # Ensure we are making progress and staying in the bracket | ||
| if c_mid <= c_low || c_mid >= c_high | ||
| c_mid = div(c_low + c_high, 2) | ||
| end | ||
| end | ||
|
|
||
| update_firmcapacity!(sys_variable, efc_gens, c_mid) | ||
| m_mid = M(first(assess(sys_variable, simulationspec, Shortfall()))) | ||
| f_mid = val(m_mid) - val(target_metric) | ||
| push!(capacities, c_mid) | ||
| push!(metrics, m_mid) | ||
|
|
||
| if f_mid * f_low > 0 | ||
| c_low, f_low = c_mid, f_mid | ||
| else | ||
| c_high, f_high = c_mid, f_mid | ||
| end | ||
| end | ||
|
|
||
| # Return the converged value (conservative lower bound of the bracket) | ||
| final_val = c_low | ||
|
|
||
| return CapacityCreditResult{typeof(params), typeof(target_metric), P}( | ||
| target_metric, final_val, final_val, capacities, metrics) | ||
|
|
||
| end | ||
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.
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.
The p_value parameter is defined in the struct and passed to the constructor, but it is never used in the assess function. Unlike the bisection-based methods (EFC and ELCC) which use p_value for statistical significance testing, the Secant implementation ignores this parameter entirely. This creates an inconsistent API where users might expect p_value to control stopping criteria. Either remove the unused parameter or implement statistical significance checks similar to the bisection methods.