Talk:Knapsack problem/Bounded: Difference between revisions

section removed
(section removed)
 
(13 intermediate revisions by 4 users not shown)
Line 15:
When computing maximum values, I in essence performed a small inner loop where I would (in the case of water) compute the maximum value for both 1 water and 2 waters. This was a trivial extension of the wikipedia algorithm (2 waters weighs twice what 1 water would weigh). And, basically, once the weight of a multiple item choice exceeds the permissible weight I just repeated the biggest previous value for fewer items.
 
A related problem is keeping track of the "best choice so far". There might be some clever way of extracting it from the values cache, but I used a best choice cache. For item <math>ij</math> which has maximum possible count <math>p_ip_j</math> and current best count <math>c_ic_j</math>, I represent the best combination so far as <math>c_i\sum c_j + P_iP_j</math> where <math>P_iP_j = \prod 1+p_jp_k</math> where <math>jk<ij</math>. (This means that when I am considering various options I can ignore an option by multiplying it by 0 and then selecting the largest remaining <math>c_ic_j</math>.)
 
In other words 0 means "no items", 1 means "1 map", 2 means "1 compass", 3 means "1 map and 1 compass", 4 means "1 water", 8 means "2 waters", 9 means "2 waters and 1 map", 12 means "1 sandwich", and 16 means "1 sandwich and 1 water". And, since I am taking tiny steps at each step of coursecomputation, I cache <math>P_iP_j</math> rather than recomputing it every time I need it. (You might think of this as a generalization of base n arithmetic where each digit position can be in an independent base (except that these "digits" are probably best thought of as being arranged from left-to-right instead of the usual right-to-left).) --[[User:Rdm|Rdm]] 21:20-ish, 27 June 2011 (UTC)
 
:Some thoughts about caching the best choice: if you don't and just calculate the best value, and after which you want the list of chosen items, you'd have to solve <math>\sum v_j c_j = v_{\rm total}</math> with the constraint <math>c_j \leq n_j</math> -- that's a reduced bounded knapsack problem in itself, a somewhat amusing situation. --[[User:Ledrug|Ledrug]] 21:22, 27 June 2011 (UTC)
 
== OOCalc? ==
 
Currently the OOCalc implementation claims that it is trying to minimize the total value of items contained in the pack. Shouldn't it be trying to maximize that instead? --[[User:Rdm|Rdm]] 15:54, 28 June 2011 (UTC)
:Yep, the image was correct, I just wrote the wrong thing in the accompanying text. Fixed now, thanks. --[[User:Paddy3118|Paddy3118]] 18:28, 28 June 2011 (UTC)
7,794

edits