From f15f9e604e541a2a3b620ac70fd0ac6e881ee234 Mon Sep 17 00:00:00 2001 From: Joshua Burman Date: Wed, 9 Feb 2022 16:45:38 -0500 Subject: [PATCH] checking that generated bundle set fits exam form item size limit --- app/helpers/solver_helper.py | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/app/helpers/solver_helper.py b/app/helpers/solver_helper.py index bbd6e89..0a9b1e6 100644 --- a/app/helpers/solver_helper.py +++ b/app/helpers/solver_helper.py @@ -2,6 +2,8 @@ from pulp import lpSum from random import randint, sample import logging +from models.bundle import Bundle + from lib.errors.item_generation_error import ItemGenerationError def build_constraints(solver_run, problem, items, bundles): @@ -27,9 +29,10 @@ def build_constraints(solver_run, problem, items, bundles): elif attribute.type == 'bundle': # TODO: account for many different bundle types, since the id condition in L33 could yield duplicates if solver_run.bundles != None: - total_bundles = randint(constraint.minimum, constraint.maximum) - selected_bundles = sample(solver_run.bundles, total_bundles) + # total_bundles = randint(constraint.minimum, constraint.maximum) + # selected_bundles = sample(solver_run.bundles, total_bundles, solver_run.bundles) total_bundle_items = 0 + selected_bundles = get_random_bundles(solver_run.total_form_items, solver_run.bundles, int(constraint.minimum), int(constraint.maximum)) for bundle in selected_bundles: con = dict(zip([item.id for item in solver_run.items], @@ -54,3 +57,20 @@ def build_constraints(solver_run, problem, items, bundles): except ValueError as error: 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, bundles: list[Bundle], min: int , max: int, found_bundles = False) -> list[Bundle]: + selected_bundles = None + total_bundle_items = 0 + total_bundles = randint(min, max) + + 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) \ No newline at end of file