Block layout algorithm

一笑奈何 提交于 2019-12-20 10:37:31

问题


I'm looking for help with improving an algorithm for placing blocks of odd shapes. My problem domain is odd, but the best analogy for my blocks is Tetris pieces, except that they can have more than four pieces. The blocks still are composed of only right angles, but they can be long and winding, they can branch, etc.

I'm trying to arrange multiple large arbitrarily shaped blocks in minimal space (I know, a bin-packing problem) but my current solution looks ugly. I'm basically placing one, then brute forcing the rest by trying to place them at the origin of my grid and then slowly pushing them in different directions until they don't collide anymore. It's not slow, but it doesn't make any attempt to fit pieces nicely so they don't waste overall space.

The only thing I can think of trying is ordering the blocks by size, placing the largest first, then fitting the smallest in at the end into any holes remaining. But there are certainly ways that can backfire.

Are there any heuristics or approximation algorithms that can help me here?

The results would look something like the following:

Also, perhaps my gravatar gives away that this is Mega Man related...


回答1:


This (polyomino shape-packing) generally seems to be a nontrivial math problem, and I'll point you to the expertise of some others who have worked on it. This guy has a bunch of polyomino examples on his website where others can submit solutions. He also has solver software in Java:

http://gp.home.xs4all.nl/Site/Polyomino_Solver.html.

http://gp.home.xs4all.nl/PolyominoSolver/downloadsolver.htm

There are also some algorithms written for this by Stephen Montgomery-Smith, which seem to be more comprehensive than the above (it solved some problems that weren't solvable with that) eventually made it into an xscreensaver (solves in real-time and cool to watch!). The following screenshot, from the screensaver, only shows shapes up to pentominoes, but it works on general shapes with general containers.

http://www.math.missouri.edu/~stephen/software/

I am unsure if either of these software approximates the best fit of polyominoes allowing for holes. But it's definitely 'decidable' this way, in the sense that you could certainly insert extra 1x1 polyominoes into your solution and see if it can find a particular result that fits, then remove the 1x1 pieces to get the result.

For your application, it might be more efficient to work backwards. All of these algorithms have complexity in the number of unit cells in each block. A good way to lay out your blocks would be to think of them as "subdivisions" in larger cells, so that a 3x3 square in your block corresponds to a 1x1 square in a rescaled version. Then, pad the blocks with empty space so they all consist of the larger blocks, run the algorithm, and strip away the extra space. Not only will this be much faster to execute, but it will also generate the space between blocks that you require.



来源:https://stackoverflow.com/questions/15016832/block-layout-algorithm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!