mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-07-12 13:29:04 +00:00
basic optimisation framework working
This commit is contained in:
parent
bd3795eead
commit
8a3b2fdd6c
3 changed files with 10 additions and 6 deletions
|
|
@ -12,7 +12,7 @@ class CostOptimiser:
|
||||||
# We add an optional buffer to the minimum gain to allow for slack in the optimisation
|
# We add an optional buffer to the minimum gain to allow for slack in the optimisation
|
||||||
BUFFER = 0.2
|
BUFFER = 0.2
|
||||||
|
|
||||||
def __init__(self, components, min_gain):
|
def __init__(self, components, min_gain, verbose=False):
|
||||||
self.components = components
|
self.components = components
|
||||||
self.min_gain = min_gain
|
self.min_gain = min_gain
|
||||||
self.gain_constraint = None
|
self.gain_constraint = None
|
||||||
|
|
@ -22,6 +22,7 @@ class CostOptimiser:
|
||||||
|
|
||||||
self.solution_cost = None
|
self.solution_cost = None
|
||||||
self.solution_gain = None
|
self.solution_gain = None
|
||||||
|
self.verbose = verbose
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def calculate_sap_gain_with_slack(cls, min_gain: int | float):
|
def calculate_sap_gain_with_slack(cls, min_gain: int | float):
|
||||||
|
|
@ -42,6 +43,8 @@ class CostOptimiser:
|
||||||
def setup(self):
|
def setup(self):
|
||||||
# Initialize Model
|
# Initialize Model
|
||||||
self.m = Model("knapsack")
|
self.m = Model("knapsack")
|
||||||
|
# Set the verbosity level
|
||||||
|
self.m.verbose = 1 if self.verbose else 0
|
||||||
|
|
||||||
# Create variables
|
# Create variables
|
||||||
self.variables = [
|
self.variables = [
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ class GainOptimiser:
|
||||||
This class is used to maximise gain, given a constrained cost
|
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
|
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
|
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 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
|
: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.
|
is infeasible. Defaults to True.
|
||||||
|
:param verbose: If True, enables verbose logging
|
||||||
"""
|
"""
|
||||||
self.components = components
|
self.components = components
|
||||||
self.max_cost = max_cost
|
self.max_cost = max_cost
|
||||||
|
|
@ -35,11 +36,15 @@ class GainOptimiser:
|
||||||
self.solution_gain = None
|
self.solution_gain = None
|
||||||
self.solution_cost = None
|
self.solution_cost = None
|
||||||
self.allow_slack = allow_slack
|
self.allow_slack = allow_slack
|
||||||
|
self.verbose = verbose
|
||||||
|
|
||||||
def setup(self):
|
def setup(self):
|
||||||
# Initialize Model
|
# Initialize Model
|
||||||
self.m = Model("knapsack")
|
self.m = Model("knapsack")
|
||||||
|
|
||||||
|
# Set the verbosity level
|
||||||
|
self.m.verbose = 1 if self.verbose else 0
|
||||||
|
|
||||||
# Create variables
|
# Create variables
|
||||||
self.variables = [
|
self.variables = [
|
||||||
[self.m.add_var(var_type=BINARY, name=str(component["id"])) for component in group] for group in
|
[self.m.add_var(var_type=BINARY, name=str(component["id"])) for component in group] for group in
|
||||||
|
|
|
||||||
|
|
@ -782,10 +782,6 @@ def optimise_with_funding_paths(input_measures, budget=None, target_gain=None, s
|
||||||
total_gain = fixed_gain + sub_gain
|
total_gain = fixed_gain + sub_gain
|
||||||
total_picks = fixed_items + picked
|
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({
|
solutions.append({
|
||||||
"fixed_ids": fixed_ids,
|
"fixed_ids": fixed_ids,
|
||||||
"items": total_picks,
|
"items": total_picks,
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue