clean up the generate_solution method
This commit is contained in:
@ -67,10 +67,8 @@ class LoftService(Base):
|
|||||||
solution = Solution(response_id=random.randint(100, 5000), forms=[]) # unsolved solution
|
solution = Solution(response_id=random.randint(100, 5000), forms=[]) # unsolved solution
|
||||||
|
|
||||||
# setup common Solver variables
|
# setup common Solver variables
|
||||||
items = LpVariable.dicts("Item", [item.id for item in self.solver_run.items], upBound=1, lowBound=0, cat='Binary')
|
items = LpVariable.dicts("Item", [item.id for item in self.solver_run.items], lowBound=0, upBound=1, cat='Binary')
|
||||||
bundles = LpVariable.dicts("Bundle", [bundle.id for bundle in self.solver_run.bundles], upBound=1, lowBound=0, cat='Binary')
|
bundles = LpVariable.dicts("Bundle", [bundle.id for bundle in self.solver_run.bundles], lowBound=0, upBound=1, cat='Binary')
|
||||||
# items = LpVariable.dicts("Item", [item.id for item in self.solver_run.items], cat='Binary')
|
|
||||||
# bundles = LpVariable.dicts("Bundle", [bundle.id for bundle in self.solver_run.bundles], cat='Binary')
|
|
||||||
|
|
||||||
# iterate for number of forms that require creation
|
# iterate for number of forms that require creation
|
||||||
for form_count in range(self.solver_run.total_forms):
|
for form_count in range(self.solver_run.total_forms):
|
||||||
@ -86,13 +84,14 @@ class LoftService(Base):
|
|||||||
problem = LpProblem('ata-form-generate', LpMinimize)
|
problem = LpProblem('ata-form-generate', LpMinimize)
|
||||||
|
|
||||||
# objective function
|
# objective function
|
||||||
# problem += lpSum(
|
problem += lpSum(
|
||||||
# [item.iif_irf_sum(self.solver_run) * items[item.id] for item in self.solver_run.unbundled_items()]
|
[
|
||||||
# +
|
bundle.count * bundles[bundle.id] for bundle in self.solver_run.bundles
|
||||||
# [bundle.tif_trf_sum(self.solver_run) * bundles[bundle.id] for bundle in self.solver_run.bundles]
|
] +
|
||||||
# )
|
[
|
||||||
# problem += lpSum([items[item.id] for item in self.solver_run.unbundled_items()] + [bundles[bundle.id] for bundle in self.solver_run.bundles])
|
items[item.id] for item in self.solver_run.unbundled_items()
|
||||||
problem += lpSum([items[item.id] for item in self.solver_run.items])
|
]
|
||||||
|
)
|
||||||
|
|
||||||
# Form Constraints
|
# Form Constraints
|
||||||
problem += lpSum(
|
problem += lpSum(
|
||||||
@ -104,87 +103,55 @@ class LoftService(Base):
|
|||||||
]
|
]
|
||||||
) == self.solver_run.total_form_items, f'Total bundle form items for form {form_number}'
|
) == self.solver_run.total_form_items, f'Total bundle form items for form {form_number}'
|
||||||
|
|
||||||
# problem += lpSum(
|
|
||||||
# [
|
|
||||||
# 1 * items[item.id] for item in self.solver_run.items
|
|
||||||
# ]
|
|
||||||
# ) == self.solver_run.total_form_items, f'Total bundle form items for form {form_number}'
|
|
||||||
|
|
||||||
# Dynamic constraints.. currently we only support Metadata and Bundles(Cases/Passages)
|
# Dynamic constraints.. currently we only support Metadata and Bundles(Cases/Passages)
|
||||||
# problem = solver_helper.build_constraints(self.solver_run, problem, items, bundles)
|
problem = solver_helper.build_constraints(self.solver_run, problem, items, bundles)
|
||||||
|
|
||||||
logging.info('Creating TIF and TCC Elastic constraints')
|
logging.info('Creating TIF and TCC Elastic constraints')
|
||||||
|
|
||||||
# Behold our very own Elastic constraints!
|
# Behold our very own Elastic constraints!
|
||||||
for tif_target in self.solver_run.objective_function.tif_targets:
|
for tif_target in self.solver_run.objective_function.tif_targets:
|
||||||
# problem += lpSum(
|
|
||||||
# [
|
|
||||||
# bundle.tif(self.solver_run.irt_model, tif_target.theta) * bundles[bundle.id]
|
|
||||||
# for bundle in self.solver_run.bundles
|
|
||||||
# ] +
|
|
||||||
# [
|
|
||||||
# item.iif(self.solver_run, tif_target.theta) * items[item.id]
|
|
||||||
# for item in self.solver_run.unbundled_items()
|
|
||||||
# ]
|
|
||||||
# ) >= tif_target.value - (tif_target.value * drift_percent)
|
|
||||||
# problem += lpSum(
|
|
||||||
# [
|
|
||||||
# bundle.tif(self.solver_run.irt_model, tif_target.theta) * bundles[bundle.id]
|
|
||||||
# for bundle in self.solver_run.bundles
|
|
||||||
# ] +
|
|
||||||
# [
|
|
||||||
# item.iif(self.solver_run, tif_target.theta) * items[item.id]
|
|
||||||
# for item in self.solver_run.unbundled_items()
|
|
||||||
# ]
|
|
||||||
# ) <= tif_target.value + (tif_target.value * drift_percent)
|
|
||||||
|
|
||||||
problem += lpSum(
|
problem += lpSum(
|
||||||
[
|
[
|
||||||
item.iif(self.solver_run, tif_target.theta) * items[item.id]
|
bundle.tif(self.solver_run.irt_model, tif_target.theta) * bundles[bundle.id]
|
||||||
for item in self.solver_run.items
|
for bundle in self.solver_run.bundles
|
||||||
]
|
] +
|
||||||
) >= tif_target.minimum(drift_percent)
|
|
||||||
problem += lpSum(
|
|
||||||
[
|
[
|
||||||
item.iif(self.solver_run, tif_target.theta) * items[item.id]
|
item.iif(self.solver_run, tif_target.theta) * items[item.id]
|
||||||
for item in self.solver_run.items
|
for item in self.solver_run.unbundled_items()
|
||||||
]
|
]
|
||||||
) <= tif_target.maximum(drift_percent)
|
) >= tif_target.minimum(drift_percent), f'Min TIF theta({tif_target.theta}) at target {tif_target.value} drift at {current_drift}%'
|
||||||
|
problem += lpSum(
|
||||||
|
[
|
||||||
|
bundle.tif(self.solver_run.irt_model, tif_target.theta) * bundles[bundle.id]
|
||||||
|
for bundle in self.solver_run.bundles
|
||||||
|
] +
|
||||||
|
[
|
||||||
|
item.iif(self.solver_run, tif_target.theta) * items[item.id]
|
||||||
|
for item in self.solver_run.unbundled_items()
|
||||||
|
]
|
||||||
|
) <= tif_target.maximum(drift_percent), f'Max TIF theta({tif_target.theta}) at target {tif_target.value} drift at {current_drift}%'
|
||||||
|
|
||||||
for tcc_target in self.solver_run.objective_function.tcc_targets:
|
for tcc_target in self.solver_run.objective_function.tcc_targets:
|
||||||
# problem += lpSum(
|
|
||||||
# [
|
|
||||||
# bundle.trf(self.solver_run.irt_model, tcc_target.theta) * bundles[bundle.id]
|
|
||||||
# for bundle in self.solver_run.bundles
|
|
||||||
# ] +
|
|
||||||
# [
|
|
||||||
# item.irf(self.solver_run, tcc_target.theta) * items[item.id]
|
|
||||||
# for item in self.solver_run.unbundled_items()
|
|
||||||
# ]
|
|
||||||
# ) >= tcc_target.value - (tcc_target.value * drift_percent)
|
|
||||||
# problem += lpSum(
|
|
||||||
# [
|
|
||||||
# bundle.trf(self.solver_run.irt_model, tcc_target.theta) * bundles[bundle.id]
|
|
||||||
# for bundle in self.solver_run.bundles
|
|
||||||
# ] +
|
|
||||||
# [
|
|
||||||
# item.irf(self.solver_run, tcc_target.theta) * items[item.id]
|
|
||||||
# for item in self.solver_run.unbundled_items()
|
|
||||||
# ]
|
|
||||||
# ) <= tcc_target.value + (tcc_target.value * drift_percent)
|
|
||||||
|
|
||||||
problem += lpSum(
|
problem += lpSum(
|
||||||
[
|
[
|
||||||
item.irf(self.solver_run, tcc_target.theta) * items[item.id]
|
bundle.trf(self.solver_run.irt_model, tcc_target.theta) * bundles[bundle.id]
|
||||||
for item in self.solver_run.items
|
for bundle in self.solver_run.bundles
|
||||||
]
|
] +
|
||||||
) >= tcc_target.minimum(drift_percent)
|
|
||||||
problem += lpSum(
|
|
||||||
[
|
[
|
||||||
item.irf(self.solver_run, tcc_target.theta) * items[item.id]
|
item.irf(self.solver_run, tcc_target.theta) * items[item.id]
|
||||||
for item in self.solver_run.items
|
for item in self.solver_run.unbundled_items()
|
||||||
]
|
]
|
||||||
) <= tcc_target.maximum(drift_percent)
|
) >= tcc_target.minimum(drift_percent), f'Min TCC theta({tcc_target.theta}) at target {tcc_target.value} drift at {current_drift}%'
|
||||||
|
problem += lpSum(
|
||||||
|
[
|
||||||
|
bundle.trf(self.solver_run.irt_model, tcc_target.theta) * bundles[bundle.id]
|
||||||
|
for bundle in self.solver_run.bundles
|
||||||
|
] +
|
||||||
|
[
|
||||||
|
item.irf(self.solver_run, tcc_target.theta) * items[item.id]
|
||||||
|
for item in self.solver_run.unbundled_items()
|
||||||
|
]
|
||||||
|
) <= tcc_target.maximum(drift_percent), f'Max TCC theta({tcc_target.theta}) at target {tcc_target.value} drift at {current_drift}%'
|
||||||
|
|
||||||
logging.info(f'Solving for Form {form_number} with a drift of {current_drift}%')
|
logging.info(f'Solving for Form {form_number} with a drift of {current_drift}%')
|
||||||
problem.solve()
|
problem.solve()
|
||||||
@ -195,7 +162,7 @@ class LoftService(Base):
|
|||||||
if current_drift == Target.max_drift(): # this is the last attempt, so lets finalize the solution
|
if current_drift == Target.max_drift(): # this is the last attempt, so lets finalize the solution
|
||||||
for v in problem.variables(): print(v.name, "=", v.varValue);
|
for v in problem.variables(): print(v.name, "=", v.varValue);
|
||||||
|
|
||||||
breakpoint()
|
logging.info(problem)
|
||||||
logging.info(f'No feasible solution found for Form {form_number}!')
|
logging.info(f'No feasible solution found for Form {form_number}!')
|
||||||
|
|
||||||
self.add_form_to_solution(problem, solution)
|
self.add_form_to_solution(problem, solution)
|
||||||
@ -205,7 +172,8 @@ class LoftService(Base):
|
|||||||
current_drift += Target.max_drift_increment()
|
current_drift += Target.max_drift_increment()
|
||||||
else:
|
else:
|
||||||
for v in problem.variables(): print(v.name, "=", v.varValue);
|
for v in problem.variables(): print(v.name, "=", v.varValue);
|
||||||
breakpoint()
|
|
||||||
|
logging.info(problem)
|
||||||
logging.info(f'Optimal solution found with drift of {current_drift}%!')
|
logging.info(f'Optimal solution found with drift of {current_drift}%!')
|
||||||
|
|
||||||
self.add_form_to_solution(problem, solution)
|
self.add_form_to_solution(problem, solution)
|
||||||
|
Reference in New Issue
Block a user