Implement Continuous Genetic Algorithm from Scratch in MATLAB
- ftmghorbani
- Oct 9, 2020
- 2 min read
Updated: Nov 28, 2020
Genetic algorithm (GA) can be used to solve optimization problems. It mimics evolution to find the best solution. One of the most significant advantages of GAs is their ability to find a global minimum or max without getting stuck in local values. The continuous means the GA we are going to create will be using floating numbers or integers instead of binary numbers.

First, GA creates an initial population of randomly generated candidate solutions, then these solutions are evaluated, and their fitness value is calculated. The fitness value determines how good a solution is, higher the fitness value better the solution. The first genetic operation is Selection; in this operation, the individuals that are going to be moving on to the next generation are selected. After the selection process, pairing operation should be done. Pairing operation pairs the selected individuals two by two for the Mating operation.
The Mating operation takes the paired parent individuals and creates offsprings, which will be replacing the individuals that were not selected in the Selection operation, so the next generation has the same number of individuals as the previous generation. This process is repeated until the termination criteria is met.
After the function to create individuals is created, another function is needed to create the population.
In first section, I have coded GA step by step. Firstly, I defined gene range (input ranges), number of genes (variables), population size (individuals), mutation rate and generation limit. Secondly, based on GA process and aforementioned parameters, initial population, the population function and fitness function are created.
Then, zero generation is created and sorted by their fitness values:
generation = sortrows(generation, number_of_genes + 1, 'descend');
sorts generation based on the columns specified in the vector column in descending order (number_of_genes + 1, which is fitness_values column)
If the initial population does not meet the requirements of our termination criteria, the next generation will be created by GA. Maximum number of generations: We could limit the maximum number of generations created by GA. Thus, we check termination criteria at this stage by the condition shown below (maximum number of generation):
if generation_count > generation_limit
break;
To create next generation, I used fittest half selection method:
next_population(1:half_population_size, :) = sorted_population(1:half_population_size, :);
The next step is to apply pairing and mating processes. I have used fittest method for pairing.
Fittest: In this method, individuals are paired two by two, starting from the fittest individual. By doing so, fitter individuals are paired together. Thus, from parent chromosomes x and y, it can be reproduced two offspring’s:
offspring(1)= α x + (1 - α) y, and offspring(2) = (1 – α) x + α y
where α = randi(1);
The final genetic operation is random mutations. Random mutations happen in the selected individuals and their offsprings to improve variety of the next generation.
Finally, GA found which genes had to be muted randomly and performed reset method for mutation operation:
mutant_indices = find(temp < mutation_rate);
muted_population(mutant_indices) = mutants;
If you are interested, you can find the code implemented in MATLAB here.




Comments