Skip to content

Enforce total capacity limit constraint in investment - #1496

Open
AdrianDAlessandro wants to merge 13 commits into
mainfrom
total_limit_rough
Open

Enforce total capacity limit constraint in investment#1496
AdrianDAlessandro wants to merge 13 commits into
mainfrom
total_limit_rough

Conversation

@AdrianDAlessandro

@AdrianDAlessandro AdrianDAlessandro commented Aug 19, 2026

Copy link
Copy Markdown
Collaborator

Description

This is an attempt at enforcing the total capacity limit constraint in the investment. It still needs some tests, but there are some components I wanted to ask about first.

I had made the total capacity limits mapped by ProcessID, but was going to ask about the function in which I do that (market.rs::collect_total_limits), because I believe it is looping over duplicate processes and just overwriting the HashMap unnecessarily. However, I have just seen #1495 - which I believe answers that question, but will require me to bring this branch up to date with it and then change my implementation.

For now, two questions:

  1. Am I correct about the above PR requiring me to refactor?
  2. Despite the refactor, is this doing the right thing?

Fixes #1493

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 Aug 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.40%. Comparing base (ed1209c) to head (3d71ff6).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1496      +/-   ##
==========================================
+ Coverage   90.35%   90.40%   +0.05%     
==========================================
  Files          61       61              
  Lines        8850     8899      +49     
  Branches     8850     8899      +49     
==========================================
+ Hits         7996     8045      +49     
  Misses        529      529              
  Partials      325      325              

☔ 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

Copy link
Copy Markdown
Collaborator

For now, two questions:

  1. Am I correct about the above PR requiring me to refactor?
  2. Despite the refactor, is this doing the right thing?

Yes and yes! Sorry, should have alerted you more to #1495 as it was intended to make your life easier not harder!

Comment thread src/simulation/investment.rs
@AdrianDAlessandro
AdrianDAlessandro marked this pull request as ready for review August 26, 2026 12:58
Copilot AI lite review requested due to automatic review settings August 26, 2026 12:58

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 introduces support for enforcing per-process total installed capacity limits during the investment asset-selection loop, alongside the existing addition-limit behaviour. It extends the input constraint model to carry a total_capacity_limit and threads an “agent share” of that limit through market selection into the investment selection routine.

Changes:

  • Add total_capacity_limit to ProcessInvestmentConstraint, including parsing from process_investment_constraints.csv and accessor plumbing (Process::agent_total_limit).
  • Generalise market-side limit collection via collect_agent_limits and pass both addition + total limits into select_best_assets.
  • Extend the investment selection loop to track remaining total capacity per process and filter options accordingly.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
src/simulation/market.rs Collects per-process agent addition/total limits via a generalised helper and passes both into investment selection.
src/simulation/investment.rs Adds total-limit tracking and applies limit-based filtering during iterative asset selection.
src/process.rs Extends investment constraint model with total_capacity_limit and adds agent_total_limit logic plus unit tests.
src/input/process/investment_constraints.rs Parses total_capacity_limit from CSV into ProcessInvestmentConstraint.
Suppressed comments (1)

src/simulation/investment.rs:533

  • remove_assets_exceeding_agent_limits does not apply the limit when only_candidates is false: the predicate short-circuits to true for every asset, so total capacity limits are never enforced.
        !(asset.is_candidate() && only_candidates)
            || remaining_agent_limits
                .get(asset.process_id())
                .is_none_or(|limit| *limit >= asset.total_capacity())
    });

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +390 to +394
// Remove assets that would exceed the remaining limits for their processes from the options
// The addition limit applies only to candidate assets, the total limit applies to all assets
remove_assets_exceeding_agent_limits(
&mut opt_assets,
&remaining_agent_addition_limits,
Comment thread src/process.rs
@AdrianDAlessandro

Copy link
Copy Markdown
Collaborator Author

@tsmbland This should be ready to review now! It's missing some tests in the investments module. I can add those for the functions I added if you're happy with them, but it's worth noting that the pre-existing code wasn't covered by tests at all

@tsmbland tsmbland left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good! Only a few small comments.

I wouldn't worry too much about tests, as a lot of the functions either require fiddly data inputs or are too simple to really need tests. You're very welcome to add some though!

Comment thread src/simulation/investment.rs Outdated
Comment thread src/simulation/investment.rs
Comment thread src/simulation/investment.rs Outdated
Comment thread src/simulation/investment.rs Outdated

@dalonsoa dalonsoa left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM!

Comment thread src/simulation/investment.rs
Comment thread src/process.rs
@AdrianDAlessandro

Copy link
Copy Markdown
Collaborator Author

I wouldn't worry too much about tests, as a lot of the functions either require fiddly data inputs or are too simple to really need tests. You're very welcome to add some though!

@tsmbland I added a couple tests. Luckily I did, because I found a bug!

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.

Implement total capacity limit constraint

4 participants