Recommendation of binpacking library of python

What is the binpacking problem?

Let's explain! The binpacking problem is a problem in which there are a random size item and multiple bins of the same size to pack it, and the number of bins is minimized.

English wiki

1D binpacking problem

There is oneDpack. with anaconda prompt

pip install oneDpack

How to use Enter the size of bin as cap and the size of what you want to put in items. Larger caps are very slow.

from oneDpack import*

cap=60
items=[2,4,5,2,7,4,7,3,7,3,7,43,8,2,4,13,6,4,3,2,4,7,9,10,40,23]
for i in packing(cap,items):
    print(i)
    print(sum(i))

output

[4, 13, 43]
60
[2, 3, 4, 4, 7, 40]
60
[2, 2, 3, 3, 4, 7, 7, 7, 7, 8, 10]
60
[2, 4, 5, 6, 9, 23]
49

2D binpacking problem

There is a reckpack. with anaconda prompt

pip install reckpack

How to use Enter the size of bins and the vertical and horizontal sizes of cargo.

import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
from collections import*
from rectpack import newPacker
from pandas import*
from numpy import*

bins = [(50,50)]
cargo = [(15,20),(30,20),(15,20),(10,20),(20,14),(15,20),(11,20),(30,10)]

packer = newPacker()

#Set the container.
for i, b in enumerate(bins):
    packer.add_bin(*b, bid=i)

#Set the box.
for i, r in enumerate(cargo):
    packer.add_rect(*r, rid=i)

#Perform mass filling.
packer.pack()


for i, abin in enumerate(packer,1):
    for r in abin:
        print(r.x,r.y,r.width, r.height)

output

Outputs the x-coordinate from the lower left, the y-coordinate from the lower left, the length from left to right, and the length from bottom to top.

0 0 30 20
30 0 20 15
30 15 20 15
0 30 15 20
0 20 30 10
15 30 14 20
29 30 11 20
40 30 10 20

It will be like this when visualized.

%matplotlib inline

def draw_result(packer):
    #Screen size
    fig = plt.figure(figsize=(20,10))
    for i, abin in enumerate(packer,1):
        ax = fig.add_subplot(i, len(packer), 1, aspect="equal")
        
        #Draw the container.
        ax.add_patch(Rectangle((0,0), abin.width, abin.height, fc="none", ec="g", lw=2, zorder=10))
        for r in abin:
            #Draw a box.
            ax.add_patch(Rectangle((r.x, r.y), r.width, r.height, fc="lightblue", ec="k"))
            cx, cy = r.x + r.width / 2, r.y + r.height / 2
            ax.text(cx, cy, r.rid, ha ="center",va="center", color="k", fontsize=14)
        
        ax.relim()
        ax.autoscale_view()

draw_result(packer)

ダウンロード.png

3D binpacking problem

We are currently searching, please let us know in the comments if you have any recommendations.

Recommended Posts

Recommendation of binpacking library of python
Python 3.6 email library
Introduction of Python
Python ast library
Recommendation of Poetry
Basics of Python ①
Basics of python ①
Copy of python
Python Library notes
Recommendation of Altair! Data visualization with Python
Introduction of Python
Chord recognition using chromagram of python library librosa
Python & Machine Learning Study Memo ②: Introduction of Library
Introduction of Python Imaging Library (PIL) using HomeBrew
[Python of Hikari-] Chapter 08-04 Module (Installation of external library)
[Python] Operation of enumerate
List of python modules
Unification of Python environment
python algorithmic trading library
Copy of python preferences
Basics of Python scraping basics
[python] behavior of argmax
Usage of Python locals ()
the zen of Python
Installation of Python 3.3 rc1
Types of recommendation systems
Install python external library
Python optimization library Pulp
# 4 [python] Basics of functions
Basic knowledge of Python
Sober trivia of python3
Summary of Python arguments
Basics of python: Output
Installation of matplotlib (Python 3.3.2)
Application of Python 3 vars
Various processing of Python
Recommendation of building a portable Python environment with conda
[Python] Get the list of ExifTags names of Pillow library
[Introduction to Python] Basic usage of the library matplotlib
Aim python library master (48) autopep8
Aim python library master (36) json2html
Aim python library master (49) psidialogs
Aim python library master (26) easyxml
[Python] Correct usage of map
Aim python library master (29) table_printer
Python evolutionary computation library Deap
Aim python library master (55) namespaces
Aim python library master (46) browserplus
Python Evolutionary Computation Library Deap (3)
Towards the retirement of Python2
Summary of python file operations
Summary of Python3 list operations
Python --Quick start of logging
Aim python library master (3) workalendar
Aim python library master (42) speedrecorder
Aim python library master (44) pynetviz
Aim python library master (8) rolex
Aim python library master (52) marktime
Aim python library master (7) numparser
Aim python library master (21) hy
Convenient library of Tensorflow TF-Slim