Program Overview
This program is made for packing two dimensional rectangular elements at orthogonal table in sequence along X axis of table, with horizontal orientation exclusively. User interface is simple. At the top left are two fields for entering Table dimension and Pack button for starting packing process, below is table for entering Elements dimensions, and on the right is graphical representation of packed elements at table. For it to work it is necessary to have installed .NET framework 4.0.
Download links
Full solution with executable version of program and open source code with detailed comments download at links.
Problem Introduction
Solving problem of packing two dimensional rectangular elements at orthogonal table involves packing a certain number of elements at one table with fixed dimensions so that the table surface is maximally filled with elements and the time needed for that operation is as short as possible.
Considering the time necessary for solving this problem, it is classified as NP hard by mathematical nomenclature. Even though it is the 21st century and with all advances in math theory, there is no complete math solution for this problem. Here is shown development and implementation of one out of many possible algorithms as solutions for this problem, developed by using Heuristic methods and does not
represent final solution of this problem based on a comprehensive mathematical analysis.
Problem Solving Theory
Theory References
Observing process of packing elements
For purpose of observation there is a need for:
ONE TABLE { Tn(L, W), n=1 }
AND FINIT SEQUENCE OF ELEMENTS { En(l, w), nЄZ and n>0}
TABLE and ELEMENTS are characterized by it's LENGTH and WIDTH.
These two characteristics with it's values are called DIMENSIONS.
Therm 'LENGTH' do not imply greater value in the relation to therm 'WIDTH'.
Allowed range for value of dimensions:
T(L, W), {LЄR and L>0} , {WЄR and W>0}
E(l, w), {lЄR and l>0 and l<=L} , {wЄR and w>0 and w<=W}
LENGTH of element can't be greater than LENGTH of TABLE.
WIDTH of element can't be greater than WIDTH of TABLE.
Because of precision, whole packing process shall be observed within the XY orthogonal coordinate system.
When placed inside coordinate sistem, TABLE and ELEMENTS obtains third characteristic ORIENTATION – POSITION OF SIDES OF TABLE OR ELEMENT, RELATIVE TO X OR Y AXIS. Considering numerus positions that table or elements can take, there are two distinctive for which ORIENTATION gets defined value : HORIZONTAL and VERTICAL.
HORIZONTAL orientation is case when longer side of table or element is parallel to X axis.
VERTICAL orientation is case when longer edge of table or element is parallel to Y axis.
Graphical presentation of five cases of TABLE with packed ELEMENTS inside XY orthogonal coordinate system.
After these five attempts of packing elements there are several important conclusions:
- Table can be positioned anywhere inside coordinate system horizontally or vertically.
- Orientation and layout of the elements before packing are irrelevant.
- Elements can be rotated and packed horizontally or vertically.
- Packing elements at the table can be random or in sequence along the X or Y axis at free surface of table, called POSITION for packing.
- Therm 'POSITION', free surface of the table, denotes part of the table that is not already occupied with some other packed element and where can be placed any of remaining elements. There can not be overlapping of elements at surface of the table.
- Shape of the POSITON does not necessarily have to be rectangular as elements.
- Selection of the next element that is going to be packed, depends on the possibility of element to be fitted inside observed position.
- When there is more than one element that can be placed at selected position the choice is random.
- Packing elements should be compact, side by side, matching dimensions of elements and dimensions of positions so that utilization of the surface of table is maximum.
- Process of packing is finished when:
- There is no more elements to be packed, all elements are packed on the table.
- Table is completely filled, sum of areas of packed elements is equal to the area of the table.
- There is no more positions on the table for packing elements, at remaining free surface of the table is not possible to fit any of remaining elements.
Developing algorithm
Algorithm that should be developed upon above observations is too complex, so for further study of this problem it is necessary to introduce some restriction.
- Before packing process starts, table shall be placed at the origin of coordinate system with horizontal orientation.
- Before packing process starts, all elements must be horizontally oriented and sorted in descending order by size of it's area then by length then by width.
- Elements can not rotate during packing process.
- Elements must be packed in sequence along X axis of table, with horizontal orientation exclusively.
- Coordinates of the first position are always coordinates of the origin, since that at the beginning of packing process table is empty and it's whole surface is free.
- Conditions for finding new free positions according to demand for a way of packing elements.
- Position must be rectangular shaped, with determined coordinates of lower left corner, and dimension that corresponds at least to one of the remaining elements. At least one of the remaining elements should be able to fit in position.
- Coordinates of new position can only be the same as coordinates of lover right or upper left corner of previously packed element.
- If the position have different shape than rectangular it is necessary to adjust it. It is done by making new positions out of old one, which must fulfill previously two conditions. If newly created positions do not fulfill conditions they are discarded.
- Order of positions is determined by sorting values of coordinates of lover left corner of position, in ascending order, first by Y than by X axis. This way elements are packed in the way as specified.
- When there is no more free positions, packing process ends.
- Conditions for selecting next element for packing
- Selection of next element that is going to be packed depends of the next characteristic of packed element - FITTING FACTOR and it's calculated numeral value for selected position, which defines possibility of element to be fitted inside observed position. The greater value of this characteristic means that element better fits to the observed position. BEST FIT therm describes FITTING FACTOR value of element with equal length and width as observed position, a 100% filled position.
- Next element that is going to be packed, is selected upon calculated value for FITTING FACTOR for selected position.
- Initial fitting factor of all elements for the observed position is zero.
- If element with it's dimensions can fit inside position, fitting factor is increased by one.
- If element have equal length as position, fitting factor is increased by one, favoring element that fits exactly across the length of position.
- If element have equal width as position, fitting factor is increased by one, favoring element that fits exactly across the width of position.
- When fitting factor is calculated for all elements, they are sorted in descending sort order first by fitting factor value, than by value of area, then by value of length, then by value of width.
- First element in sorted sequence is selected and if it's fitting factor is greater than zero, it is placed at selected position so that the bottom left corner of the element matches the lower left corner of position.
- If value of fitting factor of first element in sorted sequence is equal zero, selected position is discarded due to lack of fitting element.
- When there is no more elements, packing process ends.
- This algorithm do not need check if table is completely filled for stopping process.
Graphical presentation of packing process step by step, according to above restriction.
- Table is placed at the origin of coordinate system and elements are sorted in descending order by size of its area.
- Selecting first position and packing one element in it.
- Since that at the beginning of packing process table is empty, first free position, Position 1, is whole area of table with it's lower left corner placed at origin.
- Coordinates of Position 1 are coordinates of the origin.
- Calculated value for fitting factor for all four elements is one, so the one with the greater size of area is selected to be packed, and it is element E1.
- Selected element E1 is placed at coordinates of selected Position 1.
- Finding new positions after placing element.
- Free surface on table does not meet the requirement for new position because of it's shape and it needs to be adjusted. Accordingly to request for packing direction, along X axis of table, with horizontal orientation exclusively, Position 1 is splited into two rectangular shapes with horizontal line from the upper right corner of the last placed element to the right edge of Position 1.
- Now there are two new positions, Position 1 and Position 2, with it's coordinates that are aligned accordingly to condition for sorting positions. Previous Position 1 is erased from the list of positions.
- Coordinates of new Position 1 are coordinates of lower right corner of E1.
- Coordinates of new Position 2 are coordinates of upper left corner of E1.
- Packing new element.
- Selected position is Position 1.
- Calculated value for fitting factor for all three elements is one, so the one with the greater size of area is selected to be packed, and it is element E4.
- Selected element E4 is placed at coordinates of selected Position 1.
- Finding new positions
- Situation on the table is similar to situation after placing first element, so the steps are also similar. Divide Position 1 into two rectangular shapes with horizontal line from the upper right corner of the last placed element to the right edge of Position 1.
- There are now two new positions and one old, with it's coordinates that are aligned accordingly to condition for sorting positions.
- Position 1 with coordinates at lower right corner of E4.
- Position 2 with coordinates at upper left corner of E4.
- Position 3, former position 2, with coordinates at upper left corner of E1.
- INADEQUATE POSITION CASE !
- Position 2 is inadequate because of it's width and shall be discarded. There is no element left that can fit across width of Position 2.
- After erasing position 2, Position 1 shall be adjusted as shown below.
- Colored in white is part of discarded Position 2, and unused space of table.
- Position 3 is now Position 2.
- Process is continued by packing the last two elements and finding new positions.
- Process has ended because there is no more free elements. All elements are packed.
Finally packed elements on table
After analysis here is written universal algorithm
Program code
Implementation of algorithm shown above in programming language C# 4.0, .Net framework 4.0 as independent class with it's public method that requires three arguments and returns one:
public DataGridView Pack_Elements(int Table_length, int Table_width, DataGridView Elements)
DataGridView with it's values that is going to be passed as argument to method, has to be the same structure as DataGridView ELEMENT_LIST declared inside class.
Values that are going to be passed to method, must be arranged and sorted accordingly to packing process demands,
length of table and elements must be greater than width, so that elements are packed with horizontal orientation.
If there is need for different orientation values should be arranged accordingly.
After finishing packing process, method returns DataGridView TABLE_LIST as argument to caller method, that contains coordinates of positions and dimensions of packed elements, that can be used for graphical presentation of packed elements at orthogonal table. Program code below is just an example made for learning. For a more detailed analysis it is better to download full project with open source code.
Code
- using System;
- using System.ComponentModel;
- using System.Windows.Forms;
- namespace Packing_two_dimensional_rectangular_elements_at_orthogonal_table
- {
- public class Pack_Elements_Along_X_Horizontally
- {
-
-
-
- DataGridView ELEMENT_LIST;
- DataGridView POSITION_LIST;
- DataGridView TABLE_LIST;
-
-
-
- int Tl, Tw;
- int n, k, p, i;
- int x, l, w, a, f;
- int Lmin, Wmin;
- int X11, Y11, L11, W11, A11;
- int X12, Y12, L12, W12, A12;
- int p_X;
- int p_Y;
- int Pl;
- int Pw;
- bool P_ok;
- const string format = "0000000000";
-
-
-
- void Initialize_Tables()
- {
-
-
-
- ELEMENT_LIST = new DataGridView();
-
-
-
-
-
- ELEMENT_LIST.Columns.Add("E_Index", "Element index");
- ELEMENT_LIST.Columns.Add("E_Length", "Element length");
- ELEMENT_LIST.Columns.Add("E_Width", "Element width");
- ELEMENT_LIST.Columns.Add("E_Area", "Element area");
- ELEMENT_LIST.Columns.Add("E_Fitting", "Element fiting faktor");
-
-
-
- ELEMENT_LIST.SortCompare +=
- new System.Windows.Forms.DataGridViewSortCompareEventHandler(E_SortCompare);
-
-
-
-
-
-
- POSITION_LIST = new DataGridView();
-
-
-
-
-
- POSITION_LIST.Columns.Add("P_X", "Position X");
- POSITION_LIST.Columns.Add("P_Y", "Position Y");
- POSITION_LIST.Columns.Add("P_Length", "Position length");
- POSITION_LIST.Columns.Add("P_Width", "Position width");
-
-
-
- POSITION_LIST.SortCompare +=
- new System.Windows.Forms.DataGridViewSortCompareEventHandler(P_SortCompare);
-
-
-
-
-
-
- TABLE_LIST = new DataGridView();
-
-
-
-
-
- TABLE_LIST.Columns.Add("E_I", "Element index");
- TABLE_LIST.Columns.Add("E_X", "Element X");
- TABLE_LIST.Columns.Add("E_Y", "Element Y");
- TABLE_LIST.Columns.Add("E_Length", "Element length");
- TABLE_LIST.Columns.Add("E_Width", "Element width");
-
-
-
- }
- void Initialize_Variables()
- {
- Tl = 0;
- Tw = 0;
- n = 0;
- k = 0;
- p = 0;
- i = 0;
- x = 0;
- l = 0;
- w = 0;
- a = 0;
- f = 0;
- Lmin = 0;
- Wmin = 0;
- X11 = 0;
- Y11 = 0;
- L11 = 0;
- W11 = 0;
- A11 = 0;
- X12 = 0;
- Y12 = 0;
- L12 = 0;
- W12 = 0;
- A12 = 0;
- p_X = 0;
- p_Y = 0;
- Pl = 0;
- Pw = 0;
- P_ok = false;
- }
- void E_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
- {
-
-
-
-
-
-
-
-
- e.SortResult = System.String.CompareOrdinal(
- e.CellValue1.ToString(), e.CellValue2.ToString());
-
-
- if (e.SortResult == 0)
- {
- e.SortResult = System.String.CompareOrdinal(
- ELEMENT_LIST.Rows[e.RowIndex1].Cells[3].Value.ToString(),
- ELEMENT_LIST.Rows[e.RowIndex2].Cells[3].Value.ToString());
-
-
- if (e.SortResult == 0)
- {
- e.SortResult = System.String.CompareOrdinal(
- ELEMENT_LIST.Rows[e.RowIndex1].Cells[1].Value.ToString(),
- ELEMENT_LIST.Rows[e.RowIndex2].Cells[1].Value.ToString());
-
-
- if (e.SortResult == 0)
- {
- e.SortResult = System.String.CompareOrdinal(
- ELEMENT_LIST.Rows[e.RowIndex1].Cells[2].Value.ToString(),
- ELEMENT_LIST.Rows[e.RowIndex2].Cells[2].Value.ToString());
-
-
-
- if (e.SortResult == 0)
- {
- e.SortResult = System.String.CompareOrdinal(
- ELEMENT_LIST.Rows[e.RowIndex2].Cells[0].Value.ToString(),
- ELEMENT_LIST.Rows[e.RowIndex1].Cells[0].Value.ToString());
- }
- }
- }
- }
- e.Handled = true;
- }
- void P_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
- {
-
-
-
-
-
-
-
- e.SortResult = System.String.CompareOrdinal(
- e.CellValue1.ToString(), e.CellValue2.ToString());
-
-
- if (e.SortResult == 0)
- {
- e.SortResult = System.String.CompareOrdinal(
- POSITION_LIST.Rows[e.RowIndex1].Cells[0].Value.ToString(),
- POSITION_LIST.Rows[e.RowIndex2].Cells[0].Value.ToString());
- }
- e.Handled = true;
- }
- public DataGridView Pack_Elements(int Table_length, int Table_width, DataGridView Elements)
- {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Initialize_Tables();
-
-
-
- Initialize_Variables();
-
-
-
- Tl = Table_length;
- Tw = Table_width;
-
-
-
-
-
- n = 0;
-
- i = 0;
-
- for (i = 0; i < Elements.RowCount; i++)
- {
- if (Elements[1, i].Value != null)
- {
- ELEMENT_LIST.Rows.Add();
- ELEMENT_LIST[0, i].Value = (int.Parse(Elements[0, i].Value.ToString())).ToString(format);
- ELEMENT_LIST[1, i].Value = (int.Parse(Elements[1, i].Value.ToString())).ToString(format);
- ELEMENT_LIST[2, i].Value = (int.Parse(Elements[2, i].Value.ToString())).ToString(format);
- ELEMENT_LIST[3, i].Value = (int.Parse(Elements[3, i].Value.ToString())).ToString(format);
- ELEMENT_LIST[4, i].Value = (int.Parse(Elements[4, i].Value.ToString())).ToString(format);
-
- n++;
- }
- }
-
-
-
-
- ELEMENT_LIST.Sort(ELEMENT_LIST.Columns[3], ListSortDirection.Descending);
-
- p_X = 0;
- p_Y = 0;
-
-
-
-
- POSITION_LIST.Rows.Add(p_X.ToString(format),
- p_Y.ToString(format),
- Tl.ToString(format),
- Tw.ToString(format));
-
- k = 1;
-
- p = 0;
-
-
-
-
- while (k > 0 && n > 0)
- {
-
- POSITION_LIST.Sort(POSITION_LIST.Columns[1], ListSortDirection.Ascending);
-
- Pl = int.Parse(POSITION_LIST[2, 0].Value.ToString());
- Pw = int.Parse(POSITION_LIST[3, 0].Value.ToString());
-
- for (i = 0; i < n; i++)
- {
-
- l = int.Parse(ELEMENT_LIST[1, i].Value.ToString());
-
- w = int.Parse(ELEMENT_LIST[2, i].Value.ToString());
-
- f = 0;
-
- if (Pl < l || Pw < w)
- {
-
- f = 0;
- }
- else
- {
-
- f++;
-
- if (Pl == l)
- {
-
- f++;
- }
-
- if (Pw == w)
- {
-
- f++;
- }
- }
-
- ELEMENT_LIST[4, i].Value = f.ToString(format);
- }
-
-
-
-
- ELEMENT_LIST.Sort(ELEMENT_LIST.Columns[4], ListSortDirection.Descending);
-
-
-
- f = int.Parse(ELEMENT_LIST[4, 0].Value.ToString());
-
-
-
-
- if (f > 0)
- {
-
-
-
- p_X = int.Parse(POSITION_LIST[0, 0].Value.ToString());
- p_Y = int.Parse(POSITION_LIST[1, 0].Value.ToString());
-
- x = int.Parse(ELEMENT_LIST[0, 0].Value.ToString());
- l = int.Parse(ELEMENT_LIST[1, 0].Value.ToString());
- w = int.Parse(ELEMENT_LIST[2, 0].Value.ToString());
-
-
-
-
-
- TABLE_LIST.Rows.Add();
- TABLE_LIST[0, p].Value = x.ToString();
- TABLE_LIST[1, p].Value = p_X.ToString();
- TABLE_LIST[2, p].Value = p_Y.ToString();
- TABLE_LIST[3, p].Value = l.ToString();
- TABLE_LIST[4, p].Value = w.ToString();
-
- p++;
-
- ELEMENT_LIST.Rows.RemoveAt(0);
-
- n--;
-
-
-
-
- if (n > 0)
- {
-
-
-
-
-
-
-
-
-
-
-
- ELEMENT_LIST.Sort(ELEMENT_LIST.Columns[3], ListSortDirection.Descending);
-
- P_ok = true;
-
-
-
-
-
- if (Pw > w)
- {
-
-
-
- X12 = 0;
- Y12 = 0;
- L12 = 0;
- W12 = 0;
- A12 = 0;
-
- P_ok = false;
-
-
-
- X12 = p_X;
- Y12 = p_Y + w;
-
-
-
- L12 = Tl - X12;
- W12 = Pw - w;
- A12 = L12 * W12;
-
-
-
-
- i = n - 1;
-
-
-
- a = int.Parse(ELEMENT_LIST[3, i].Value.ToString());
- while (a <= A12 && i > -1)
- {
-
-
-
- a = int.Parse(ELEMENT_LIST[3, i].Value.ToString());
-
-
-
- Lmin = int.Parse(ELEMENT_LIST[1, i].Value.ToString());
- Wmin = int.Parse(ELEMENT_LIST[2, i].Value.ToString());
-
-
-
-
-
- if (L12 >= Lmin && W12 >= Wmin)
- {
- POSITION_LIST.Rows.Add(X12.ToString(format),
- Y12.ToString(format),
- L12.ToString(format),
- W12.ToString(format));
-
- k++;
-
- P_ok = true;
-
- i = -1;
- }
-
- i--;
- }
- }
-
-
-
-
-
- if (Pl > l)
- {
-
-
-
- X11 = 0;
- Y11 = 0;
- L11 = 0;
- W11 = 0;
- A11 = 0;
-
-
-
- X11 = p_X + l;
- Y11 = p_Y;
-
-
-
- L11 = Tl - X11;
- W11 = w;
- A11 = L11 * W11;
-
-
-
-
- if (!P_ok)
- {
- W11 = W11 + W12;
- A11 = L11 * W11
- }
-
-
-
-
- i = n - 1;
-
-
-
- a = int.Parse(ELEMENT_LIST[3, i].Value.ToString());
- while (a <= A11 && i > -1)
- {
-
-
-
- a = int.Parse(ELEMENT_LIST[3, i].Value.ToString());
-
-
-
- Lmin = int.Parse(ELEMENT_LIST[1, i].Value.ToString());
- Wmin = int.Parse(ELEMENT_LIST[2, i].Value.ToString());
-
-
-
-
-
- if (L11 >= Lmin && W11 >= Wmin)
- {
- POSITION_LIST.Rows.Add(X11.ToString(format),
- Y11.ToString(format),
- L11.ToString(format),
- W11.ToString(format));
-
- k++;
-
- i = -1;
- }
-
- i--;
- }
- }
- }
- }
-
-
-
- if (n > 0)
- {
-
-
-
- POSITION_LIST.Rows.RemoveAt(0);
-
- k--;
- }
- }
-
-
-
- return TABLE_LIST;
- }
- }
- }
Conclusion
This article shows that by using common algorithm development and programming knowledge, we can find a solution for very complex mathematical problem without need of knowing complex math theory for describing such problem.
Algorithm and program code developed inside this article solves only one type of packing problem, packing two dimensional rectangular elements at orthogonal table in sequence along X axis of table, with horizontal orientation exclusively and can be easily changed to solve the same problem with different element orientation, or withthe possibility that elements can rotate during packing process or to change axes along with elements that will be packed.
It can also be a good starting point for any deeper mathematical analysis of such problem.