An exploratory algorithm for the rectangle packing problem on the basis of the best fit algorithm and the lowest front-line strategy

Nowadays, the wasting of resources is one of the fundamental challenges of the industrial sector. The purpose of this issue is to arrange a set of rectangles with specific dimensions in a rectangular page with a specific width and unlimited height without overlapping. The fundamental challenge in this issue is that this is an NP-complete issue. Therefore, it is difficult to achieve the best arrangement, which has the maximum rate of resource utilization and also has a linear running time. Many algorithms have been presented to estimate a practical solution for this issue. In the past decades, the best fit method has been one of the most useful methods for this purpose. This study presents a combinatorial algorithm based on two algorithms, including the lowest front-line strategy and the best-fit algorithm. The running results indicate that the suggested algorithm performs well, despite its simplicity. The time complexity of the suggested algorithm is O(nm), in which n is the number of input rectangles and m is the number of the created front lines. INDEX TERMS Rectangle packing problem, Best-fit algorithm, lowest front-line strategy


I. INTRODUCTION
Decreasing the wasting of resources in industry and business can be very advantageous. The rectangle packing problem is one of the most practical issues in the industrial sector. The rectangle packing problem has many uses in the industry, including cutting leather, wood, metals, paper, glass, cloth, and paperboard [7][8][9]32] for designing and manufacturing cars [1], fighters [12], and ships [11], and for designing VLSI [31,14,13,32] and in similar issues like timing and replacement [15,16]. It also has uses in the assignment and timing of the radio frequencies spectrum [15]. In the rectangle packing problem, we have a set of rectangles with specific dimensions and the target is to arrange these rectangles in a rectangular page with a constant width and unlimited height without overlapping, provided that the packing is orthogonal [33]; it means that the sides of the rectangles that are inserted into the rectangular page are parallel to the sides of the main page rectangle [14,17]. Also, the problem may not be guillotine cut; it means that we cannot place the replaced rectangles in several groups that each group is separated from the others by vertical or horizontal lines [7,31]. Rotation of the rectangles is acceptable, but the rotation angle could be only 90 degrees [18,19].
As stated, one of the fundamental challenges in the industry is waste reduction and in other words, increasing the utilization rate. Utilization rate equals to the sum of the area of the input rectangles divided by the area of the main page used and is represented by U [3,18,20]. Utilization can be calculated by equation (1): U= (1) In this equation, width represents the main page width, and height represents the main page height and and are width and height of the replaced rectangles, respectively, and m is the number of these rectangles [18].
Despite the full application of this issue, its practical implementation is a little difficult, because the most fundamental challenge of this issue is that this belongs to the NP-complete set [2][3][4][5][6]33]. Therefore, with an increase in the problem scale, achieving a practical solution for the problem in a suitable time could be difficult or even impossible. However, various estimated algorithms have been presented due to the full application of the rectangle packing problem and the conditions prevailing over it in the past four decades. In [35], comprehensive research has been done in this context. The best-fit algorithm is one of the most useful algorithms. The purpose of this study is to represent an algorithm for the rectangle packing problem with an optimal utilization rate and linear running time using the best-fit algorithm. This article will be structured as follows: in the second section, we will introduce the works done on the rectangle packing context and also the best-fit decreasing height and the lowest front line algorithms. In the third section, we will present our algorithm. In the fourth section, we will represent the results of algorithm implementation, and in the fifth section, we will conclude.

II. Background of the research
One of the algorithms that have attracted many researchers during the past four decades is the best-fit algorithm. It's a layered algorithm in which the layers are created gradually, as the rectangles are inserted. The rectangle places into the higher level if it can't be placed in a layer. The rectangle will always be located in a layer that has less waste after insertion [7]. Wei et al. [37] have presented an algorithm based on the best-fit algorithm with a branch & bound approach for the rectangle packing problem with constant dimensions. This algorithm has a reasonable utilization rate and also a linear running time. Despite a desirable utilization rate for more massive data, the running time of the algorithm increases. Huang et al. [27] have presented an algorithm based on two algorithms, including the best-fit algorithm and particle swarm optimization algorithm. They compared their method with the presented classical methods and got more efficient results. But the fundamental challenge of their approach is the long-running time of the algorithm. Bruke et al. [36] have presented an algorithm based on the best-fit algorithm, and this is a linear order algorithm. This algorithm has a reasonable utilization rate compared to the optimizing algorithms. The use of very similar data is one of the challenges of this algorithm. BL (bottom left) algorithm is one of the first algorithms presented for the packing problem, and Brenda [36] presented it. The purpose Of the BL algorithm is to place the rectangles at the lowest and leftmost points possible. This algorithm is suitable for little input, and its Running order is O ( ). Chazelle [30] have presented an algorithm called BLF (Bottom Left Fill). The number of possible modes for inserting a rectangle is decreased in the BLF algorithm. While in the BL algorithm, it is not possible to use the leftover spaces from placing the rectangles, in the BLF algorithm, the rectangle is placed at the lowest point possible. The BLF algorithm uses unused spaces. The time complexity of this algorithm is O( ). Liu and Teng [29] have presented an algorithm for the rectangle packing problem based on two algorithms, including the BL algorithm and the genetic algorithm. They improved the initial BL algorithm because it does not identify some of the modes. As the mode in which the rectangles are alternately big and small; the big rectangles have ascending sizes, and the small ones have descending scales. The positive point of this algorithm is the use of an intelligent crowd function for the genetic algorithm. The running order of this algorithm is O( ). Hu et al. [33] have presented an algorithm based on two algorithms, including best-fit and BL algorithms. This algorithm has all of the properties of the two mentioned algorithms. This algorithm categorizes the rectangles first and then places the created categories one by one. This method is suitable for rectangles with different sizes. The time complexity of this algorithm is O(mMlogM), in which m and M could be equal to the number of input rectangles. Liu et al. [18] have presented a solution for rectangle packing problem by combining the lowest front-line algorithm with the genetic optimization algorithm. Arrangement of the rectangles from the lowest point possible to the highest point is a prominent feature of this algorithm that reduces the space waste. It also utilizes a smart function to cross local optimum modes. The long running time of this algorithm for identifying and improving various modes is its fundamental challenge. In the following, two algorithms are introduced, including best-fit decreasing height algorithm and the lowest frontline strategy.

A. The lowest front-line strategy
The lowest front-line strategy is designed based on the BL algorithm and is defined as follows [18]: First step: the algorithm starts with the initial quantification of the front line. In the beginning, the front lines set contained only one line, and that is the horizontal line at the bottom of the page. Second step: locating the rectangle in a place using the lowest front-line strategy.
is the position where the i-th rectangle is placed. First, we select and check one line of the front line set to see whether the width of the rectangle that is going to place is equal to or smaller than the width of the selected line. If the conditions are right, that rectangle will be placed at the top left of the page. If the conditions are not right, we increase the height of the lowest line until it reaches the second lowest front line, and the conditions will be rechecked. This process repeats until one line of the front lines set gets selected. In this step, if several lines exist with the lowest heights, the selection will be based on the X-axis. If we increase height from a lower line to a higher line, these two lines will be merged. Third step: in this step, the front line will be updated. Some of the old lines will be converted to the new lines, and new lines will be added to the front-line set. As the points mentioned in the second step, the adjacent lines with equal height must be merged to form a single line. Fourth step: if all of the rectangles are placed, the algorithm ends, and otherwise, a new rectangle will be selected, and we will go to the second step.   The lowest front-line strategy does not sort the input rectangles before placing it, and this is a challenge for this algorithm. It increases page height and decreases the utilization rate. Also, this algorithm always tries to find the lowest and the leftmost point possible to place the rectangles. The way the height increases in this algorithm is its positive point.

B. the best-fit decreasing height method
In the best-fit decreasing height algorithm, first, the rectangles are sorted in decreasing order. Then the biggest rectangle will be placed at the bottom-left corner, and the first layer will be created. In order to place the next rectangles, if the rectangles could not be placed in the created spaces of the created layer, we create a layer above the highest layer and place the rectangle in it. If there are several layers with enough space to place the rectangles, a layer will be selected that has less waste after the rectangle placement [23,24]. The time complexity of this algorithm is O(nlogn) [25]. The best-fit decreasing height algorithm is layered and is suitable for non-guillotine cut issues. Finding the best position with the lowest waste is a benefit of this algorithm. Also, due to sorting the inputs, this algorithm has a better utilization rate compared to the best-fit algorithm without sorting. In this algorithm, we cannot place a rectangle between two layers, and this causes wasting. Therefore, we can place the rectangle between the layers in the proposed algorithm.
This study presents an algorithm that has the properties of both best-fit decreasing height algorithm and the lowest front-line strategy. This algorithm, similar to the best-fit method, always tries to find the best position for the rectangle. However, this algorithm tries to decrease the total height by increasing the height of the layers, inspired by the lowest front-line strategy.

III. The proposed algorithm
The steps of our algorithm are as follows: First step: at first, the algorithm starts with initial quantification. The height of this page is zero at the beginning, and its final value is specified at the end of the algorithm. In this algorithm, we have a set of lines called the front line for saving empty spaces. In the beginning, the front line is the bottom borderline of the page. The information of the input rectangles such as width, height, and the number of each one will be received at this stage of the algorithm.
In this algorithm, we use an array to save empty spaces. The first row of this frontline array is the starting point of the line, and the second row of the frontline shows the endpoint of the line. The third row of the frontline array represents the distance of this line from the lowest front line, which is the bottom borderline of the page. The fourth row of the frontline array shows the height of the next frontline after this line. With these four points, we can imagine a rectangle. If the value of the fifth row of the frontline array is 1, it means that this line is not occupied and when a rectangle is placed, this value will become -1.
The fifth row of the array is added to adhere to the notoverlapping assumption. When placing rectangles into a line using the fifth row of the frontline array, it will be checked whether this place is full or empty. Second step: in this step of the algorithm, the rectangles that have more height than width will be rotated 90 degrees. It means that the height and the width will be replaced. Third step: in this step, the rectangles will be sorted in decreasing height size order. Fourth step: a rectangle will be selected from the beginning of the inputs list, and then, we search among the frontlines set for a place that has enough space for the rectangle. Then among the selected lines, we choose a line that has less wasting. At this point, there are three modes: • If a line is found among the frontlines set that have sufficient width and height for placing the rectangle, we select it; and if there are several lines for placing, we choose the one that wastes less space. • If we cannot find a line with required height and width, we start searching among the longest frontlines for a line that has required width for placing the rectangle. Also, if there are several lines, we choose the one that has less width and increases the height of the page lesser. • If we cannot find a position to place the rectangle in the two predicted modes mentioned aboveif we did not find any line that has enough space for placing the rectanglethen we create a new layer above the longest frontline and place the rectangle in the bottom-left of the created layer and increase the height of the page. Fifth step: in this step, we update the frontline and empty spaces, and convert some of the old lines into new lines and also add new lines. Then in this step, we must identify all of the empty spaces. Then we must identify all of the empty houses that have common borders. In order for the algorithm to make the most of the wasted space available, if one or more than two houses have common borders, it must be merged with the house that creates larger free space after merging compared to other houses. This step plays an essential role in reducing waste. Sixth step: in this step, we check the ending of the algorithm. If there is not a rectangle in the input list to place, the algorithm ends. Otherwise, we go to the beginning of the fifth step and continue the algorithm again. In the end, we calculate the utilization rate of the algorithm. Figure 2 shows the flowchart of the algorithm process.

IV. The evaluation results
In this study, in addition to the proposed algorithm, we used the best-fit decreasing height algorithm [33] and the lowest frontline strategy [34] in the MATLAB programming language v.2014 and also a hardware with Intel(R) Core (TM) i7-4710HQ CPU @ 2.50GHz processor and 12 Gb internal storage.
We used the data in the Liu algorithm [18] for the evaluation of the algorithms. Then we compared the results of the three algorithms with the results in the Liu study [35][36][37][38][39][40][41][42][43][44]. Table 1 shows these data, and also the width of the rectangle page is 400 units.  Table 2 shows the results of these algorithms. The proposed algorithm in this study has the best performance among these algorithms. Our method reached the utilization rate of 94.37% in 0.023 seconds. Whereas the method presented in [45][46][47][48][49][50][51][52][53] reached the utilization rate average of 85.51% after 300 times repeating the algorithm and 26.3 seconds, and at best, it has reached the rate of 87.75%.  Figure 3 shows the utilization rate diagram of the three best-fit decreasing height, the lowest frontline strategy, and the proposed algorithms. In this diagram, the utilization rate percentage of every algorithm has been drawn after placing each rectangle. The horizontal axis represents the number of placed rectangles, and the vertical axis represents the utilization rate percentage.

A-the effects of the sorting and 90-degree rotation on the utilization rate
In this section, we analyze the effects of the sorting and rotation on the utilization rate. Sorting the input rectangles has a major impact on the utilization rate. We sorted the input rectangles in descending order of height, width, and area of the rectangles, and then we ran the algorithm. The results of running the algorithm show that sorting in descending order of height has better results. Table 3 shows the results of this comparison, and figure 5 shows the utilization rate diagram for various sorting modes.  We removed the rotation step and ran the algorithm and then compared the results with the algorithm with rotation. The results show that rotation has a positive effect on the utilization rate. Table 4 shows the results of this experiment. Figure 6 shows the diagram of the comparison between running the algorithm with and without the rotation.

B-comparing the algorithm with random data
Comparing the algorithm with low volume data and also, in some cases, comparing the algorithm with high similarity data, is one of the challenges of the presented algorithms. Practically, we are facing various data with different sizes in the industry. Therefore, in this section, we analyzed the proposed algorithm, the lowest frontline strategy, and the best-fit algorithm, by producing random data. The results of the running show the utilization rate of the proposed algorithm is better than the other two algorithms, and also it has a shorter running time. Table 5 shows the utilization rate and the running time of these algorithms. Also, figure 8 shows the running time diagram of these three algorithms.  Table 5: the results of the utilization rate of the proposed algorithm by producing random data

C-The time complexity
The time complexity of the proposed method is the polynomial order. It means that in the worst case, it needs O(m) time for searching between m frontlines and finding the proper line to place the rectangle. Since this searching must be done for all of the rectangles, and the total number of rectangles is n. Therefore, the time complexity of this algorithm is generally linear in time order and equals to O(nm). The results of the implementation indicate that the value of m is always less than n. It means that at the end of the algorithm implementation, the total number of the frontlines is always less than the total number of the rectangles. In table 5, the total number of rectangles column is n, and the total number of frontlines column is m, and m is always less than n. The diagram in figure 7 demonstrates how the number of frontlines increases per number of input data.

V. The evaluation results
In this study, we presented an algorithm for the rectangle packing problem. Also, we implemented the proposed algorithm along with the best-fit decreasing height and the lowest frontline strategy. Also, we compared the proposed algorithm with the algorithm presented in [18].
The results of the evaluation show the better utilization rate of the proposed algorithm in comparison with other mentioned algorithms. The proposed algorithm has improved the utilization rate by 86.72% in comparison with the lowest frontline strategy, 8.26% in comparison with the best-fit decreasing height algorithm, and 7.43% in comparison with the algorithm presented by Liu. The time complexity of the proposed algorithm is O(nm), in which n is the number of the input rectangles and m is the number of created front lines. Since this problem is NP-complete, there are modes that the presented algorithm cannot find them. Therefore, for future studies, we can use this algorithm along with optimizing algorithms like genetic algorithms. In the algorithm that we will present, we first consider a solution to the problem with the best-fit algorithm presented in this study. Then we improve the presented solution every time we repeat the genetic algorithm.