92 lines
3.6 KiB
Python
92 lines
3.6 KiB
Python
from pulp import lpSum, LpProblem
|
|
from random import randint, sample
|
|
|
|
import logging
|
|
|
|
from helpers.common_helper import *
|
|
|
|
from models.bundle import Bundle
|
|
from models.solver_run import SolverRun
|
|
from models.item import Item
|
|
|
|
from lib.errors.item_generation_error import ItemGenerationError
|
|
|
|
def build_constraints(solver_run: SolverRun, problem: LpProblem,
|
|
items: list[Item], bundles: list[Bundle]) -> LpProblem:
|
|
logging.info('Creating Constraints...')
|
|
|
|
try:
|
|
total_form_items = solver_run.total_form_items
|
|
constraints = solver_run.constraints
|
|
|
|
for constraint in constraints:
|
|
attribute = constraint.reference_attribute
|
|
min = constraint.minimum
|
|
max = constraint.maximum
|
|
|
|
if attribute.type == 'metadata':
|
|
logging.info('Metadata Constraint Generating...')
|
|
con = dict(
|
|
zip([item.id for item in solver_run.items], [
|
|
boolean_to_int(item.attribute_exists(attribute))
|
|
for item in solver_run.items
|
|
]))
|
|
|
|
problem += lpSum(
|
|
[
|
|
len(bundle.items_with_attribute(attribute)) * bundles[bundle.id] for bundle in solver_run.bundles
|
|
] +
|
|
[
|
|
con[item.id] * items[item.id] for item in solver_run.unbundled_items()
|
|
]
|
|
) >= round(total_form_items * (min / 100)), f'{attribute.id} - {attribute.value} - min'
|
|
|
|
problem += lpSum(
|
|
[
|
|
len(bundle.items_with_attribute(attribute)) * bundles[bundle.id] for bundle in solver_run.bundles
|
|
] +
|
|
[
|
|
con[item.id] * items[item.id] for item in solver_run.unbundled_items()
|
|
]
|
|
) <= round(total_form_items * (max / 100)), f'{attribute.id} - {attribute.value} - max'
|
|
elif attribute.type == 'bundle':
|
|
logging.info('Bundles Constraint Generating...')
|
|
# TODO: account for many different bundle types, since the id condition in L33 could yield duplicates
|
|
if solver_run.bundles != None:
|
|
# make sure the total bundles used in generated form is limited between min-max set
|
|
problem += lpSum([
|
|
bundles[bundle.id] for bundle in solver_run.bundles
|
|
]) == randint(int(constraint.minimum),
|
|
int(constraint.maximum))
|
|
|
|
logging.info('Constraints Created...')
|
|
return problem
|
|
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)
|
|
logging.info(f'Selecting Bundles (total of {total_bundles})...')
|
|
|
|
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)
|