Python rectangle packing

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.

8

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật