Skip to content

Commit aa1ec50

Browse files
committed
Add early_termination_count parameter
1 parent 303d8c3 commit aa1ec50

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

src/muse/carbon_budget.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,8 @@ def bisection(
373373
sample_size: int = 2,
374374
refine_price: bool = True,
375375
price_too_high_threshold: float = 10,
376-
fitter: str = "slinear",
377376
tolerance: float = 0.1,
377+
early_termination_count: int = 5,
378378
) -> float:
379379
"""Applies bisection algorithm to escalate carbon price and meet the budget.
380380
@@ -396,8 +396,9 @@ def bisection(
396396
refine_price: Boolean to decide on whether carbon price should be capped, with
397397
the upper bound given by price_too_high_threshold
398398
price_too_high_threshold: Upper limit for carbon price
399-
fitter: Not used in this method
400399
tolerance: Maximum permitted deviation of emissions from the budget
400+
early_termination_count: Will terminate the loop early if the last n solutions
401+
are the same
401402
402403
Returns:
403404
New value of global carbon price
@@ -439,10 +440,10 @@ def bisection(
439440
)
440441
lb = emissions_cache[up]
441442

442-
# Terminate early if the last 5 solutions are the same
443-
if len(emissions_cache) >= 7:
444-
last_five = list(emissions_cache.values())[-5:]
445-
if all(x == last_five[0] for x in last_five):
443+
# Terminate early if many consecutive emissions are the same
444+
if len(emissions_cache) >= early_termination_count + 2:
445+
recent_values = list(emissions_cache.values())[-early_termination_count:]
446+
if all(x == recent_values[0] for x in recent_values):
446447
break
447448

448449
# Exit loop if lower or upper bound on emissions is close to threshold

0 commit comments

Comments
 (0)