Computing optimal "fit"

Status
Not open for further replies.

hansmuff

Ars Tribunus Angusticlavius
9,668
Allright folks this is probably a simple and well-known problem with a known solution but I can't find it.
I was putting MP3s on CDs for my car and trying to figure out how to maximize CD space usage.
What I wanted was to have combinations of MP3s that would fill my CDs as much as possible so I would minimize slack.

I wrote a small program that did this brute-force, trying all combinations of all files to find out which combination would approximate my max. time on the CD best.
Then I added some code to it to eliminate some unneccessary combinations that the program knew would be too long/short.

What I am asking here is what known solutions for this sort of problems are out there. Mine is not optimal to say the least because once you want to distribute 200 titles over a few CDs it takes very very long to calculate the optimal sequences, even with some elimination.

Thanks for any input.

hansmuff
 

qball

Ars Tribunus Militum
2,238
Actually, I don't think that there is any intellegent algorithmic way of doing it. You can fine tune so that you don't calculate multiple combinations of files where a subset of those files is already too big (I assume this is the optimization you did). But this doens't affect the big-O running time, which must be frightful. If you want a "good" solution instead of an optimal one, that's a lot easier to achieve (stop when you get to [capacity - n%, capacity]).

-Q
 

bash666

Ars Legatus Legionis
24,417
This problem is clearly NP, and seems alittle like the traveling salesman. You have to figure the WORST case scenario of randomly putting tracks onto a CD would at most result in 1 extra CD. Doesn't it make more sense to organize the tracks logically(by artist and album) rather tthan to minimize the number of discs.

However, if you with to organize them by say album and artist, you coudl create large blocks of all the sogs off a certain CD, and then run the algorithm on these blocks, which should be a much smaller set.

Chers,
Kevin
 
Since normally CD tracks tend to be around the same length there are a few clever heuristics you can use to "cheat" in the search. When I hack solutions to these kinds of problems I usually begin with making the stupid version first so that I will get a feel for the subject. Then I add the heuristics applicable to the stupid solution, sometimes this will fit within the requirements and I will stop. Sometimes it won't.

Anyway the things I can come up with right now you can (ab)use for heuristics is the following:

- Each CD will contain 14-20 tracks or there abouts.

- Take roughly equal sized tracks and play with the diffs.

Also since the data isn't completly random, i.e. it is focused within a particular area there is a possibility of using a neural network.

// tiny
 
This problem is roughly the bin-packing problem, which is known to be NP-complete. I.e. there are no known algorithms which run in polynomial time to solve this problem. There are a couple good approximation algorithms for this, though.

For example, the BFD (Best Fit Decreasing) algorithm is guaranteed to require no more than 11/9 times the optimal number of CDs, and in many cases it will give you the optimal number of CDs.

The BFD algorithm is:
1) Sort your files by size.
2) Start with one CD. We iterate through the files from largest to smallest.
3) For each file find the CD which it would come closest to filling. Put it on that CD. If there is no CD with sufficient space left, put it on a new blank CD.

There are a few algorithms which would give you the optimal packing and perform well on almost all inputs, but they still run in exponential time on some inputs, and tend to be rather complicated. BFD is one of the best approximation algorithms for the bin-packing problem, and will give you CDs which will probably be fuller than other approximation algorithms.

floccinau
 

Dandy Andy

Ars Tribunus Militum
2,458
I know it's slightly O.T., but I remember that Kirsty McColl was supposed to have picked the playlist of U2's "The Joshua Tree". A pundit at the time, who was impressed with the order of the tracks, asked Miss McColl what her reasoning was. She said that she liked the first track the best, the next one she liked second-best, and so on.

Oh well, I liked it.

:)
 

OneEyedPete

Wise, Aged Ars Veteran
130
This is like the 0/1 Knapsack Problem.

"Given a set of items, weight and profit associated with each item, and an upper bound for the capacity of the knapsack. The task is to find a subset of items which maximizes the total of the profits in the subset, yet all selected items fit into the knapsack, i.e., the total weight does not exceed the given capacity."

However in your case there is no "profit" unless you have ranked your songs in some manner, like take all classical first and country last.

There are lots of example code on how to implement this algorithm.
 
Status
Not open for further replies.