Skip to content

Commit f4e740e

Browse files
committed
Add helper methods for extracting keys + duals
1 parent 3f2cce6 commit f4e740e

2 files changed

Lines changed: 34 additions & 18 deletions

File tree

src/simulation/optimisation.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -78,35 +78,39 @@ impl Solution<'_> {
7878
})
7979
}
8080

81+
/// Returns constraint keys along with their corresponding dual values
82+
fn zip_duals<'a, I, T>(&'a self, keys: I, offset: usize) -> impl Iterator<Item = (T, f64)> + 'a
83+
where
84+
I: Iterator<Item = T> + 'a,
85+
{
86+
keys.zip(self.solution.dual_rows()[offset..].iter().copied())
87+
}
88+
8189
/// Keys and dual values for commodity balance constraints.
8290
pub fn iter_commodity_balance_duals(
8391
&self,
8492
) -> impl Iterator<Item = (&CommodityID, &RegionID, &TimeSliceID, f64)> {
8593
// Each commodity balance constraint applies to a particular time slice
8694
// selection (depending on time slice level). Where this covers multiple timeslices,
8795
// we return the same dual for each individual timeslice.
88-
self.constraint_keys
89-
.commodity_balance_keys
90-
.iter()
91-
.zip(self.solution.dual_rows())
92-
.flat_map(|((commodity_id, region_id, ts_selection), price)| {
93-
self.time_slice_info
94-
.iter_selection(ts_selection)
95-
.map(move |(ts, _)| (commodity_id, region_id, ts, *price))
96-
})
96+
self.zip_duals(
97+
self.constraint_keys.commodity_balance_keys.iter(),
98+
self.constraint_keys.commodity_balance_keys_offset(),
99+
)
100+
.flat_map(|((commodity_id, region_id, ts_selection), price)| {
101+
self.time_slice_info
102+
.iter_selection(ts_selection)
103+
.map(move |(ts, _)| (commodity_id, region_id, ts, price))
104+
})
97105
}
98106

99107
/// Keys and dual values for capacity constraints.
100108
pub fn iter_capacity_duals(&self) -> impl Iterator<Item = (AssetID, &TimeSliceID, f64)> {
101-
self.constraint_keys
102-
.capacity_keys
103-
.iter()
104-
.zip(
105-
self.solution.dual_rows()[self.constraint_keys.commodity_balance_keys.len()..]
106-
.iter()
107-
.copied(),
108-
)
109-
.map(|((asset_id, time_slice), dual)| (*asset_id, time_slice, dual))
109+
self.zip_duals(
110+
self.constraint_keys.capacity_keys.iter(),
111+
self.constraint_keys.capacity_keys_offset(),
112+
)
113+
.map(|((asset_id, time_slice), dual)| (*asset_id, time_slice, dual))
110114
}
111115
}
112116

src/simulation/optimisation/constraints.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ pub struct ConstraintKeys {
2323
pub capacity_keys: CapacityConstraintKeys,
2424
}
2525

26+
impl ConstraintKeys {
27+
/// Start offset for commodity balance constraints
28+
pub fn commodity_balance_keys_offset(&self) -> usize {
29+
0
30+
}
31+
32+
/// Start offset for capacity constraints
33+
pub fn capacity_keys_offset(&self) -> usize {
34+
self.commodity_balance_keys.len()
35+
}
36+
}
37+
2638
/// Add asset-level constraints
2739
///
2840
/// Note: the ordering of constraints is important, as the dual values of the constraints must later

0 commit comments

Comments
 (0)