将对象数组拆分为多个“桶”。

huangapple go评论131阅读模式
英文:

Splitting an array of objects into multiple "buckets."

问题

我觉得这就像计算机科学101,但我从未上过正式的编程课程。我只是一个通过必要的经验自学成才的管理员。我更喜欢尽量从零开始自己解决问题,但这个问题对我来说有点困难,因为我的智商一般,我会很感激一些帮助。我的脑袋里有点像个椒盐脆饼。

最近我一直在思考如何有效地将一组具有不同大小数值的对象排列成尽量少的固定大小的“桶”。比如,如果我有1,000个值从1到100的实例,我想弄清楚如何将它们排列成尽量少的桶,假设每个桶中的值的总和(而不是对象的数量)不能超过100、200或其他大小。我不仅想知道需要多少个桶,这就像1年级或2年级的数学。

如果我解释得不够清楚,可以想象这样一个问题。UPS有100,000个包裹。每个包裹的重量在1到100磅之间,你知道每个包裹的重量。每辆UPS卡车最多可以装载10,000磅。应该将哪些包裹放到哪辆卡车上,以最小化所需的卡车数量?不,我不在UPS工作。这只是我脑海中首先浮现的比喻。是的,我知道在这个比喻中没有考虑体积。这不是为了工作、课程或考试而做的。最近这个问题一直困扰着我,让我感到很愚蠢。

过去我做过一些事情。它们都感觉有点“粗糙”和不精确,但对我来说很容易理解,编码速度快,而且在批处理对象并分发它们的处理时执行速度更快,而不是在一个地方执行单一数组对象的串行循环。

我已经按降序排列了对象,根据任务的适当标准确定了桶的大小和数量,然后按顺序迭代数组,将每个对象分配给下一个桶,当我用完桶时,回到第一个桶。如果我要将1到100的确切值分到3个桶中,100放入桶1,99放入桶2,98放入桶3,97放入桶1,依此类推。

我还选择了一个桶的大小,然后迭代数组,将对象分配给当前的桶,直到它的容量用完,然后为下一个对象创建一个新桶,同时在每个当前对象上检查每个先前的桶,看看它们是否有足够的剩余容量来容纳它,然后再为它创建一个新桶。

还有一些变化和组合。

我不是在寻找完全编码的、特定语言的解决方案(我认为我在PowerShell方面相当有能力,JavaScript能力较弱),也不是要被喂食“答案”。我正在寻找高层次的概述或一点关于如何解决这类问题的方向,以便让我的思路能够自己完成。这似乎是一个很常见的问题,但我在搜索时使用的措辞与我尝试实现的目标差距很大。

英文:

I assume this is like CompSci 101, but I've never taken a formal coding class. I'm just an admin self taught through experience by necessity. I prefer to figure things out on my own from as close to "scratch" as is reasonably possible, but this one is proving to be a bit confounding at my mediocre IQ and I would appreciate some help. I've kind of got a... a pretzel in my head.

I've been thinking recently about how I could efficiently arrange an array of objects, each with a numerical value of varying size, into the minimum possible quantity of fixed size "buckets." Like if I had 1,000 instances of values from 1-100 and I wanted to figure out how to arrange them into the least number of buckets assuming the sum of the values in each bucket (not the quantity of objects) cannot exceed 100, or 200, or whatever size. Not just how many buckets do I need. That's like 1st or 2nd grade math.

If I'm not explaining that well enough, think of a problem like this. UPS has 100,000 packages. Each weighs anywhere from 1-100 pounds, and you know how much each weighs. Each UPS truck can carry 10,000 pounds. Which packages should go onto which trucks to minimize the number of trucks needed? No, I don't work for UPS. That's just the first analogy which came to mind. Yes, I know I'm discounting volume in this analogy. This isn't specifically for a job, a class, or a test. It's just been bugging me lately and making me feel stupid.

I've done a few things in the past. They all feel rather "ghetto" and imprecise, but they're easy for me to comprehend, quick to code, and quicker to execute when I want to batch objects and distribute their processing rather than having a simple serial loop on a single array of objects executing in one place.

I've sorted the objects by descending value, decided on my bucket size and quantity by whatever criteria appropriate for the task, then iterated through the array sequentially assigning each object to the next bucket, wrapping back around to the first bucket as I run out of buckets. If I were sorting the exact values of 1-100 into 3 buckets, 100 goes into bucket 1, 99 into bucket 2, 98 into bucket 3, 97 into bucket 1, and so on.

I've also picked a bucket size and iterated through the array assigning to the current bucket until it runs out of capacity, then created a new bucket for the next object, while also checking each previous bucket on each current object to see if one of them has sufficient remaining capacity to accommodate it before creating a new bucket for it.

And some variations and combinations of those.

I'm not really looking for a fully coded language specific solution (I think I'm reasonably competent with PowerShell, and JavaScript to a lesser extent) or to be spoon fed "the answer." I'm looking for a high level overview or a bit of direction on how problems like this are addressed to get my light bulb to turn on enough to finish on my own. This seems like a common enough problem, but the verbiage I've used when searching doesn't turn up anything close to what I'm trying to accomplish.

答案1

得分: 0

你描述的问题家族在抽象上更常被称为"装箱问题",你的具体约束描述了我们可能称之为"最小箱子实例装箱问题" - 必须将不同尺寸的有限物品装入尽可能小数量的箱子中,每个箱子有特定的最大容量。

你描述的尝试听起来都像所谓的首次适应算法 - 你可能已经发现它们可以相对高效地实现(无论是在计算上还是在源代码复杂性上),但有时会产生次优的装箱分布,意味着你最终会使用比你本可以使用的更多的箱子/桶。

装箱问题与其他装箱问题密切相关,例如0-1背包问题的变种 - 从技术上讲,这是一个最大箱子数量为1的装箱问题。

英文:

The family of problems you've described in abstract is more commonly known as "bin packing problems", with your specific constraints describing what we might call a "minimum-bin instance packing problem" - having to pack a finite set of items with differing sizes into a smallest-possible set of bins, each of a given maximum capacity.

The attempts you've described all sound like so-called first-fit algorithms - as you've probably found they can be implemented relatively efficiently (in terms of both computational and source code complexity), but sometimes end up producing sub-optimal packing distributions, meaning you'll end up using more bins/buckets than you could have.

Bin packing is closely related to other packing problems, such as the 0-1 variant of the Knapsack Problem - technically a bin packing problem with a maximum bin count of 1.

huangapple
  • 本文由 发表于 2023年8月10日 23:23:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877178.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定