muse2/simulation/
update.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! Code for updating the simulation state.
use super::optimisation::Solution;
use super::CommodityPrices;
use crate::agent::AssetPool;
use crate::commodity::Commodity;
use log::info;
use std::collections::{HashMap, HashSet};
use std::rc::Rc;

/// Update commodity flows for assets based on the result of the dispatch optimisation.
pub fn update_commodity_flows(_solution: &Solution, _assets: &mut AssetPool) {
    info!("Updating commodity flows...");
}

/// Update commodity prices for assets based on the result of the dispatch optimisation.
pub fn update_commodity_prices(
    commodities: &HashMap<Rc<str>, Rc<Commodity>>,
    solution: &Solution,
    prices: &mut CommodityPrices,
) {
    info!("Updating commodity prices...");
    let commodities_updated = update_commodity_prices_from_solution(solution, prices);

    // Find commodities not updated in last step
    let remaining_commodities = commodities
        .keys()
        .filter(|id| !commodities_updated.contains(*id))
        .cloned();
    update_remaining_commodity_prices(remaining_commodities, prices);
}

/// Update the commodity prices for which there are values in the solution
fn update_commodity_prices_from_solution(
    solution: &Solution,
    prices: &mut CommodityPrices,
) -> HashSet<Rc<str>> {
    info!("Updating commodity prices...");

    let mut commodities_updated = HashSet::new();

    for (commodity_id, price) in solution.iter_commodity_prices() {
        prices.insert(Rc::clone(commodity_id), price);
        commodities_updated.insert(Rc::clone(commodity_id));
    }

    commodities_updated
}

/// Update prices for any commodity not updated by the dispatch step.
///
/// **TODO**: This will likely take additional arguments, depending on how we decide to do this step
///
/// # Arguments
///
/// * `commodity_ids` - IDs of commodities to update
/// * `prices` - Commodity prices
fn update_remaining_commodity_prices<I>(_commodity_ids: I, _prices: &mut CommodityPrices)
where
    I: Iterator<Item = Rc<str>>,
{
    info!("Updating remaining commodity prices...");
}