add constructed bundles to constraints, minor refatoring

This commit is contained in:
Josh Burman
2021-12-15 19:37:16 +00:00
parent 4afe81e15b
commit bfeb3c4207
3 changed files with 17 additions and 4 deletions

View File

@ -16,6 +16,10 @@ def items_csv_to_dict(items_csv_reader, irt_model):
for key, col in enumerate(headers):
if key == 0:
item[col] = row[key]
if key == 2:
# make sure id exists
if row[key]:
item['passage'] = row[key]
# b param - tmep fix! use irt model b param for proper reference
elif key == (1 - len(headers)):
item['b_param'] = row[key]

View File

@ -1,4 +1,5 @@
from pulp import lpSum
from random import randint, sample
def build_constraints(solver_run, problem, items):
total_form_items = solver_run.total_form_items
@ -19,5 +20,14 @@ def build_constraints(solver_run, problem, items):
problem += lpSum([con[item.id]
* items[item.id]
for item in solver_run.items]) <= round(total_form_items * (max / 100)), f'{attribute.id} - {attribute.value} - max'
elif attribute.type == 'bundle':
# currently only using passage bundles, but will eventually make more dynamic for many bundle types
bundles = solver_run.bundles('passage')
total_bundles = randint(constraint.minimum, constraint.maximum)
selected_bundles = sample(bundles, total_bundles)
for bundle in selected_bundles:
problem += lpSum([items[item.id] for item in solver_run.items if item.passage == bundle['id']]) == bundle['count'], f'Bundle constrain for {bundle["type"]} ({bundle["id"]})'
return problem

View File

@ -63,11 +63,11 @@ class LoftService(Base):
# create problem
problem = LpProblem("ata-form-generate", LpMinimize)
# constraints
# form item count constraint
problem += lpSum([items[item.id]
for item in self.solver_run.items]) == self.solver_run.total_form_items, 'Total form items'
# generic constraints
# dynamic constraints
problem = solver_helper.build_constraints(self.solver_run, problem, items)
# multi-objective functions and constraints
@ -90,8 +90,7 @@ class LoftService(Base):
# add return items and create as a form
form_items = service_helper.solution_items(problem.variables(), self.solver_run)
# remove items
self.solver_run.remove_items(form_items)
# add form to solution
solution.forms.append(Form.create(form_items, self.solver_run, LpStatus[problem.status]))