java - Knapsack algorithm optimized for weight instead of values -
is possible modify 1-0 knapsack algorithm optimize final total weight of items in bags first choice (and values second choice), maintaining same algorithmic complexity?
i'm working on this java implementation (in end of article).
more specifically, i'm thinking of changing piece of code
if (wt[item-1]<=weight){ v[item][weight]=math.max (val[item-1]+v[item-1][weight-wt[item-1]], v[item-1][weight]); }else{ v[item][weight]=v[item-1][weight]; }
with other condition firstly control if weight closer threshold adding item , than, if weight not change, if value better.
have got idea how without change complexity?
thank you
edit with "firstly control if weight closer threshold adding item" mean reaching weight limit of backpack. in other words "maximizing weight can carry in bag" without breaking it
are trying following? choose items weight maximized, while still respecting weight limit. if there multiple optimal solutions each achieve maximum possible weight, choose among them choosing solution has largest total value.
if so, suggest following. (i'm thinking knapsack problem , not java implementation of it.)
let m
= maximum value [edited] among items , n
= number of items. replace each value (in objective function) weight + value/mn
.
then model maximize total weight, while still respecting weight limit. , if there multiple solutions same optimal weight, choose 1 maximum value. dividing mn
ensures you'll never choose solution better value @ expense of worse weight.
Comments
Post a Comment