Skip to content

Commit 04f8738

Browse files
committed
Fix: run_dispatch_for_year: Correctly handle case where there are no existing assets
1 parent 144ced3 commit 04f8738

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
@@ -196,23 +196,35 @@ fn run_dispatch_for_year(
196196
year: u32,
197197
writer: &mut DataWriter,
198198
) -> Result<(FlowMap, CommodityPrices, ReducedCosts)> {
199-
// Dispatch optimisation with existing assets only
200-
let solution_existing =
201-
DispatchRun::new(model, assets, year).run("final without candidates", writer)?;
202-
let flow_map = solution_existing.create_flow_map();
203-
204-
// Perform a separate dispatch run with existing assets and candidates (if there are any)
205-
let solution = if candidates.is_empty() {
206-
solution_existing
207-
} else {
208-
DispatchRun::new(model, assets, year)
209-
.with_candidates(candidates)
210-
.run("final with candidates", writer)?
211-
};
212-
213-
// Calculate commodity prices and asset reduced costs
214-
let (prices, reduced_costs) =
215-
calculate_prices_and_reduced_costs(model, &solution, assets, year);
199+
// Run dispatch optimisation with existing assets only, if there are any. If not, then assume no
200+
// flows (i.e. all are zero)
201+
let (solution_existing, flow_map) = (!assets.is_empty())
202+
.then(|| -> Result<_> {
203+
let solution =
204+
DispatchRun::new(model, assets, year).run("final without candidates", writer)?;
205+
let flow_map = solution.create_flow_map();
206+
207+
Ok((Some(solution), flow_map))
208+
})
209+
.transpose()?
210+
.unwrap_or_default();
211+
212+
// Perform a separate dispatch run with both existing assets and candidates to get prices and
213+
// reduced costs, if there are any. If not, use the previous solution.
214+
let solution_for_prices = (!candidates.is_empty())
215+
.then(|| {
216+
DispatchRun::new(model, assets, year)
217+
.with_candidates(candidates)
218+
.run("final with candidates", writer)
219+
})
220+
.transpose()?
221+
.or(solution_existing);
222+
223+
// If there were either existing or candidate assets, we can calculate prices and reduced costs.
224+
// If not, return empty maps.
225+
let (prices, reduced_costs) = solution_for_prices
226+
.map(|solution| calculate_prices_and_reduced_costs(model, &solution, assets, year))
227+
.unwrap_or_default();
216228

217229
Ok((flow_map, prices, reduced_costs))
218230
}

0 commit comments

Comments
 (0)