Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9079369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:49:12+00:00 2026-06-16T19:49:12+00:00

I need to implement the Cutting Stock Problem with a php script. As my

  • 0

I need to implement the Cutting Stock Problem with a php script.
As my math skills are not that great I am just trying to brute force it.

Starting with these parameters

  • $inventory is an array of lengths that are available to be cut.
  • $requestedPieces is an array of lengths that were requested by the
    customer.
  • $solution is an empty array

I have currently worked out this recursive function to come up with all possible solutions:

function branch($inventory, $requestedPieces, $solution){
    // Loop through the requested pieces and find all inventory that can fulfill them
    foreach($requestedPieces as $requestKey => $requestedPiece){
        foreach($inventory as $inventoryKey => $piece){
            if($requestedPiece <= $piece){
                $solution2 = $solution;
                array_push($solution2, array($requestKey, $inventoryKey));
                $requestedPieces2 = $requestedPieces;
                unset($requestedPieces2[$requestKey]);
                $inventory2 = $inventory;
                $inventory2[$inventoryKey] = $piece - $requestedPiece;
                if(count($requestedPieces2) > 0){
                    branch($inventory2, $requestedPieces2, $solution2);
                }else{
                    global $solutions;
                    array_push($solutions, $solution2);
                }
            }
        }
    }
}

The biggest inefficiency I have discovered with this is that it will find the same solution multiple times but with the steps in a different order.

For example:

  • $inventory = array(1.83, 20.66);
  • $requestedPieces = array(0.5, 0.25);

The function will come up with 8 solutions where it should come up with 4 solutions.
What is a good way to resolve this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:49:14+00:00Added an answer on June 16, 2026 at 7:49 pm

    This does not answer your question, but I thought it could be worth being mentioned:

    You have several other ways to solve your problem, rather than brute forcing it. The wikipedia page on the topic is pretty thorough, but I’ll just describe two others simpler ideas. I will use the wikipedia terminology for certain words, namely master for inventory piece, and cut for a requested piece. I will use set to denote a set of cuts pertaining to a given master.

    The first one is based on the greedy algorithm, and consist in filling a set with the largest available cut, until no more cut may fit, and repeat that same process for each master, yielding a set for each one of them.

    The second one is more dynamic: it uses recursion (like yours), and look for the best fit for the remaining length of master and cuts at each step of the recursion, the goal being to minimize the wasted length when no more cuts can fit.

    function branch($master, $cuts, $set){
         $goods = array_filter($cuts, function($v) use ($master) { return $v <= $master;});
         $res = array($master,$set,$cuts);
         if (empty($goods))
             return $res;
         $remaining = array_diff($cuts, $goods);
         foreach($goods as $k => $g){
             $t = $set;
             array_push($t, $g);
             $r = $remaining;
             $c = $goods;
             for ($i = 0; $i < $k; $i++)
                 array_push($r,array_shift($c));
             array_shift($c);
             $t = branch($master - $g, $c, $t);
             array_walk($r, function($k,$v) use ($t) {array_push($t[2], $v);});
             if ($t[0] == 0) return $t;
             if ($t[0] < $res[0])
                 $res = $t;
         }
         return $res;
    }
    

    The function above should give you the optimal set for a given master. It returns an array of 3 values:

    • the wasted length on master
    • the set
    • the remaining cuts

    The parameters are

    • the master length,
    • the cuts to be performed (must be sorted in descending order),
    • the set of cuts already scheduled (a preexisting set, which would be empty for the first call for each master)

    Caveats: It depends on the masters’ order, you could certainly write a function which tries all the relevant possibilities to find the best order of masters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to implement an SVN pre-commit hook which executes a script that itself
I need to implement portable code, but I do not know how to deal
i need to implement a messaging scenario that consumes messages from throusands of destinations
I need to implement a method that concatenates different characters into a char* without
I need to implement a tree of decisions, very simple, not complicated in objective
I need to implement a recursive function that returns 1 if the number is
I need to implement a simple macro that finds the modulo of two numbers
When we use NSURLConnection, we just need implement the delegate API - (void) connection:(NSURLConnection
Need to implement an app that has a feature to play sounds. Each sound
I need to implement a key-value data structure that search for a unique key

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.