From b7ba14bd43cdc1a45ffeef318408619a61d4764e Mon Sep 17 00:00:00 2001 From: Josh Burman Date: Fri, 17 Dec 2021 03:42:58 +0000 Subject: [PATCH] make sure remaining items added to form after bundles are added, are not part of a bundle --- app/helpers/solver_helper.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/app/helpers/solver_helper.py b/app/helpers/solver_helper.py index 3fa9971..08d6c49 100644 --- a/app/helpers/solver_helper.py +++ b/app/helpers/solver_helper.py @@ -28,6 +28,7 @@ def build_constraints(solver_run, problem, items): # # 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) + total_bundle_items = 0 for bundle in selected_bundles: con = dict(zip([item.id for item in solver_run.items], @@ -36,6 +37,18 @@ def build_constraints(solver_run, problem, 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})' + total_bundle_items += bundle.count + + # make sure all other items added to the form + # are not a part of any bundle + # currently only supports single bundle constrains, will need refactoring for multiple bundle constraints + con = dict(zip([item.id for item in solver_run.items], + [(getattr(item, attribute.id, None) == None) + for item in solver_run.items])) + problem += lpSum([con[item.id] + * items[item.id] + for item in solver_run.items]) == solver_run.total_form_items - total_bundle_items, f'Remaining items are not of a bundle type' + return problem except ValueError as error: