Skip to content

Comments

add remaining_demand_absolute_tolerance param#1137

Open
Aurashk wants to merge 3 commits intomainfrom
add-unmet-demand-threshold
Open

add remaining_demand_absolute_tolerance param#1137
Aurashk wants to merge 3 commits intomainfrom
add-unmet-demand-threshold

Conversation

@Aurashk
Copy link
Collaborator

@Aurashk Aurashk commented Feb 19, 2026

Description

This adds a new configurable model parameter demand_tolerance so that model won't fail if the remaining demand is negligible

Fixes #1115

Type of change

  • Bug fix (non-breaking change to fix an issue)
  • New feature (non-breaking change to add functionality)
  • Refactoring (non-breaking, non-functional change to improve maintainability)
  • Optimization (non-breaking change to speed up the code)
  • Breaking change (whatever its nature)
  • Documentation (improve or add documentation)

Key checklist

  • All tests pass: $ cargo test
  • The documentation builds and looks OK: $ cargo doc
  • Update release notes for the latest release if this PR adds a new feature or fixes a bug
    present in the previous release

Further checks

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

Copilot AI review requested due to automatic review settings February 19, 2026 21:14
@codecov
Copy link

codecov bot commented Feb 19, 2026

Codecov Report

❌ Patch coverage is 90.90909% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 87.52%. Comparing base (97d84ca) to head (d88d907).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
src/model/parameters.rs 83.33% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1137      +/-   ##
==========================================
- Coverage   87.52%   87.52%   -0.01%     
==========================================
  Files          55       55              
  Lines        7626     7634       +8     
  Branches     7626     7634       +8     
==========================================
+ Hits         6675     6682       +7     
  Misses        649      649              
- Partials      302      303       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new remaining_demand_absolute_tolerance parameter to address issue #1115, where the investment appraisal would fail with NaN errors when service demand remained constant across consecutive milestone years. The tolerance allows the model to treat negligibly small remaining demand as zero, preventing spurious failures.

Changes:

  • Added remaining_demand_absolute_tolerance parameter with default value of 1e-12
  • Modified is_any_remaining_demand function to use the tolerance when checking for unmet demand
  • Added validation function and comprehensive unit tests for the new parameter

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/model/parameters.rs Defines the new remaining_demand_absolute_tolerance parameter with default value, validation function, and comprehensive unit tests
src/simulation/investment.rs Updates is_any_remaining_demand to use the new tolerance parameter when checking if demand is met

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +131 to +133
/// Absolute tolerance when checking if remaining demand is close enough to zero
#[serde(default = "default_remaining_demand_absolute_tolerance")]
pub remaining_demand_absolute_tolerance: Dimensionless,
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The schemas/input/model.yaml file needs to be updated to include the new remaining_demand_absolute_tolerance parameter. As noted in the comment at line 90-91 of this file, when adding or changing a field in ModelParameters, the schema must also be updated. This parameter should be documented similar to other parameters like price_tolerance in the schema file.

Copilot uses AI. Check for mistakes.
Comment on lines +857 to 859
fn is_any_remaining_demand(demand: &DemandMap, absolute_tolerance: f64) -> bool {
demand.values().any(|flow| *flow > Flow(absolute_tolerance))
}
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The modified is_any_remaining_demand function lacks test coverage. A test should be added to verify the behavior with different tolerance values, including edge cases like:

Copilot uses AI. Check for mistakes.
#[rstest]
#[case(0.0, true)] // Valid minimum value (exactly zero)
#[case(1e-10, true)] // Valid very small positive value
#[case(1e-6, true)] // Valid default value
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment says "Valid default value" but uses 1e-6 instead of the actual default value 1e-12 defined at line 82. Update this test case to use the correct default value of 1e-12.

Copilot uses AI. Check for mistakes.
@Aurashk Aurashk requested a review from alexdewar February 19, 2026 21:32
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.

Investment appraisal fails when demand is equal in 2 consecutive years in (a slight adaptation of) the simple model

1 participant