使用 PHP ML - 机器学习库,根据单个输入值生成解决方案

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

问题:

我有一扇2000mm高的门。

我有 2 种类型的面板来建造门:

615mm 标准面板和 495mm 标准面板。

对于上述高度,我的最佳解决方案必须是:

1 x 615mm 面板标准

2 x 495mm 面板标准

1 x 495mm 面板,我从中切割 100mm 以达到 2000mm 的高度。 这是从 495 而不是 615mm 切割的最佳解决方案,因为它会损失太多 material.

示例: 1845mm 高度 -

最优解是:

3 x 615mm 面板(3x 615mm = 1845mm)。

另一个例子:

3000mm高度-

最优解:

4 x 615mm 面板

1 x 540mm 面板(默认 615mm 从中切割 75mm 以填充 3000mm 高度)

我的问题是,我可以使用 PHP-ML 库中的任何算法来训练和预测给定输入(高度,在我的例子中)的解决方案。如果答案是肯定的,哪种算法最适合我的情况?

分类

SVC 或 k-最近邻或 朴素贝叶斯

请看我附上的图片。你会明白我想说的。

我想使用那个库,这样它就可以 return 为给定的高度提供几个解决方案,以及一个最佳解决方案。

您的具体任务很容易被暴力破解,在线查看:https://3v4l.org/dQmdb

代码如下:

<?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}

我之前的回答不正确,但我把它作为我们喜欢使事情过于复杂的例子。


旧答案

这是一个经典的1D Cutting stock problem,可以表述为整数线性规划问题。

你应该知道这是 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.

考虑到给定的信息,您必须自己实现一个算法:https://neos-guide.org/content/cutting-stock-problem

和视频:https://www.youtube.com/watch?v=NoiPrt4OsQA


如果您非常想利用机器学习,请检查遗传算法:https://github.com/ffsantos92/2d-cutting-stock-problem