code development programming

Dynamic language and semantics

Given the opportunity to revisit my Genetic Algorithms library, I wanted to revisit my open letter to Jon Skeet, about semantics and dynamic languages, and compare how the code looks in C++ vs Python. I accept that C++ is an overly verbose language but the problem I want to discuss is similar in C# and Java. I’m just going to pick a few examples, but feel free to explore the code yourself, either as C++ templates or as Python examples. These examples all expect a certain knowledge of Genetic Algorithms, so I’d recommend reviewing my previous post first if you are unfamiliar with the concepts.

If you’re trying to compare them directly from the repositories, please note that I have removed some comments and logging, and changed the formatting in this post for reasons of clarity, and have yet to apply those changes back to the repository. I have also added some functionality to the python code that doesn’t currently exist in the repository to ensure the examples are comparable.


In C++

   *            SAMPLE FITNESS FUNCTIONS                                 *

  template<class _Ctype, int _Csize>
  struct fitness_base {
    virtual double operator()(_Ctype chrom[_Csize]) = 0;
  } ;

  template<class _Ctype, int _Csize>
  struct maxones : public fitness_base<_Ctype,_Csize>{
    double operator()(_Ctype chrom[_Csize]) {
      return std::count(chrom, chrom+_Csize, 1);    
  } ;

The fitness function for a chromosome returns a number that is used to rank the chromosome such that fitter solutions are more likely to survive and breed. For the example below, note that by default, the chromosome is an array of type _Ctype (default: bool), of length _CSize (default: 32)

The fitness function must be passed as a parameter to the constructor so that different problems can be explored using the same representation. This requires a type for fitness functions fitness_base which is defined as virtual to ensure it can be overridden, and contains an () operator so it can be called. In C#, this would be a delegate type, and we would be able to use the function/action types from the library as the base class for maxones. The fitness function shown simply counts the number of ones in the chromosome.

In Python

    def fitness(self):
        return sum(self.genome) 

Functions are first-class objects in Python, so we don’t need anything special to pass them as arguments, or require overloading the () operator. However, Python also allows patching classes post-initialisation, so we can overwrite the fitness function, or we can use Python’s multiple inheritance mechanism to subclass 2 different versions of chromosome, one of which overrides the fitness function, and the other overriding the crossover function.

Note that the fitness function is different between the two fitness functions is simple, but I now prefer the cleanliness of sum to a conditional count, but the overall character of the code will not be altered by the 2-character difference.

Run one generation

In C++

    void run_once_replace() { // one generation = psize crossovers
      pop_t newpop;

      for(int i = 0; i < _population_size; ++i) {
          chrom_t child = select_chromosome() + select_chromosome();



In Python

    def run_once(self):
        "Run one iteration of the genetic algorithm"
        # Use replacement algorithm
        new_pop = []
        for i in range(len(self.pop)):
            a, b = self.select_two_members()
            new_pop.append(a + b)
        self.pop = new_pop

In these examples, apart from some small syntactic sugar that allows us to return 2 values from a function, the C++ and Python code are just as clear as each other.


In contrast to my letter, there isn’t as big a difference as I believed when I originally wrote the letter, at least in terms of the resultant code, although I remember the pain trying to get the types lining up properly in the first place. Semantics still matter, and types definitely got in the way designing the chromosomes, but actually, outside some specific cases, with well structured code, static and dynamic languages can both make semantic meaning clear.

ai artificialintelligence c++ code development programming search

Updated slides on Genetic Algorithms

I had the opportunity at work to revisit my Generic Algorithm talk, to refactor it with a bit more time to hopefully make it clearer. I also ported the C++ template code to Python to make it easier to demo. I’ll be talking about the implementation differences in a future post but I’ve included the links for the talk below for public consumption.

ai artificialintelligence code programming

Why AI doesn’t scare me

Tripod Machine
The chances of anything coming from Mars are a million to one…

You may have heard about a letter going about where a lot of people say they’re scared of AI and autonomous weapons. There are some very interesting signatories on the list. As someone who studied AI, and knows a couple of people on that list from my course, the letter interested me greatly.

I’ll discuss the implications of autonomous weapons in a future post, but a lot of the reporting and comments about the letter talked about fear of AI itself. As if smarter machines in and of themselves are the greater threat to humanity, without considering whether or not these smarter machines are designed to kill. This fear appears to be unrelated to whether the machines are smarter than us, just about being smart enough to be a threat (although I want to discuss the singularity in a later post too).

Perhaps for a small minority, the fear comes from the same place as fear of other humans. The ignorance breeding fear that someone or something smarter or different from you will be threat to your way of life, or your life itself. There’s legitimate fears within that, linked back through history to the luddites whose jobs were at risk from automated machines.

It’s clear that jobs will replace humans with machines, and the better machines get, the further up the pay scale the threat will come, just as globalization pushed low paid jobs to other countries. It’s a real problem that needs to be addressed politically via training, diversification of the economy, and other tactics, but I don’t want this to be a political blog, so decide for yourself how jobs can be created.

If your fear comes from not being the smartest person in the room – get over yourself. Success builds on success, and humanity’s greatest achievements have come from those who stand on the shoulders of giants. These days, these may be iron giants. AI that is smarter than us already exists, machines are better than us at Chess, Jeopardy, code breaking, and calculating Sine tables.

AI is already all around us – it does fraud detection, voice recognition and a bunch of other things – Once it works, it’s not AI any more (as .Net Rocks reiterates), and it will get better.

If you want to know more, the following article and video are definitely worth a look.

<How safe is AI : >

<How smart is today’s AI : >


Google Code Migration : Genetic Algorithm Templates

With the closure of Google Code, I’ve moved some projects to github. All personal projects so far, but related to talks our blog posts from the past, so may still be of interest.

The first project I want to highlight again is written in C++ and implements genetic algorithms using mainly C++ templates, just to see how powerful they were. It taught me a lot about generic code, and how a poor type system can interfere with the clarity of your code. It also prepared me for one of my first talks, about Genetic Algorithms at a Beauty of Code techmeetup.

I’d like to look at a Python port, to see if my expectations of using dynamic typing would answer the concerns I have about code clarity. For now though, it’s available for reference. It’s not production tested, and there are parts that are embarrassing, but it might be interesting if you want to know what genetic algorithms might look like.

code development programming

DDD Scotland 2011 Voting and Teasers

DDD Scotland voting With voting now open for Developer Developer Developer Scotland 2011, I thought it was time to give everyone a teaser of the 3 talks I submitted to inspire you to vote for me πŸ˜‰ – but be sure to vote for some of the other sessions too, there’s a great line-up and some tough competition for speakers places.

HTML5 for mobile

HTML5 Canvas : Card back
A simple HTML5 game in 1 hour. Covering real-world usage of canvas, CSS3, ECMAScript 5, and a few other goodies. I’m not going to say too much, except that the image here is rendered via canvas (which neatly, in Firefox, offers a “Save As…” png option in the context menu). This canvas works in Firefox, Chrome, Android and iPhone, and possibly others.

The talk will be given using Firefox, but participants will be able to follow along on their Androids and iPhones (well, it’s one way to get people off Twitter πŸ˜‰ – @craignicol btw, if you hadn’t guessed)

If angle brackets or JavaScript scare you, this is not the talk you are looking for.

Genetic Algorithms

Lights Out
Want computers to solve problems themselves? Maybe find a solution to light out like the game on the left?

I’ll run through the theory and a bit of practice, using the game above as an example of how to encode a problem to be solved by a Genetic Algorithm.

There may be some C++ (yeah, I am old skool πŸ˜‰ )



IronPython has been around for a little while as an official .Net language. Whilst many .Net developers are fleeing to Ruby on Rails, wouldn’t it be nice to program in a dynamic language using the libraries you already know? Wouldn’t you like to write the same program with much less code. And wouldn’t you like to see how a language where everything is dynamic really works?

code programming

July Tech Meetup Glasgow: The Beauty of Code – Techmeetup

The video for my Genetic Algorithm talk has been posted to the TechMeetup blog for those who want to watch it (and can withstand my stuttering πŸ˜‰ ). If you want to grab the original presentation itself, it’s in the repository for the C++ template project: geneticalgorithmtemplates \ talks and presentations \ techmeetup glasgow 20090701 (edit : now moved to github)

Craig Nicol on Genetic Algorithms

Craig provided a high-level introduction to what genetic algorithms are and how they can be used.Genetic algorithms is a search technique used in computing to find exact or approximate solutions to optimization and search problems. As the name suggests, they’re inspired by the process of evolution in the natural world. The typical genetic algorithm consists of a genetic representation of the solution domain, and a fitness function for evaluating the solution domain.Craig used example to clarify the topic and his presentation stimulated an interesting discussion of potential applications of the technology.

July Tech Meetup Glasgow: The Beauty of Code – Techmeetup

Craig Nicol talks about Genetic Algorithms from TechMeetup on Vimeo.

Hope to see you at the techmeetup talk tonight.

Blogged with the Flock Browser
code programming

Genetic Algorithm Templates

I had a great time at the Tech MeetUp (@techmeetup) in Glasgow this week. If you want to meet up with other tech minded folk in Scotland, it’s definitely worth checking out. See the details at the end of this post.

At the meeting this week, I got talking to a few folk (including John Gallagher who gave a great talk) about AI and Genetic Algorithms. This inspired me to dig out my old experimental code for doing GAs using C++ Templates. The code’s very rough and ready, and it was written long before I discovered TDD or subversion, but it should be standards-compliant, and it compiles in the latest GCC (I can’t vouch for other compilers, if you try it, let me know). As it’s a source-code library, you’ll have to compile it yourself, but if you don’t know how to use a compiler, you’re probably reading the wrong blog anyway. I will be tidying up the code and creating a wish list as I get the chance, but I’m throwing it out there in case it’s useful to anyone.

If you’re interested, check it out at the link below and let me know what you think.

Genetic Algorithm Templates (edit : link moved to github)

If you’re interested in Tech MeetUp, you can see videos from previous talks at their website

The Tech MeetUp is the informal opportunity to meet other developers and tech companies, to showcase your hacks or projects, and to find out what’s happening around us. Help build the tech community – set up a profile and come along to a Tech MeetUp.

Home – Techmeetup

You can find out about upcoming meetings on their mailing list

Description: An easily accessible and friendly community of tech minds, skills and startups around Scotland and Northern UK.

Tech Meetup | Google Groups

Blogged with the Flock Browser