Skip to content

User guide for commodity graphs - #1575

Merged
tsmbland merged 2 commits into
mainfrom
graph_docs
Nov 6, 2026
Merged

tsmbland merged 2 commits into
mainfrom
graph_docs

Conversation

@tsmbland

@tsmbland tsmbland commented Nov 2, 2026 •

Copy link
Copy Markdown
Collaborator

Description

I thought it would be useful to have a section in the documentation on commodity graphs, as this is an important part of how MUSE works, and a fundamental improvement over MUSE1 where users had to manually define hierarchies. Rather than writing this manually, I tried to get copilot to do it mostly for me. This is based off #1569, where region-level graphs have been combined.

My process was as follows:

  • Used a smaller model (Claude Sonnet) with a short prompt for a first draft.
  • The result was pretty poor, but reading this was helpful for me to clarify in my mind exactly what I wanted and come up with a more detailed plan
  • I then prompted a larger model (Claude Opus) with the following:

I want a section in the user-facing docs describing commodity graphs in the context of the model. It should mention the fact that graphs are built and why:

  • to establish the potential flow relationships between commodities and regions implied by the processes
  • to perform validation, to make sure that service demands can be met by the available processes, and that commodities abide by the rules for their type
  • to ensure system integrity
  • to determine the order of investments and price calculation: MUSE invests downstream to upstream, and calculates prices upstream to downstream
  • to identify circularities
  • to help visualise the model, via the save-graphs command
  • for performance reasons, e.g. identifying independent commodity groups that can be run in parallel (not technically what we do currently, but this is a longer-term justification so worth including anyway)

The docs should focus on user facing implications, rather than algorithmic details. The goal is to help users to build models and diagnose problems

A few things to mention (allows users to understand visualised graphs and diagnose problems)

  • nodes are commodities in a region (i.e. a market)
  • edges are processes
  • primary vs secondary edges
  • a graph is created for each year. This is then validated for each timeslice to take into account process availabilities and demand
  • information about visualising the graph, helping users to understand the visualisation
  • the investment order is determined by performing a topological sort after collapsing strongly connected components. The price calculation order is the reverse of the investment order
  • within cycles, markets are ordered with a MILP. Do not discuss the algorithm, but give a link to the in-code documentation. Do not discuss the investment process for circularities, only that the order is determined here
  • independent market sets are grouped into layers, for performance reasons

There should be at least one diagrammatic example, maybe more. E.g. we should give an example of a set of commodities and processes, and how the nodes in the graph represent commodities and the edges are processes. For simplicity, limit this to one region (don't include regions on the graph). Colour code by SVD/SED/OTH. Include a circularity. Then from the graph, write down/show what the investment order would be, walking it through. It might be easier to give dot code and I go away and create a png/svg - not sure. Use the example models for inspiration.

This documentation should be easy to read for a MUSE newcomer. The aim is to educate users to help them use MUSE effectively, not to confuse them. Aim for conciseness and do not include unnecessary details that aren't important for MUSE users to know.

Come up with a detailed plan


  • It then came back with a detailed plan which I was mostly happy with, so I set it off to implement this plan
  • The result was remarkably(/scarily) good! It even designed commodity graphs and generated svgs. I had a few comments so sent a follow up prompt and made a few manual changes (mostly to make it more concise), but on the whole I didn't intervene much.

Notes:

  • The example model doesn't exactly match one of the built in example models. I wanted to include a circularity, but the circularity example is a bit too complex for this.
  • I think one further improvement we could make is to make the graphs from the save-graphs command more visually appealing. The first graph in the doc is good inspiration. If we did this then maybe we could combine these sections. Beyond the scope of this PR though.
  • I've committed the svgs, and the corresponding dot files include the command that copilot used to generate them. I wonder if it would be worth including this in the doc build workflow? Maybe not as they're not likely to change often...
  • I hope that this will be helpful both for users and for AI itself. That was part of my justification for using a big AI model (Opus) to put this together, so hopefully smaller models can read this and understand everything they need to know about commodity graphs.

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 Nov 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.90%. Comparing base (44a754e) to head (e794663).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1575   +/-   ##
=======================================
  Coverage   90.90%   90.90%           
=======================================
  Files          61       61           
  Lines        9116     9116           
  Branches     9116     9116           
=======================================
  Hits         8287     8287           
  Misses        507      507           
  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.

@tsmbland
tsmbland marked this pull request as ready for review November 3, 2026 08:52
@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.

This looks great! Very comprehensive. I've suggested a few small things, but feel free to ignore.

And the graph the AI generated does look much prettier than ours... I like the key too. Shall we open an issue for this? I'm not sure how much flexibility we have with tweaking the output of petgraph, but it might be worth a go. (Could also -- optionally? -- include a key in this output for the often-opaque abbreviations used for commodities, processes, etc.)

Comment thread docs/model/commodity_graphs.md Resolved
Comment thread docs/model/commodity_graphs.md Resolved
Comment thread docs/model/commodity_graphs.md Resolved
Comment thread docs/model/commodity_graphs.md Resolved

When reading these graphs, note that:

- nodes are labelled `COMMODITY | REGION` and edges are labelled `PROCESS | REGION`;

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.

I wonder if, for single-region models, we should just hide the | REGION part? It just makes the output a bit more cluttered. (Not something for this PR anyway -- just a thought.)

@tsmbland tsmbland Nov 6, 2026 •

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.

Agree. I was also thinking we could draw boxes around each region (i.e. each region is a subgraph), so we wouldn't need to label the region on nodes and edges. I had a go at this before in d8e85db (not on this branch, but could use as inspiration for a future PR)

@tsmbland

tsmbland commented Nov 6, 2026

Copy link
Copy Markdown
Collaborator Author

And the graph the AI generated does look much prettier than ours... I like the key too. Shall we open an issue for this? I'm not sure how much flexibility we have with tweaking the output of petgraph, but it might be worth a go. (Could also -- optionally? -- include a key in this output for the often-opaque abbreviations used for commodities, processes, etc.)

petgraph is limited for this, but dot files are just text files so we can build them manually however we want

Base automatically changed from graph_multi_region to main November 6, 2026 12:30
Copilot AI balanced review requested due to automatic review settings November 6, 2026 12:43
@tsmbland
tsmbland enabled auto-merge November 6, 2026 12:43

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.

🟢 Approval recommended

The commodity-graph documentation update has no active review issues.

No open issues 🎉

What changed in this PR

Adds a user-facing guide to commodity graphs, building on the unified yearly graphs introduced in #1569. It explains how graphs support model validation, investment ordering and price calculation, with examples to help users diagnose problems.

Changes:

  • Documents graph structure, validation rules, circularities and investment layers.
  • Adds three illustrated examples with editable DOT sources and rendered SVGs.
  • Links the guide from existing documentation and clarifies the SED producer requirement in the input schema documentation.
File Description
schemas/​input/​commodities.yaml Clarifies when SED commodities require a producer.
docs/​user_guide.md Links graph visualisation guidance to the new chapter.
docs/​SUMMARY.md Adds the chapter to the documentation navigation.
docs/​model/​README.md Links the investment-order overview to commodity graphs.
docs/​model/​prices.md Links to the explanation of price calculation order.
docs/​model/​investment.md Links to the investment-order explanation and example.
docs/​model/​commodity_graphs.md Adds the guide, examples and troubleshooting advice.
docs/​images/​save_graphs_simple.svg Illustrates graph output for the simple model.
docs/​images/​save_graphs_simple.dot Provides the source for the simple-model illustration.
docs/​images/​investment_order_example.svg Illustrates investment layers and a circularity.
docs/​images/​investment_order_example.dot Provides the investment-order diagram source.
docs/​images/​commodity_graph_example.svg Illustrates commodity types and process relationships.
docs/​images/​commodity_graph_example.dot Provides the commodity-graph diagram source.
docs/​glossary.md Defines commodity graphs and their role in MUSE2.

Review effort: Balanced

@tsmbland
tsmbland merged commit 60a012a into main Nov 6, 2026
@tsmbland
tsmbland deleted the graph_docs branch November 6, 2026 12:47
@alexdewar

Copy link
Copy Markdown
Member

And the graph the AI generated does look much prettier than ours... I like the key too. Shall we open an issue for this? I'm not sure how much flexibility we have with tweaking the output of petgraph, but it might be worth a go. (Could also -- optionally? -- include a key in this output for the often-opaque abbreviations used for commodities, processes, etc.)

petgraph is limited for this, but dot files are just text files so we can build them manually however we want

Sounds good. I found this crate for building dot files programmatically: https://docs.rs/dot/0.2.0/dot/index.html

Might be worth a look. Or we can just generate the text ourselves, as you say.

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