简体   繁体   English

关于从2D数组创建排名路径列表,我需要了解什么?

[英]What do I need to know about creating a list of ranked pathways from 2D array?

Suppose I have a matrix of equipment and their stats. 假设我有一个设备矩阵和它们的统计信息。 Hats, shirts, pants, boots. 帽子,衬衫,裤子,靴子。 Each inner-array can vary in size, but there's always going to be a set amount of the inner-arrays - in this case 4. 每个内部数组的大小都可以变化,但是总是会有一定数量的内部数组-在这种情况下为4。

var array = [
  [1,9,2,8,3],        // hat
  [2,8,3,6],          // shirt
  [1,3],              // pants
  [9,3,2,6,8,2,1,5,2] // boots
]

I want to find the optimal pathway through the matrix, then remove an item in such a way that the next route (and therefore sum of the route) determined is the next-best following the first. 我想找到通过矩阵的最佳路径,然后以使确定的下一条路线(因而是该路线的总和)是第一条路线中的第二条最佳路线的方式删除项目。 In this example, [9, 8, 3, 9] would be best, correct? 在此示例中, [9, 8, 3, 9]最好,对吗? So we can remove a 9 in [0] to reach 8 giving a drop of only 1. 因此,我们可以在[0]中删除9以达到8从而仅减少1。

I could sum all the possible routes and determine it from there but the size of the inner-arrays could be much bigger than shown. 我可以汇总所有可能的路线并从那里确定,但是内部数组的大小可能比所示的大得多。

I've spent some time thinking about it, and researched around. 我花了一些时间思考它,并进行了研究。 The only thing I can see is the Hungarian Algorithm but it stretches past my maths/compsci knowledge right now. 我唯一能看到的是匈牙利算法,但它现在已经超出了我的数学/综合知识。 Would that be the most applicable knowledge in this case? 在这种情况下,那将是最适用的知识吗? It seems to cater for the lowest possible 'cost' of a route but I need the opposite. 它似乎可以满足一条路线的最低“成本”,但我需要相反的选择。

My idea, at least as a thought: 我的想法,至少作为一个想法:

  1. Pull the highest number in each inner-array, create new array from those. 在每个内部数组中提取最高编号,从中创建新数组。 Rank this [0]. 对此排名[0]。
  2. Compare the highest number in each inner-array with the next lowest. 比较每个内部阵列中的最高编号与第二最低的编号。 Order the differences between each one. 订购每个之间的差异。
  3. Remove the highest number from the inner-array with the lowest difference found in #2. 从内部数组中删除编号为2的最低差的最高编号。
  4. Repeat #1 through #3. 重复#1至#3。

In the example above, I would expect something like below. 在上面的示例中,我期望以下内容。

[9, 8, 3, 9]
[8, 8, 3, 9]
[8, 8, 3, 8]
[8, 6, 3, 8]
[8, 6, 3, 6]
[8, 6, 3, 5]

EDIT: I think I butchered the explanation for this problem, let me fix it up a bit. 编辑:我想我已经解释了这个问题,让我修复一下。

EDIT2: Essentially the minimum-loss across sets, removing only one item from only one inner-array. EDIT2:从本质上讲,是最小损失的集合,仅从一个内部数组中除去一项。

UPDATE : I originally misinterpreted your question. 更新 :我最初误解了您的问题。 You subsequently clarified the question, and I provide here a completely new solution. 您随后澄清了这个问题,在此我提供了一个全新的解决方案。

The strategy I use is to flatten all the elements in all sub-arrays into a single array but do so in a way that remembers which original sub-array they come from. 我使用的策略是将所有子数组中的所有元素展平为单个数组,但是这样做要记住它们来自哪个原始子数组。 I then sort the flattened array, first by value in descending order, then by sub-array number in ascending order. 然后,我对展平的数组进行排序,首先按值降序排序,然后按子数组编号升序排序。 For example, the 1st 3 elements of the sorted flattened array would be [[9,0], [9,3], [8,0],...] representing the value 9 from sub-array 0, then value 9 from sub-array 3, then value 8 from sub-array 0, etc. I then proceed through the list, taking as many values as I need until I reach n, but leaving room for any sub-arrays for which I have not yet chosen a value. 例如,排序的展平数组的前3个元素将是[[9,0],[9,3],[8,0],...],表示子数组0中的值9,然后是值9从子数组3开始,然后从子数组0开始,取值为8,依此类推。然后,我遍历该列表,并根据需要获取尽可能多的值,直到达到n,但为尚未使用的任何子数组留出空间选择一个值。

Note that this solution works for any number of sub-arrays each with any number of elements. 请注意,此解决方案适用于任意数量的子数组,每个子数组具有任意数量的元素。

 const array = [ [1,9,2,8,3], // hat [2,8,3,6], // shirt [1,3], // pants [9,3,2,6,8,2,1,5,2] // boots ]; for (let n = 0; n < 17; n += 1) { const valuesChosen = getTopNBest(array, n); console.log(`n = ${n}: ${JSON.stringify(valuesChosen)}`); } function getTopNBest(array, n) { const numTypes = array.length; const allElmtsRanked = []; array.forEach((sub, typeNum) => { sub.forEach(elmt => {allElmtsRanked.push([elmt, typeNum]);}); }); allElmtsRanked.sort((a,b) => b[0] - a[0] !== 0 ? b[0] - a[0] : a[1] - b[1]); const valuesChosen = array.map(() => null); let totalNumValuesExamined = 0; let numSecondaryValuesChosen = 0; let numUnrepresentedTypes = numTypes; let currPair, currValue, currTypeNum; while (numUnrepresentedTypes !== 0 || numSecondaryValuesChosen < n) { currPair = allElmtsRanked[totalNumValuesExamined]; currValue = currPair[0]; currTypeNum = currPair[1]; totalNumValuesExamined += 1; if (valuesChosen[currTypeNum] === null) { valuesChosen[currTypeNum] = currValue; numUnrepresentedTypes -= 1; } else if (numSecondaryValuesChosen < n) { numSecondaryValuesChosen += 1; valuesChosen[currTypeNum] = currValue; } } return valuesChosen; } 

You subsequently asked why I sorted first by value then by sub-array number. 随后,您问为什么我先按值然后按子数组号排序。 Compare the following two scenarios: 比较以下两种情况:

var array = [
  [8],
  [9,9,9,9,9],
  [8],
  [8]
]

...versus... ...与...

var array = [
  [9,8],
  [9,9],
  [9,8],
  [9,8]
]

If you simply flattened these and sorted the flattened array without retaining any information about which sub-array the values came from, you'd end up with the same sorted flattened array in both cases, ie 如果仅对这些扁平化的数组进行扁平化,而又不保留有关值来自哪个子数组的任何信息,则在两种情况下,最终都会得到相同的扁平化数组,即

var sortedFlattenedArray = [9,9,9,9,9,8,8,8]

Let's say you want the best/most optimal pathway. 假设您想要最好/最理想的途径。 You'd now get [9,9,9,9] with either scenario whereas with the first scenario you really want [8,9,8,8] . 现在,无论哪种情况,您都将得到[9,9,9,9] ,而第一种情况下,您确实需要[8,9,8,8] You can only get this when you've remembered the sub-array/type number, eg: 仅在记住子数组/类型编号后才能获取此信息,例如:

var sortedFlattenedArray = [[9,1],[9,1],[9,1],[9,1],[9,1],[8,0],[8,2],[8,3]]

The actual strategy thus allows you to ignore reasonably-high-but-sub-optimal values from already sampled types when you no longer want any more sub-optimal values for any type but you're still looking for the highest possible value for a particular type. 因此,当您不再需要任何类型的更多次优值,但仍在寻找特定值的最大可能值时,实际策略可以让您忽略已采样类型的合理高但次优值类型。

Here's another way of looking at it. 这是另一种查看方式。 The strategy here allows you to flatten and sort all the original arrays but remember where each element came from . 这里的策略使您可以对所有原始数组进行展平和排序, 但要记住每个元素的来源 This in turn allows you to be selective when choosing a pathway by saying, "Ah, the next value is pretty high, which is good, but wait, it's for a hat (which you could only know by being able to read it's associated sub-array/type number) and I've already retrieved my quota of 'sub-optimal values', so I'll ignore this hat value. However, I'll keep moving through the sorted flattened array to find highest remaining value for a shirt (which, again, you could only discover by being able to read its associated sub-array/type number) for which I still haven't yet found any value." 反过来,这可以让您在选择路径时保持选择性,说:“啊,下一个值很高,这很好,但是请耐心等待,因为它是一顶帽子(您只能通过读取它的关联子项来知道-阵列/型号),我已经取回我的“次优值”配额,所以我会忽略这个帽子值,但是,我会继续通过分拣扁平阵列迁移到找到一个最高的剩余价值衬衫(同样,您只能通过读取其关联的子数组/类型编号来发现它),但我仍没有找到任何价值。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据对对象的了解,从数组中查找和删除对象? - How do I find and remove an object from an array based on what I know about the contents of that object? 如何在JavaScript中交换2D数组中的两个元素? (对我在Chrome中的console.log中看到的内容感到困惑) - How to swap two elements inside of a 2D Array in JavaScript? (Confused about what I’m seeing from console.log in Chrome) 如果不知道索引,如何遍历2d数组 - How do I loop through 2d array if I do not know the indexes 我正在用 Javascript 创建一个二维数组。 如何跳过创建它的一部分? - I'm creating a 2D array in Javascript. How do I skip creating a part of it? 如何将2D数组从JavaScript传递到PHP - How do I pass a 2D array from JavaScript into PHP 在 JavaScript 中从一维数组创建二维数组 - Creating a 2D array from a 1D array in JavaScript 刚刚看到了用JavaScript创建的工厂样式对象(没有文字)。 我还需要知道什么? - Just seen a factory style object creating in JavaScript (without literals). What else do I need to know? 使用 javascript 在 2D 数组周围创建填充的最佳方法是什么 - what is best way of creating padding around 2D array with javascript 从常规数组创建新的2D数组 - Creating a new 2D array from regular array 从其他二维数组 Javascript 的部分创建二维数组 - Creating 2d array from section of other 2d array Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM