Repository navigation
Refactoring to allow multiple asset selection algorithms - #1577
Conversation
7e8fd94 to
40e4737
Compare
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are identified for the agent-selection refactor.
No open issues 🎉
What changed in this PR
Refactors asset selection to support multiple agent algorithms, isolating the existing behaviour as competitive and preparing for the portfolio algorithm described in #1574.
Changes:
- Adds an optional
agent_typeinput, defaulting tocompetitive, and routes market selection accordingly. - Moves competitive selection, appraisal and debug output into a dedicated module.
- Adds reusable custom debug writers and updates tests, fixtures and benchmark imports.
| File | Description |
|---|---|
tests/data/simple/debug_asset_selection_competitive.csv |
Adds expected competitive-selection debug output. |
src/simulation/market.rs |
Routes asset selection by agent type. |
src/simulation/investment/competitive/optimisation.rs |
Updates imports for the relocated optimisation code. |
src/simulation/investment/competitive/metrics.rs |
Relocates appraisal metrics and narrows visibility. |
src/simulation/investment/competitive/debug.rs |
Owns competitive debug CSV output and its tests. |
src/simulation/investment/competitive/costs.rs |
Houses competitive appraisal cost calculations. |
src/simulation/investment/competitive/constraints.rs |
Updates imports for relocated appraisal constraints. |
src/simulation/investment/competitive/coefficients.rs |
Adjusts coefficient interfaces and helper visibility. |
src/simulation/investment/competitive.rs |
Encapsulates competitive selection and related tests. |
src/simulation/investment.rs |
Retains shared utilities and exposes the competitive module. |
src/output.rs |
Adds lazily created custom debug writers and tests. |
src/input/agent/search_space.rs |
Updates test agents with the default agent type. |
src/input/agent.rs |
Parses optional agent types and tests defaults and validation. |
src/fixture.rs |
Updates agent fixtures and removes the shared appraisal fixture. |
src/agent.rs |
Defines AgentType and adds it to agents. |
schemas/input/agents.yaml |
Documents the optional agent_type field. |
benches/assets.rs |
Updates imports to the competitive module. |
Review effort: Balanced
|
@alexdewar Again, you missed all the conversations I had with Adam about this (mostly on Teams and not captured in issues). Happy to discuss further if you need more explanation. |
3db7b64 to
c241bd6
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #1577 +/- ##
==========================================
+ Coverage 89.12% 89.26% +0.14%
==========================================
Files 61 63 +2
Lines 9160 9244 +84
Branches 9160 9244 +84
==========================================
+ Hits 8164 8252 +88
+ Misses 677 675 -2
+ Partials 319 317 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
c241bd6 to
0c4d505
Compare
Pure move: appraisal.rs becomes competitive/metrics.rs and the appraisal submodules move alongside it, in preparation for supporting multiple asset selection algorithms. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…module select_best_assets, its helpers and the candidate/tranche logic move from investment.rs and market.rs into investment/competitive.rs. No behaviour change. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Option building and process exclusion move inside the algorithm, so select_assets_for_single_market only computes demand portions and limits. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Allows modules such as asset selection algorithms to own their debug files, which are created lazily on first write. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Remove appraisal-specific code from the output module. The competitive algorithm now writes debug_asset_selection_competitive.csv and debug_asset_selection_competitive_time_slices.csv via DataWriter::write_debug_rows. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
The optional agent_type column selects the asset selection algorithm for each agent. Currently the only option is `competitive` (the default), so existing models are unaffected. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
0c4d505 to
f6a556f
Compare
|
This makes sense to me as a first pass. As discussed, we can always fiddle with the interface for investment algos later. Off the top of your head, is there any more functionality in Not directly related to this PR, but In terms of the output stuff, I've got a couple of ideas:
|
| /// The region(s) in which the agent operates. | ||
| regions: String, | ||
| /// The algorithm that the agent uses to select assets (defaults to `competitive`). | ||
| agent_type: Option<AgentType>, |
There was a problem hiding this comment.
I don't think you need to make this an Option type as you've designated a default for the enum.
| let writer = match self.custom_writers.entry(file_name) { | ||
| Entry::Occupied(entry) => entry.into_mut(), | ||
| Entry::Vacant(entry) => { | ||
| entry.insert(csv::Writer::from_path(self.output_path.join(file_name))?) |
There was a problem hiding this comment.
How about we double-check the file doesn't already exist, as it would if someone passes the same
name as one of the existing output files:
| entry.insert(csv::Writer::from_path(self.output_path.join(file_name))?) | |
| // Create a new `csv::Writer`, ensuring file doesn't already exist | |
| let file = OpenOptions::new() | |
| .write(true) | |
| .create_new(true) | |
| .open(self.output_path.join(file_name)?; | |
| entry.insert(csv::Writer::from_writer(file) |
I'd also assert that the name starts with debug_ and ends with .csv.
| /// Get options from existing and potential assets for the given parameters | ||
| pub fn get_asset_options<'a>( | ||
| all_existing_assets: &'a [AssetRef], | ||
| demand: &'a DemandMap, | ||
| agent: &'a Agent, | ||
| commodity: &'a Commodity, | ||
| region_id: &'a RegionID, | ||
| year: u32, | ||
| capacity_tranche_fraction: Dimensionless, | ||
| ) -> impl Iterator<Item = AssetRef> + 'a { |
There was a problem hiding this comment.
Is this functionality specific to the competitive agent type? It seems pretty generic
There was a problem hiding this comment.
It's mostly generic, yes. The specific thing here is the tranching - setting candidate capacities according to capacity_tranche_fraction. The other algorithm doesn't need to do this.
There is probably a better way of sharing the generic parts, but I'll hold off for now and properly re-assess the interface once this current batch of PRs is done
| /// Get candidate assets which produce a particular commodity for a given agent | ||
| /// | ||
| /// Each candidate represents one investment tranche with a defined capacity. | ||
| /// - For processes with a defined `tranche_size`, the capacity is set to `tranche_size`. | ||
| /// - For processes without a defined `tranche_size`, the capacity is calculated based on the total | ||
| /// demand for the commodity and the asset's maximum annual production per unit capacity | ||
| /// (see `calculate_candidate_asset_capacity_scale`), then multiplied by | ||
| /// `capacity_tranche_fraction` to infer the tranche size. | ||
| fn get_candidate_assets<'a>( | ||
| demand: &'a DemandMap, | ||
| agent: &'a Agent, | ||
| region_id: &'a RegionID, | ||
| commodity: &'a Commodity, | ||
| year: u32, | ||
| capacity_tranche_fraction: Dimensionless, | ||
| ) -> impl Iterator<Item = AssetRef> + 'a { |
| let run_description = &run_description; | ||
| result.activity.iter().map(move |(time_slice, activity)| { |
There was a problem hiding this comment.
You only need to take a ref because you're moving it into the closure. This also works:
| let run_description = &run_description; | |
| result.activity.iter().map(move |(time_slice, activity)| { | |
| result.activity.iter().map(|(time_slice, activity)| { |
| } | ||
|
|
||
| /// Prepend the current debug context (if any) to a run description | ||
| pub fn debug_run_description(&self, run_description: &str) -> String { |
There was a problem hiding this comment.
Given that we'll often be returning run_description as is, you could also return a Cow<'a, str>
so that you only allocate a new string if needed. Not esp important
| @@ -0,0 +1,668 @@ | |||
| //! The competitive asset selection algorithm. | |||
There was a problem hiding this comment.
I assume the functions in this file haven't been changed much beyond being rehomed?
| @@ -0,0 +1,197 @@ | |||
| //! Debug output for the competitive asset selection algorithm. | |||
There was a problem hiding this comment.
I feel like output.rs might be a better name for this file
|
Looks like tests are failing after the conversion of |
Description
We want to support multiple different asset selection algorithms. The idea is that we will have different agent "types" which select assets in different ways (this is different to objectives which are used within each algorithm as a way of assessing asset options).
This is just a refactor of the current algorithm to make it possible to easily slot in new algorithms, introducing the concept of an agent type. I've called the current type "competitive", as I believe the algorithm was designed to try to simulate a competitive market. This is in contrast to the "portfolio" type introduced in #1574, in which agents are meant to be a single decision maker in change of a portfolio of assets.
The design decision is to make
select_assetsthe front-end for each algorithm, with any implementation details up to the algorithm (these should be private, although currently some need to be public for the benchmark, unfortunately). This includes making the algorithm in charge of its own debug files. To allow this I had to modify theoutputmodule to acceptcustom_writers.Fixes # (issue)
Type of change
Key checklist
$ cargo test$ cargo docpresent in the previous release
Further checks