Skip to content

Combine region graphs into single graph for each year - #1569

Merged
tsmbland merged 7 commits into
mainfrom
graph_multi_region
Nov 6, 2026
Merged

tsmbland merged 7 commits into
mainfrom
graph_multi_region

Conversation

@tsmbland

@tsmbland tsmbland commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Description

We currently build separate commodity graphs for each region, which we then combine into a single investment graph to get the investment order. This works ok for now, as there aren't any edges between regions (that would imply trade), but this is surely something that we'll want. The way I see it, there's no reason to have multi-region models if we don't have trade, so either way building separate graphs for different regions is a bit dumb. Not a high priority, but I think there will be some changes incoming to the graph code, and I wanted to get this out of the way. I think it's also a bit more consistent with how the rest of the code works.

I first tried this when we were discussing trade (https://github.com/EnergySystemsModellingLab/MUSE2/tree/rough_trade_implementation), and I've mostly just copied over some of the changes from that. There are some other changes in that branch about allowing flows between regions, and prettifying the graph, which I haven't copied over, but these can come later.

Copilot has flagged some performance issues, which I think I agree with, although they're probably minor.

Fixes # (issue)

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

@codecov

codecov Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.42857% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.90%. Comparing base (1b9a848) to head (7ef1093).
⚠️ Report is 9 commits behind head on main.

Files with missing lines Patch % Lines
src/graph.rs 88.46% 3 Missing and 3 partials ⚠️
src/graph/validate.rs 86.66% 1 Missing and 1 partial ⚠️
src/graph/investment.rs 97.29% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1569      +/-   ##
==========================================
+ Coverage   90.88%   90.90%   +0.01%     
==========================================
  Files          61       61              
  Lines        9121     9116       -5     
  Branches     9121     9116       -5     
==========================================
- Hits         8290     8287       -3     
+ Misses        509      507       -2     
  Partials      322      322              

☔ 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.

github-actions[bot]

This comment was marked as outdated.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Repeated demand-map and graph-node scans introduce validation scaling regressions as model size increases.

2 open issues
What changed in this PR

Combines regional commodity graphs into one graph per year, simplifying graph handling and preparing MUSE2 for future inter-region trade.

Changes:

  • Adds region identities to commodity nodes and process edges.
  • Adapts validation and investment ordering to year-keyed graphs.
  • Saves one DOT file per year and updates affected tests.
File Description
tests/​cli.rs Expects the year-only DOT filename.
src/​input.rs Returns commodity graphs keyed by year.
src/​graph/​validate.rs Uses regional edge availability and demand in combined graphs.
src/​graph/​investment.rs Derives investment order from a single yearly graph.
src/​graph.rs Builds region-qualified yearly graphs and updates DOT output.

Review effort: Balanced

Comment thread src/graph/validate.rs
Comment on lines +49 to +50
for ((region_id, dem_year, dem_ts_sel), &flow) in &commodity.demand {
if *dem_year != year || dem_ts_sel != time_slice_selection || flow <= Flow(0.0) {
Comment thread src/graph/validate.rs
Comment on lines 54 to +57
let commodity_node_index = filtered_graph
.node_indices()
.find(|&idx| filtered_graph.node_weight(idx) == Some(&commodity_node))
.unwrap_or_else(|| {
filtered_graph.add_node(GraphNode::Commodity(commodity_id.clone()))
});
.unwrap_or_else(|| filtered_graph.add_node(commodity_node));
@tsmbland tsmbland mentioned this pull request Nov 3, 2026
1 of 11 tasks
@tsmbland
tsmbland marked this pull request as ready for review November 3, 2026 09:03
@tsmbland
tsmbland requested a review from alexdewar November 3, 2026 09:04

@alexdewar alexdewar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Makes sense and seems to simplify things a bit. I've added some comments.

I don't think performance is likely to be impacted much unless the model is huge (though I suppose you never know). I'm not sure how the GitHub benchmark action is configured (I couldn't find any files related to it in the repo), but it's odd that it's reporting a massive performance regression (~30%). I tried reproducing this locally and I don't see such large differences, BUT the results were really variable -- most benchmarks reported no difference most of the time, but they often indicated performance improvements or regressions, seemingly at random.

Maybe we need to tweak the configuration a bit. I'm seeing a lot of warnings like this:

Warning: Unable to complete 20 samples in 5.0s. You may wish to increase target time to 18.4s, or reduce sample count to 10.

It seems like the examples benchmarks take ages to complete and if cargo bench isn't able to collect many samples within the timeframe, that would explain why we're seeing such variability.

Comment thread docs/user_guide.md Resolved
Comment thread src/graph/investment.rs Resolved
Comment thread src/graph/investment.rs Resolved
Comment thread src/graph/investment.rs Resolved
Comment thread src/graph/investment.rs Resolved
Comment thread src/graph/investment.rs Resolved
Comment thread src/graph/investment.rs Resolved
Comment thread src/graph.rs
Commodity(CommodityID),
/// A node representing a commodity in a specific region
#[display("{_0} | {_1}")]
Commodity(CommodityID, RegionID),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should this be renamed to CommodityMarket?

I've also just noticed that MarketSet::Single has a tuple as its inner value, rather than two separate values, like here. Should we change that one for consistency?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Good point. Not sure why MarketSet::Single has a tuple. I'll change it, but will defer to avoid conflicts with other PRs

@tsmbland tsmbland mentioned this pull request Nov 5, 2026
11 tasks
@tsmbland

tsmbland commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator Author

@alexdewar Thanks. I have another PR tidying up the graph::investment submodule, so I'll make some of these changes in that branch to avoid conflicts

Co-authored-by: Alex Dewar <alexdewar@users.noreply.github.com>
@tsmbland
tsmbland enabled auto-merge November 6, 2026 12:23
@tsmbland
tsmbland merged commit b1ea6fe into main Nov 6, 2026
@tsmbland
tsmbland deleted the graph_multi_region branch November 6, 2026 12:30
tsmbland added a commit that referenced this pull request Nov 6, 2026
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.

3 participants