From b0118655179e4c26c3ae523dd7264e0341f326a9 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Tue, 8 Oct 2024 18:43:24 +0100 Subject: [PATCH] adding the ability to switch off optimisation --- backend/app/plan/router.py | 37 ++++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/backend/app/plan/router.py b/backend/app/plan/router.py index 2dc03e60..f4924c71 100644 --- a/backend/app/plan/router.py +++ b/backend/app/plan/router.py @@ -631,27 +631,38 @@ async def trigger_plan(body: PlanTriggerRequest): for p in input_properties: if not recommendations.get(p.id): continue + input_measures = prepare_input_measures(recommendations[p.id], body.goal) current_sap_points = int(p.data["current-energy-efficiency"]) target_sap_points = epc_to_sap_lower_bound(body.goal_value) sap_gain = CostOptimiser.calculate_sap_gain_with_slack(target_sap_points - current_sap_points) - if body.budget: - optimiser = GainOptimiser( - input_measures, max_cost=body.budget, max_gain=sap_gain if sap_gain > 0 else 0 - ) + if not body.optimise: + if body.goal != "Increasing EPC": + raise NotImplementedError("Only EPC optimisation is currently supported") + solution = [] + for sub_list in input_measures: + # Select the entry with the highest gain, and if tied, choose the one with the lowest cost + best_measure = max(sub_list, key=lambda x: (x['gain'], -x['cost'])) + solution.append(best_measure) else: - # The minimum gain is the minimum number of SAP points required to get to the target SAP band - # If the gain is negative, the optimiser will return an empty solution - optimiser = CostOptimiser( - input_measures, - min_gain=sap_gain - ) - optimiser.setup() - optimiser.solve() - solution = optimiser.solution + if body.budget: + optimiser = GainOptimiser( + input_measures, max_cost=body.budget, max_gain=sap_gain if sap_gain > 0 else 0 + ) + else: + # The minimum gain is the minimum number of SAP points required to get to the target SAP band + # If the gain is negative, the optimiser will return an empty solution + optimiser = CostOptimiser( + input_measures, + min_gain=sap_gain + ) + + optimiser.setup() + optimiser.solve() + solution = optimiser.solution selected_recommendations = {r["id"] for r in solution}