Merge pull request #20 from yardstick/feature/QUANT-1298-case-based-constraints
QUANT-1298: Case based constraints
This commit is contained in:
commit
13f0f383e0
@ -2,7 +2,7 @@ import csv
|
||||
import io
|
||||
import re
|
||||
|
||||
def items_csv_to_dict(items_csv_reader, irt_model):
|
||||
def items_csv_to_dict(items_csv_reader):
|
||||
items = []
|
||||
headers = []
|
||||
|
||||
@ -18,10 +18,14 @@ 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 passage id exists
|
||||
if row[key]:
|
||||
item['passage_id'] = row[key]
|
||||
# b param - tmep fix! use irt model b param for proper reference
|
||||
elif key == len(headers) - 1:
|
||||
item['b_param'] = row[key]
|
||||
elif key > 1:
|
||||
elif key > 2 and key < len(headers) - 1:
|
||||
item['attributes'].append({
|
||||
'id': col,
|
||||
'value': row[key],
|
||||
@ -53,7 +57,7 @@ def solution_to_file(buffer, total_form_items, forms):
|
||||
row = [form.status]
|
||||
|
||||
for result in form.tif_results + form.tcc_results:
|
||||
row += [f'value - {result.value}\nresult - {round(result.result, 2)}']
|
||||
row += [f'target - {result.value}\nresult - {round(result.result, 2)}']
|
||||
|
||||
# provide generated items and cut score
|
||||
row += [round(form.cut_score, 2)] + [item.id for item in form.items]
|
||||
|
@ -1,22 +1,56 @@
|
||||
from pulp import lpSum
|
||||
from random import randint, sample
|
||||
import logging
|
||||
|
||||
from lib.errors.item_generation_error import ItemGenerationError
|
||||
|
||||
def build_constraints(solver_run, problem, items):
|
||||
total_form_items = solver_run.total_form_items
|
||||
constraints = solver_run.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
|
||||
for constraint in constraints:
|
||||
attribute = constraint.reference_attribute
|
||||
min = constraint.minimum
|
||||
max = constraint.maximum
|
||||
|
||||
con = dict(zip([item.id for item in solver_run.items],
|
||||
[item.attribute_exists(attribute)
|
||||
for item in solver_run.items]))
|
||||
problem += lpSum([con[item.id]
|
||||
* items[item.id]
|
||||
for item in solver_run.items]) >= round(total_form_items * (min / 100)), f'{attribute.id} - {attribute.value} - min'
|
||||
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'
|
||||
if attribute.type == 'metadata':
|
||||
con = dict(zip([item.id for item in solver_run.items],
|
||||
[item.attribute_exists(attribute)
|
||||
for item in solver_run.items]))
|
||||
problem += lpSum([con[item.id]
|
||||
* items[item.id]
|
||||
for item in solver_run.items]) >= round(total_form_items * (min / 100)), f'{attribute.id} - {attribute.value} - min'
|
||||
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':
|
||||
# TODO: account for many different bundle types, since the id condition in L33 could yield duplicates
|
||||
total_bundles = randint(constraint.minimum, constraint.maximum)
|
||||
selected_bundles = sample(solver_run.bundles, total_bundles)
|
||||
total_bundle_items = 0
|
||||
|
||||
return problem
|
||||
for bundle in selected_bundles:
|
||||
con = dict(zip([item.id for item in solver_run.items],
|
||||
[(getattr(item, bundle.type, False) == bundle.id)
|
||||
for item in solver_run.items]))
|
||||
problem += lpSum([con[item.id]
|
||||
* items[item.id]
|
||||
for item in solver_run.items]) == bundle.count, f'Bundle constraint for {bundle.type} ({bundle.id})'
|
||||
total_bundle_items += bundle.count
|
||||
|
||||
# make sure all other items added to the form
|
||||
# are not a part of any bundle
|
||||
# currently only supports single bundle constraints, will need refactoring for multiple bundle constraints
|
||||
con = dict(zip([item.id for item in solver_run.items],
|
||||
[(getattr(item, attribute.id, None) == None)
|
||||
for item in solver_run.items]))
|
||||
problem += lpSum([con[item.id]
|
||||
* items[item.id]
|
||||
for item in solver_run.items]) == solver_run.total_form_items - total_bundle_items, f'Remaining items are not of a bundle type'
|
||||
|
||||
|
||||
return problem
|
||||
except ValueError as error:
|
||||
logging.error(error)
|
||||
raise ItemGenerationError("Bundle min and/or max larger than bundle amount provided", error.args[0])
|
||||
|
@ -1,4 +1,5 @@
|
||||
from lib.irt.models.three_parameter_logistic import ThreeParameterLogistic
|
||||
from lib.errors.item_generation_error import ItemGenerationError
|
||||
|
||||
class ItemResponseFunction():
|
||||
def __init__(self, irt_model):
|
||||
@ -8,5 +9,4 @@ class ItemResponseFunction():
|
||||
if self.model_data.model == '3PL':
|
||||
return ThreeParameterLogistic(self.model_data, kwargs).result()
|
||||
else:
|
||||
# potentially error out
|
||||
return None
|
||||
raise ItemGenerationError("irt model not supported or provided")
|
||||
|
@ -19,7 +19,7 @@ class ServiceListener(SqsListener):
|
||||
logging.info('Process complete for %s', service.file_name)
|
||||
|
||||
def main():
|
||||
logging.info('Starting Solver Service (v1.0.9)...')
|
||||
logging.info('Starting Solver Service (v1.1.0)...')
|
||||
listener = ServiceListener(
|
||||
os.environ['SQS_QUEUE'],
|
||||
region_name=os.environ['AWS_REGION'],
|
||||
@ -31,6 +31,6 @@ def main():
|
||||
|
||||
if __name__ == '__main__':
|
||||
myname=os.path.basename(sys.argv[0])
|
||||
pidfile='/tmp/%s' % myname # any name
|
||||
pidfile='/tmp/%s' % myname
|
||||
daemon = Daemonize(app=myname,pid=pidfile, action=main, foreground=True)
|
||||
daemon.start()
|
||||
|
6
app/models/bundle.py
Normal file
6
app/models/bundle.py
Normal file
@ -0,0 +1,6 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
class Bundle(BaseModel):
|
||||
id: int
|
||||
count: int
|
||||
type: str
|
@ -1,5 +1,5 @@
|
||||
from pydantic import BaseModel
|
||||
from typing import List
|
||||
from typing import List, Optional
|
||||
|
||||
from models.attribute import Attribute
|
||||
|
||||
@ -8,6 +8,7 @@ from lib.irt.item_information_function import ItemInformationFunction
|
||||
|
||||
class Item(BaseModel):
|
||||
id: int
|
||||
passage_id: Optional[int]
|
||||
attributes: List[Attribute]
|
||||
b_param: float = 0.00
|
||||
|
||||
@ -27,4 +28,4 @@ class Item(BaseModel):
|
||||
for attribute in self.attributes:
|
||||
if attribute.id == ref_attribute.id and attribute.value == ref_attribute.value:
|
||||
return True
|
||||
return False
|
||||
return False
|
||||
|
@ -4,11 +4,13 @@ from typing import List, Optional
|
||||
from models.item import Item
|
||||
from models.constraint import Constraint
|
||||
from models.irt_model import IRTModel
|
||||
from models.bundle import Bundle
|
||||
from models.objective_function import ObjectiveFunction
|
||||
from models.advanced_options import AdvancedOptions
|
||||
|
||||
class SolverRun(BaseModel):
|
||||
items: List[Item]
|
||||
bundles: Optional[Bundle]
|
||||
constraints: List[Constraint]
|
||||
irt_model: IRTModel
|
||||
objective_function: ObjectiveFunction
|
||||
@ -27,3 +29,37 @@ class SolverRun(BaseModel):
|
||||
def remove_items(self, items):
|
||||
self.items = [item for item in self.items if item not in items]
|
||||
return True
|
||||
|
||||
def generate_bundles(self):
|
||||
bundle_constraints = (constraint.reference_attribute for constraint in self.constraints if constraint.reference_attribute.type == 'bundle')
|
||||
|
||||
for bundle_constraint in bundle_constraints:
|
||||
type_attribute = bundle_constraint.id
|
||||
|
||||
for item in self.items:
|
||||
attribute_id = getattr(item, type_attribute, None)
|
||||
|
||||
# make sure the item has said attribute
|
||||
if attribute_id != None:
|
||||
# if there are pre-existing bundles, add new or increment existing
|
||||
# else create array with new bundle
|
||||
if self.bundles != None:
|
||||
# get index of the bundle in the bundles list if exists or None if it doesn't
|
||||
bundle_index = next((index for (index, bundle) in enumerate(self.bundles) if bundle.id == attribute_id and bundle.type == type_attribute), None)
|
||||
|
||||
# if the index doesn't exist add the new bundle of whatever type
|
||||
# else increment the count of the current bundle
|
||||
if bundle_index == None:
|
||||
self.bundles.append(Bundle(
|
||||
id=attribute_id,
|
||||
count=1,
|
||||
type=type_attribute
|
||||
))
|
||||
else:
|
||||
self.bundles[bundle_index].count += 1
|
||||
else:
|
||||
self.bundles = [Bundle(
|
||||
id=attribute_id,
|
||||
count=1,
|
||||
type=type_attribute
|
||||
)]
|
||||
|
@ -15,10 +15,14 @@ class LoftService(Base):
|
||||
def process(self):
|
||||
try:
|
||||
self.solver_run = SolverRun.parse_obj(self.retreive_attributes_from_message())
|
||||
self.solver_run.generate_bundles()
|
||||
self.solution = self.generate_solution()
|
||||
self.result = self.stream_to_s3_bucket()
|
||||
except ItemGenerationError as error:
|
||||
self.result = self.stream_to_s3_bucket(error)
|
||||
except TypeError as error:
|
||||
logging.error(error)
|
||||
self.result = self.stream_to_s3_bucket(ItemGenerationError("Provided params causing error in calculation results"))
|
||||
|
||||
def retreive_attributes_from_message(self):
|
||||
logging.info('Retrieving attributes from message...')
|
||||
@ -37,7 +41,7 @@ class LoftService(Base):
|
||||
items_csv_reader = csv_helper.file_stream_reader(items_csv)
|
||||
|
||||
# add items to attributes dict
|
||||
attributes['items'] = service_helper.items_csv_to_dict(items_csv_reader, attributes['irt_model'])
|
||||
attributes['items'] = service_helper.items_csv_to_dict(items_csv_reader)
|
||||
logging.info('Processed Attributes...')
|
||||
|
||||
return attributes
|
||||
@ -71,10 +75,10 @@ class LoftService(Base):
|
||||
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
|
||||
# multi-objective constraints
|
||||
for target in self.solver_run.objective_function.tif_targets:
|
||||
problem += lpSum([item.iif(self.solver_run, target.theta)*items[item.id]
|
||||
for item in self.solver_run.items]) <= target.value, f'min tif theta ({target.theta}) target value {target.value}'
|
||||
@ -88,8 +92,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]))
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user