LoginSignup
4
3

More than 3 years have passed since last update.

pythonのbinpackingライブラリーのすゝめ

Last updated at Posted at 2019-12-14

binpacking問題とは

説明しよう!binpacking問題とはあるランダムなサイズの品物とそれを詰める同じサイズのビンが複数あり、そのビンの数を最小にしようという問題である。

英語wiki

1次元binpacking問題

oneDpackというのがあります。
anconda promptで

pip install oneDpack

使い方
capとしてbinのサイズ、itemsに入れるもののサイズを入れます。
capが大きいととても遅くなります。

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))

出力

[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

2次元binpacking問題

reckpackというのがあります。
anconda promptで

pip install reckpack

使い方
binsのサイズ、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()

# 容器を設定する。
for i, b in enumerate(bins):
    packer.add_bin(*b, bid=i)

# 箱を設定する。
for i, r in enumerate(cargo):
    packer.add_rect(*r, rid=i)

# マス埋めを実行する。
packer.pack()


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

出力

左下からのx座標,左下からのy座標,左から右への長さ,下から上への長さを出力します。

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

可視化するとこうなります。

%matplotlib inline

def draw_result(packer):
    # 画面のサイズ
    fig = plt.figure(figsize=(20,10))
    for i, abin in enumerate(packer,1):
        ax = fig.add_subplot(i, len(packer), 1, aspect="equal")

        # 容器を描画する。
        ax.add_patch(Rectangle((0,0), abin.width, abin.height, fc="none", ec="g", lw=2, zorder=10))
        for r in abin:
            # 箱を描画する。
            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

3次元binpacking問題

目下捜索中です、おすすめがあればコメントにて教えてください。

4
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3