Simple, dependency-free knapsack solver
These instructions will get you a copy of the project installed in your composer dependencies.
PHP 7
Adding knapper to your project is as simple as adding it to your composer.json
composer require es02/knapper
$knapper = new Knapper($items, $boxes);
$results = $knapper->pack();
To use Knappper you will need an array of objects to be packed and an array of boxes to pack them into. Optionally you can supply the following fields:
- maxCubic (Default: null) - Set an upper limit on cubic weight for boxes
- maxWeight (Default: null) - set an upper limit on physical weight for boxes
- weightType (Options: 'g', 'kg', 'oz', 'lb') (Default: 'g') - Choose metric or imperial measurements for that weight
Some carriers require boxes in a shipment to remain below certain cubic/gross weight limits. These options allow you to achieve this with minimal effort.
$knapper = new Knapper($items, $boxes, 15, 15 , 'kg');
$results = $knapper->pack();
Both arrays require the following fields:
- length
- width
- height
- weight
$items[] = array('length' => 10, 'width' => 50, 'height' => 200, 'weight' => 2);
$boxes[] = array('length' => 20, 'width' => 60, 'height' => 250, 'weight' => 2.5);
In addition both support the following optional fields:
- name (Default: null) - Allows you to set a human readable name for your item/box
- lengthType (Options: 'cm', 'm', 'in', 'ft') (Default: 'cm') - Sets measurement type
- weightType (Options: 'g', 'kg', 'oz', 'lb') (Default: 'g') - Sets measurement type
Boxes can have the following optional fields
- quantity (Default: 0) - Sets whether or not a box size has a limited supply, leave at 0 for unlimited
Items can have the following optional fields
- thisWayUp (Default: false) - Sets whether or not the item can be placed on it's side
Where not manually set, optional fields will use their default values
$items[] = array(
'name' => "Sculpture"
'length' => 10,
'width' => 50,
'height' => 200,
'weight' => 2,
'lengthType' => 'in',
'weightType' => 'lb',
'thisWayUp' => false
);
$boxes[] = array(
'name' => "Large Packing Box",
'length' => 20,
'width' => 60,
'height' => 250,
'weight' => 2.5,
'lengthType' => 'in',
'weightType' => 'lb',
'quantity' => 20
);
Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We use SemVer for versioning. For the versions available, see the tags on this repository.
- Aimi Hobson - Initial work - ArachnidsGrip
This project is licensed under the GNU GPL - see the LICENSE.md file for details
- ReadMe template from PurpleBooth
- Changelog Template from KeepAChangelog
- Contributing template from PurpleBooth