Skip to content
26 changes: 18 additions & 8 deletions benches/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
//! assets (from among existing and candidate assets) to meet demand for a given commodity/region
//! market during agent investment.
use criterion::{BatchSize, BenchmarkId, Criterion, criterion_group, criterion_main};
use muse2::asset::{AssetPool, AssetRef};
use muse2::asset::{AssetIterator, AssetPool, AssetRef};
use muse2::example::Example;
use muse2::input::load_model;
use muse2::model::Model;
use muse2::output::DataWriter;
use muse2::process::{Process, ProcessID};
use muse2::simulation::candidate_assets_for_next_year;
use muse2::simulation::investment::collect_preset_demands_for_year;
use muse2::simulation::investment::competitive::{get_asset_options, select_best_assets};
use muse2::simulation::investment::competitive::{get_candidate_assets, select_best_assets};
use muse2::simulation::market::{
collect_agent_limits, get_demand_portion_for_market, get_responsible_agents,
};
Expand Down Expand Up @@ -196,16 +196,25 @@ fn criterion_benchmark(c: &mut Criterion) {
Arc::new(build_synthetic_processes(&templates, n)),
);

let opt_assets: Vec<AssetRef> = get_asset_options(
&existing_assets,
let processes: Vec<_> = agent
.iter_search_space(region_id, &commodity.id, YEAR)
.cloned()
.collect();
let mut opt_assets: Vec<AssetRef> = existing_assets
.iter()
.filter_agent(&agent.id)
.filter_region(region_id)
.filter_primary_producers_of(&commodity.id)
.cloned()
.collect();
opt_assets.extend(get_candidate_assets(
&demand,
&agent,
commodity,
&processes,
region_id,
commodity,
YEAR,
model.parameters.capacity_tranche_fraction,
)
.collect();
));
let agent_addition_limits = collect_agent_limits(
&agent,
region_id,
Expand Down Expand Up @@ -240,6 +249,7 @@ fn criterion_benchmark(c: &mut Criterion) {
let run = || {
select_best_assets(
black_box(&model),
&[],
opt_assets,
agent_addition_limits,
agent_total_limits,
Expand Down
2 changes: 1 addition & 1 deletion src/graph/investment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ fn find_feedback_processes(
/// feedback process, i.e. one whose inputs from within the SCC all appear after the market in the
/// order. Producers with no inputs from within the SCC always qualify. This ensures that every
/// market still has a producer when feedback processes are excluded in the second investment
/// pass (see `select_assets_for_cycle`).
/// pass (see `run_agents_for_cycle`).
/// * The objective minimises the number of “forward” edges (edges that would point from an earlier
/// market to a later one), counted within the original SCC and treated as unit penalties. A small
/// bias (<1) is added to nudge exporters earlier without outweighing the main objective (a bias
Expand Down
3 changes: 2 additions & 1 deletion src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use crate::asset::{Asset, AssetID, AssetRef};
use crate::commodity::CommodityID;
use crate::process::ProcessID;
use crate::region::RegionID;
use crate::simulation::optimisation::{FlowMap, Solution};
use crate::simulation::activity::FlowMap;
use crate::simulation::optimisation::Solution;
use crate::simulation::prices::PriceMap;
use crate::time_slice::TimeSliceID;
use crate::units::{Activity, Capacity, Flow, Money, MoneyPerActivity, MoneyPerFlow};
Expand Down
20 changes: 15 additions & 5 deletions src/simulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ use log::info;
use std::path::Path;
use std::sync::Arc;

pub mod activity;
use activity::FlowMap;
pub mod optimisation;
use optimisation::{DispatchRun, FlowMap};
use optimisation::DispatchRun;
pub mod investment;
use investment::{collect_preset_demands_for_year, perform_agent_investment};
use investment::{AgentRunMode, collect_preset_demands_for_year, run_agents_for_year};
pub mod market;
pub mod prices;
pub use prices::PriceMap;
Expand Down Expand Up @@ -82,9 +84,17 @@ pub fn run(model: &Model, output_path: &Path, debug_model: bool) -> Result<()> {

// Perform agent investment
info!("Running agent investment...");
let selected_assets =
perform_agent_investment(model, year, &existing_assets, &prices, &mut writer)
.context("Agent investment failed")?;
// NB: activities from asset selection are not currently used
let selected_assets = run_agents_for_year(
model,
year,
&existing_assets,
AgentRunMode::Invest,
&prices,
&mut writer,
)
.context("Agent investment failed")?
.assets;

// Run dispatch optimisation to get updated prices for the next iteration
info!("Running dispatch optimisation...");
Expand Down
30 changes: 30 additions & 0 deletions src/simulation/activity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! Types representing the activities of assets and the commodity flows derived from them.
use crate::asset::AssetRef;
use crate::commodity::CommodityID;
use crate::time_slice::TimeSliceID;
use crate::units::{Activity, Flow};
use indexmap::IndexMap;

/// A map of asset activities in each time slice
pub type ActivityMap = IndexMap<(AssetRef, TimeSliceID), Activity>;

/// A map of commodity flows for assets in each time slice
pub type FlowMap = IndexMap<(AssetRef, CommodityID, TimeSliceID), Flow>;

/// Create a map of commodity flows from asset activities.
///
/// The flow of each commodity is the asset's activity multiplied by its flow coefficient.
pub fn create_flow_map<'a, I>(activity: I) -> FlowMap
where
I: IntoIterator<Item = (&'a AssetRef, &'a TimeSliceID, Activity)>,
{
let mut flows = FlowMap::new();
for (asset, time_slice, activity) in activity {
for flow in asset.iter_flows() {
let flow_key = (asset.clone(), flow.commodity.id.clone(), time_slice.clone());
flows.insert(flow_key, activity * flow.coeff);
}
}

flows
}
Loading
Loading