make sure to generate the correct solution before breaking the loop

This commit is contained in:
Adrian Manteza 2022-03-14 14:45:02 +00:00
parent 98922bfc4c
commit ff9d9b3d49
3 changed files with 22 additions and 16 deletions

View File

@ -9,7 +9,7 @@ class Target(BaseModel):
@classmethod @classmethod
def max_drift(cls): def max_drift(cls):
return 15 # 10% elasticity return 10 # 10% elasticity
@classmethod @classmethod
def max_drift_increment(cls): def max_drift_increment(cls):

View File

@ -156,9 +156,7 @@ class LoftService(Base):
] ]
) <= tcc_target.value + (tcc_target.value * drift_percent) ) <= tcc_target.value + (tcc_target.value * drift_percent)
# solve problem logging.info(f'Solving for Form {form_number} with a drift of {current_drift}%')
logging.info(f'Solving for Form {form_number}')
problem.solve() problem.solve()
if LpStatus[problem.status] == 'Infeasible': if LpStatus[problem.status] == 'Infeasible':
@ -170,25 +168,34 @@ class LoftService(Base):
print(problem.objective.value()) print(problem.objective.value())
print(problem.objective) print(problem.objective)
current_drift += Target.max_drift_increment() if current_drift == Target.max_drift(): # this is the last attempt, so lets finalize the solution
else: logging.info(f'No feasible solution found for Form {form_number}%!')
logging.info(f'solution found with drift of {current_drift}%!')
self.add_form_to_solution(problem, solution)
break break
logging.info(f'Solved for Form {form_number}...generating form and adding to solution') current_drift += Target.max_drift_increment()
else:
logging.info(f'Optimal solution found with drift of {current_drift}%!')
# add return items and create as a form self.add_form_to_solution(problem, solution)
form_items = service_helper.solution_items(problem.variables(), self.solver_run)
# add form to solution break
solution.forms.append(Form.create(form_items, self.solver_run, LpStatus[problem.status]))
logging.info('Form generated and added to solution...')
logging.info('Solution Generated.') logging.info('Solution Generated.')
return solution return solution
def add_form_to_solution(self, problem, solution):
# add return items and create as a form
form_items = service_helper.solution_items(problem.variables(), self.solver_run)
form = Form.create(form_items, self.solver_run, LpStatus[problem.status])
solution.forms.append(form)
logging.info('Form generated and added to solution...')
def stream_to_s3_bucket(self, error=None): def stream_to_s3_bucket(self, error=None):
self.file_name = f'{service_helper.key_to_uuid(self.key)}.csv' self.file_name = f'{service_helper.key_to_uuid(self.key)}.csv'
solution_file = None solution_file = None

View File

@ -119,7 +119,6 @@ class SolverSandbox:
print(problem.objective) print(problem.objective)
drift += 1 drift += 1
else: else:
print(f"solution found with drift of {drift}!") print(f"solution found with drift of {drift}!")