random select recursive solution for bundle constraints
This commit is contained in:
@ -74,33 +74,36 @@ class LoftService(Base):
|
||||
response_id=random.randint(100, 5000),
|
||||
forms=[]
|
||||
)
|
||||
|
||||
|
||||
# initiate problem
|
||||
problem = None
|
||||
count = 0
|
||||
|
||||
if bundle_constraint:
|
||||
# generate valid bundle combinations
|
||||
bundle_combinations = solver_helper.valid_bundle_combinations(
|
||||
self.solver_run.total_form_items,
|
||||
int(bundle_constraint.maximum),
|
||||
int(bundle_constraint.minimum),
|
||||
self.solver_run.bundles)
|
||||
# # generate valid bundle combinations
|
||||
# bundle_combinations = solver_helper.valid_bundle_combinations(
|
||||
# self.solver_run.total_form_items,
|
||||
# int(bundle_constraint.maximum),
|
||||
# int(bundle_constraint.minimum),
|
||||
# self.solver_run.bundles)
|
||||
|
||||
# scramble bundle_combinations to ensure distinctiveness for each form generated
|
||||
random.shuffle(bundle_combinations)
|
||||
# # scramble bundle_combinations to ensure distinctiveness for each form generated
|
||||
# random.shuffle(bundle_combinations)
|
||||
|
||||
for bundles in bundle_combinations:
|
||||
problem = self.solve_problem(items, bundles)
|
||||
|
||||
# if optimal solution found, break loop
|
||||
if LpStatus[problem.status] == 'Optimal':
|
||||
break
|
||||
# for bundles in bundle_combinations:
|
||||
# problem = self.solve(items, bundles)
|
||||
|
||||
# # if optimal solution found, break loop
|
||||
# if LpStatus[problem.status] == 'Optimal':
|
||||
# break
|
||||
bundles_amount = random.randint(int(bundle_constraint.minimum), int(bundle_constraint.maximum))
|
||||
problem = self.recursive_solve(items, bundles_amount)
|
||||
else: # no bundles
|
||||
problem = self.solve_problem(items)
|
||||
|
||||
problem = self.solve(items)
|
||||
|
||||
# successfull form, increment and exit out of loop
|
||||
f += 1
|
||||
|
||||
|
||||
# add return items and create as a form
|
||||
form_items = service_helper.solution_items(problem.variables(), self.solver_run)
|
||||
|
||||
@ -108,8 +111,22 @@ class LoftService(Base):
|
||||
solution.forms.append(Form.create(form_items, self.solver_run, LpStatus[problem.status]))
|
||||
|
||||
return solution
|
||||
|
||||
def solve_problem(self, items: list[Item], bundles: list[Bundle] or None = None) -> LpProblem:
|
||||
|
||||
def recursive_solve(self, items, bundles_amount, attempts = 1000) -> LpProblem:
|
||||
selected_bundles = solver_helper.get_random_bundles(
|
||||
self.solver_run.total_form_items,
|
||||
bundles_amount,
|
||||
self.solver_run.bundles)
|
||||
|
||||
problem = self.solve(items, selected_bundles)
|
||||
|
||||
# if optimal solution found, break loop
|
||||
if LpStatus[problem.status] == 'Optimal' or attempts == 0:
|
||||
return problem
|
||||
else:
|
||||
return self.recursive_solve(items, max, attempts - 1)
|
||||
|
||||
def solve(self, items: list[Item], bundles: list[Bundle] or None = None) -> LpProblem:
|
||||
# create problem
|
||||
problem = LpProblem("ata-form-generate", LpMinimize)
|
||||
|
||||
|
Reference in New Issue
Block a user