Tag Archives: bins and packs

Bin packing algorithm

binpacking

binpacking

People working with ERP systems and applications related to them would have come across the

Bin packing problem.usually this is solved using knapsack algorithm. But it doesnt gives correct solution.

Here is the implementation of bin packing algorithm in VB.net.Packing boxes with maximum height limits can be done.

The methodology will be so efficient that the solution will be best fit for the boxes.

————————————————————————————

 

        If IsNumeric(cbobox.Text) Then
            Dim check As String
            Dim bins()() As Integer ‘the bin elements stored as two dimensional array representing bin number and the weights
            Dim item As Integer
            item = objds.Tables(0).Rows.Count – 1
            Dim h As Integer
            Dim itw As Integer = 0
            Dim itemw As String
            Dim objitemweight As String = String.Empty
            Dim it As Integer = 0
            Dim itno As String
            Dim itemno As String
            Dim itremove As ArrayList = New ArrayList()
  
  For h = 0 To item
                itw = grdDisplayitems.Item(2, h).Value
                If (h <> item) Then
                    itemw = itw.ToString() + ” ”
                Else
                    itemw = itw.ToString()
                End If

                objitemweight += itemw

            Next

         
            Dim elements() As String
            Dim elementnumber(elements.GetUpperBound(0)) As Integer
            Dim v As Integer
            For v = 0 To elements.GetUpperBound(0)

                If IsNumeric(elements(v)) Then
                    elementnumber(v) = CType(elements(v), Integer)
                Else
                    ‘MessageBox.Show(“error”)
                End If

            Next

            ReDim bins(0)
            Dim BinNumber, BinElement, BinCount As Integer
            Dim BestBin, BestBinAmount As Integer
            Dim i, j, k As Integer

          ‘If required make a copy of array elements and proceed

            ReDim bins(0)(0)
            For i = 0 To elementnumber.GetUpperBound(0)
                BestBin = -1
                BestBinAmount = -1

                For j = 0 To BinNumber

                    BinElement = bins(j).GetUpperBound(0)
                    BinCount = 0
                    For k = 0 To BinElement
                        BinCount += bins(j)(k)
                    Next

                    If BestBinAmount < BinCount AndAlso BinCount + elementnumber(i) <= cbobox.Text Then

                        BestBinAmount = BinCount
                        BestBin = j
                    End If

                Next

                If BestBin = -1 Then
                    ReDim Preserve bins(BinNumber + 1)
                    BinNumber += 1
                    ReDim bins(BinNumber)(1)
                    BinElement = 0
                    bins(BinNumber)(BinElement) = elementnumber(i)
                Else
                    ‘bins(BestBin)(BinElement).ToString()
                    BinElement = bins(BestBin).GetUpperBound(0)
                    ReDim Preserve bins(BestBin)(BinElement + 1) ‘This is one of the best thing in vb apart from c# redim statement
                    bins(BestBin)(BinElement) = elementnumber(i)
                  
                End If

            Next

 

————————————————————————————

Happy programming