Tile (scalable) stacking algorithm

倾然丶 夕夏残阳落幕 提交于 2019-12-04 03:41:10

As others pointed - problem description is not very clear. But i'm assuming that you can scale each tile as you need (at least your examples shows that tile scaling is possible). So my solution will be simple (but maybe not what you want nor optimal):

  • Scale each tile by factor :
    EDGEspace / (EDGEtile * N ½)
  • Place each tile in current row, if tile gets out of space limits - advance to next row.

Here N is nearest perfect square greater or equal to the total number of tiles.

p.s. If you need some spacing between tiles - just make above scale factor a little bit smaller.

Hope that helps.

It may appear that this is a packing problem, however if we try to solve this problem exactly as it described it is not. In other words there is no solution because, again, there is no problem in a question as it is described. If we have ONLY ONE box and fixed set of tiles and requirement that they ALL must fit into box there is no room for optimization. I can see several related optimization problems:

1. Given fixed set of tiles that must be packed into boxes of same or different sizes find optimal packing order so that minimal number of boxes is used.

2. Given single box of an arbitrary size and set of tiles find optimal (maximum) set of tiles that can be fit into a box.

3. Given a box and set of tiles - answer the question if it is possible to fit them into a box or not.

Which one of these are you trying to solve?

The way problem is set right now is meaningless, because no matter which order you place the tiles in the box they will always use same amount of space no matter how they are arranged, as soon as they all fit in of course.

Try a monte-carlo algorithm:

Repeat until result is good enough or until you aren't seeing any improvement
  Select (with removal) a random first tile
  Place the first tile at a random position
  Repeat until no remaining tiles
    Select (with removal) a random tile
    Place it adjoining to the existing "tile blob" 
      (you might have to do a search here to find the best place to plug it in)
  Check to see if you have a new best filled-area percentage

All random tile selections should be weighted by the tile's area so that you tend to place the larger ones first.

I don't think this is a (bin)-packing problem because I wrote one for the 1D bin-packing problem. I think the problem here is solved by the 2D-cutting-stock problem, maybe there is also a 2D-bin-packing. What you want is to try the knappsack-problem too. This problem is hard to solve (NP) and there is no solution. It's a bit like the Travelsalesman problem where the number of solution is exponential to the number of cities. If you can reduce the ccomplexity to a 1D problem you may try my bin-packing algorithm at phpclasses.org.

I am not sure if it's what you want but you can look at TreeMap algorithm.

Wikipedia definition

C++ implementation

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