basic optimisation framework working

This commit is contained in:
Khalim Conn-Kowlessar 2025-08-12 23:01:32 +01:00
parent bd3795eead
commit 8a3b2fdd6c
3 changed files with 10 additions and 6 deletions

View file

@ -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 = [

View file

@ -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

View file

@ -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; Ill 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,