samdark/sack

维护者

详细信息

github.com/samdark/sack

源代码

问题

资助包维护!
Patreon

1.0.0 2022-09-06 12:49 UTC

This package is auto-updated.

Last update: 2024-09-09 19:39:11 UTC


README

Latest Stable Version Total Downloads Build status Scrutinizer Code Quality Code Coverage Mutation testing badge static analysis type-coverage

此包实现了“0-1背包问题”算法,即允许找到以最佳方式填充指定体积的背包,背包中有一定体积和价值的物品。

它可以应用于

  • 用最有价值的物品填充盒子。
  • 已知每个任务的价值和所需天数,选择一周内的最佳任务。
  • 已知一个人想要参观的景点数量和参观所需时间,选择在有限时间内要参观的景点。
  • 等。

安装

可以使用composer安装此包

composer require samdark/sack --prefer-dist

通用使用

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";
}

测试

单元测试

该包使用 PHPUnit 进行测试。要运行测试

./vendor/bin/phpunit

变异测试

该包的测试使用 Infection 变异框架和 Infection 静态分析插件 进行检查。要运行它

./vendor/bin/roave-infection-static-analysis-plugin

静态分析

代码使用 Psalm 进行静态分析。要运行静态分析

./vendor/bin/psalm