-1

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

neubert
  • 15,947
  • 24
  • 120
  • 212
Bogdan C
  • 389
  • 1
  • 4
  • 17

1 Answers1

3

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

terales
  • 3,116
  • 23
  • 33
  • 1
    Very nice answer. I know you worked a lot to provide me this answer. Thank you very much. I will see if I can somehow transform into a php algorithm from cutting stock or adapt that genetic algorithm (which would be insane if I would succeed). – Bogdan C Jan 25 '18 at 21:08
  • And if you succeed either way try to open source it to help others and receive feedback from community – terales Jan 25 '18 at 21:14
  • So far you are the only who understood what I want. You pointed me in the best direction so far, better than anyone else. – Bogdan C Jan 25 '18 at 21:19
  • Added simpler solution – terales Jan 25 '18 at 23:49
  • This can be modified to return more than one solution per given height? Example, for 2000, your solution returns 4 x 495mm. Other solution is 3 x 495 + 1 x 615mm. I can`t have a solution where the sum of panel heights is less than my height, because it would remain an empty space. – Bogdan C Jan 26 '18 at 09:10
  • It won't return solution with sum of panel heights less then door height. Did you try it? – terales Jan 26 '18 at 09:29
  • Yes. 495 x 4 = 1980. Height is 2000. There is a gap of 20mm. – Bogdan C Jan 26 '18 at 09:29
  • I must ignore the solutions where `$waste > 0`; I dont get it why it returns me solutions with panels sum under the height. – Bogdan C Jan 26 '18 at 11:33
  • Ah, missed the sign. Good catch! – terales Jan 26 '18 at 14:51
  • Fixed. See an updated answer – terales Jan 26 '18 at 15:59
  • Thank you. One minor observation. Instead of returning me 0 panels, it should recalculate and offer other solution. This code returns 0 panels used on height 2000. But there can be used 1 x 615 and 3 x 495 (from which I cut 100mm to reach 2000). – Bogdan C Jan 26 '18 at 16:03
  • Checked: https://3v4l.org/dQmdb It returns exactly 1 x 615 and 3 x 495. Am I missing something? – terales Jan 26 '18 at 16:05
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/163963/discussion-between-bogdan-c-and-terales). – Bogdan C Jan 26 '18 at 16:09