Bin packing algorithm



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 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() + ” ”
                    itemw = itw.ToString()
                End If

                objitemweight += itemw


            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)
                End If


            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)

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

                        BestBinAmount = BinCount
                        BestBin = j
                    End If


                If BestBin = -1 Then
                    ReDim Preserve bins(BinNumber + 1)
                    BinNumber += 1
                    ReDim bins(BinNumber)(1)
                    BinElement = 0
                    bins(BinNumber)(BinElement) = elementnumber(i)
                    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




Happy programming


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s