1

I understand the basics of the 0-1 problem and its solution.

I have a variant of it that I'm trying to solve in a decent way and I'm struggling on it, mostly because of the 0-weighted items. These are the rules:

  • I have k finite sets of items. Each item has a weight and a value.
  • I must take exactly one item from each set.
  • Zero-weighted items are allowed. But since you are not allowed to take more than one item from each set, it's not always convenient to take them.
  • Weights in each set are the first natural numbers {0, 1, 2, 3...i}.
  • In each set, values are non-decreasing when the weight increases.
  • I want to determine which items maximize the total value for a certain total weigth.

example:

Set Weight 0 Weight 1 Weight 2 Weight 3 Weight 4 Weight 5
A 3 6 6 9 15 26
B 0 6 8 - - -
C 1 1 10 15 16 -

The notation "B3" means "Select the object from set B that weights 3".

The solutions for a maximum total weight = n are:

  • n=0 -> [A0; B0; C0] => Total Value = 4
  • n=1 -> [A0; B1; C0] => Total Value = 10
  • n=2 -> [A0; B0; C2] OR [A1;B1;C0] => Total Value = 13
  • n=3 -> [A0; B1; C2] => Total Value = 19
  • n=4 -> [A0; B1; C3] => Total Value = 24
  • n=5 -> [A1; B1; C3] OR [A5;B0;C0] => Total Value = 27
  • n=6 -> [A5; B1; C0] => Total Value = 33
  • n=7 -> [A5; B0; C2] => Total Value = 36
  • n=8 -> [A5; B1; C2] => Total Value = 42
  • n=9 -> [A5; B1; C3] => Total Value = 47
  • n=10-> [A5; B2; C3] => Total Value = 49
  • n=11-> [A5; B2; C4] => Total Value = 50
  • n>=12 it's the same as n=11.

Do you have some clue for implementing a decent algorithm to find the best combination of items to solve this problem with a generic collection of such set? Thanks!

boulayo
  • 11
  • 2

2 Answers2

0

If weights aren't too large, use the standard pseudo-polynomial-time algorithm (using dynamic programming) for the knapsack problem. It is easy to adjust to this setting. See https://en.wikipedia.org/wiki/Knapsack_problem#0-1_knapsack_problem.

If weights can be large, use integer linear programming, and apply an off-the-shelf ILP solver.

D.W.
  • 167,959
  • 22
  • 232
  • 500
0

If I have realized the problem, I think this problem is more similar to matching problem than knapsack problem.

In other words, you can introduce a bipartite graph with two sets $S$ of objects (A, B, C, ...) and $T$ of weights ($0, 1, 2 ,...$) and a weighted arc between each object i to each weight j; e.g. weight of the edge $v_{A5}$ is equal to $26$ in your example.

enter image description here

Then, to produce a solution with maximum weight, you can not select more than one item from each weight (Otherwise, we don't need to solve any problem and the solution is to select the largest weights from each object). Therefore, you can solve the maximum matching on the proposed graph (if you want to select at most one item from each object and at most one item from each weight).

But, if you can select more than one item from each weight then you should add another constraint to the problem; e.g. Weighted sum of the selected objects is less than $k$. In this case, introduce decision variables $x_{ij}:=\{1: $ if we select object from set i that weights j , $0:$ Otherwise $\}$ and solve the following ILP model:

$min\ \sum_{i,j}w_{ij}x_{ij}\ $

  1. $\sum_{j}x_{ij} = 1 \ \ i\in S$
  2. $\sum_{i,j}w_{ij}x_{ij} \le k$
  3. $x_{ij} \in \{0,1\}$