Mutations and fitness functions
To kick things off, we added a mutation for loop in the crossover function.
We shall get back to line 114 later, but that line is related to the restructuring of the chromosome so as to store the fitness value within the first index of the chromosome. So basically in line 114, we are skipping over the fitness value when carrying out mutations. ‘mutRate’ is a small value, around 0.001-0.03. And since the crossover deals with two chromosomes at a time, we can mutate both chromosomes at once. Here, Go really starts to shine through as a language for fast prototyping as long as no serious problems are encountered.
Now the fitness function:
We will use the fitness proportionate selection, also known as roulette wheel selection for a probabilistic approach to genetic selection of two individuals for mating. The idea here is to allow the individuals with the highest fitness to be selected more often for mating and thus distribute the good attributes of these individuals to more individuals in the subsequent generations.
This function basically states that a ‘p’ individual’s relative fitness is equal to its ‘f’ fitness divided by the total summated fitness of every individual in the whole population.
This in Go code looks something like this (well partially like this)
As can be seen, the initial fitness function is a maximisation function that does a basic addition of all elements of a chromosome and stores the result as the initial fitness in the first element of the individuals chromosome (line 33).
The argument for adding the fitness value into the chromosome and particularly in the first element of the chromosome is purely one of convenience. In future we will be shipping individuals between populations due to the HFC Model (or Island model) employed. This method allows the individual to carry all of its attributes to the new population and avoids us having to develop a central database to keep track of each individuals fitness values. This saves time and allows for high scalability. The advantage gained from using the first element in the chromosome allows for chromosomes with variable length if that ever becomes a requirement in future.
We have a variable ‘popFitSum’ that holds the summated fitness of the whole population. And as can be seen on a line 47, the comment states that Go doesn’t like jumping around between ints and floats, this actually broke the application several times but I couldn’t pin point the problem as it disappeared after an eclipse restart. Additionally I can’t get a float out of the operation on line 47, this is proving to be abit of a headache. I may have to change the whole application to use slices of floats instead of ints if no other solution can be found.