https://github.com/samdark/sack
This package implements "0-1 Knapsack Problem" algorithm i.e. allows to find the best way to fill a knapsack of a specified volume with items of a certain volume and value.
https://github.com/samdark/sack
knapsack-problem
Last synced: 11 months ago
JSON representation
This package implements "0-1 Knapsack Problem" algorithm i.e. allows to find the best way to fill a knapsack of a specified volume with items of a certain volume and value.
- Host: GitHub
- URL: https://github.com/samdark/sack
- Owner: samdark
- License: mit
- Created: 2022-09-06T11:38:27.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2025-02-21T15:19:19.000Z (about 1 year ago)
- Last Synced: 2025-03-27T12:38:11.379Z (11 months ago)
- Topics: knapsack-problem
- Language: PHP
- Homepage:
- Size: 20.5 KB
- Stars: 9
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
Sack
[](https://packagist.org/packages/samdark/sack)
[](https://packagist.org/packages/samdark/sack)
[](https://github.com/samdark/sack/actions?query=workflow%3Abuild)
[](https://scrutinizer-ci.com/g/samdark/sack/?branch=master)
[](https://scrutinizer-ci.com/g/samdark/sack/?branch=master)
[](https://dashboard.stryker-mutator.io/reports/github.com/samdark/sack/master)
[](https://github.com/samdark/sack/actions?query=workflow%3A%22static+analysis%22)
[](https://shepherd.dev/github/samdark/sack)
This package implements "0-1 Knapsack Problem" algorithm i.e. allows to find the best way to fill
a knapsack of a specified volume with items of a certain volume and value.
It could be applied to:
- Filling a box with most valued items.
- Selecting best tasks for a week knowing each task value and effort in days.
- Selecting attractions to visit in a limited time knowing how much one wants to visit an attraction and time
required for a visit.
- etc.
## Installation
The package could be installed with composer:
```shell
composer require samdark/sack --prefer-dist
```
## General usage
```php
declare(strict_types=1);
use Samdark\Sack\Item;
use Samdark\Sack\SackFiller;
require __DIR__ . '/vendor/autoload.php';
// Items to select from
$items = [
new Item('Guitar', 1, 1500),
new Item('Player', 4, 3000),
new Item('Laptop', 3, 2000),
];
$sackVolume = 7;
$filler = new SackFiller($sackVolume);
$result = $filler->fill($items);
echo "Possible items:\n\n";
echo "Name\tVolume\tValue\n";
foreach ($items as $item) {
echo "{$item->getName()}\t{$item->getVolume()}\t{$item->getValue()}\n";
}
echo "\n\nMaximum value for sack of $sackVolume is {$result->getValue()}:\n\n";
echo "Name\tVolume\tValue\n";
foreach ($result->getItems() as $item) {
echo "{$item->getName()}\t{$item->getVolume()}\t{$item->getValue()}\n";
}
```
## Testing
### Unit testing
The package is tested with [PHPUnit](https://phpunit.de/). To run tests:
```shell
./vendor/bin/phpunit
```
### Mutation testing
The package tests are checked with [Infection](https://infection.github.io/) mutation framework with
[Infection Static Analysis Plugin](https://github.com/Roave/infection-static-analysis-plugin). To run it:
```shell
./vendor/bin/roave-infection-static-analysis-plugin
```
### Static analysis
The code is statically analyzed with [Psalm](https://psalm.dev/). To run static analysis:
```shell
./vendor/bin/psalm
```