Results 1 to 5 of 5

Thread: knapsack program

  1. #1
    andrews is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 4.0
    Join Date
    Nov 2011
    Posts
    112
    Reputation
    70

    knapsack program

    Hi,

    I have following program for solving the simple solution for the knapsack 0-1 problem.
    The code must contains a fault because the solutions are not good, but where is it ?

    Dim capacity As Integer = 16
    Dim size() As Integer = {3, 4, 7, 8, 9}
    Dim values() As Integer = {4, 5, 10, 11, 13}
    Dim totval(capacity) As Integer
    Dim best(capacity) As Integer
    Dim n As Integer = values.Length
    Dim i, j As Integer
    For j = 0 To n - 1
    For i = 0 To capacity
    If (i >= size(j)) Then
    If (totval(i) < totval(i - size(j)) + values(j)) Then
    totval(i) = totval(i - size(j)) + values(j)
    best(i) = j
    End If
    End If
    Next
    Next


    MessageBox.Show(totval(capacity).ToString) ' gives the solution value


    Dim totcap As Integer = 0
    i = capacity


    While (totcap <= capacity)
    MessageBox.Show(best(i).ToString) ' gives the choosen items
    totcap += values(best(i))
    i -= 1
    End While


    End Sub

    Is there someone who can give the right code?
    Thanks for any response

  2. #2
    IanRyder is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Sep 2012
    Location
    Healing, NE Lincs, UK
    Posts
    1,128
    Reputation
    744
    Hi,

    I have following program for solving the simple solution for the knapsack 0-1 problem. The code must contains a fault because the solutions are not good, but where is it ?
    I would love to try and help here but my own Knapsack program only adds Beef Sandwiches, a bit of Fruit, a few Biscuits and a Bottle of Pop whenever I run it which I do not think will help since your Knapsack program seems to be using numbers?

    If you would like to add an explanation of what your "Knapsack 0-1" program is supposed to be doing, along with what it's currently doing and what's going wrong then I am sure that someone here may be able to give you some valuable advice.

    Cheers,

    Ian

    BTW, I am sure you have made enough posts by now to know that when posting code you should be using Code tags for readability?

  3. #3
    andrews is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 4.0
    Join Date
    Nov 2011
    Posts
    112
    Reputation
    70
    Ian,
    The Beef Sandwiches, a bit of Fruit, a few Biscuits and a Bottle of Pop are in my program the indexes of the arrays size and values.
    If somebody could help, knowing well the knapsack problem, I suppose that there must no explanation what I am expecting from this program.
    But what is going wrong in the code I do not know , but I know that the results are not good.
    I am looking now to another program who seems to be better but the data structures are different but I am not still ready.
    Ian, thanks for your response

  4. #4
    Herman is offline VB.NET Forum Idol
    .NET Framework
    .NET 4.0
    Join Date
    Oct 2011
    Location
    Montreal, QC, CA
    Posts
    866
    Reputation
    1212

  5. #5
    andrews is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 4.0
    Join Date
    Nov 2011
    Posts
    112
    Reputation
    70
    Yes, it is this I am trying but I have also to know what items are choosen.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •