Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bin-packing (or knapsack?) problem

I have a collection of 43 to 50 numbers ranging from 0.133 to 0.005 (but mostly on the small side). I would like to find, if possible, all combinations that have a sum between L and R, which are very close together.*

The brute-force method takes 243 to 250 steps, which isn't feasible. What's a good method to use here?

Edit: The combinations will be used in a calculation and discarded. (If you're writing code, you can assume they're simply output; I'll modify as needed.) The number of combinations will presumably be far too large to hold in memory.

* L = 0.5877866649021190081897311406, R = 0.5918521703507438353981412820.

like image 865
Charles Avatar asked Nov 05 '22 08:11

Charles


1 Answers

The basic idea is to convert it to an integer knapsack problem (which is easy).

Choose a small real number e and round numbers in your original problem to ones representable as k*e with integer k. The smaller e, the larger the integers will be (efficiency tradeoff) but the solution of the modified problem will be closer to your original one. An e=d/(4*43) where d is the width of your target interval should be small enough.

If the modified problem has an exact solution summing to the middle (rounded to e) of your target interval, then the original problem has one somewhere within the interval.

like image 191
Rafał Dowgird Avatar answered Nov 09 '22 06:11

Rafał Dowgird