Skip to content

Commit 0e95129

Browse files
committed
Track keys for fixed-asset constraints
1 parent 005a17d commit 0e95129

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

src/simulation/optimisation.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,19 @@ impl Solution<'_> {
112112
)
113113
.map(|((asset_id, time_slice), dual)| (*asset_id, time_slice, dual))
114114
}
115+
116+
/// Keys and dual values for fixed asset constraints.
117+
pub fn iter_fixed_asset_duals(
118+
&self,
119+
) -> impl Iterator<Item = (AssetID, &CommodityID, &TimeSliceID, f64)> {
120+
self.zip_duals(
121+
self.constraint_keys.fixed_asset_keys.iter(),
122+
self.constraint_keys.fixed_asset_keys_offset(),
123+
)
124+
.map(|((asset_id, commodity_id, time_slice), dual)| {
125+
(*asset_id, commodity_id, time_slice, dual)
126+
})
127+
}
115128
}
116129

117130
/// Perform the dispatch optimisation.

src/simulation/optimisation/constraints.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ pub type CommodityBalanceConstraintKeys = Vec<(CommodityID, RegionID, TimeSliceS
1515
/// Indicates the asset ID and time slice covered by each capacity constraint
1616
pub type CapacityConstraintKeys = Vec<(AssetID, TimeSliceID)>;
1717

18+
/// Indicates the asset ID, commodity ID and time slice for each fixed asset constraint
19+
pub type FixedAssetConstraintKeys = Vec<(AssetID, CommodityID, TimeSliceID)>;
20+
1821
/// The keys for different constraints
1922
pub struct ConstraintKeys {
2023
/// Keys for commodity balance constraints
2124
pub commodity_balance_keys: CommodityBalanceConstraintKeys,
2225
/// Keys for capacity constraints
2326
pub capacity_keys: CapacityConstraintKeys,
27+
/// Keys for fixed asset constraints
28+
pub fixed_asset_keys: FixedAssetConstraintKeys,
2429
}
2530

2631
impl ConstraintKeys {
@@ -33,6 +38,11 @@ impl ConstraintKeys {
3338
pub fn capacity_keys_offset(&self) -> usize {
3439
self.commodity_balance_keys.len()
3540
}
41+
42+
/// Start offset for capacity constraints
43+
pub fn fixed_asset_keys_offset(&self) -> usize {
44+
self.capacity_keys_offset() + self.capacity_keys.len()
45+
}
3646
}
3747

3848
/// Add asset-level constraints
@@ -75,7 +85,7 @@ pub fn add_asset_constraints(
7585
// need to add different constraints for assets with flexible and non-flexible flows.
7686
//
7787
// See: https://github.com/EnergySystemsModellingLab/MUSE_2.0/issues/360
78-
add_fixed_asset_constraints(
88+
let fixed_asset_keys = add_fixed_asset_constraints(
7989
problem,
8090
variables,
8191
assets,
@@ -88,6 +98,7 @@ pub fn add_asset_constraints(
8898
ConstraintKeys {
8999
commodity_balance_keys,
90100
capacity_keys,
101+
fixed_asset_keys,
91102
}
92103
}
93104

@@ -240,14 +251,15 @@ fn add_fixed_asset_constraints(
240251
time_slice_info: &TimeSliceInfo,
241252
commodity_balance_keys: &CommodityBalanceConstraintKeys,
242253
capacity_keys: &CapacityConstraintKeys,
243-
) {
254+
) -> FixedAssetConstraintKeys {
244255
// Sanity check: we rely on the dual rows corresponding to these constraints being
245256
// immediately after the commodity balance and capacity constraints in `problem`.
246257
assert!(
247258
problem.num_rows() == commodity_balance_keys.len() + capacity_keys.len(),
248259
"Fixed asset constraints must be added immediately after commodity constraints."
249260
);
250261

262+
let mut keys = FixedAssetConstraintKeys::new();
251263
for asset in assets.iter() {
252264
// Get first PAC. unwrap is safe because all processes have at least one PAC.
253265
let pac1 = asset.iter_pacs().next().unwrap();
@@ -264,7 +276,12 @@ fn add_fixed_asset_constraints(
264276
// We are enforcing that (var / flow) - (pac_var / pac_flow) = 0
265277
let var = variables.get(asset.id, &flow.commodity.id, time_slice);
266278
problem.add_row(0.0..=0.0, [(var, 1.0 / flow.flow), pac_term]);
279+
280+
// Keep track of the order in which constraints were added
281+
keys.push((asset.id, flow.commodity.id.clone(), time_slice.clone()));
267282
}
268283
}
269284
}
285+
286+
keys
270287
}

0 commit comments

Comments
 (0)