gpt4 book ai didi

javascript - 背包问题还是排列问题?如何解决我的问题?

转载 作者:行者123 更新时间:2023-11-28 03:03:14 25 4
gpt4 key购买 nike

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

我有一个大小为 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);
});

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

感谢您的帮助

最佳答案

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

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

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

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

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

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

关于javascript - 背包问题还是排列问题?如何解决我的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60809470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com