def compare_pyshipping_with_shotput(): from random import randint from pyshipping import binpack_simple as binpack from pyshipping.package import Package from time import time items = [] py_items = [] box_dims = sorted( [randint(100, 200), randint(100, 200), randint(100, 200)]) num_items = 500 for _ in xrange(num_items): item_dims = sorted( [randint(20, 100), randint(20, 100), randint(20, 100)]) items.append(ItemTuple(str(volume(item_dims)), item_dims, 0)) py_items.append(Package((item_dims[0], item_dims[1], item_dims[2]), 0)) start = time() items_packed = pack_boxes(box_dims, items) end = time() shotput = { 'num_parcels': len(items_packed), 'items_per_parcel': [len(parcel) for parcel in items_packed], 'time': end - start } py_box = Package((box_dims[0], box_dims[1], box_dims[2]), 0) start = time() py_items_packed = binpack.packit(py_box, py_items) end = time() pyshipping = { 'num_parcels': len(py_items_packed[0]), 'items_per_parcel': [len(parcel) for parcel in py_items_packed[0]], 'time': end - start } if len(items_packed) > len(py_items_packed[0]): best_results = 'pyshipping' elif len(items_packed) < len(py_items_packed[0]): best_results = 'shotput' else: best_results = 'tie' return { 'shotput': shotput, 'pyshipping': pyshipping, 'best_results': best_results }
def binpack(packages, bin=None, iterlimit=5000): """Packs a list of Package() objects into a number of equal-sized bins. Returns a list of bins listing the packages within the bins and a list of packages which can't be packed because they are to big.""" if not bin: bin = Package("600x400x400") return allpermutations(packages, bin, iterlimit)
def allpermutations_helper(permuted, todo, maxcounter, callback, bin, bestpack, counter): if not todo: return counter + callback(bin, permuted, bestpack) else: others = todo[1:] thispackage = todo[0] for dimensions in set(permutations((thispackage[0], thispackage[1], thispackage[2]))): thispackage = Package(dimensions, nosort=True) if thispackage in bin: counter = allpermutations_helper(permuted + [thispackage], others, maxcounter, callback, bin, bestpack, counter) if counter > maxcounter: raise Timeout('more than %d iterations tries' % counter) return counter
def test(): fd = open('testdata.txt') vorher = 0 nachher = 0 start = time.time() for line in fd: packages = [Package(pack) for pack in line.strip().split()] if not packages: continue bins, rest = binpack(packages) if rest: print("invalid data", rest, line) else: vorher += len(packages) nachher += len(bins) print(time.time() - start) print(vorher, nachher, float(nachher) / vorher * 100)
def test(func): fd = open('testdata.txt') vorher = 0 nachher = 0 start = time.time() counter = 0 for line in fd: counter += 1 if counter > 450: break packages = [Package(pack) for pack in line.strip().split()] if not packages: continue bins, rest = func(packages) if rest: print "invalid data", rest, line else: vorher += len(packages) nachher += len(bins) print time.time() - start, print vorher, nachher, float(nachher) / vorher * 100
def test_pyshipping(): """This test is to demonstrate how the pyshipping logic behaves with the same inputs as test_bin_pack_complex, with the exception that pyShipping allows for `Package` rotations along any axis. `Packages`s are conceptually equivalent to `Parcel`s. `bin`s are conceptually equivalent to `Compartment`s. Still, we see that even in a case where a perfect fit is achievable, the `pyshipping.bin_pack_simple` algorithm does not find it and instead ends its iteration prematurely. """ bin = Package("5x4x3") packages = [ Package("4x3x2"), Package("1x3x3"), Package("4x3x1"), Package("2x1x3"), Package("3x1x3"), ] bins, rest = binpack(packages, bin)