Area optimization algorithm - algorithm

I'm having the requirement, to insert a specific amount of rectangles (which have a defined width but a random height) into another rectangle (which has a defined height and the same defined width as the rectangles to insert). The goal here is, that those inserted rectangles should fill-out the target rectangle as much as possible.
For instance:
I don't need to get as much rectangles as possible into the black, the goal is to fillout the black rect as much as possible, best case, entirely.
In reality, there are many "black" rectangles and thousands of "reds", I'm looking for an effective algorithm to calculate. I have to implement this in ECMA-/Javascript so it's not really the fastest of all platforms.
I looked into some algos like Richard E. Korf's "Optimal Rectangle Packing" or "Bin packings problems", but I couldn't really translate those for this specific problem.
Can somebody recommend me a method/algorithm ?

Because your red and black triangles both have a defined width, you can reduce the problem to a number line, so to speak. Basically, if you ever flipped a red one on its side, you'd most likely end up with wasted space - much more wasted space than putting it in the 'normal fitting' way.
So with this in mind, you can reduce the problem exactly to the traditional knapsack problem where the capacity is the height of the black rectangle and the 'weight' of the red triangles are their height. The width can be entirely abstracted out of the problem.
Another advantage (as pointed out by xvatar) is that the value density of the candidates are all equal. That is to say that you don't have the "brick-ring" issue the traditional knapsack problem has. Given the choice between bricks and rings to fill your knapsack with, the rings are obvious candidates. In this case, they're all the same so there are no obvious candidates.
It would seem the big blocks are easy candidates, but this greedy approach doesn't fly. Consider the scenario where there are 5 units of space left, and we have bricks of 4, 3 and 2. If we go with the greedy solution, we add the 4, leaving 1 open space. If we would instead have gone with 3 and 2, we would not have the 1 open space remaining.
It also stands to note that once you have identified what rectangles go in, it doesn't matter what order they go in.
Further reading: http://en.wikipedia.org/wiki/Knapsack_problem

Related

Minimum area calculation algorithm (Place tiles on edge only)

I have different dimension of small rectangles (1cm x 2xm, 2cmx3cm, 4cm*6cm etc). The number of different type rectangles may vary depending on case. Each type of different rectangles may have different number of counts.
I need to create a big rectangle with all these small rectangles which these small rectangles can only be placed on the edges. no rotations. The final outer rectangle should ideally be smiliar to a square shape. X ~Y. Not all edges need to be filled up. There can be gaps in between smaller rectangles. Picture Example:
http://i.stack.imgur.com/GqI5z.png
I am trying to write a code that finds out the minimum possible area that can be formed.
I have an algorithm that loop through all possible placement to find out the minimum area possible. But that takes a long run time as number of different type rectangles and number of rectangles increase. i.e. 2 type of rectangles, each has 100 + rectangles. 8 for loops. That will be ~100^8 iterations
Any ideas on better and faster algorithm to calculate the minimum possible area? code is in python, but any algorithm concept is fine.
for rectange_1_top_count in (range(0,all_rectangles[1]["count"]+1)):
for rectange_1_bottom_count in range(0,all_rectangles[1]["count"]-rectange_1_top_count+1):
for rectange_1_left_count in (range(0,all_rectangles[1]["count"]-rectange_1_top_count-rectange_1_bottom_count+1)):
for rectange_1_right_count in ([all_rectangles[1]["count"]-rectange_1_top_count-rectange_1_bottom_count-rectange_1_left_count]):
for rectange_2_top_count in (range(0,all_rectangles[2]["count"]+1)):
for rectange_2_bottom_count in (range(0,all_rectangles[2]["count"]-rectange_2_top_count+1)):
for rectange_2_left_count in (range(0,all_rectangles[2]["count"]-rectange_2_bottom_count-rectange_2_top_count+1)):
for rectange_2_right_count in [(all_rectangles[2]["count"]-rectange_2_bottom_count-rectange_2_left_count-rectange_2_top_count)]:
area=calculate_minimum_area()
if area< minimum_area:
minimum_area=area
This looks like an NP-hard problem, so there exists no simple and efficient algorithm. It doesn't mean that there is no good heuristic that you can use, but if you have many small rectangles, you won't find the optimal solution fast.
Why is it NP-hard? Let's assume all your rectangles have height 1 and you have on rectangle of height 2, then it would make sense to look for a solution with total height 2 (basically, you try to form two horizontal lines of height-1 rectangles with the same length). To figure out if such a solution exists, you would have to form two subsets of your small rectangles, both adding up to the same total width. This is called the partition problem and it is NP-complete. Even if there may be gaps and the total widths are not required to be the same, this is still an NP-hard problem. You can reduce the partition problem to your rectangle problem by converting the elements to partition into rectangles of height 1 as outlined above.
I'll wait for the answer to the questions I posted in the comments to your question and then think about it again.

How to fill an area with a collection of rectangles and no space left

I have a collection of different sized rectangles that need to be placed on a 2D surface of a known dimension, with the condition that:
there is no overlap between the blocks,
the rectangles may not be rotated,
there is no area left blank (whole surface needs to be filled).
The rectangles are actually generated by breaking down the surface area, so they ought to fill up the area completely once you start putting everything together.
I figured there would be a dozen algorithms available for this problem, but the algorithms I find are more like best effort algorithms such as sprite generators that do not have the precondition that the whole area needs to be (can be...) filled -- which obviously is not necessary when building sprites, however, it is in my case.
I am a bit lost here, either this problem isn't as simple as I thought, or I am searching on wrong keywords.
Some topics I have found but do not fully suit my needs:
What algorithm can be used for packing rectangles of different sizes into the smallest rectangle possible in a fairly optimal way? (in my case, the area is preset)
How to arrange N rectangles to cover minimum area (in my case, minimum area must equal zero)
Is there any algorithm out there that may suit my needs?
IMHO, the most natural solution is recursive. For the form of source area is not set. And after removing a rectangle from it, we have the same task, only with smaller area and -1 rectangle.
I would start from the edges, because there the possible variants are already limited. So, simply go by spiral, trying to put rectangles along the edge. If no rectangle fits, go back. That will be the simplest and not so slow raw force method.

Programmatically arrange rectangular UI objects in an abstract way, without gaps

Given a collection of say, 50, images with various widths and heights, how would one go about programmatically arranging them in an interesting* abstract way? (see image below)
By interesting I mean, no large gaps, and no easily distinguishable rows or columns (negative space forms a lot of T-like intersections).
For my specific case, all images have a set max dimension of 150px, which could mean the height OR width is a max of 150px (could be 150px by 450px, or 378px by 150px).
This seems like it could be a classic programming challenge but I'm finding the topic hard to Google...
EDIT: Changed image to show that there is no restriction on how the overall arrangement must be (doesn't have to fit inside a set area)
If you are not opposed to jquery plugin, you can check this out - http://masonry.desandro.com/
Your problem is NP-Hard.
This thread shows that even with one type of nXm rectangles, it is NP-Hard to find if there is a solution , so your more generalized problem is of course NP-Hard as well [The only one type of rectangle is a private case of this problem]
You could try a backtracking solution if you are after optimized solution, or a heuristic approach such as genetic algorithms or hill climbing, which will be faster - but will usually find a non optimal result.
I have built something similar to this (although it is probably not the most sophisticated solution). My approach was to use a quadtree to organize the rectangles that I had placed on the canvas. I then just basically went around the center point in a spiral, trying to place new rectangles, and using the quadtree to detect collisions. If I detected a collision then I would move the rectangle I was trying to place to the edge of the rectangle that it collided with that was furthest from the center and repeat the collision checking process.
Again, probably not the most sophisticated method, and it does tend to leave some larger gaps between rectangles (the borders between them are not uniform), but to my taste it gave good results.

Tiling rectangle with fixed size tiles

I have been struggling with finding a convenient solution for the following problem:
Suppose we have a wall of a given size and 4 types of tiles of sizes 4 x 2, 2 x 2, 2 x 1, 1 x 1. There are certain rectangular regions inside the perimeter of the wall which can not be tiled (i.e. holes). There is also a special type of tile which has a variable dimension A x B with A < 1. This is used to pad the tiling to the margin of the rectangle, if needed.
Find a tiling of the wall which respects the following constraints:
Tiles of the same size can not be placed one below the other, with the same alignment (i.e. tiles appearing on a row below have to be shifted such that there is no gap which looks like a cross between adjoining tiles of the same size)
A minimum number of tiles is used
Tiles which exceed the boundaries of the rectangle will be trimmed to the margin; the incomplete tile thus produced will be broken in smaller tiles; this could possibly involve the use of a special tile which should always sit next to the margin of the rectangle or the margin of a hole, wherever the situation might arise
Here is what I've tried so far:
I've looked into algorithms for solving this using domino tiling but most don't seem to care that the tiling process can not produce gaps which look like a cross where tiles meet. Also, to me the problem seems a bit different as there are more types of tiles and it also seems that the rectangle does not have to be exactly filled (it is possible for small spaces to remain near the margins which will be filled using special tiles)
I've tried to generate all possible tilings using a branch and bound technique with state node pruning so that only those states where tiles which do not break the constraints are added will be explored, but this is definitely not scalable.
I've also looked into packing algorithms but to my knowledge, this might lead to a certain tiling where there are small untiled spaces which can remain inside the premises of the wall.
It might be possible that I've overlooked something, or not had enough insight while exploring the above techniques.
With all these being said, do you guys have any hints or suggestions on a way to approach this which yields results?
This is an example of a tiling which respects constraints 1 and 3, but is not optimal
Do you need the optimal tiling, or are you willing to settle for "pretty good"? Finding the optimal solution is likely exceedingly hard. Intuitively, I would suggest the following heuristic:
1. Pretend there are no holes in the wall, tile with large tiles.
2. Remove all tiles which intersect with holes.
3. current_size = largest
4. For each empty space: tile as much as possible with current_size
5. current_size = the size just below current_size
6. return to 4

Placing 2D shapes in a rectangle efficiently. How to approach it?

I've been searching far and wide on the seven internets, and have come to no avail. The closest to what I need seems to be The cutting stock problem, only in 2D (which is disappointing since Wikipedia doesn't provide any directions on how to solve that one). Another look-alike problem would be UV unwrapping. There are solutions there, but only those that you get from add-ons on various 3D software.
Cutting the long talk short - what I want is this: given a rectangle of known width and height, I have to find out how many shapes (polygons) of known sizes (which may be rotated at will) may I fit inside that rectangle.
For example, I could choose a T-shaped piece and in the same rectangle I could pack it both in an efficient way, resulting in 4 shapes per rectangle
as well as tiling them based on their bounding boxes, case in which I could only fit 3
But of course, this is only an example... and I don't think it would be much use to solving on this particular case. The only approaches I can think of right now are either like backtracking in their complexity or solve only particular cases of this problem. So... any ideas?
Anybody up for a game of Tetris (a subset of your problem)?
This is known as the packing problem. Without knowing what kind of shapes you are likely to face ahead of time, it can be very difficult if not impossible to come up with an algorithm that will give you the best answer. More than likely unless your polygons are "nice" polygons (circles, squares, equilateral triangles, etc.) you will probably have to settle for a heuristic that gives you the approximate best solution most of the time.
One general heuristic (though far from optimal depending on the shape of the input polygon) would be to simplify the problem by drawing a rectangle around the polygon so that the rectangle would be just big enough to cover the polygon. (As an example in the diagram below we draw a red rectangle around a blue polygon.)
Once we have done this, we can then take that rectangle and try to fit as many of that rectangle into the large rectangle as possible. This simplfies the problem into a rectangle packing problem which is easier to solve and wrap your head around. An example of an algorithm for this is at the following link:
An Effective Recursive Partitioning Approach for the Packing of Identical Rectangles in a Rectangle.
Now obviously this heuristic is not optimal when the polygon in question is not close to being the same shape as a rectangle, but it does give you a minimum baseline to work with especially if you don't have much knowledge of what your polygon will look like (or there is high variance in what the polygon will look like). Using this algorithm, it would fill up a large rectangle like so:
Here is the same image without the intermediate rectangles:
For the case of these T-shaped polygons, the heuristic is not the best it could be (in fact it may be almost a worst case scenario for this proposed approximation), but it would work very well for other types of polygons.
consider what the other answer said by placing the t's into a square, but instead of just leaving it as a square set the shapes up in a list. Then use True and False to fill the nested list as the shape i.e. [[True,True,True],[False,True,False]] for your T shape. Then use a function to place the shapes on the grid. To optimize the results, create a tracker which will pay attention to how many false in a new shape overlap with trues that are already on the grid from previous shapes. The function will place the shape in the place with the most overlaps. There will have to be modifications to create higher and higher optimizations, but that is the general premise which you are looking for.

Resources