Guest User

Untitled

a guest
Dec 13th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. from copy import deepcopy
  2.  
  3. def bin_pack(items, min_pm, max_pm, current_packing=None, solution=None):
  4. """Generate optimal packs of cables, the sum being between min and max capacity."""
  5. if current_packing is None:
  6. current_packing = []
  7. if not items:
  8. # Stop conditions: we have no item to fit in packages
  9. if solution is None or len(current_packing) < solution:
  10. # If our solution doesn't respect min_pm, it's not returned, return best known solution instead
  11. for pack in current_packing:
  12. if sum((item[3] for item in pack)) < min_pm:
  13. return solution
  14. # Solutions must be cleanly copied because we pop and append in current_packing
  15. return deepcopy(current_packing)
  16. return solution
  17. # We iterate by poping items and inserting in a list of list of items
  18. item = items.pop()
  19. # Try to fit in current packages
  20. for pack in current_packing:
  21. if sum((item[3] for item in pack)) + item[3] <= max_pm:
  22. pack.append(item)
  23. solution = bin_pack(items, min_pm, max_pm, current_packing, solution)
  24. pack.remove(item)
  25. # Try to make a new package
  26. if solution is None or len(solution) > len(current_packing):
  27. current_packing.append([item])
  28. solution = bin_pack(items, min_pm, max_pm, current_packing, solution)
  29. current_packing.remove([item])
  30. items.insert(-1, item)
  31. return solution
Add Comment
Please, Sign In to add comment