将加权项目启发式分配到箱子(固定数量的箱子或每个箱子固定数量的体积)。数据可以是列表、字典、元组列表或csv文件的形式。
binpacking的Python项目详细描述
这个包包含贪婪算法来解决两个典型的装箱问题 问题,(i)将物品分类到固定数量的箱子中,(ii)分类 将物品放入数量较少的固定大小的箱子中。下面是一个用法示例
>>>importbinpacking>>>>>>b={'a':10,'b':10,'c':11,'d':1,'e':2,'f':7}>>>bins=binpacking.to_constant_bin_number(b,4)# 4 being the number of bins>>>print("===== dict\n",b,"\n",bins)=====dict{'a':10,'b':10,'c':11,'d':1,'e':2,'f':7}[{'c':11},{'b':10},{'a':10},{'f':7,'e':2,'d':1}]>>>>>>b=list(b.values())>>>bins=binpacking.to_constant_volume(b,11)# 11 being the bin volume>>>print("===== list\n",b,"\n",bins)=====list[10,10,11,1,2,7][[11],[10],[10],[7,2,1]]
假设您有一个项目列表,每个项目都有一个权重w_i。 典型的问题是
- 我们如何将项目分配到最小数量的箱n 等体积v?
- 我们如何将这些项目精确地分配到nbin中 携带的项目,约为同等重量?
像这样的问题在现代计算中很容易发生。假设你有 运行计算,其中必须有许多不同大小的文件 加载到内存中。但是,您只有一台8GB的 猛撞。您应该如何绑定文件,以便运行 最少编程次数?这相当于解决问题 一。
问题2呢?说你必须运行大量 计算。每一份工作你都知道 完成。但是,你只有一个4核的CPU。应该怎么做 将作业分配到4个核心,以便它们都在 大致相同的时间?
该包提供命令行工具“binpacking”,使用哪个 可以轻松地bin-pack csv文件,其中包含可以识别的列 有分量的。要查看用法,请输入
$ binpacking -h Usage: binpacking [options] Options: -h, --help show this help message and exit -f FILEPATH, --filepath=FILEPATH path to the csv-file to be bin-packed -V V_MAX, --volume=V_MAX maximum volume per bin (constant volume algorithm will be used) -N N_BIN, --n-bin=N_BIN number of bins (constant bin number algorithm will be used) -c WEIGHT_COLUMN, --weight-column=WEIGHT_COLUMN integer (or string) giving the column number (or column name in header) where the weight is stored -H, --has-header parse this option if there is a header in the csv-file -d DELIM, --delimiter=DELIM delimiter in the csv-file (use "tab"for tabs) -q QUOTECHAR, --quotechar=QUOTECHAR quotecharacter in the csv-file -l LOWER_BOUND, --lower-bound=LOWER_BOUND weights below this bound will not be considered -u UPPER_BOUND, --upper-bound=UPPER_BOUND weights exceeding this bound will not be considered
安装
pip install binpacking
示例
在存储库的目录中
cd examples/ binpacking -f hamlet_word_count.csv -V 2000 -H -c count -l 10 -u 1000 binpacking -f hamlet_word_count.csv -N 4 -H -c count
或者在python中
importbinpackingb={'a':10,'b':10,'c':11,'d':1,'e':2,'f':7}bins=binpacking.to_constant_bin_number(b,4)print("===== dict\n",b,"\n",bins)b=list(b.values())bins=binpacking.to_constant_volume(b,11)print("===== list\n",b,"\n",bins)