TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Algorithmic fitting of Japanese candy

116 pointsby EndXA11 months ago

18 comments

crazygringo11 months ago
I loved reading this, because it feels like it&#x27;s only the start. Now I&#x27;m really curious (and if anyone can point me to resources) --<p>1) Surely there are a bunch of more obvious optimizations, like don&#x27;t place candies where they would fall because of gravity and nothing supporting them underneath, or always placing the candies in order of largest to smallest, and ignore anything symmetrical or rotationally equivalent to a previous solution... and how much would this reduce the search space?<p>2) What is the actual metric for fitting in a box &quot;in the best way&quot;? That lower layers are as full as possible before adding upper layers? That shifting of contents is minimized while upright? That shifting of contents is minimized in any orientation? To minimize small gaps and try to make non-used space as contiguous and compact as possible? Or is the box size arbitrary and the goal is to find the minimal box volume?<p>3) Is there separately a metric for &quot;good enough&quot;? Should the metric not to be to fit in a specified box, but rather find the smallest box of a set of standard box sizes that can fit the candies, with any arbitrary arrangement rather than a &quot;best&quot;? Is this a much easier problem, or is it just as hard or harder?<p>I feel like Amazon must have solved this for all practical purposes.<p>I suspect that it would work well and be fast if you simply place items in order from largest to smallest by volume, filling from bottom to top, quickly determine which box sizes are too small, and then just find the first arrangement that works. Curious if there are pathological cases where that wouldn&#x27;t work?
评论 #40749685 未加载
评论 #40754889 未加载
评论 #40749905 未加载
resolutebat11 months ago
A friend of a friend worked at NASA on essentially this, except that instead of fitting Japanese candy in a box, they were fitting cargo into the Space Shuttle. As you can imagine this introduces a whole slew of new complications, notably mass distribution and that things must be unloaded in a certain order.
评论 #40749094 未加载
alexpotato11 months ago
While working at a Big Bank, a new policy was instituted where we had to fill out time sheets. We also had to allocate our &quot;budgeted time&quot; into these timesheets (Regardless of what we actually did).<p>It ended up being a bin packing problem that no one actually did till I wrote a script to do it for me. This in turn led to some interesting conversations with my manager and the head of Capital Markets and Banking&#x27;s CFO.<p>You can read more details about it here: <a href="https:&#x2F;&#x2F;twitter.com&#x2F;alexpotato&#x2F;status&#x2F;1296856648435326976" rel="nofollow">https:&#x2F;&#x2F;twitter.com&#x2F;alexpotato&#x2F;status&#x2F;1296856648435326976</a>
评论 #40749895 未加载
GuB-4211 months ago
Not only that, but the optimal solution may involve packing at an angle, the most obvious being fitting a candy bar that is too long across the package, in diagonal. If you look at some of the best square packings, the results can be surprisingly messy[1], and few of them have been proven.<p>It is clear that optimal algorithms are out of the question, which is the conclusion of the article, even with the constraints of orthogonal placement of simple boxes. In fact, in real life, when taking into account packages that are not simple boxes, items that can be squished a little and those that shouldn&#x27;t be, etc... I wouldn&#x27;t be surprised if humans were actually better than computers.<p>[1] <a href="https:&#x2F;&#x2F;kingbird.myphotos.cc&#x2F;packing&#x2F;squares_in_squares.html" rel="nofollow">https:&#x2F;&#x2F;kingbird.myphotos.cc&#x2F;packing&#x2F;squares_in_squares.html</a>
评论 #40752248 未加载
zimpenfish11 months ago
&gt; &quot;How hard could it be?&quot;<p>Having worked on something that required &quot;optimal&quot; box packing for deliveries[1], the answer is &quot;very, and then some (and that was with the help of that fancy USMIL pallet-packing algorithm, IIRC)&quot;.<p>[1] They wanted to use the smallest possible box plus some other constraints like &quot;product X cannot have anything on top of it&quot;, etc.
vslira11 months ago
A couple of months ago I tried using z3 to create an itinerary for my honeymoon. It seemed like a very straightforward implementation of a TSP with a few extra restrictions. It&#x27;s 2024 and I had an M2 - which is basically alien tech for the period when these problems started being solved - so I figured that in 5 minutes&#x27; time I&#x27;d have the perfect walking trip in Rome. To quote TFA, how hard could it be?<p>The whole story might become a blog post some day, but the punchline is that Moore&#x27;s law ain&#x27;t got nothing on NP-hard problems
评论 #40750644 未加载
reedf111 months ago
If this is presented at programmers, why not just say &quot;Here is a cool practical case of a knapsack&#x2F;bin packing problem.&quot;? The article seems to be intentionally avoiding this.
评论 #40748016 未加载
评论 #40749395 未加载
评论 #40750126 未加载
jiggawatts11 months ago
The author mentioned 10^20 combinations taking millions of years, but a modern server GPU can put out 10^15 computations per second (about a petaflop), assuming you use them in FP16 mode. Keep in mind that 65K divisions of a box one meter per side is 15 micrometers! This means that roughly speaking, it would be possible to brute-force through all possibilities in about 10^5 seconds, which is just one day. It helps that this type of algorithm is almost all computation with very little data transfer to and from main memory, and is &quot;embarrassingly parallel&quot;.<p>Some light optimisation such as utilising symmetries to reduce work, combined with multiple GPUs in parallel could bring this down to hours or even minutes.<p>It would be a fun thing to cloud-host, spinning up spot-priced GPUs on demand.<p>Similar brute-force tricks can be applied to other NP-hard problems such as optimising electronic circuit board wiring, factory floor planning, etc...<p>The ridiculous amount of compute we have available to us in a GPU reminds me of this quote from Stargate Atlantis:<p>JEANNIE: The energy you&#x27;d need would be enormous to the point of absurd.<p>McKAY: Absurd we can do. We have something called a Zero Point Module which essentially does what we&#x27;re attempting on a smaller scale -- extract energy from subspace time.
评论 #40755035 未加载
madaxe_again11 months ago
Used to provide ecommerce solutions, from purchase through to despatch and beyond.<p>A frequently asked for feature was box-packing - clients would complain that their packers would chuck something tiny in a huge box, use loads of packaging, and still manage to have the thing damaged on arrival due to shaking about in there.<p>So we implemented the feature. It worked great. Clients bought it.<p>Literally nobody used it, as humans inevitably decide they know better. Instead, we just ended up with clients reporting “the boxer told our packer to put a stick of gum in a 1m2 palletised delivery!”, as that was what the packer would report, when it had done no such thing, so all we had done was move the point of blame from minimum wage warehouse workers to ourselves.<p>We withdrew the feature.
评论 #40749729 未加载
bencyoung11 months ago
I once had a financial bin packing problem at a previous company. Rather than going down a dynamic programming route which was highly exponential, we decided to use a combination of monte-carlo simulation and a very basic genetic algorithm to try and find a solution. This made use of the fact that we could try solutions extremely quickly, and also timebound the overall time taken (something that was important as we had a limited time window). We&#x27;d still be able to run 1-2e6 different approaches in under a second.<p>Although we couldn&#x27;t guarentee the &quot;best&quot; approach possible we generally came up with something good enough, often in many orders of magnitude less time.<p>A good example of the performance improvements you can make when you move from thinking of what the best solution is to what a &quot;good enough&quot; one is.
freeone300011 months ago
“A few minutes” seems… reasonable? Like, you can let it run for a few hours on a list, come back in the morning, see which combos work. You ship it out once a month, it’s gotta be quicker than testing them by hand.
评论 #40748468 未加载
Cthulhu_11 months ago
(2016). Also posted 8 years ago, 50 comments: <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=12973964">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=12973964</a>
cypherpunks0111 months ago
Wouldn&#x27;t using a constraint programming solver like CP-SAT be overall a faster way to do this?<p>Then one would only have to specify the constraints and let the solver optimize a solution, rather than trying to write a customized packing problem algorithm from scratch. I&#x27;m sure it&#x27;d be fun, but when trying to best solve a real-world problem, I&#x27;d think using well-researched tools would be the quickest and safest way to go.
poopcat11 months ago
I loved how you used your skills to address a ~seemingly~ simple problem. The explanation was also cool
Minor49er11 months ago
I stumbled across a PHP library that aims to solve this kind of thing<p><a href="https:&#x2F;&#x2F;github.com&#x2F;dvdoug&#x2F;BoxPacker?tab=readme-ov-file#boxpacker">https:&#x2F;&#x2F;github.com&#x2F;dvdoug&#x2F;BoxPacker?tab=readme-ov-file#boxpa...</a>
lkdfjlkdfjlg11 months ago
&gt; Hey I know, I&#x27;m a programmer, I&#x27;ll just write an algorithm to do it for me. How hard could it be?<p>Hey I&#x27;m a programmer, I&#x27;ll tell a computer to brute-force it.<p>To someone who only has a hammer everything looks like a nail.
nayuki11 months ago
I see all the &lt;canvas&gt; elements are blank because of JavaScript code errors.
评论 #40749603 未加载
deusum11 months ago
This would be a fascinating Code Golf challenge.