Skip to content

How to choose a number subset with sum closest to x?

3 messages · Dimitri Shvorob, Charles C. Berry

#
I am looking for a function that, when supplied a vector of floats x,  an
integer n, and float s, would find me an n-subset of x with the sum closest
to s. Can anyone point me to a package/function that can do the job - better
yet, provide a relevant code sample? (There are a few relevant packages on
CRAN, but lack of examples in their docs is a real problem).

Thank you.
#
On Fri, 29 Jan 2010, Dimitri Shvorob wrote:

            
See if this helps:

 	http://finzi.psych.upenn.edu/Rhelp08/2009-November/218369.html

HTH,

Chuck
Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901
#
Oh, I have considered this, but just don't see how the problem can be cast as
LSAP. (In the example, sum of squared pairwise distances ('costs') is
minimized, whereas here I am squaring the distance of a sum from the target
number. (How do I define individual assignment's cost here?)
Thanks anyway! I will be sure to study the (longer) code snippet in that
post.

PS. Sorry, my post became duplicated.