Home > front end >  Placing array of rectangles inside a given area using Minizinc
Placing array of rectangles inside a given area using Minizinc

Time:05-31

I want to place a set of rectangles into an area with a given width and minimize the total height. I have made a solution in miniZinc, but the running time is quite long.

The final result came after a couple of seconds, but the running didn't stop until 10 minutes had passed. Is it a way to make the running time a bit faster/ improve the performance? And should I use cumulative constraints ?

CodePudding user response:

This appears to be a straightforward Rectangle Packing Problem. Algorithms that work well for this specific problem, including variations such as Square Packing, Rectilinear packing with/without rotations.

In MiniZinc in particular this is a topic discussed in the second MiniZinc Coursera course: Advanced Modeling for Discrete Optimization. This should give you all required knowledge to create a good model for the problem.

CodePudding user response:

Here are some comments in addition to @Dekker1's answer.

First some questions: Which solver did you use? Was the first answer found directly the optimal solution?

You might get a faster solve time with some other FlatZinc solver. I tested the model that you originally included in the question (and later removed) and with some different FlatZinc solvers. Also, I printed out the objective value (makespan) to compare the intermediate solutions.

  • Gecode: Finds a solution immediately with a makespan of 69, but then takes long time find the optimal value (which is 17). After 15 minutes no improvement was done, and I stopped the run. For Gecode you might get (much) better result with different search strategies, see more on this here: https://www.minizinc.org/doc-2.3.1/en/lib-annotations.html#search-annotations .

  • Chuffed: finds a makespan of 17 almost directly, but it took in all 8min28s to prove that 17 is the optimal value. Testing with free search is not faster (9min23s).

  • OR-tools: Finds the makespan (and proves that it's optimal) of 17 in 0.6s.

startX: [0, 0, 0, 3, 3, 13, 0, 14, 6, 9, 4, 6, 0, 10, 7, 15]
startY: [0, 3, 7, 0, 6, 5, 12, 0, 2, 6, 12, 0, 15, 12, 12, 12]
makespan: 17
----------
==========

The OR-tools solver can sometimes be faster when using free search (the -f flag), but in this case it's slower: 4.2s. At least with just 1 thread. When adding some more threads (here 12), the optimal solutions was found in 0.396s with the free search flag.

There are quite a lot of different FlatZinc solver that one can test. See the latest MiniZinc Challenge page for some of them: https://www.minizinc.org/challenge2021/results2021.html .

Regarding cumulative, it seems that some solvers might be faster with this constraint, but some are slower. The best way is to compare with and without the constraint on some different problem instances.

In summary, one might occasionally have to experiment with different constraints and/or solvers and/or search strategies.

  • Related