Search code examples
phpmachine-learningartificial-intelligencecombinationsphp-ml

Using PHP ML - Machine Learning library, to generate solutions based on a single input value


Problem:

I have a door of 2000mm height.

I have 2 types of panels to build the door:

615mm standard panels and 495mm standard panels.

For the above height, my optimal solution would have to be:

1 x 615mm panel standard

2 x 495mm panel standard

1 x 495mm panel from which I cut 100mm to reach the 2000mm height. Here is the best solution to cut from 495 instead of 615mm because it would be a lost of too much material.

Example: 1845mm height -

Optimal solution is:

3 x 615mm panels ( 3x 615mm = 1845mm).

Another example:

3000mm height -

Optimal solution:

4 x 615mm panels

1 x 540mm panel (default 615mm from which is cut 75mm to fill the 3000mm height)

My question is, can I use any algorythm from PHP-ML library to train and predict solutions for input given (height, in my case). If the answer is yes, which algorithm is best suitable for my case?

Classification

SVC or k-Nearest Neighbors or Naive Bayes

Please see the pic i attached. You will understand what I want to say.

I want to use that Library so it can return me several solutions for given height, and an optimal one.

enter image description here


Solution

  • Your specific task is could be easily brute forced, check it online: https://3v4l.org/dQmdb

    Here is a code:

    <?php
    
    // Examples:
    // Door           2000    1845    3000
    // 615mm panel    1       3       5
    // 495mm panel    3       0       0
    // panel loss     100     0       75
    
    function calcOptimalPanels ($doorHeight) {
      $bigHeight = 615;
      $smallHeight = 495;
    
      $bigFit = floor($doorHeight / $bigHeight);
      $smallFit = floor($doorHeight / $smallHeight);
    
      $options = [];
    
      for ($big = 0; $big <= $bigFit; $big++) {
        for ($small = 0; $small <= $smallFit; $small++) {
          $waste = $bigHeight * $big + $smallHeight * $small - $doorHeight;
    
          if ($waste === 0) // Get first combination without waste
            return getFormattedResult($big, $small, $waste);
    
          if ($waste > 0)  // Omit combinations smaller then door
            continue;
    
          $options[$waste] = getFormattedResult($big, $small, $waste);
        }
      }
    
      $minWaste = min(array_keys($options));
    
      return $options[$minWaste];
    }
    
    function getFormattedResult($big, $small, $waste) {
      return ['615mm' => $big, '495mm' => $small, 'waste' => $waste];
    }
    
    echo '2000: ' . json_encode(calcOptimalPanels(2000)) . "\n";
    echo '1845: ' . json_encode(calcOptimalPanels(1845)) . "\n";
    echo '2340: ' . json_encode(calcOptimalPanels(1845 + 495)) . "\n";
    echo '3000: ' . json_encode(calcOptimalPanels(3000)) . "\n";
    
    // Result:
    // 2000: {"615mm":1,"495mm":3,"waste":100}
    // 1845: {"615mm":3,"495mm":0,"waste":0}
    // 2340: {"615mm":3,"495mm":1,"waste":0}
    // 3000: {"615mm":1,"495mm":5,"waste":90}
    

    My previous answer is not correct but I leave it as an example of our love to overcomplicate things.


    Old answer

    This is a classic 1D Cutting stock problem which can be formulated as an integer linear programming problem.

    You should be aware that this is an NP-complete problem:

    This basically means that their is no way of being guaranteed the best solution without checking every possible solution. This is not to say that a solution reached by one of the following algorithms is not optimal, it may be.

    With given info in mind, you have to implement an algorithm yourself: https://neos-guide.org/content/cutting-stock-problem

    and video: https://www.youtube.com/watch?v=NoiPrt4OsQA


    If you desperately want to leverage machine learning then check genetic algorithm: https://github.com/ffsantos92/2d-cutting-stock-problem