Knapsack with continuous (non distinct) constraint

后端 未结 7 660
南方客
南方客 2021-02-03 12:05

I watched Dynamic Programming - Kapsack Problem (YouTube). However, I am solving a slightly different problem where the constraint is the budget, price, in double, not integer.

7条回答
  •  自闭症患者
    2021-02-03 12:55

    The answer to your question depends on several factors:

    1. How large is the value of constraint (if scaled to cants and converted to integers).
    2. How many items are there.
    3. What kind of knapsack problem is to be solved
    4. What is required precision.

    If you have very large constraint value (much more than millions) and very many items (much more than thousands)

    Then the only option is Greedy approximation algorithm. Sort the items in decreasing order of value per unit of weight and pack them in this order.

    If you want to use a simple algorithm and do not require high precision

    Then again try to use greedy algorithm. "Satisfaction value" itself may be very rough approximation, so why bother inventing complex solutions when simple approximation may be enough.

    If you have very large (or even continuous) constraint value but pretty small number of items (less than thousands)

    Then use branch and bound approach. You don't need to implement it from scratch. Try GNU GLPK. Its branch-and-cut solver is not perfect, but may be enough to solve small problems.

    If both constraint value and number of items are small

    Use any approach (DP, branch and bound, or just brute-force).

    If constraint value is pretty small (less than millions) but there are too many (like millions) items

    Then DP algorithms are possible.

    Simplest case is the unbounded knapsack problem when there is no upper bound on the number of copies of each kind of item. This wikipedia article contains a good description how to simplify the problem: Dominance relations in the UKP and how to solve it: Unbounded knapsack problem.

    More difficult is the 0-1 knapsack problem when you can pack each kind of item only zero times or one time. And the bounded knapsack problem, allowing to pack each kind of item up to some integer limit times is even more difficult. Internet offers lots of implementations for these problems, there are several suggestions in the same article. But I don't know which one is good or bad.

提交回复
热议问题