r/dailyprogrammer • u/nint22 1 2 • Jan 02 '13
[1/2/2013] Challenge #115 [Difficult] Pack-o-Tron 5000
Description:
Overdrive Mega-Corporation is coming out with a new and brilliant commercial electronic device that packs your bags for you! It is named "Pack-o-Tron 5000": an automated box-packing solution. Moving to a new home? Traveling overseas? Going on a business trip? No matter what, this device will help you pack your boxes optimally, reducing empty space and fitting more objects in less area!
As the lead engineer, you are tasked with designing the code that generates the "optimal placement" of items in a given area. Fortunately for you, the "Pack-o-Tron 5000" only works with very specific definitions of "items" and "areas". (Shh, don't tell the customers that, marketing is still working on it).
An "area" is an empty 2D grid, where the length and width are integers representing inches. As an example, a suitcase could be defined as a 36-inch by 48-inch grid. An "item" is a 2D object, whose length and width are integers representing inches. As an example, a tooth-brush item can be 1-inch in width, and 3-inches long.
Your goal is to place all given items in a given area as optimally as possible. "Optimally" is defined as having the smallest minimum-rectangle that spans your set of items.
Note that the "area" is defined as a grid where the origin is in the top-left and X+ grows to the right, with Y+ growing to the bottom. An item's origin is in the top-left, with X+ growing to the right, and Y+ growing to the bottom. A minimum-rectangle that spans your set of items is a rectangle on the grid that includes all items placed, and is either equal to or smaller than the given area. Your goal is to make this minimum-span as small as possible. You are allowed to place your objects in any position, as long as it is in the grid. You are also allowed to rotate your objects by 90 degrees.
Formal Inputs & Outputs:
Input Description:
You will first be given two integers: the width and height of your starting (empty) grid. After, you will be given another integer representing the number of following items you are to place in this grid. For each item, you will be given two integers: the width and height of the object. All of this is done through standard input. (i.e. console).
Output Description:
Once you have optimally placed the given items in the given grid such that it as the smallest minimum-span possible, you must print each item's x and y coordinate (as an integer), and the object's size (to show us if there has been any rotations).
Sample Inputs & Outputs:
Take a look at the example images here. For all images that have the two 1x3 items, you would be given the following sample input:
8 8
2
1 3
1 3The most optimal result (the last image) is a 2x3 minimum-span, which can be described in the following:
0 0 1 3
1 0 1 3For those who are keen observers, an equivalent solution is the same pair of objects, but mirrored on the diagonal axis:
0 0 3 1
0 1 3 1Note:
This is essentially a clean version of the Packing Problem. Since the packing problem is in the computational complexity class of NP-hard, there is no known algorithm to run in P-time (...yet! Maybe there is! But that's the whole P-NP relationship problem, isn't it? :P). Instead, look into heuristic algorithm designs, and try to avoid brute-force solutions.
Us mods are putting together an achievement system - those who give us a good solution will get some sort of cool title in their user-flare, since this challenge is frankly very very difficult.
7
u/Splanky222 0 0 Jan 03 '13
So... are integer programming solvers in play? It seems like this problem lends itself to an integer program nicely, and a branch-and-cut method would give a surefire solution
6
u/domlebo70 1 2 Jan 04 '13
Just spent a couple of hours on this. It's bullshit hard. The fact rectangles can be rotated makes it entirely non trivial. I was using a binary tree and inserting each rectangle depending on size to the left or right, but this only gets me so far.
3
u/jeff303 0 2 Jan 03 '13
So, can anyone provide more of a hint around the whole "heuristic algorithm designs" concept?
3
u/Volvaux Jan 03 '13
Just spitballing here, would a possible approach to the problem be to set a heuristic area value for your optimized grid equal to the length of your longest piece, times the sum of the smallest values of the remaining pieces plus the largest pieces width, then use the properties of a heap in order to try and optimize piece placement? So, for an example, if you were to look at the first solution, even though you had an 8x8 grid, mathematically speaking, your "optimal" bounding box should be 3 (length of largest piece) * (1+1) (sum of widths) = 6. Your "optimal" grid is thus a 3*2, so you place an empty clone of the initial grid into a heap, with additional metadata that tells your program which pieces have already been placed into it. From there, it's simply a recursive approach, using the area as your first comparison criteria for heap placement, followed by how densely packed it is as your tie breaker.
2
u/Cosmologicon 2 3 Jan 02 '13
As a suggestion, maybe you could randomly generate an example input of 100 rectangles and we could have an achievement for whoever produces the best solution for that input? I should be able to make a python script to verify that a solution is valid shortly...
2
u/domlebo70 1 2 Jan 07 '13
Okay. I've spent about 6 or so hours on this problem over the past couple of days. I decided to do a bit of research, and found many people mentioning algorithms that centered around splitting based on the length and width. I decided to take this approach and build it around a binary tree.
My solution is written in Scala. Basically my algorithm works like this (note, the code works slightly differently):
The binary tree is composed of Nodes (containing either another Node, or a Leaf), as per usual. A leaf however, can be one of two types: a Rectangle, or it can be AreaRemaining. This is an example tree after inserting 1 Rectangle of size (125, 75):
We have an AreaRemaining Node up top with 200 by 200 units remaining in all subtrees. The node directly under the 200x200 is where I start separating containers. All I do is recurse the tree from the left looking for a node with available space. When I find it, I split it by the longest side. So in this example I am drawing a line horizontally and creating two new AreaRemaining nodes (AR(20, 6) and AR(20, 14)). If I was to draw it, it would look like this at this stage:
I didn't illustrate the AR(20, 6) node, because it gets split again and contains the AR(8, 6) and the Rect(12, 6). Now we just recurse, and keep splitting into nodes. I have an extra Position object to help with hte drawing, but it's not neccessary. When I add two more rectangles:
I end up with a tree like so:
I won't draw the diagram like above because it takes too long, but the program output looks like this:
Where to go from here? My solution is fairly naive, and doesnt take into account rotation. An easy optimization will be to presort rectangles, which is easy enough. Rotation is also fairly easy. Would love some feedback.
Code:
4
u/Cosmologicon 2 3 Jan 03 '13 edited Jan 05 '13
Here's a python script to verify your solution is correct. I didn't test it except on the example given in the problem statement, so it's commented in case anybody wants to check it. There's no spoilers in here, it just verifies a solution.
3
u/ixid 0 0 Jan 03 '13
You should add simple and hard examples to be solved so people have standard answers to work on. With an answer given for the simple one.
4
u/Deathisfatal 0 0 Jan 03 '13 edited Jan 03 '13
I've created a program in C++ using brute-force to calculate this. I'm still working on a more advanced/clever method, but I thought I'd share this seeing as there are no solutions as of yet. It's probably not the prettiest code (it's 3 am and I should go to sleep...) but it works. It randomly moves one object around the area at a time, then calculates the area the objects take up and saves that if it's the lowest so far. Once the lowest found score has appeared a certain number of times, it's assumed that's the best answer.
It takes a very long time to calculate large areas due to how it works, but it copes very well with the 8x8 grid.
Screenshot of example input: http://i.imgur.com/1aXad.png
Bigger grid: http://i.imgur.com/Y173a.png
Source: https://dl.dropbox.com/u/5110268/Challenge%20115.rar
0
u/[deleted] Jan 05 '13
[deleted] — view deleted comment
4
u/[deleted] Jan 06 '13 edited Jan 06 '13
[removed] — view removed comment