6

I'm working on a project which involves packing multiple rectangles in a larger rectangle(the bounding box). Rectangles can't overlap with each other or with the boundaries of the bounding box. Rectangles can rotate, increasing the state space to n! * 2^n for problems of n rectangles.

I'm trying to write a Python program that 'solves' these problems, eg it should find all possible solutions given a set of rectangles and a bounding box. I'm using a depth-first search algorithm right now, but I feel like I'm missing a lot of optimisations to speed up my program. My algorithm works as follows:

  1. I have a list with values representing the heights of the columns in the larger bounding box, initialised to all 0's.
  2. I look for the first empty spot in the bounding box, which is represented by the column with the smallest value.
  3. If the current rectangle fits in that spot, I 'place' the rectangle by increasing the height of the right columns by the height of the rectangle.
  4. Repeat 2 and 3 until no more rectangles can be fit and backtrack to other possible solutions.

In (pseudo-)code it looks like this:

def solve(rectangles):

    # Solution found
    if rectangles is empty:
        add_to_solutions()
        return

    position = find_first_empty_spot()
    for rectangle in rectangles:
        for r in [rectangle, rectangle.rotated()]:
            if rectangle fits at given position:
                place_rectangle_in_bounding_box(r)
                remove r from rectangles
                solve(remaining_rectangles)
                remove_rectangle_from_bounding_box(r)

Are the basics of my algorithm correct or am I missing some (obvious) improvements? It would be great to solve problems of sizes up to 20 rectangles but my current algorithm would take way too much time to solve them.

And: I'm trying to find 'all' possible solutions to the problem, I can't just stop after finding 'a' solution, so a lot of heuristics found in the literature are not applicable.

  • How about some dynamic programming: Lets say the large space is size N, and you have a list of rectangles L. after placing some rectangles you have n space remaining, and l rectangles. Once you figure out all the possible combinations that l rectangles can make in this space, then in the future when you come across the same situation, you already know the answer. – OverlordAlex May 4 '15 at 9:33
  • look here stackoverflow.com/a/21282418/2521214 and here stackoverflow.com/a/18693107/2521214 for some ideas ,... what are the criteria ? is rotation needed? (rotated rectangles occupy usually more space – Spektre May 4 '15 at 12:09
  • 2
    You may be interested by the Survey on two-dimensional packing article. – Arseni Mourzenko May 4 '15 at 14:01
  • What are you really after? The number of possible placements? The most dense packing (ie the packing with the smallest minimal bounding box)? A packing with a "high score" according to some scoring function? Making a list of 20!*2^20 packings sounds pretty unreasonable. – Doc Brown May 4 '15 at 15:45
  • I don't understand the problem. Where does the n! come from? If you have for example just one 2x1 rectangle and a 3x3 box, then there are 12 ways, not 2. And this is even assuming you're restricted to right angles and integers. – Stefan Pochmann May 4 '15 at 17:36
3

The problem you are asking about is a well known problem which has a plethora of applications: For example the task to minimize material waste in furniture production: Certain pieces (your list of rectangles) have to be cut out of boards of plywood in a given size (your bounding rectangle). As you seem to have already figured out finding all possible solutions is a combinatorial NP-hard problem.

Therefore in the industry we usually don't insist on finding all possible solutions but instead use an approximation algorithm which might of course not always deliver the best possible solution. So currently the best answer I can give to your question is to point you to this BPP example here illustrating the use of a solver from the open source Python OpenOpt package.

From my understanding your wish to enumerate all possible solutions is impossible to fulfill in an acceptable time even for a moderate number of saying 20 rectangles or so. If this is not true I'm also eager to know and learn.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.