Python rectangle packing

algorithmspython

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.

Best Answer

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.

Related Topic