As a small example, say I have N=6
elements
{ 0.03, 0.25000039, 1.391, 500.1, 0.5000001, 1.75001 }
and K=3
then the combination { 0.25000039, 0.5000001, 0.2500001 }
, which sums up to 1.00000059
would be the combination that sums up the closest to a whole number.
Is it possible to solve this without "brute force" (iterating through all n choose k
elements)?
I can think of this:
Up to here it's giving a close answer. To make it 100% you then repeat the same process this time without the biggest number. Also I omitted this in the algorithm that whenever you want to select a new number if there are no numbers left you are done and that's as close as you can get. Why I left this out is because you might want to allow a number to appear multiple times and therefore you don't need this part.
This is something I came up with. It might need some testing and if you think it's wrong please tell me in the comments. :)
I guess this task can be reduced to classical Knapsack problem . So the solution as we know it would be NP-complete.
I had the same problem in one of my projects, where I had to pick a subset of size k
out of an original set of size n
. The subset should satisfy some constraints and maximize (or minimize) a function calculated on the subset. The problem looks trivial if n
is small, but as long as n
greater than 30
, it starts becoming more challenging given a large number of combinations. G.netic algorithm (GA) is very helpful and practical in this case. For example, you can follow this example , and it will give the result in 1 minute. What you need is to modify the objective function, eg, out["F"] = (np.sum(self.L[x]) - target)**2
. Of course, the GA cannot guarantee an optimal solution, but it should give you at least the near optimal solution quickly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.