refactor in prep for enemies

This commit is contained in:
Joshua Burman
2023-11-09 16:12:26 -05:00
parent 49178380a4
commit 11979193de
7 changed files with 159 additions and 115 deletions

View File

@ -11,8 +11,9 @@ from models.item import Item
from lib.errors.item_generation_error import ItemGenerationError
# should probably be factored out into a bundle class method or a method in the solver run
def build_constraints(solver_run: SolverRun, problem: LpProblem,
items: list[Item], bundles: list[Bundle], selected_items: list[Item], selected_bundles: list[Bundle]) -> LpProblem:
items: list[Item], bundles: list[Bundle], selected_items: list[Item], selected_bundles: list[Bundle], current_drift: int) -> LpProblem:
logging.info('Creating Constraints...')
try:
@ -47,7 +48,7 @@ def build_constraints(solver_run: SolverRun, problem: LpProblem,
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 selected_bundles != None:
if selected_bundles != None and selected_bundles > 0:
# make sure the total bundles used in generated form is limited between min-max set
problem += lpSum([
bundles[bundle.id] for bundle in selected_bundles
@ -55,6 +56,52 @@ def build_constraints(solver_run: SolverRun, problem: LpProblem,
int(constraint.maximum))
logging.info('Constraints Created...')
# Behold our very own Elastic constraints!
for tif_target in solver_run.objective_function.tif_targets:
problem += lpSum([
bundle.tif(solver_run.irt_model, tif_target.theta)
* bundles[bundle.id]
for bundle in selected_bundles
] + [
item.iif(solver_run, tif_target.theta) *
items[item.id]
for item in selected_items
]) >= tif_target.minimum(
), f'Min TIF theta({tif_target.theta}) at target {tif_target.value} drift at {current_drift}%'
problem += lpSum([
bundle.tif(solver_run.irt_model, tif_target.theta)
* bundles[bundle.id]
for bundle in selected_bundles
] + [
item.iif(solver_run, tif_target.theta) *
items[item.id]
for item in selected_items
]) <= tif_target.maximum(
), f'Max TIF theta({tif_target.theta}) at target {tif_target.value} drift at {current_drift}%'
for tcc_target in solver_run.objective_function.tcc_targets:
problem += lpSum([
bundle.trf(solver_run.irt_model, tcc_target.theta)
* bundles[bundle.id]
for bundle in selected_bundles
] + [
item.irf(solver_run, tcc_target.theta) *
items[item.id]
for item in selected_items
]) >= tcc_target.minimum(
), f'Min TCC theta({tcc_target.theta}) at target {tcc_target.value} drift at {current_drift}%'
problem += lpSum([
bundle.trf(solver_run.irt_model, tcc_target.theta)
* bundles[bundle.id]
for bundle in selected_bundles
] + [
item.irf(solver_run, tcc_target.theta) *
items[item.id]
for item in selected_items
]) <= tcc_target.maximum(
), f'Max TCC theta({tcc_target.theta}) at target {tcc_target.value} drift at {current_drift}%'
return problem
except ValueError as error:
logging.error(error)
@ -62,7 +109,7 @@ def build_constraints(solver_run: SolverRun, problem: LpProblem,
"Bundle min and/or max larger than bundle amount provided",
error.args[0])
# should probably be factored out into a bundle class method or a method in the solver run
def get_random_bundles(total_form_items: int,
bundles: list[Bundle],
min: int,