jmyrberg / mknapsack

Algorithms for solving knapsack problems with Python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Suggestion to improve README and wrong result

fredericoschardong opened this issue · comments

What is the meaning of the output?

The testcase in the README resulted in

Total profit: 376
Solution: [0, 0, 1, -1, 0, -1, -1, -1, 1, -1]
Number of backtracks performed: 29
Global optimum: 1

I presume the solution 0,1 means which bag and -1 means not included. Is that correct? Moreover, what does global optimum mean?

If I am not mistaken, that would mean the selected weights are: 18+9+23+59+76=185 and total profit is 78+35+89+94+80=376. That seems to be it. It would be good to have some sort of explanation like this in the README.

However, the aforementioned result is not optimal. Apparently the solution would be the following:

weights: 18+9+23+20+59+61=190
profit: 78+35+89+36+94+75=407

Am I doing anything wrong?

Thank you for your post.

Let's address the results interpretation first. You can find some answers with the help of docstring of the mtm function by typing help(mtm) in Python. You're correct about the solution vector, -1 means that the item is not included in any knapsack, and otherwise the number corresponds to the index of the knapsack. Global optimum means that the algorithm should've found the global optimal solution (1=yes, 0=no). This is not the case when the algorithm hasn't had time to finish all the backtracks, meaning that the current best solution found is returned. This can happen, for example, if the problem is very big, or if the max_time parameter has been set to a small number.

On the second point, you're correct that the weights and profits for the two knapsacks are as follows:

0: w=18+9+59=86 < 90, z=78+35+94=207
1: w=23+76=99 < 100, z=89+80=169

However, the solution you suggest is not viable due to exceeding the capacity of knapsack 1:

0: w=18+9+59=86 < 90, z=78+35+94=207
1: w=23+20+61=104 > 100, z=89+36+75=200

Hope this helps.

Thank you. I agree with all your comments.