Skip to content

Commit 179db46

Browse files
authored
Merge pull request #884 from EnergySystemsModellingLab/fix-dispatch-with-no-existing
Fix `run_dispatch_for_year` for no existing assets case
2 parents 8a495f3 + 04f8738 commit 179db46

1 file changed

Lines changed: 29 additions & 17 deletions

File tree

src/simulation.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -188,23 +188,35 @@ fn run_dispatch_for_year(
188188
year: u32,
189189
writer: &mut DataWriter,
190190
) -> Result<(FlowMap, CommodityPrices, ReducedCosts)> {
191-
// Dispatch optimisation with existing assets only
192-
let solution_existing =
193-
DispatchRun::new(model, assets, year).run("final without candidates", writer)?;
194-
let flow_map = solution_existing.create_flow_map();
195-
196-
// Perform a separate dispatch run with existing assets and candidates (if there are any)
197-
let solution = if candidates.is_empty() {
198-
solution_existing
199-
} else {
200-
DispatchRun::new(model, assets, year)
201-
.with_candidates(candidates)
202-
.run("final with candidates", writer)?
203-
};
204-
205-
// Calculate commodity prices and asset reduced costs
206-
let (prices, reduced_costs) =
207-
calculate_prices_and_reduced_costs(model, &solution, assets, year);
191+
// Run dispatch optimisation with existing assets only, if there are any. If not, then assume no
192+
// flows (i.e. all are zero)
193+
let (solution_existing, flow_map) = (!assets.is_empty())
194+
.then(|| -> Result<_> {
195+
let solution =
196+
DispatchRun::new(model, assets, year).run("final without candidates", writer)?;
197+
let flow_map = solution.create_flow_map();
198+
199+
Ok((Some(solution), flow_map))
200+
})
201+
.transpose()?
202+
.unwrap_or_default();
203+
204+
// Perform a separate dispatch run with both existing assets and candidates to get prices and
205+
// reduced costs, if there are any. If not, use the previous solution.
206+
let solution_for_prices = (!candidates.is_empty())
207+
.then(|| {
208+
DispatchRun::new(model, assets, year)
209+
.with_candidates(candidates)
210+
.run("final with candidates", writer)
211+
})
212+
.transpose()?
213+
.or(solution_existing);
214+
215+
// If there were either existing or candidate assets, we can calculate prices and reduced costs.
216+
// If not, return empty maps.
217+
let (prices, reduced_costs) = solution_for_prices
218+
.map(|solution| calculate_prices_and_reduced_costs(model, &solution, assets, year))
219+
.unwrap_or_default();
208220

209221
Ok((flow_map, prices, reduced_costs))
210222
}

0 commit comments

Comments
 (0)