I played with genetic algorithms earlier this year. A few evenings ago, the idea of solving the numbers round in the TV show Countdown with genetic algorithms popped into my head. So, here are the efforts of a frantic Sunday morning coding session.

My application doesn't quite conform to the true rules - I create six random source numbers and a single target number. The application then creates a candidate solution by inserting a random operator (add, subtract, multiply or divide) between each number. This candidate solution then spawns a mutated version - the mutation is a 50/50 chance of either randomly changing an operator or randomly exchanging the order of two of the numbers.

Whichever version of the solution is nearer to the target number lives on to the next generation where it spawns a further mutation. This process repeats until the rounded down result of a solution matches the target number.

The application is small and to get things finished in a short space of time, I've dispensed of any real framework. The main logic is in GeneticCountdownModel, but I've put the fitness function in CandidateSolutionPair. In a similar fashion, CandidateSolutions are responsible for their own cloning and mutations.

The application uses my SoftSkin - a skin I've been playing with over the last few weeks. I've included the source, but be warned it's a bit of a mess at the moment. It's still work in progress with lots of little hacks and copy-and-pastes and is desperate need of some serious refactoring.

The application lives here and the source code is available here.
0

Add a comment

About Me
About Me
Labels
Labels
Blog Archive
Loading