def prepare_input_measures(property_recommendations, goal): """ Basic function to convert recommendations_to_upload to a format that is suitable for the optimiser - large :param property_recommendations: object containing the recommendations, created in the plan trigger api :param goal: goal to be optimised for, should be one of the keys in gain_map. E.g. if the gain is SAP points, the goal should reflect that desired gain :return: Nested list of input measures """ goal_map = { "Increase EPC": "sap_points" } goal_key = goal_map[goal] if not goal_key: raise NotImplementedError("Not implemented this gain type - investigate me") ventilation_rec = [rec for rec in property_recommendations if rec[0]["type"] == "mechanical_ventilation"][0] input_measures = [] for recs in property_recommendations: # We don't actually optimise ventilation if recs[0]["type"] == "mechanical_ventilation": continue if recs[0]["type"] in ["internal_wall_insulation", "external_wall_insulation", "cavity_wall_insulation"]: # Wall insulation and mechanical ventilation are paired. You can't have wall insulation without mechanical # ventilation ventilation_cost = ventilation_rec[0]["total"] if ventilation_rec else 0 ventilation_gain = ventilation_rec[0][goal_key] if ventilation_rec else 0 input_measures.append( [ { "id": rec["recommendation_id"], "cost": rec["total"] + ventilation_cost, "gain": rec[goal_key] + ventilation_gain, "type": rec["type"] } for rec in recs ] ) else: input_measures.append( [ { "id": rec["recommendation_id"], "cost": rec["total"], "gain": rec[goal_key], "type": rec["type"] } for rec in recs ] ) return input_measures