Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd committed Jul 12, 2022
0 parents commit 323dab2
Show file tree
Hide file tree
Showing 16 changed files with 1,099 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: stloyd
32 changes: 32 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Tests
on: [push, pull_request]
jobs:
tests:
name: PHP ${{ matrix.php-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: true
matrix:
php-version:
- "8.1"

env:
php-extensions: xdebug, yaml

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: ${{ env.php-extensions }}

- uses: "ramsey/composer-install@v2"

- name: Running static analyse
run: php vendor/bin/phpstan analyse src/ tests/ --level max

- name: Running tests
run: php vendor/bin/phpunit --coverage-text
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/composer.phar
/composer.lock
/.php-cs-fixer.cache
/.phpunit.result.cache
/.idea/
/vendor/
42 changes: 42 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

/*
* This file is part of the PHP-LAFF package.
*
* (c) Joseph Bielawski <stloyd@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

$fileHeaderComment = <<<'EOF'
This file is part of the PHP-LAFF package.
(c) Joseph Bielawski <stloyd@gmail.com>
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
EOF;

$finder = (new PhpCsFixer\Finder())
->in(__DIR__.'/src')
->in(__DIR__.'/tests')
->append([__FILE__])
;

return (new PhpCsFixer\Config())
->setRules(
[
'@Symfony' => true,
'@Symfony:risky' => true,
'header_comment' => ['header' => $fileHeaderComment],
'protected_to_private' => false,
'native_constant_invocation' => ['strict' => false],
'modernize_strpos' => true,
]
)
->setRiskyAllowed(true)
->setFinder($finder)
;
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Joseph Bielawski

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
119 changes: 119 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# PHP-LAFF Analyzer
PHP Implementation of the Largest Area Fit First (LAFF) 3D (three dimensions: length, width, height) box packing algorithm.

With this library you can easily:
- get the required dimensions of the container that will fit all given packages,
- split packages per defined amount of containers,
- split packages per layer in a given container,
- get information about the wasted amount of space per container and per layer,
- get the number of remaining packages that couldn't fit into given containers,

## Algorithm definition

Implementation of the used algorithm was defined by M. Zahid Gürbüz, Selim Akyokus, Ibrahim Emiroglu, and Aysun Güran in a paper called ["An Efficient Algorithm for 3D Rectangular Box Packing"](http://www.zahidgurbuz.com/yayinlar/An%20Efficient%20Algorithm%20for%203D%20Rectangular%20Box%20Packing.pdf).

## Installation

> **Note**
> To use this library you need PHP in version 8.1+
```bash
composer require php-laff/analyzer
```

## Usage

### Get the size of the required container for selected packages
```php
<?php

use LAFF\Analyzer\Analyzer;
use LAFF\Analyzer\Model\Package;

$packages = [
new Package(50, 50, 8),
new Package(33, 8, 8),
new Package(16, 20, 8),
new Package(3, 18, 8),
new Package(14, 12, 8),
];

$analyzer = new Analyzer();
$analyzer->analyze($packages);

$containers = $analyzer->getContainers();
/** @var Container $container */
$container = reset($containers);

var_dump($container->toArray());
// Output:
// array(3) {
// ["length"]=>
// int(50)
// ["width"]=>
// int(50)
// ["height"]=>
// int(16)
// }
var_dump($container->countLayers());
// Output:
// int(2)
var_dump($analyzer->getWastePercentage());
// Output (%):
// int(32)
var_dump($analyzer->getWasteVolume());
// Output (cm3):
// int(13552)
```

### Check how many packages can be fitter into a given container
```php
<?php

use LAFF\Analyzer\Analyzer;
use LAFF\Analyzer\Model\Container;
use LAFF\Analyzer\Model\Package;

$packages = [
new Package(50, 50, 8),
new Package(33, 8, 8),
new Package(16, 20, 8),
new Package(3, 18, 8),
new Package(14, 12, 8),
];

$container = new Container(65, 60, 8);

$analyzer = new Analyzer();
$analyzer->analyze($packages, [$container]);

var_dump($container->full);
// Output:
// bool(true)
var_dump($container->countLayers());
// Output:
// int(1)
var_dump($container->getWastePercentage());
// Output (%)
//: int(15)
var_dump($container->getWasteVolume());
// Output (cm3):
// int(4752)
```

## Development
To install dependencies, launch the following commands:
```bash
composer install
```

## Run Tests
To execute full test suite, static analyse or coding style fixed, launch the following commands:
```bash
composer test
composer phpstan
composer cs-fixer
```

## Kudos
There is already a library for LAFF in PHP: [Cloudstek/php-laff](https://github.com/Cloudstek/php-laff); while both use the same algorithm, the internals is different. The main difference between those libraries is that this one can work on an array of containers. I want to say "thank you" for the work on that library in the past!
37 changes: 37 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "php-laff/analazer",
"description": "PHP Implementation of the Largest Area Fit First (LAFF) 3D (three dimension) box packing algorithm",
"license": "MIT",
"keywords": ["largest area fit first", "box packing algorithm", "packaging", "optimization problems", "three-dimensional packing", "combinatorial problem"],
"authors": [
{
"name": "Joseph Bielawski",
"email": "stloyd@gmail.com"
}
],
"autoload": {
"psr-4": {
"LAFF\\Analyzer\\": "src/LAFF/Analyzer/"
}
},
"autoload-dev": {
"psr-4": {
"LAFF\\Analyzer\\Tests\\": "tests/LAFF/Analyzer/"
}
},
"require": {
"php": ">=8.1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.8",
"phpunit/phpunit": "^9.5",
"phpstan/phpstan": "^1.8"
},
"minimum-stability": "stable",
"scripts": {
"cs-fixer": "@php vendor/bin/php-cs-fixer fix",
"test": "@php vendor/bin/phpunit",
"coverage": "@php vendor/bin/phpunit --coverage-text",
"phpstan": "@php vendor/bin/phpstan analyse src/ tests/ --level max"
}
}
20 changes: 20 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="LAFF Test Suite">
<directory>tests/LAFF/Analyzer/</directory>
</testsuite>
</testsuites>

<coverage>
<include>
<directory suffix=".php">src/LAFF/Analyzer</directory>
</include>
</coverage>
</phpunit>
Loading

0 comments on commit 323dab2

Please sign in to comment.