Mr & Mrs Kipling

927 words • reading time 5 minutes

TL;DR – This post is about genetic algorithms (and cake)

Far away and just as long ago there lived a Baker and his wife. They were called the Kiplings. Now Mrs Kipling made the finest cakes in all the land – people would come from far and wide to taste her cakes. Later Mrs Kipling came to be issued a royal warrant of appointment. This however angered Mr Kipling who was naturally jealous of Mrs Kipling’s achievements and thought that he was every bit the cake maker she was. Sadly in a fit of rage he locked Mrs Kipling away in a tower and removed her tongue so that she could never bake nor speak of her infamous recipe again.

Whilst Mrs Kipling had not grown up to be the most beautiful child in the world, nor was she blessed with long golden hair she did eventually come to be visited in the tower. You see a boy by the name of Charles Perrault wanted to impress his childhood sweetheart – the young princess Puckett by giving her the finest cake in the whole land. He had heard that if you could sneak a sample of cake to the base of the tower in which Mrs Kipling was imprisoned she might score it on a scale of 1 to 10 by instructing a crow to caw the required number of times. Only though whilst Mr Kipling was away building his baking empire in his wife’s absence.

Young Charles was no fool and had discovered the dark art of Computer Science in his spare time. It is with this tool that he set about recreating the greatest cake in the land.

After thinking long and hard about the problem at hand our Charles came to the conclusion that he knew two things:

  1. That the cake contained: butter, caster sugar, self-raising flour & eggs.
  2. The ingredients were linked together in some way.

With that he set off baking cakes…with python

Charles had a biologist friend, Cordelia who liked to doodle in their school classes. After he had finished explaining to her about his cake project Charles had asked her what it was that she was drawing. She said she was not sure; only that her father Reginald had told her that he used similar doodles to determine the chance of a offspring’s genotype. Charles later discovered that a genotype was like some information, a set of properties that dictated what an organism would look like. He also found out that a genotype also had an associated phenotype which was how that information was observed in the organism.

He thought that if cakes were organisms then the quantities of the ingredients contained within them would be the genotype and the way the cake tasted would be its phenotype.

e.g. 1kg sugar, 500g flour, 500g butter & 5 eggs would be the the genotype and the phenotype? Disgusting.

Charles asked himself, could he make cakes into organisms? He began with a list of ingredients cake = [] into which he put the information of the cake he was about to bake. His first cake was a bit like [100, 700, 650, 2] (Charles always listed his ingredients as he had recalled seeing them in the Kipling’s shop: butter, caster sugar, self-raising flour then eggs)

He noted that in baking is cake strange things happened to his ingredients sometimes it was as if the baking process randomly changed the amounts of his ingredients. To help him model the problem he wrote some code to reflect that.

def bake(cake):
	# 'Mutate' the amount of the ingredient
	for ingredient in range(len(cake)):
		if random.random() < 0.01:
			# The mixing bowl can only hold 10kg...
			cake[ingredient] = random.randint(1,10000)
	return cake

Charles quickly grew tired of manually typing in the cakes so he wrote some code to deal with that also…

def make():
	cake = []
	for _ in range(4):
	return cake

As part of Charles vision for cakes as an organism he knew that somehow he had to get the cakes to mate as well….

def mate(a, b):
	cake_jr = []
	midpoint = random.randint(0,4)
	for i in range(midpoint):
	for i in range(4 - midpoint):
		cake_jr.append(b[midpoint + i])
	return cake_jr

Then our Charles simply had to figure out how Mrs Kipling scored the cakes and he could genetically evolve the best cake!

As it happened Mrs Kipling simply scored the cake based on whether or not the quantity of a particular ingredient was correct. Charles called a cakes score it’s fitness since it would be used to determine how the cake would mate.

def fitness(cake):
	fitness = 1
	if cake[0] == 255:
		fitness += 1
	if cake[1] == 255:
		fitness += 1
	if cake[2] == 255:
		fitness += 1
	if cake[3] == 4:
		fitness += 1
	return int(math.exp(fitness))

Mrs Kipling knew that a small ingredient tweak made a big difference to her cake so the score became better in proportion to the correct quantities e.g. Exponential.

Adding this to his code Charles discovered that it only took 20 seconds to discover the best cake recipe in the land.

He went on to conclude that this was the best use of genetic algorithms ever.

Note: There are a number of scoring methods in the source code (see Github) the system used to produce the screen shot is exponential – who wants to wait more than 20 seconds for cake?

The Source Code for this is on Github.

Further Reading – The Nature of Code
Further Reading Cool Example