Skip to content

Fix: Use consistent ordering for commodity prices between runs#601

Merged
alexdewar merged 2 commits intomainfrom
fix-use-btreemap-for-prices
Jun 4, 2025
Merged

Fix: Use consistent ordering for commodity prices between runs#601
alexdewar merged 2 commits intomainfrom
fix-use-btreemap-for-prices

Conversation

@alexdewar
Copy link
Copy Markdown
Member

Description

While working on #595 I noticed that commodity prices were in a random order after we removed the contents of CommodityPrices::add_from_solution and were therefore all being added (as NaN) in add_remaining. The issue is that we're using a HashSet to store the IDs of commodities without prices, so, while CommodityPrices retains insertion order (being an IndexMap), its contents can end up in a random order anyway. We just didn't notice before because we only had one commodity without a price.

There are different ways we could fix this, but I've opted to change CommodityPrices to use a BTreeMap rather than an IndexMap, so the contents are kept ordered according to the commodity ID (i.e. alphabetically). It doesn't particularly matter, but doing it this way means that the commodities are consistently in the same order for different milestone years, which seems like a nice property to have.

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

Further checks

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

@alexdewar alexdewar requested review from Copilot and tsmbland June 4, 2025 09:59
@alexdewar alexdewar added the bug Something isn't working label Jun 4, 2025
@alexdewar alexdewar added this to MUSE Jun 4, 2025
@alexdewar alexdewar moved this to 👀 In review in MUSE Jun 4, 2025
@codecov
Copy link
Copy Markdown

codecov Bot commented Jun 4, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 84.54%. Comparing base (d7fc05f) to head (091e015).
Report is 5 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #601      +/-   ##
==========================================
+ Coverage   84.51%   84.54%   +0.03%     
==========================================
  Files          37       37              
  Lines        3255     3255              
  Branches     3255     3255              
==========================================
+ Hits         2751     2752       +1     
+ Misses        318      317       -1     
  Partials      186      186              

☔ View full report in Codecov by Sentry.
📢 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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 ensures commodity prices are stored in a consistent, sorted order by switching from an IndexMap to a BTreeMap and deriving the necessary ordering traits on key types.

  • Derive Ord/PartialOrd on TimeSliceID and all ID types to support ordering.
  • Replace IndexMap with BTreeMap for CommodityPrices.
  • Update imports and macro-derived traits for ID types.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/time_slice.rs Added Ord/PartialOrd derives to TimeSliceID
src/simulation/prices.rs Swapped IndexMap for BTreeMap in CommodityPrices
src/id.rs Extended define_id_type! to derive Ord/PartialOrd
Comments suppressed due to low confidence (3)

src/simulation/prices.rs:13

  • Consider adding a unit test that inserts a known set of commodity IDs and asserts the iteration order remains consistent across runs when using BTreeMap.
pub struct CommodityPrices(BTreeMap<(CommodityID, RegionID, TimeSliceID), f64>);

src/simulation/prices.rs:11

  • [nitpick] Update this doc comment to note that the map now enforces sorted key ordering by commodity ID for deterministic output.
/// A map relating commodity ID + region + time slice to current price (endogenous)

src/simulation/prices.rs:9

  • [nitpick] Switching from IndexMap (O(1) operations) to BTreeMap (O(log N) operations) may affect performance; consider benchmarking if the map grows large.
use std::collections::{BTreeMap, HashSet};

Copy link
Copy Markdown
Collaborator

@tsmbland tsmbland left a comment

Choose a reason for hiding this comment

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

Seems reasonable!

@alexdewar alexdewar merged commit 751befc into main Jun 4, 2025
7 checks passed
@alexdewar alexdewar deleted the fix-use-btreemap-for-prices branch June 4, 2025 10:11
@github-project-automation github-project-automation Bot moved this from 👀 In review to ✅ Done in MUSE Jun 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

3 participants