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.

Overview

Sack

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

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:

composer require samdark/sack --prefer-dist

General usage

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"; }">
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. To run tests:

./vendor/bin/phpunit

Mutation testing

The package tests are checked with Infection mutation framework with Infection Static Analysis Plugin. To run it:

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

Static analysis

The code is statically analyzed with Psalm. To run static analysis:

./vendor/bin/psalm
You might also like...
Ip2region is a offline IP location library with accuracy rate of 99.9% and 0.0x millseconds searching performance. DB file is ONLY a few megabytes with all IP address stored. binding for Java,PHP,C,Python,Nodejs,Golang,C#,lua. Binary,B-tree,Memory searching algorithm

Ip2region是什么? ip2region - 准确率99.9%的离线IP地址定位库,0.0x毫秒级查询,ip2region.db数据库只有数MB,提供了java,php,c,python,nodejs,golang,c#等查询绑定和Binary,B树,内存三种查询算法。 Ip2region特性

VoteSwiper helps citizens to find a political party that matches their own views in a playful way.

VoteSwiper / WahlSwiper - Website VoteSwiper (in Germany better known as WahlSwiper) is a cross-platform voting advice app for Android, iOS and web br

Implementation of the Token Bucket algorithm in PHP.

Token Bucket This is a threadsafe implementation of the Token Bucket algorithm in PHP. You can use a token bucket to limit an usage rate for a resourc

XXTEA encryption algorithm library for PHP.

XXTEA for PHP Introduction XXTEA is a fast and secure encryption algorithm. This is a XXTEA library for PHP. It is different from the original XXTEA e

PHP implementation of Rapid Automatic Keyword Exraction algorithm (RAKE) for extracting multi-word phrases from text

PHP implementation of Rapid Automatic Keyword Exraction algorithm (RAKE) for extracting multi-word phrases from text.

A PocketMine-MP plugin to regulate player hunger and prohibit eating certain foods easily

NoEats A PocketMine-MP plugin to regulate player hunger and prohibit eating certain foods easily. Features Managing hunger. Permission bypass. Support

This car rental project system project in PHP focuses mainly on dealing with customers regarding their car rental hours and certain transactions.
This car rental project system project in PHP focuses mainly on dealing with customers regarding their car rental hours and certain transactions.

Car-Rental Online Car Rental Management System This car rental project system project in PHP focuses mainly on dealing with customers regarding their

[virion] It Implements Simple Using Form Library System

SimpleForm [virion] It Implements Simple Using Form Library System How To Use First, declare the use statement. use AidenKR\SimpleForm\SimpleForm; use

Releases(1.0.0)
Owner
Alexander Makarov
Engineer having fun, lead of Yii framework. PGP: 6A325E4A
Alexander Makarov
StickWithIt is an online food ordering website created using PHP. You can view and purchase various items as well as remove items from the cart.

StickWithIt (App Name) StickWithIt is an online food ordering website created using PHP. The database used here is MYSQL database. The tool used here

Jenil Gajjar 1 May 11, 2022
This is a plugin for pocketmine-mp, when locking a player's items helps players not to lose items or throw things around causing server lag.

[] LockedItem| v1.0.0 Player's item lock Features Player's item lock Players aren't afraid of losing items For Devolopers You can access to LockedItem

JeroGamingYT 3 Jan 4, 2022
A web app for the resolution of a mobile game in wich you have 4 images and a list of letters, then a few boxes to fill with the word connecting the four images.

4images_1mot_solutions A web app for the resolution of a mobile game in wich you have 4 images and a list of letters, then a few boxes to fill with th

FOTSO Claude 3 Jan 13, 2022
Share value objects that contain the same primitive value as a singleton

sharable-value-objects Share value objects that contain the same primitive value as a singleton. Singletons are kept under WeakReference objects. Inst

mpyw 5 Nov 14, 2021
Gamecom.io - Find the best game, community, mod, or servers for you! Data retrieved from @modcommunity.

A website made by Christian Deacon that helps users find any game, community, mod, or server. All data retrieved by @modcommunity. This website is ope

Christian Deacon 10 Nov 2, 2022
Do you want CronJob to follow the solar date?You need this package to solve this problem.

Shamsic Maybe it happened to you that you wanted to use CronJob in your project and you realized that you cannot manage the exact dates that are in th

Amin Ghaninia 5 Jul 19, 2022
Laravel package to generate sweepstakes using the Round Robin algorithm

Laravel package to generate sweepstakes using the Round Robin algorithm. Supports any number of teams, as long as they are greater than a minimum value specified in the configuration file. Built with Laravel Collections for better handling of arrays.

TonyStore 6 Aug 3, 2022
A simple implementation of the api-problem specification. Includes PSR-15 support.

ApiProblem This library provides a simple and straightforward implementation of the IETF Problem Details for HTTP APIs, RFC 7807. RFC 7807 is a simple

Larry Garfield 236 Dec 21, 2022
⭕️ A Knight's Tour problem solver written in php

Knight's tour problem solver A knight's tour problem solver using "Warnsdorff's Rule" - https://vrgl.ir/IB9LJ ?? About the Project ?? Screenshots ?? B

Mahdiyar Ghannad 8 Nov 6, 2022
This project is based on the problem statement provided by the Minstry of HRD (India) for Smart India Hackathon '17.

This project is based on the problem statement provided by the Minstry of HRD (India) for Smart India Hackathon '17. As per the given problem statement, we need to solve the problem of bunching of marks at certain level, and problem of high scorers being at disadvantageous position due to lower competitive percentile.

Saransh Dave 4 Oct 13, 2022