Repository navigation
Modify the "competitive" investment algorithm #1563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b9cbce7
16a9115
1cd4ad3
ce1836d
5437db6
100a894
db8ea3a
b940d6a
22a28dc
ff73c3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,8 +19,9 @@ fields: | |
| Optional (defaults to `portfolio` if omitted). With `portfolio`, the capacities of all | ||
| existing and candidate assets are chosen together in a single optimisation. With | ||
| `competitive`, existing and candidate assets compete in rounds, with the best option according | ||
| to the agent's objective selected in each round. See the [Agents](../model/agents.md) page for | ||
| details. | ||
| to the agent's objective selected in each round. Competitive agents are experimental and | ||
| require `please_give_me_broken_results = true` in `model.toml`. See the | ||
| [Agents](../model/agents.md) page for details. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could even link to the relevant subheading |
||
| - name: decision_rule | ||
| type: string | ||
| description: The decision rule applied to objectives | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ use crate::agent::{ | |
| AgentType, DecisionRule, | ||
| }; | ||
| use crate::commodity::CommodityMap; | ||
| use crate::model::ALLOW_DANGEROUS_OPTION_NAME; | ||
| use crate::process::ProcessMap; | ||
| use crate::region::{RegionID, parse_region_str}; | ||
| use anyhow::{Context, Result, bail, ensure}; | ||
|
|
@@ -58,8 +59,17 @@ pub fn read_agents( | |
| processes: &ProcessMap, | ||
| region_ids: &IndexSet<RegionID>, | ||
| milestone_years: &[u32], | ||
| allow_dangerous_options: bool, | ||
| ) -> Result<AgentMap> { | ||
| let mut agents = read_agents_file(model_dir, region_ids)?; | ||
| ensure!( | ||
| allow_dangerous_options | ||
| || agents | ||
| .values() | ||
| .all(|agent| agent.agent_type != AgentType::Competitive), | ||
| "Competitive agents are experimental. To use them, set the \ | ||
| {ALLOW_DANGEROUS_OPTION_NAME} option to true." | ||
| ); | ||
|
|
||
| // We read commodity portions first as they are required by `read_agent_search_spaces` | ||
| let mut agent_commodities = read_agent_commodity_portions( | ||
|
|
@@ -165,9 +175,6 @@ where | |
| mod tests { | ||
| use super::*; | ||
| use crate::agent::DecisionRule; | ||
| use crate::fixture::assert_patched_runs_ok_simple; | ||
| use crate::patch::FilePatch; | ||
| use itertools::Itertools; | ||
| use rstest::rstest; | ||
| use std::{fs, iter}; | ||
| use tempfile::tempdir; | ||
|
|
@@ -257,24 +264,4 @@ mod tests { | |
| } | ||
| } | ||
| } | ||
|
|
||
| #[rstest] | ||
| #[case::all_competitive(["competitive"; 4])] | ||
| #[case::all_portfolio(["portfolio"; 4])] | ||
| #[case::mixed(["competitive", "portfolio", "competitive", "portfolio"])] | ||
| fn model_runs_with_agent_types(#[case] agent_types: [&str; 4]) { | ||
| let mut lines = vec![ | ||
| "id,description,regions,decision_rule,decision_lexico_tolerance,agent_type".to_string(), | ||
| ]; | ||
| for (id, agent_type) in ["A0_GEX", "A0_GPR", "A0_ELC", "A0_RES"] | ||
| .iter() | ||
| .zip(agent_types) | ||
| { | ||
| lines.push(format!("{id},,all,single,,{agent_type}")); | ||
| } | ||
| let lines = lines.iter().map(String::as_str).collect_vec(); | ||
| assert_patched_runs_ok_simple!(vec![ | ||
| FilePatch::new(AGENT_FILE_NAME).with_replacement(&lines) | ||
| ]); | ||
| } | ||
|
Comment on lines
-261
to
-279
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a pity that we can't have tests that require dangerous options. I do think it would be worth having a test that this still runs, even if it's an experimental feature. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ use crate::time_slice::TimeSliceSelection; | |
| use crate::units::{Capacity, Dimensionless, Flow, FlowPerCapacity}; | ||
| use anyhow::{Result, ensure}; | ||
| use coefficients::calculate_coefficients_for_assets; | ||
| use indexmap::IndexMap; | ||
| use indexmap::IndexSet; | ||
| use itertools::{Itertools, chain}; | ||
| use log::{debug, warn}; | ||
|
|
@@ -95,7 +96,7 @@ pub fn select_assets( | |
| agent, | ||
| region_id, | ||
| prices, | ||
| demand.clone(), | ||
| demand, | ||
| year, | ||
| debug_writer, | ||
| ) | ||
|
|
@@ -245,7 +246,7 @@ pub fn select_best_assets( | |
| agent: &Agent, | ||
| region_id: &RegionID, | ||
| prices: &Prices, | ||
| mut demand: DemandMap, | ||
| demand: &DemandMap, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a reason not to move |
||
| year: u32, | ||
| mut debug_writer: Option<&mut DebugDataWriter>, | ||
| ) -> Result<Vec<AssetRef>> { | ||
|
|
@@ -269,9 +270,11 @@ pub fn select_best_assets( | |
|
|
||
| // Iteratively select the best asset until demand is met | ||
| let mut round = 0; | ||
| let mut best_assets: Vec<AssetRef> = Vec::new(); | ||
| let mut selected_assets: Vec<AssetRef> = Vec::new(); | ||
| let mut unmet_demand = demand.clone(); | ||
| let mut previous_solution = IndexMap::new(); | ||
| while is_any_remaining_demand( | ||
| &demand, | ||
| &unmet_demand, | ||
| model.parameters.remaining_demand_absolute_tolerance, | ||
| ) { | ||
| // Remove assets that would exceed the remaining limits for their processes from the options | ||
|
|
@@ -289,6 +292,7 @@ pub fn select_best_assets( | |
| commodity.id, | ||
| region_id | ||
| ); | ||
|
|
||
| // Appraise all options in parallel: each asset's appraisal is independent (all shared | ||
| // state is read-only within this block), so we can safely use Rayon here. | ||
| // Each HiGHS solve inside `appraise_investment` is configured to use only one thread | ||
|
|
@@ -303,11 +307,13 @@ pub fn select_best_assets( | |
|
|
||
| Ok(Some(appraise_investment( | ||
| model, | ||
| &selected_assets, | ||
| asset, | ||
| commodity, | ||
| objective_type, | ||
| &coefficients[asset], | ||
| &demand, | ||
| &coefficients, | ||
| demand, | ||
| &previous_solution, | ||
| )?)) | ||
| }) | ||
| .collect::<Result<Vec<_>>>()? // propagate any solver error | ||
|
|
@@ -322,7 +328,7 @@ pub fn select_best_assets( | |
| year, | ||
| &format!("{} {} round {}", commodity.id, agent.id, round), | ||
| &outputs, | ||
| &demand, | ||
| demand, | ||
| commodity.time_slice_level, | ||
| )?; | ||
| } | ||
|
|
@@ -366,23 +372,24 @@ pub fn select_best_assets( | |
| ); | ||
|
|
||
| // Record the selected asset | ||
| record_asset_selection(best_output.asset, &mut best_assets); | ||
| record_asset_selection(best_output.asset, &mut selected_assets); | ||
| previous_solution = best_output.solution; | ||
|
|
||
| demand = best_output.unmet_demand; | ||
| unmet_demand = best_output.unmet_demand; | ||
|
Comment on lines
+376
to
+378
|
||
| round += 1; | ||
| } | ||
|
|
||
| // Convert Candidate assets to Ready | ||
| // At this point we also assign the agent ID to the asset | ||
| for asset in &mut best_assets { | ||
| for asset in &mut selected_assets { | ||
| if asset.is_candidate() { | ||
| asset | ||
| .make_mut() | ||
| .select_candidate_for_investment(agent.id.clone()); | ||
| } | ||
| } | ||
|
|
||
| Ok(best_assets) | ||
| Ok(selected_assets) | ||
| } | ||
|
|
||
| /// Prepare existing assets for reappraisal. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The equations in this para don't render properly for me with
mdbookand neither do the ones in the last para. Maybe you need to escape some underscores?