checking that generated bundle set fits exam form item size limit
This commit is contained in:
parent
ab9b5525a4
commit
f15f9e604e
@ -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)
|
Loading…
x
Reference in New Issue
Block a user