Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from copy import deepcopy
- def bin_pack(items, min_pm, max_pm, current_packing=None, solution=None):
- """Generate optimal packs of cables, the sum being between min and max capacity."""
- if current_packing is None:
- current_packing = []
- if not items:
- # Stop conditions: we have no item to fit in packages
- if solution is None or len(current_packing) < solution:
- # If our solution doesn't respect min_pm, it's not returned, return best known solution instead
- for pack in current_packing:
- if sum((item[3] for item in pack)) < min_pm:
- return solution
- # Solutions must be cleanly copied because we pop and append in current_packing
- return deepcopy(current_packing)
- return solution
- # We iterate by poping items and inserting in a list of list of items
- item = items.pop()
- # Try to fit in current packages
- for pack in current_packing:
- if sum((item[3] for item in pack)) + item[3] <= max_pm:
- pack.append(item)
- solution = bin_pack(items, min_pm, max_pm, current_packing, solution)
- pack.remove(item)
- # Try to make a new package
- if solution is None or len(solution) > len(current_packing):
- current_packing.append([item])
- solution = bin_pack(items, min_pm, max_pm, current_packing, solution)
- current_packing.remove([item])
- items.insert(-1, item)
- return solution
Add Comment
Please, Sign In to add comment