/php-CuttingStockProblem

A PHP function for solving the Cutting Stock Problem.

Primary LanguagePHPGNU General Public License v3.0GPL-3.0

php-CuttingStockProblem

A PHP function for solving the Cutting Stock Problem. GPL.
For more information, see Wikipedia

How to use:

  • Require library: require_once "src/cuttingStock.php";
  • Call the function cuttingStock(); - see below for parameters.

Parameters to pass:

    1. $inputPieces - an array of sizes of pieces available to cut from.
    1. $outputSizes - an array of sizes of pieces to cut.
    1. $margin - a buffer to add to each cut, default 0.
    1. $showWorking - true/false - whether script will show working out, default false.

When passing $inputPieces the array index MUST start with 1 (i.e. the first piece!). If your array index starts with 0, an error will be generated.

Output:

A multidimentional array where $array['piecesToCut'] is an associative array containing a list of cuts to make and from which element sizeToCut is the amount to cut from element inputPiece; and where $array['leftovers'] is an indexed array (starting at 1) containing the amount left over on each $inputPiece.

Example:

  • Upload everything to a web server and run index.php.
  • Live demo is available at my website

Algorithm:

It uses a greedy brute-force type algorithm to make decisions; I'm sure there are more efficient ways!

Questions/Comments:

Send me an e-mail at j@jlls.info