Skip to content

Add commodity constraints input and validation - #1487

Open
dc2917 wants to merge 15 commits into
mainfrom
add-commodity-constraints-input-and-validation
Open

Add commodity constraints input and validation#1487
dc2917 wants to merge 15 commits into
mainfrom
add-commodity-constraints-input-and-validation

Conversation

@dc2917

@dc2917 dc2917 commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Description

This PR adds a new module for reading and validating an input csv file for commodity constraints.

Commodity constraints are stored in a new CommodityConstraint struct, and the set of constraints provided are read and stored in a CommodityConstraintsMap type, which maps CommodityConstraints by region ID and year.

CommodityConstraints have a single field, limits, which stores the range of values to which the commodity is constrained.

An example commodity_constraints.csv file has been added to the "simple" setup for demonstration, and a corresponding schema has been added.

Fixes #1448

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

@dc2917

dc2917 commented Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

@tsmbland I've taken a stab at this but some feedback would be helpful before I add tests

@tsmbland tsmbland left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Good start, but a few comments

Comment thread src/input/commodity/constraints.rs Outdated
Comment thread src/input/commodity/constraints.rs Outdated
Comment thread src/input/commodity/constraints.rs Outdated
Comment thread src/commodity.rs Outdated
Comment thread src/commodity.rs Outdated
Comment thread src/input/commodity/constraints.rs
dc2917 added 3 commits August 25, 2026 17:23
- Cleaner parsing of region_id
- Change units of limits from money to flow
- Store time slice selection and balance type in `CommodityConstraint` struct
- Store vectors of `CommodityConstraint`s in `CommodityConstraintMap`
@dc2917
dc2917 force-pushed the add-commodity-constraints-input-and-validation branch from c872f0a to 273b323 Compare August 25, 2026 16:23
@dc2917
dc2917 force-pushed the add-commodity-constraints-input-and-validation branch from 273b323 to 328243d Compare August 25, 2026 16:28
@codecov

codecov Bot commented Aug 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.90909% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.35%. Comparing base (236974e) to head (3363674).
⚠️ Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
src/input/commodity/constraints.rs 92.52% 2 Missing and 11 partials ⚠️
src/input/commodity.rs 66.66% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1487      +/-   ##
==========================================
+ Coverage   90.31%   90.35%   +0.03%     
==========================================
  Files          60       61       +1     
  Lines        8663     8850     +187     
  Branches     8663     8850     +187     
==========================================
+ Hits         7824     7996     +172     
- Misses        525      529       +4     
- Partials      314      325      +11     

☔ View full report in Codecov by Harness.
📢 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.

@dc2917
dc2917 force-pushed the add-commodity-constraints-input-and-validation branch from be131f2 to 03f77ef Compare August 26, 2026 07:50
@dc2917
dc2917 marked this pull request as ready for review August 26, 2026 07:51
@dc2917
dc2917 requested a review from tsmbland August 26, 2026 07:51

@tsmbland tsmbland left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks very good on the whole, just a few suggestions

Comment thread examples/simple/commodity_constraints.csv Outdated
Comment thread schemas/input/commodity_constraints.yaml Outdated
Comment thread schemas/input/commodity_constraints.yaml
Comment thread schemas/input/commodity_constraints.yaml Outdated
Comment thread src/input/commodity/constraints.rs Outdated
Comment thread src/input/commodity/constraints.rs Outdated
Comment thread src/input/commodity.rs Outdated
Comment thread src/input/commodity/constraints.rs Outdated
Comment on lines +212 to +219
let time_slice1 = TimeSliceID {
season: "summer".into(),
time_of_day: "all-day".into(),
};
let time_slice2 = TimeSliceID {
season: "summer".into(),
time_of_day: "all-day".into(),
};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

These are identical?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good spot, one should have been "winter" - perhaps unnecessary though?

season: "summer".into(),
time_of_day: "all-day".into(),
};
let time_slice_info = TimeSliceInfo {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You could consider using the time_slice_info fixture instead of building one from scratch

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah, that would have been handy haha

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I guess it doesn't matter here, but technically this TimeSliceInfo isn't valid because:

  • season lengths should sum to 1
  • time slice lengths should sum to 1 (and be consistent with season lengths)
  • seasons and times_of_day options should match those in the time_slices entries ("all day" vs "day"/"night")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I seeee, yeah I guessed "all-day" was day+night, didn't realise there was also morning, evening, peak etc. Had no idea what any of this time-slicing was to be honest.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

So something like this: 3363674?

Commodities specify seasonal time slice levels, constraints are over whole seasons (two of them). Time slices are defined for those two seasons, with both day and night specified as the time of day for each. Then time_slice_info contains the two seasons (1/2 year each), and the four time slices themselves (1/4 year each).

@dc2917
dc2917 requested a review from tsmbland August 26, 2026 14:06

@tsmbland tsmbland left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good!

Still a small error in the tests. As I don't think it affects the validity of the tests I'll approve, but probably worth fixing up for the sake of correctness!

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.

Add file reading and validation code for commodity constraints

2 participants