Skip to content

Include secondary edges in investment graphs#1361

Open
tsmbland wants to merge 1 commit into
mainfrom
secondary_edges_in_investment_graph
Open

Include secondary edges in investment graphs#1361
tsmbland wants to merge 1 commit into
mainfrom
secondary_edges_in_investment_graph

Conversation

@tsmbland

@tsmbland tsmbland commented Jun 22, 2026

Copy link
Copy Markdown
Collaborator

Description

We use (what we're currently calling) InvestmentGraphs for two purposes:

  • to solve the order of investments
  • to solve the order of commodity price calculations

Both of these orders are based on the same topological sorting of InvestmentGraphs, just in reverse (investment are calculated for downstream commodities first, prices are calculated for upstream commodities first). At the moment, we exclude non-primary flows from these graphs (i.e. flows that don't contribute to production of the primary commodity). This made sense when we were only using these graphs to solve the investment order, since investments (for now) only target demands of primary output commodities. However, it doesn't make sense for price calculations as we do want secondary outputs to be able to contribute to prices (kind of making an assumption here, but it would feel strange/inconsistent if they couldn't). I don't want to have to create different graphs for investments/price calculations, so the simplest solution is just to no longer exclude non-primary flows from InvestmentGraphs.

This does have some implications for investments, but I think they're small:

  • markets that would once have been in the same Layer, could now be pushed to separate Layers if there's a secondary flow between them. I think the only implication of this is a potential performance cost (could require more dispatch rounds). Since investments still only target primary outputs, I don't think there's any functional change.
  • Secondary flows may introduce Cycles that weren't there before. This could change results as these go through an extra "cycle balancing" step to equilibrate demands/supply. If anything, I think this could result in excess capacity invested in as a result of ignoring secondary outputs being "trimmed away", which may be a good thing anyway.

Fixes #1331

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 Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.75%. Comparing base (651b4e2) to head (b755a93).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1361   +/-   ##
=======================================
  Coverage   89.75%   89.75%           
=======================================
  Files          58       58           
  Lines        8529     8529           
  Branches     8529     8529           
=======================================
  Hits         7655     7655           
  Misses        561      561           
  Partials      313      313           

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

@tsmbland tsmbland marked this pull request as ready for review June 22, 2026 11:12
Copilot AI review requested due to automatic review settings June 22, 2026 11:12

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.

Pull request overview

This PR updates the construction of InvestmentGraphs so they retain secondary (non-primary) flows rather than filtering them out, ensuring that the derived topological order can reflect secondary outputs (notably for commodity price-calculation ordering, per #1331).

Changes:

  • Stop filtering CommodityGraph edges to only GraphEdge::Primary when initialising an InvestmentGraph.
  • Update module/function documentation to reflect that only SVD/SED commodity-node filtering remains.

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

Comment thread src/graph/investment.rs
Comment on lines 75 to 88
@@ -84,7 +84,7 @@ fn init_investment_graph_for_year(
}
_ => None,
},
|_, e| matches!(e, GraphEdge::Primary(_)).then_some(e.clone()),
|_, e| Some(e.clone()),
);
@tsmbland tsmbland requested a review from alexdewar June 22, 2026 11:20
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.

Flaw with the price calculation order?

2 participants