将加权项目启发式分配到箱子(固定数量的箱子或每个箱子固定数量的体积)。数据可以是列表、字典、元组列表或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。 典型的问题是

  1. 我们如何将项目分配到最小数量的箱n 等体积v
  2. 我们如何将这些项目精确地分配到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)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java为什么maven enforcer插件在maven版本3.6.1中失败,但在3.6.2中通过?   尝试从字符串获取日期时发生java ParseException   java使用ArrayList查找匹配的值。contains()并将其从ArrayList中删除   javascript UTF8编码在飞碟中不起作用   java Android:设备启动时是否可以访问数据库?   java如何在蛇游戏中创建网格   java颜色背景不起作用   java是否可以使用实例pojo与JDBC模板一起插入?   在Tomcat WebApp中运行的Java代码比从JVM调用时运行得慢得多   java GUI添加了组件,但没有显示空白框架   java计算圆中的每个笛卡尔点   sockets Java应用程序在中挂起。hasNext();   雅加达ee Java ee 7批处理API MultiResourceItemReader对应项   java获取当前jsp中的完整文件路径   java字符串数组添加重复项