nawebco/box-packer
Composer 安装命令:
composer require nawebco/box-packer
包简介
Yet another solution to the packing/knapsack problem.
README 文档
README
This module implements a rough solution to the bin packing problem. It was originally developed to support an e-commerce merchant's desire to offer a shipping option only if all the items in an order would fit into a certain box. This implementation supports multiple boxes as well.
The bin-packing problem is NP hard. This implementation is a heuristic that won't always provide an optimal solution, but it is fast and it's unlikely to provide a solution that's too difficult for a human to replicate when packing a box with real (physical) items.
Briefly, this algorithm works as follows:
- Largest (by volume) items are packed first.
- Items are packed in layers. Layers are as wide and long as the box and are as tall as the tallest object in the layer.
- Items are packed alongside each other in each layer with the longest edge on the bottom, oriented in a way that yields the largest remaining area.
- If an item doesn't fit in any existing layers, the algorithm starts a new layer.
- Each time an item is added, the algorithm attempts to add it to the lower layers in any remaining spaces (including above other items), unless doing so would increase the height of the layer.
- Each time a new layer is started, the algorithm checks to make sure the total height of the layers hasn't exceeded the box height.
Using NAWebCo\BoxPacker
To install, run composer require nawebco/box-packer in your project's root directory. If you aren't already using
Composer's autoloader, be sure to require vendor/autoload.php wherever you need access to the BoxPacker.
Usage example:
<?php
require 'vendor/autoload.php';
use NAWebCo\BoxPacker\Packer;
use NAWebCo\BoxPacker\GenericPackable;
$packer = new Packer();
// Width, length, height, description
// The order of the dimensions doesn't matter.
$packer->addBox(new GenericPackable(10, 15, 6, 'Big box'));
// Width, length, height, description
// The order of the dimensions doesn't matter.
$packer->addItem(new GenericPackable(8.5, 11, 2, 'First book'));
$packer->addItem(new GenericPackable(8.5, 11, 1.2, 'Second book'));
$packer->addItem(new GenericPackable(3, 3, 3, 'Carved wooden block'));
$result = $packer->pack();
if( $result->success() ){
echo "Everything fits!\n\n";
foreach( $result->getPackedBoxes(true) as $boxResults ){
$box = $boxResults['box'];
$contents = $boxResults['contents'];
$containerDescription = $box->getDescription() ?: 'One box';
echo "$containerDescription contains:\n";
foreach( $contents as $item ){
$itemDescription = $item->getDescription() ?: 'Item';
echo "$itemDescription ({$item->getWidth()}, {$item->getLength()}, {$item->getHeight()})\n";
}
}
} else {
echo count($result->getNotPackedItems()) . " items didn't fit.";
}
In the example above, packable items are represented by the Solid class. Any class that implements the SolidInterface
can be run through the Packer.
Requirements
- PHP version 5.6 or higher
License
NAWebCo\BoxPacker is MIT-licensed.
nawebco/box-packer 适用场景与选型建议
nawebco/box-packer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.97k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 05 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 nawebco/box-packer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nawebco/box-packer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.97k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 22
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-05-09