random select recursive solution for bundle constraints

This commit is contained in:
Joshua Burman
2022-02-09 03:26:09 -05:00
parent 8ce5e6e540
commit 14a07ff9b2
2 changed files with 53 additions and 20 deletions

View File

@ -56,6 +56,22 @@ def build_constraints(solver_run: SolverRun, problem: LpProblem, items: list[Ite
logging.error(error)
raise ItemGenerationError("Bundle min and/or max larger than bundle amount provided", error.args[0])
def get_random_bundles(total_form_items: int, total_bundles: int, bundles: list[Bundle], found_bundles = False) -> list[Bundle]:
selected_bundles = None
total_bundle_items = 0
while found_bundles == False:
selected_bundles = sample(bundles, total_bundles)
total_bundle_items = sum(bundle.count for bundle in selected_bundles)
if total_bundle_items <= total_form_items:
found_bundles = True
if found_bundles == True:
return selected_bundles
else:
return get_random_bundles(total_form_items, total_bundles - 1, bundles)
def valid_bundle_combinations(total_form_items: int, total_bundles: int, min_bundles: int, bundles: list[Bundle], selected_bundle_combinations: list[list[Bundle]] = []) -> list[list[Bundle]]:
if total_bundles < min_bundles:
return selected_bundle_combinations