【问题标题】:Knapsack problem or permutation? How to solve my problem?背包问题还是排列?如何解决我的问题?
【发布时间】:2020-07-03 16:11:45
【问题描述】:

您好,希望有人可以帮助我或至少给我正确方向的看法。

我有一个包含 100 到 500 个项目的数组。每个项目都有属性 name(string)、weight(float)、points(float)、position(int)。最大权重不是一个固定的数字,位置是(1,2,3,4,5)每个位置都需要填写。每个物品只能使用一次。结果应该是 7 个项目的最高分的 15 种组合。

据我所知,我的笔记本电脑因内存已满而崩溃。

我在 typescript 中尝试了一个使用 firebase 的解决方案。

import * as functions from "firebase-functions";

// // Start writing Firebase Functions
// // https://firebase.google.com/docs/functions/typescript
//
export const potItems = functions.https.onRequest((request, response) => {
    let possibleItems: any[] = [];
    let counter: number = 0;
    const items = [
        { name: "James Harden", position: "PG", weight: 15.9, points: 62.63 },
        { name: "Russell Westbrook", position: "PG", weight: 14.9, points: 56.12 },
        { name: "LeBron James", position: "SF", weight: 15.9, points: 55.67 },
        { name: "Bradley Beal", position: "SG", weight: 14.8, points: 52.69 },
        { name: "Anthony Davis", position: "PF", weight: 14.6, points: 52.16 },
        { name: "Damian Lillard", position: "PG", weight: 13.5, points: 49.34 },
        { name: "Nikola Vucevic", position: "C", weight: 12.9, points: 47.97 },
        { name: "Domantas Sabonis", position: "PF", weight: 12.8, points: 47.6 },
        { name: "Kristaps Porzingis", position: "PF", weight: 13.5, points: 47.18 },
        { name: "Andre Drummond", position: "C", weight: 12.2, points: 45.97 },
        { name: "Kawhi Leonard", position: "SF", weight: 13.2, points: 46.08 },
        { name: "Hassan Whiteside", position: "C", weight: 12.2, points: 45.83 },
        { name: "DeAndre Ayton", position: "C", weight: 11.8, points: 45.57 },
        { name: "Paul George", position: "SF", weight: 11.1, points: 43.8 },
        { name: "D'Angelo Russell", position: "PG", weight: 11.5, points: 44.0 },
        { name: "Julius Randle", position: "PF", weight: 11.3, points: 42.46 },
        { name: "DeMar DeRozan", position: "SG", weight: 11.2, points: 40.6 },
        { name: "Coby White", position: "PG", weight: 10.5, points: 37.85 },
        { name: "Ricky Rubio", position: "PG", weight: 11.1, points: 37.83 },
        { name: "Robert Covington", position: "SF", weight: 9.1, points: 37.26 },
        { name: "James Harden", position: "PG", weight: 15.9, points: 62.63 },
        { name: "Russell Westbrook", position: "PG", weight: 14.9, points: 56.12 },
        { name: "LeBron James", position: "SF", weight: 15.9, points: 55.67 },
        { name: "Bradley Beal", position: "SG", weight: 14.8, points: 52.69 },
        { name: "Anthony Davis", position: "PF", weight: 14.6, points: 52.16 },
        { name: "Damian Lillard", position: "PG", weight: 13.5, points: 49.34 },
        { name: "Nikola Vucevic", position: "C", weight: 12.9, points: 47.97 },
        { name: "Domantas Sabonis", position: "PF", weight: 12.8, points: 47.6 },
        { name: "Kristaps Porzingis", position: "PF", weight: 13.5, points: 47.18 },
        { name: "Andre Drummond", position: "C", weight: 12.2, points: 45.97 },
        { name: "Kawhi Leonard", position: "SF", weight: 13.2, points: 46.08 },
        { name: "Hassan Whiteside", position: "C", weight: 12.2, points: 45.83 },
        { name: "DeAndre Ayton", position: "C", weight: 11.8, points: 45.57 },
        { name: "Paul George", position: "SF", weight: 11.1, points: 43.8 },
        { name: "D'Angelo Russell", position: "PG", weight: 11.5, points: 44.0 },
        { name: "Julius Randle", position: "PF", weight: 11.3, points: 42.46 },
        { name: "DeMar DeRozan", position: "SG", weight: 11.2, points: 40.6 },
        { name: "Coby White", position: "PG", weight: 10.5, points: 37.85 },
        { name: "Ricky Rubio", position: "PG", weight: 11.1, points: 37.83 },
        { name: "Robert Covington", position: "SF", weight: 9.1, points: 37.26 }
    ];

        for (let index0 = 0; index0 < items.length; index0++) {
          for (let index1 = 0; index1 < items.length; index1++) {
            if (index1 == index0) {
              break;
            }
            for (let index2 = 0; index2 < items.length; index2++) {
              if (index2 == index0 || index2 == index1) {
                break;
              }
              for (let index3 = 0; index3 < items.length; index3++) {
                if (index3 == index0 || index3 == index1 || index3 == index2) {
                  break;
                }
                for (let index4 = 0; index4 < items.length; index4++) {
                  if (
                    index4 == index0 ||
                    index4 == index1 ||
                    index4 == index2 ||
                    index4 == index3
                  ) {
                    break;
                  }
                  for (let index5 = 0; index5 < items.length; index5++) {
                    if (
                      index5 == index0 ||
                      index5 == index1 ||
                      index5 == index2 ||
                      index5 == index3 ||
                      index5 == index4
                    ) {
                      break;
                    }
                    for (let index6 = 0; index6 < items.length; index6++) {
                      if (
                        index6 == index0 ||
                        index6 == index1 ||
                        index6 == index2 ||
                        index6 == index3 ||
                        index6 == index4 ||
                        index6 == index5
                      ) {
                        break;
                      }
                      let item: number[] = [];
                        item.push(index0);
                        item.push(index1);
                        item.push(index2);
                        item.push(index3);
                        item.push(index4);
                        item.push(index5);
                        item.push(index6);
                        item.sort((a, b) => a - b);
                        counter++;
                        possibleItems.push(counter);
                        console.log("item " + counter + ": " + item);

                    }
                  }
                }
              }
            }
          }
        }
        response.send("All items: " + possibleItems);
});

这里的项目只是它们的外观示例

感谢您的帮助

【问题讨论】:

  • 您没有充分描述您要解决的问题。 The outcome should be the 15 combinations with the max value of 7 items. - 15 种组合是什么?什么是“最大值”?
  • 如果算法达不到标准,作为 firebase 函数运行对您没有多大帮助;仍然会有内存和时间限制。
  • 好吧,我将值更改为积分。所以我需要7个项目的15个最佳点组合

标签: javascript typescript firebase


【解决方案1】:

那么,你有 7 个职位要填补以使积分总和最大化?

按位置对项目进行分组,按点对每个组进行排序。

现在您可以在每组中选择 top 位置的组合,以获得最高分。

由于您有数百个项目并且只需要 15 个顶级组合,因此您可能永远不会在排序后查看大多数项目,而只会处理每个组中的前几个。

我不知道您选择这 15 种组合的衡量标准是什么。如果你只是最大化他们的总结点,你可以选择任何你想要的。如果您想使组合更加均匀(每个项目的组合中的点分散较小,或者组合之间的汇总点分散较小),您可能需要添加更多限制。

我认为像linear programming 这样的东西在这种情况下可能会有所帮助,因为您正在优化线性目标函数(总和)。

【讨论】:

  • 我的问题是我也只能使用特定数量的重量。因此,假设我们选择了 6 个项目,我可以更好地删除第 6 个项目并取另外两个,因为它们结合了更好的积分结果。这就是为什么我不能对它们进行排序并占据最佳位置的问题。
猜你喜欢
  • 2012-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多