diff --git a/recommendations/optimiser/CostOptimiser.py b/recommendations/optimiser/CostOptimiser.py index 294a6bba..50f4b884 100644 --- a/recommendations/optimiser/CostOptimiser.py +++ b/recommendations/optimiser/CostOptimiser.py @@ -12,7 +12,7 @@ class CostOptimiser: # We add an optional buffer to the minimum gain to allow for slack in the optimisation BUFFER = 0.2 - def __init__(self, components, min_gain): + def __init__(self, components, min_gain, verbose=False): self.components = components self.min_gain = min_gain self.gain_constraint = None @@ -22,6 +22,7 @@ class CostOptimiser: self.solution_cost = None self.solution_gain = None + self.verbose = verbose @classmethod def calculate_sap_gain_with_slack(cls, min_gain: int | float): @@ -42,6 +43,8 @@ class CostOptimiser: def setup(self): # Initialize Model self.m = Model("knapsack") + # Set the verbosity level + self.m.verbose = 1 if self.verbose else 0 # Create variables self.variables = [ diff --git a/recommendations/optimiser/GainOptimiser.py b/recommendations/optimiser/GainOptimiser.py index b11f7e87..7b2e56d2 100644 --- a/recommendations/optimiser/GainOptimiser.py +++ b/recommendations/optimiser/GainOptimiser.py @@ -9,7 +9,7 @@ class GainOptimiser: This class is used to maximise gain, given a constrained cost """ - def __init__(self, components, max_cost, max_gain, allow_slack=True): + def __init__(self, components, max_cost, max_gain, allow_slack=True, verbose=False): """ This function will try and maximise the gain, given a constrained cost. If we specific a max_gain, then the optimisation routine is constained to try not to exceed a maximum increase @@ -23,6 +23,7 @@ class GainOptimiser: :param max_gain: Maximum gain constraint :param allow_slack: If True, allows the model to use slack variables to relax the cost constraint if the model is infeasible. Defaults to True. + :param verbose: If True, enables verbose logging """ self.components = components self.max_cost = max_cost @@ -35,11 +36,15 @@ class GainOptimiser: self.solution_gain = None self.solution_cost = None self.allow_slack = allow_slack + self.verbose = verbose def setup(self): # Initialize Model self.m = Model("knapsack") + # Set the verbosity level + self.m.verbose = 1 if self.verbose else 0 + # Create variables self.variables = [ [self.m.add_var(var_type=BINARY, name=str(component["id"])) for component in group] for group in diff --git a/recommendations/tests/test_optimisers.py b/recommendations/tests/test_optimisers.py index bc16eb4a..07b42882 100644 --- a/recommendations/tests/test_optimisers.py +++ b/recommendations/tests/test_optimisers.py @@ -782,10 +782,6 @@ def optimise_with_funding_paths(input_measures, budget=None, target_gain=None, s total_gain = fixed_gain + sub_gain total_picks = fixed_items + picked - # you can change the objective here; I’ll use max gain under budget - if budget is not None and total_cost > budget + 1e-9: - continue - solutions.append({ "fixed_ids": fixed_ids, "items": total_picks,