Let P=[P1, P2, ..., Pk]
be k
positive integers and let T
be a positive integer. I would like to generate all combinations that sum up to at most T
. That is, sum(x[i] * P[i] for i in 1:k) <= T
where x[i] = 1
iff i
is chosen in the combination.
Example.
Let P=[1, 2, 3]
and T=4
. The combinations should be:
1
2
3
1, 2
1, 3
2, 3
So only the combination 1, 2, 3
cannot be there because 1 + 1 + 3 = 5 > 4
.
I thought of generating all combinations first and then start verifying the constraint sum(x[i] * P[i] for i in 1:k) <= T
. But this approach could be more time consuming than other clever approaches. How can we generate such combinations?
NB. If you know any function in Python or Matlab that can be used to generate such combinations, you can provide it.
Thank you.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…