-
Notifications
You must be signed in to change notification settings - Fork 29
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
base: main
Are you sure you want to change the base?
Changes from all commits
e8dc466
5211e61
1067e68
cc42813
46165bf
d52649a
c91d0e3
f0fd736
cafc0ca
5c4eeab
5a54b97
f2b0084
18e08f9
7c42177
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
|
Comment on lines
42
to
157
|
||
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.