Simulated Bee Colony in F#

24 Apr 2011

April 2011’s issue of MSDN Magazine had an interesting piece on Bee Colony Algorithms by Dr. James McCaffrey,, explaining the concepts and providing an example, applying the algorithm to the Traveling Salesman Problem. In a nutshell, the algorithm is a meta-heuristic, that is, a method that is not guaranteed to produce an optimal solution, but will search for “decent” solutions in a large space. In a real-life bee hive, bees scout for areas rich with food, keep visiting them until they are exhausted, and tell other bees about good spots so that more bees come search that area. By analogy, the algorithm uses scout bees, which search for new random solutions, and recruit inactive bees which become active and start searching for improved solutions around their current solution.

I found the algorithm intriguing, and thought it would be a good learning exercise to try and adapt it to F#.

Disclaimer: I am still learning the ropes in F#, so take the code that follows with a grain of salt. I’ll gladly take advice and criticism to make this better – my intent is to share my learning experience with the language, not to teach you best practices.

In the case of the Traveling Salesman Problem, the goal is to find the shortest (or some other cost measure) closed route connecting a list of cities. In order to do this, we need to be able to create random solutions, as well as solutions in the neighborhood of an existing solution.

Assuming we begin with an initial list of Cities (the cities our salesman needs to visit), we can generate random solutions by shuffling that list, using the Fisher-Yates shuffle algorithm. We can generate the sequence of index pairs that need to be swapped with the following

Applied on a list, it produces a sequence of tuples, representing the successive pairs of items that should be permuted. Now we just need to apply the permutations to our initial list. Rather than updating the same array of indexes in place, I figured it would be fun to try out the List.permute function. List.permute works by applying a function to a list; the function maps every index of the original list, to the destination index of the permuted list. For instance, mapping each index to itself will return an identical list:

The SwapIndexMap function defines how each index should be mapped, given the 2 indexes that should be swapped; the Swap function applies it to a list, and returns the result of the permutation. We are now all set to write the list Shuffle function:

Starting from a list, we create a sequence of indexes to be swapped, we scan that sequence, starting from the original list, and applying permutation after permutation until the sequence is exhausted – and we return the last item of the sequence. Let’s try the interactive window again, just to check that nothing is wildly wrong:

Given what we have gone through, the other problem, generating a solution in the neighborhood of an existing solution is a piece of cake. We just need to switch two consecutive items in the list: the only issue is with the last item of the list, which should be swapped with the first item. Building off our SwapIndexMap function, we get

We now have two of the building blocks we need for the algorithm: a function which returns random solutions, inspired by the GenerateRandomMemoryMatrix method of the article, and a function which produces a solution in the neighborhood of an existing solution, similar to the GenerateNeighborMemoryMatrix method. Next, we’ll need to evaluate the relative quality of two solutions – but that will be for next time.

In the meanwhile, let me know if you have any comments or suggestions!