Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #445 +/- ##
==========================================
+ Coverage 95.50% 95.53% +0.03%
==========================================
Files 31 31
Lines 4515 4571 +56
Branches 4515 4571 +56
==========================================
+ Hits 4312 4367 +55
- Misses 100 101 +1
Partials 103 103 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@alexdewar Could you have a look at this and we can talk about it on Monday? It works and gives the expected results, but it's definitely wasteful and I could use some advice from someone with more Rust experience |
|
Sure. I only just noticed this, but I'll take a look first thing on Mon. |
alexdewar
left a comment
There was a problem hiding this comment.
I've made a couple of suggestions. The main one is that I think you can combine the first and last loops in CommodityPrices::add_from_solution. In general the approach looks good though and it's nice clean code.
| } | ||
|
|
||
| // Calculate highest capacity dual for each commodity/timeslice | ||
| let mut highest_duals: IndexMap<CommodityPriceKey, f64> = IndexMap::new(); |
There was a problem hiding this comment.
A HashMap is fine here, because we don't care about ordering:
| let mut highest_duals: IndexMap<CommodityPriceKey, f64> = IndexMap::new(); | |
| let mut highest_duals = HashMap::new(); |
(I also don't think you need the type hints here, though the compiler might not agree! 😛)
| for (commodity_id, time_slice, price) in solution.iter_commodity_prices() { | ||
| // Calculate highest capacity dual for each commodity/timeslice | ||
| let mut highest_duals = HashMap::new(); | ||
| for (asset_id, time_slice, dual) in solution.iter_capacity_duals() { |
There was a problem hiding this comment.
@alexdewar Thanks for the comments! The only other thing I'm unsure about is this loop, as it's repeating a lot of asset-level computations for every timeslice (getting the asset, getting the pacs, checking if the flows are positive). I think better would be to have nested loops, but that would require re-designing iter_capacity_duals, and maybe it's not even worth it for the extra work / trade-off with readability. What do you think?
There was a problem hiding this comment.
Good point... I don't think there's a way around that without making the data structures more complicated though, so I'd leave it as is for now. We can always optimise this stuff further down the line if benchmarking shows it's a bottleneck.
One thing we could do to avoid having to look up the assets multiple times (though I don't think we should do it just yet) is to switch from saving AssetIDs to Rc<Asset>s for the constraint "keys". We'd also have to change AssetPool to store Rc<Asset>s etc. If we did this, we'd have to be careful that code wasn't making use of assets after they'd been decommissioned though (wouldn't be a problem for this bit of code). Could use Weaks for that, but that would make things a bit more fiddly.
We could also improve performance for finding PACs. The way we store commodity flows is not particularly optimal in general, so I've opened an issue for it (#449). (Who knows how much impact fixing that would actually have on overall program performance, but it's not an especially big change so we should probably do it at some point.)
alexdewar
left a comment
There was a problem hiding this comment.
The code looks good to me... I have no idea about your questions though 🙃. I think this will be fine for now and we can figure out some of the other details further down the line, as you say.
You could write a test for this, though I appreciate it might be fiddly. (If you changed the function signature to take the duals as separate arguments that might make it slightly easier.) In general there aren't any tests for the dispatch optimisation stuff yet though and I think we'll want to consider how we approach it before we add them (#448). Probably something to add to the to-do list for the next engagement.
|
@alexdewar Thanks for the review! I'm going to merge and will think about tests... another time 😂 |
Description
Implements the new commodity price calculation as suggested by @ahawkes in #433
Main changes to the code:
iter_capacity_dualsto retrieve the dual values for the capacity constraintsadd_asset_capacity_constraintsto make sure these constraints are added immediately after the commodity balance constraintsadd_from_solutionI still have a couple of questions:
I think it's probably ok to leave these as open questions for now, and revisit when we come to look at more complex models
I haven't added any tests, but we currently have no tests anyway for the constraints
Fixes #433
Type of change
Key checklist
$ cargo test$ cargo docFurther checks