Doing the Martin Shuffle (with your iPod)I've got an iPod shuffle. A lot of people do. So you probably know they're great, but they don't have a display. If a song is playing and you want to know what it is, you're out of luck. I can think of two ways that could be fixed:
I believe that both could be implemented with just a software update, without altering the iPod hardware. You don't need a new button, you can use a sequence of existing ones, such as double-click on the 'play' button. Or the rarely-used battery check button. Note if you use these button sequences on an iPod that hasn't been updated, there are no ill effects. So go ahead, Steve; I grant you rights to these ideas, free of charge. |
Ipod Shuffle | ||
The Martin ShuffleThe two ideas above are good if you want to identify a song, but not if you want to find a song. But for that my friend Charles Martin came up with an idea that works with no hardware or software changes needed. I call it the Martin Shuffle, and it works like this:
Note this is a randomized algorithm; you use randomness to solve a deterministic problem faster than you could without randomness. So now there are two questions: how close do you have to get before you switch to non-shuffle mode, and how long will it take, on average, to find a song with this approach? |
Charles Martin | ||
Markov Decision Processes to the RescueWhat we need is a policy for when to hit the shuffle button and when to switch to the sequential button. The tricky part is that shuffling is random -- can we determine the optimal policy when we don't know where we'll end up? It turns out that we can if we treat this as a Markov Decision Process, or MDP. In an MDP you need to define the following:
Now the basic idea for finding the optimal policy in an MDP is simple: For each state of the problem, choose the action that minimizes the sum of the cost of the action and the expected cost of getting from the resulting state to the target. The Value Iteration AlgorithmThe value of a state V[s] is defined as the expected cost it will take to reach the target state from s. Once we compute V[s] for each s we can easily implement the basic idea above. The complication is that in general V[s] is defined in terms of other V[s'], but V[s'] is defined in terms of V[s]. Where do we start? Fortunately, MDPs can be solved by an algorithm called value iteration that starts with an initial guess for all V[s] and then updates the guesses repeatedly, until there are no more changes (or until all changes are smaller than epsilon). This iterative algorithm is guaranteed to converge.To compute the initial guess for each state in the iPod problem let's just assume you always use Sequential; therefore the initial value of each state s is the absolute value of s - t, where t is the target state. To update the value for a state, we take the expected value of the best action. The expected value of Sequential is still the absolute value of s - t. The expected value of Shuffle is the cost T of shuffling and identifying the resulting song, plus the average value of the V[r] for each possible resulting state r (which I originally thought was every state except the current state, but an interesting article by Brian E. Hansen convinced me that it is possible to randomly skip from a song to the same song). |
A.A. Markov | ||
Coding a SolutionWe can now show some code for valueiteration on the iPod problem. (You can also see code for a general MDP solver.) We first make the assumption that the target, t, is always the middle song, N/2. (We can do this without loss of generality because the songs are actually arranged in a circle, not a line segment: from the last song you can go forward to the first. So the numbering is arbitrary because every point on a circle is isomorphic.)
This is Python code; if you're not familiar with Python you should know that [abs(s-t) for s in states] iterates s over each element of states and collects the values of abs(s-t) into a list. Also, range(N) returns a list of the numbers from 0 to N-1, inclusive, and V1, V2 = V2, V1 swaps V2 and V1. All assignment in Python is done by moving pointers, not by creating copies of objects. The rest you should be able to figure out. Besides valueiteration, all we need is a trivial function to compute the average (mean) of a sequence of numbers, and a main function that calls valueiteration and prints out some statistics on the results:
|
Python | ||
And the Answer is...After a few seconds the program produces this output:
So that answers our two questions. Assuming 5 seconds per shuffle, then on a 250 song iPod you should use the policy of shuffling until you get within 35 songs, and you should expect to spend 30.4 seconds on average finding a song. The switching point and the total time go down if you can identify a song faster, and up if you are slower. On a 125 song (512MB) iPod, you should shuffle until you get within 25 songs, and expect to spend 20.0 seconds (assuming 5 seconds to shuffle), according to this output:
Happy shuffling! | |||
|
AddendumOK, now that the iPod Nano (with a display) is out, some may claim that this page is irrelevant. But I think it is still of use for those with shuffles, or for those interested in MDPs.A reader asked why I have global V in main. The reason is that that way I have the value of V available for inspection in the interactive interpreter, even though I don't return the value. Andre Kloss presents a Python script to show the last played songs; you install the script in the root directory of your iPod and run it from there when you connect to your computer. Andre also points out that Martin Fiedler's shuffle database generator can be used to change the order of shuffling; you may be able to choose an order that makes searching easier for you. I coded the value iteration algorithm from memory without consulting a reference, and it turns out I forgot an important factor: the future discounting factor, gamma (γ). This can have an effect on convergence of the values. To verify that the results I have are reasonable, I wrote a function, run to run a simulated random search for a song, on an N song iPod, with shuffle time T, and with the policy of shuffling when farther than p songs away from the target. The results of the simulation (when averaged over 100,000 runs) are all within 0.1 of the values computed by my MDP routine. Here is the run function and the new main:
And here are the new output results:
You can see that the values from my program agrees with the simulation. Analytic SolutionDarius Bacon wrote in to say that "the best cutoff was 'obviously' going to be O(sqrt(N)) (for T = 1, anyway)", and that he had confirmed by checking my numbers that it was sqrt(N T). Darius may be more discerning than most in seeing this as 'obvious,' but those who know the classic drop two eggs from a building puzzle may remember the answer was sqrt(N) there as well.Later, Houman Alborzi wrote to say the same thing, and provided an analytic derivation:
Let cost(i) indicate the average cost of finding a song that has distance i from current song. The recurrence equations for cost(i) are then: cost(0) = 0 cost(i) = min(1+cost(i-1), T+P) where P is average cost of a position resulting from shuffle: for N an even number P = 2/N(cost(0)+cost(1)+cost(2)+...+cost(N/2)) for N an odd number P = 2/N(cost(0)+cost(1)+cost(2)+...+cost(N/2)+1/2(cost((N+1)/2)) The strategy is to shuffle when i is bigger than a threshold value t, that is: for i<=t :cost(i)=1+cost(i-1)=i for i>=t+1 :cost(i)=T+P for even and odd N: N/2 * P = (0 + 1 + ... + t + (N/2-t)*(T+P) N/2 * P = (t+1)*t/2 + (N/2-t)*(T+P) P*(N/2 -(N/2-t))= (t+1)*t/2 + (N/2-t)*T P*t = (t+1)*t/2 + (N/2-t)*T P= (t+1)/2 + N/2*T/t-T so: for i<=t :cost(i)=i for i>=t+1 :cost(i)=(t+1)/2 + N/2*T/t Find the threshold value t that minimizes P, the average case cost of the strategy: dP/dt = 0 1/2 - N*T/2/t^2 = 0 or, t=sqrt(N T) (neglecting the fact that t should be an integer) Moreover, P can be calculated as:(again neglecting the fact that t should be an integer) P = (t+1)/2 + N/2*T/t-T P = sqrt(N T)/2+1/2 + sqrt(N T)/2-T P = sqrt(N T)+1/2 -T P = t+1/2 -T |
iPod Nano |