MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: 0/1 knapsack-like minimalization problem and file

  • To: mathgroup at smc.vnet.net
  • Subject: [mg108771] Re: 0/1 knapsack-like minimalization problem and file
  • From: Daniel Lichtblau <danl at wolfram.com>
  • Date: Wed, 31 Mar 2010 05:25:43 -0500 (EST)

Peter Sisak wrote:
> Given the files in a directory, by what method can I receive the list of 
> the files that in size is closest to, but not larger than, an arbitrary
>  x value, given in bytes? Each file can be listed at most once. The file 
> names and their respective sizes must be read in by Mathematica. (The directory contains files with count of around two-three hundred, thus making a brute force "sum then compare" search of all possible combinations would most probably not finish before the next ice age coming...)
>   An extension to the above problem: how to find the lists of files, if there are more than one lists permitted, and the aim is to minimise the total difference between the respective groups and the (identical) x value? (Think archiving data using the least amount of identical-type CDs, or wasting the least amount, and leaving the few odd ones out.)
> 
> Any ideas how to pull this? Thanks in advance
> Peter Sisak
> 

I keep forgetting that FindMinimum has very effective ILP capabilities, 
at least for the case of 0-1 programming. The variant below is 
significantly faster than the one I first sent in response.

closestSum2[vals_, target_] := Module[
   {x, vars, len=Length[vals], obj, constraints, max, best},
   vars = Array[x,len];
   obj = vars.vals;
   constraints = Join[Map[0<=#<=1&, vars],
     {obj<=target, Element[vars,Integers]}];
   {max, best} = FindMaximum[{obj, constraints}, vars];
   {max, vars/.best}
   ]

Here is an example with 300 random values in the range 0-10^4, where our 
target to approach but not exceed is 10^6.

vals = RandomInteger[10000, 300];

In[23]:= InputForm[Timing[closestSum[vals, 1000000]]]

Out[23]//InputForm=
{12.624081, {1.*^6, {0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 0, 
1, 0,
    1, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 
1, 1,
    1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1, 1, 
1, 0,
    0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 
0, 0,
    0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 
0, 1,
    0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1, 
1, 0,
    1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1, 
0, 0,
    0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 
0, 0,
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1,
    1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1,
    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 0,
    1, 0, 1, 1, 1, 0}}}

Daniel Lichtblau
Wolfram Research


  • Prev by Date: Re: 0/1 knapsack-like minimalization problem and file
  • Next by Date: Re: floating point, step forward issue, Manipulate Appearance -> "Labeled"
  • Previous by thread: Re: 0/1 knapsack-like minimalization problem and file
  • Next by thread: Re: 0/1 knapsack-like minimalization problem and file