Add commodity constraints input and validation - #1487
Conversation
|
@tsmbland I've taken a stab at this but some feedback would be helpful before I add tests |
tsmbland
left a comment
There was a problem hiding this comment.
Good start, but a few comments
| // Extract fields from record | ||
| let commodity_id = commodity_ids.get_id(&record.commodity_id)?; | ||
| // Validation ensures single region_id, so take that at index 0 | ||
| let region_id = parse_region_str(&record.region_id, region_ids)?[0].clone(); |
There was a problem hiding this comment.
| let region_id = parse_region_str(&record.region_id, region_ids)?[0].clone(); | |
| let region_id = region_ids.get_id(&record.region_id)? |
| ensure!( | ||
| self.region_id != "all" && !self.region_id.contains(";"), | ||
| "Only single regions are permitted" | ||
| ); |
There was a problem hiding this comment.
This will be validated by get_id (see comment below)
| let region_id = parse_region_str(&record.region_id, region_ids)?[0].clone(); | ||
| let years = parse_year_str(&record.years, milestone_years)?; | ||
| let ts_selection = time_slice_info.get_selection(&record.time_slice)?; | ||
| let limits = parse_range(&record.limits, Money(0.0)..=Money(f64::INFINITY)) |
There was a problem hiding this comment.
| let limits = parse_range(&record.limits, Money(0.0)..=Money(f64::INFINITY)) | |
| let limits = parse_range(&record.limits, Flow(0.0)..=Flow(f64::INFINITY)) |
Limits are on amount of commodity consumed/produced (i.e. Flow), rather than a monetary amount
| #[derive(PartialEq, Debug, Clone)] | ||
| pub struct CommodityConstraint { | ||
| /// The range of values the commodity is constrained to lie between | ||
| pub limits: RangeInclusive<Money>, |
There was a problem hiding this comment.
This also needs to store the TimeSliceSelection that the constraint applies to, and the BalanceType
There was a problem hiding this comment.
That makes sense. I wasn't sure what to do with those.
| pub type CommodityLevyMap = HashMap<(RegionID, u32, TimeSliceID), MoneyPerFlow>; | ||
|
|
||
| /// A map of [`CommodityConstraint`]s, keyed by region ID and year | ||
| pub type CommodityConstraintsMap = HashMap<(RegionID, u32), Rc<CommodityConstraint>>; |
There was a problem hiding this comment.
You can have multiple constraints for each (region, year), so this should be
HashMap<(RegionID, u32), Vec<CommodityConstraint>>
Not sure you necessarily need the Rc, but may be wrong (probably). If required, it should probably be Arc to keep things parallel-compatible
There was a problem hiding this comment.
Ah OK yeah, I understand what you were referring to in your comment on the issue now.
| /// | ||
| /// A `HashMap<CommodityID, CommodityConstraintsMap>` mapping commodity IDs to their | ||
| /// commodity-constraints maps, or an error. | ||
| pub fn read_commodity_constraints( |
There was a problem hiding this comment.
We also want to disallow SVD commodities from having commodity constraints, so you'll need to pass in the map of the commodities rather than just the IDs (i.e. &IndexMap<CommodityID, Commodity>)
Another slightly tricky thing is that, since OTH commodities can either be consumed or produced (but not both), we don't want users to supply production constraints for OTH commodities that are consumed, and vice-versa. Probably worth opening an issue about this rather than attempting this here, as we may have to do this in the graph validation stage
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
CommodityConstraintstruct, and the set of constraints provided are read and stored in aCommodityConstraintsMaptype, which mapsCommodityConstraints 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
Key checklist
$ cargo test$ cargo docpresent in the previous release
Further checks