Clever Algorithms in Python

Ever since I got hold of Clever Algorithms – Nature Inspired Programming Recipes by Jason Brownlee, I had a desire to assimilate it. Jason has done us a great service by researching, collating and proposing a unified template for understanding “Nature inspired algorithms”.  The nature of the subject itself spans varied fields, is relatively new, fragmented, incomplete, spread across various papers and is not amenable to understand as a whole. So I was naturally (no pun intended) excited when I saw this book.

My first attempt at reading through it was frustrating to say the least. It was not Jason’s writing style per-se, but the nature of the subject itself. It is not amenable for reading as a programming book. Jason himself cautions in the first paragraph: “rather than being read cover to cover”, it is a handbook and a technique guide-book. In-spite of that warning I plunged in trying to gather as much as possible. Only later I realized that to make this knowledge my own, I needed to go slow and experiment with each algorithm. But blindly copying what he had done, would be of no avail.

So I set myself up with the following goals:

Goal 1: I will try to implement the algorithms in a language that I have no familiarity with.

Goal 2: I will try to stick to the original pseudo code as far as possible to increase my understanding of the technique

Goal 3: I will try to question and verify every line in the code listing provided by him, so that my implementation would be from understanding, rather than duplicating, or worse copying.

Goal 4: Have an alternate (or accompanying, if you will) source for people who come across this book, with more contextual information on implementing these algorithms. (sort of standing on the shoulders of giants!)

Goal 5: Provide a narrative of my experience, implementing these excellent algorithmic techniques.

To realize goal 1, I chose python as the language to implement the algorithms. As you can see I am a .NET guy throughout my career. So trying to implement the algorithms in python would force me to re-evaluate every line of code that I write. The problem is compounded by the fact that Jason’s original code listing is in Ruby. I had to learn just enough Ruby to understand his code and translate that in to python!. This has a dual advantage: one i get to learn a new language while applying it to a non trivial problem, two it forced me to look at each line of code from an algorithmic stand point.

To realize goal 2, I changed my implementation functions and structure to reflect the pseudo code listing that he provided. This makes it easy to follow how the pseudo code translates to implementation. In all fairness to Jason, he did a great job of it. But I wanted to take it one step further and make it more consumable. The subject matter is hard enough, I wanted to remove any further obstacles to assimilating this seminal work from Jason.

To realize goal 3, I changed some of the objective functions that he provided to others where possible. I also made some changes to some data structures and input variables to further kick the tires. I also included a test suite that exercises the algorithms. So anyone who just want to see how the algorithms behave for different inputs, can do so easily. I also put in a lot more comments in places where I ran in to difficulties. I hope it would make it more valuable to someone trying to follow along.

To realize goal 4, I want people to see how the algorithms play out in a different language. By writing this in a language that I am unfamiliar with, I hope it gives one confidence to attempt them in your language of choice. Each .py file also has a header which lays out the Name, Taxonomy, Strategy and Heuristics of the algorithm. (This is mostly from Jason’s Clever Algorithms book and algorithm template that he designed). The reason I chose to do so was, while understanding one algorithm, I wanted the code and the algorithm context at the same place. I found that very helpful in understanding and assimilating each algorithm.

To realize goal 5, I would be blogging about this. His 45 algorithms are divided in to Stochastic Algorithms, Evolutionary Algorithms, Physical Algorithms, Probabilistic Algorithms, Swarm Algorithms, Immune Algorithms and Neural Algorithms. As I finish implementing each category of algorithms, I will write a blog describing it.

I would like to sincerely thank Jason Brownlee for making available his Clever Algorithms. (By the way, Jason Brownlee did his Phd from the same university that I went to: Swinburne University Melbourne (I am from class of 2000)

Note: I tried as far as possible to write the code in a ‘pythonic’ way. Since I was learning python for this, I would like to beg forgiveness in advance from expert python developers. Any suggestions are always welcome to improve my code.

I am using Python 2.7.2 with Eclipse IDE for Java Developers Version: Helios Service Release 2. I have the PyDev ver 2.1.0xxx plugin for eclipse to work in python.

 

Posted in Technology Tagged with: , ,
One comment on “Clever Algorithms in Python
  1. An ambitious set of goals Sai, best of luck! Let me know if there is anything I can do to help you on your journey!

%d bloggers like this: