From 9870bad1ea016c23daab8da73213e8bc89dba463 Mon Sep 17 00:00:00 2001 From: Josh Burman Date: Fri, 17 Dec 2021 02:25:03 +0000 Subject: [PATCH] better bundle constraint --- app/helpers/solver_helper.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/helpers/solver_helper.py b/app/helpers/solver_helper.py index c12c59c..3fa9971 100644 --- a/app/helpers/solver_helper.py +++ b/app/helpers/solver_helper.py @@ -25,13 +25,17 @@ def build_constraints(solver_run, problem, items): * items[item.id] for item in solver_run.items]) <= round(total_form_items * (max / 100)), f'{attribute.id} - {attribute.value} - max' elif attribute.type == 'bundle': - # TODO: account for many different bundle types, since the id condition in L33 could yield duplicates + # # TODO: account for many different bundle types, since the id condition in L33 could yield duplicates total_bundles = randint(constraint.minimum, constraint.maximum) selected_bundles = sample(solver_run.bundles, total_bundles) for bundle in selected_bundles: - problem += lpSum([items[item.id] for item in solver_run.items if getattr(item, bundle.type, None) == bundle.id]) == bundle.count, f'Bundle constraint for {bundle.type} ({bundle.id})' - + con = dict(zip([item.id for item in solver_run.items], + [(getattr(item, bundle.type, False) == bundle.id) + for item in solver_run.items])) + problem += lpSum([con[item.id] + * items[item.id] + for item in solver_run.items]) == bundle.count, f'Bundle constraint for {bundle.type} ({bundle.id})' return problem except ValueError as error: