good chemistry is complicated, and a little bit messy LW 

PerlMonks 
(tye)Re: Packaging Algorithmby tye (Sage) 
on Nov 07, 2000 at 21:10 UTC ( #40362=note: print w/replies, xml )  Need Help?? 
Okay, how many crate sizes can you buy at UHaul? Or are you expecting him to buy sheet cardboard and build a custom crate in order to achieve maximal packing? The number of crate sizes to try is just a few. But then, I wouldn't try any sizes of crates at all! Where did you get this crazy idea to try every possible origin for a package? If I put packages together tightly, then every package shares part of a side with another package. And we aren't talking about some theory trying to prove that we have the maximally tightest packing (which would be very hard, sure). Just assume that a reasonable packing exists where one package hits a corner of the crate (when was the last time you put packages in a crate in such a way that no package shared a corner with the crate?). Now all you have to do is place a package in the corner of an infinite box (the positive octant) and add boxes in the resulting "corners". As you place boxes, keep track of the bounding crate. Iterate through all of the possibilities. Sure, this isn't trivial to code and the number of possiblities is huge. If you have a large number of packages, then try at random and report the best solution so far once you are tired of waiting. Who cares if it is brute force? Also, if you haven't placed all of the boxes yet and you are already worse off then your best solution so far, no need to continue adding packages. Take N boxes. First box: N choices (orientation doesn't matter yet) Second box: N1 boxes * <=3 corners * 6 orientations Third box: N2 boxes * <=5 corners * <=6 orientations ... If I change the *3*5*7*9... terms to *4*6*8*10... then we are bounded by N! * 2^(N1)*N! * 6^(N1) == N!^2 * 12^(N1). So assuming a worsethanworstpossible case where we don't trim any chunks from the decision tree (we try all of the combinations in exactly the wrong order and don't eliminate overlapping boxes until we've placed all boxes), then we can still exhaustively search for 5 packages pretty quickly. For 6 or 7, easy trimming of the decision tree might let you exhaustively search (hard to predict). For more than 7, you'd probably have to settle for stopping early and having a less optimal solution. Brute force is often a very good way to solve reallife problems. That is why they computers that are fast. (:  tye (but my friends call me "Tye")
In Section
Seekers of Perl Wisdom

