0

I have N(for example 30) integer numbers V[i], and M(for example 8) packs, each pack have an expected value P[j].

I want to assign each integer number to one pack, the following expression calculate the difference between the sum of V[k] that in pack j and the expected value of pack j.

diff[j] = abs(P[j] - sum(V[k] that in pack j))

The target is to find the best solution that minimize sum(diff[j]).

I don't know what's the type of this kind of problem. Can this be solved by Linear programming, or is it a NP-Complete problem?

Ali
  • 56,466
  • 29
  • 168
  • 265
HYRY
  • 94,853
  • 25
  • 187
  • 187
  • I doubt this is what you meant, but I should point out that "linear programming" and "NP-Complete" do not come close to partitioning the problem space. – murgatroid99 Jun 27 '14 at 23:44

3 Answers3

2

Regardless of whether this is NP-hard or not, you may be able to efficiently solve your problem for the problem instances you need using easily accessible integer programming software. For your problem, you could define x_{ij} to define if X[i] is assigned to group j. You would then also define variables d_j, which are the diff[j] in your formulation. Then your model is:

min_{x, d} \sum_{j=1}^M d_j
s.t.       d_j >= P[j] - \sum_{i=1}^N X[i]x_{ij} \forall j
           d_j >= \sum_{i=1}^N X[i]x_{ij} - P[j] \forall j
           \sum_{j=1}^M x_ij = 1 \forall i
           x_{ij}\in \{0, 1\}

This is a mixed integer optimization model, which can be solved, for instance, using the lpsolve or lpSolveAPI packages in R or the intlinprog function in MATLAB.

josliber
  • 43,891
  • 12
  • 98
  • 133
0

I can prove that your problem is NP by reducing another NP problem to this one. In other words, I'll show that if I can answer this problem, I can immediately answer another NP problem.

The specific problem I'll be reducing is the Subset Sum Problem:

Let V be the set of numbers. We want to know if sum(V)==0. Let P={0} (a length-one array containing only 0). In this situation, the optimal solution to your problem has sum(diff[j])==0 if and only if the subset sum is 0. In other words, the Subset Sum Problem is a special case of your problem, so your problem is at least as hard as the Subset Sum Problem.

Therefore, your problem is NP. I'm still not sure if it's NP-Complete.

murgatroid99
  • 19,007
  • 10
  • 60
  • 95
  • I need to assign all the numbers to one pack, if there is only one pack, then the `sum(diff[j])` is `sum(V) - P[0]`. – HYRY Jun 28 '14 at 00:46
  • I'm not sure what you're trying to say, but I added a sentence to try to explain my answer better. – murgatroid99 Jun 28 '14 at 00:48
  • Does the specific problem has only one pack? For example there five numbers `[1,2,3,4,5]`, and one pack `[12]`, the there is only one choice, the different is `1+2+3+4+5 - 12 = 3`. – HYRY Jun 28 '14 at 00:56
0

This is NP-hard by reduction from 2-partition (2P). Change the current problem to a decision problem asking if sum(diff[j])=0. Given an instance of 2P, let P[0] = P[1] = sum(V)/2. If there exists a two partitioning, then there clearly exists some assignment with sum(diff[j])=0.

There is a psuedo-polytime algorithm for 2-partition, but it's not likely to work for this problem since it doesn't apply to >=3-partitions.

It appears as if this is analogous to bin-packing, but I'm not 100% sure because you can overflow a 'pack' but not a 'bin'.

dfb
  • 13,133
  • 2
  • 31
  • 52