BIOLOGICALLY INSPIRED ALGORITHMS 2(GA)
In the second blog post of the BIOLOGICALLY INSPIRED ALGORITHMS series I'll be writing about Genetic Algorithm, I first encountered Genetic Algorithms while working on the same industrial IoT intrusion detection project I mentioned in my PSO post. While PSO mimicked swarm behaviour, GA fascinated me with its Darwinian approach to optimization. For this blog post, I’ve written Python code to solve the Rastrigin function instead of the client-specific problem. The full code is available in the GitHub repo linked below.
KEY CONCEPTS
Chromosome: A candidate solution encoded as a string of parameters (genes).
Population: A group of chromosomes.
Fitness Function: Evaluates solution quality (lower = better for minimization).
Selection: Favors fitter chromosomes for reproduction.
Crossover: Combines two parents to create offspring.
Mutation: Randomly alters genes to maintain diversity.
ALGORITHM STEPS
Initialization: Generate a random population of chromosomes.
Evaluation: Calculate fitness for each chromosome.
Selection: Choose parents based on fitness.
Crossover: Create offspring by mixing parent genes.
Mutation: Randomly tweak offspring genes.
Replacement: Form a new population from parents/offspring.
Termination: Stop after meeting criteria (e.g., max generations).
THE RASTRIGIN FUNCTION
Get Funboy Ea’s stories in your inbox
Join Medium for free to get updates from this writer.
A notorious optimization benchmark with many local minima. Its global minimum is x = [0, 0, …, 0]. The n-dimensional version is defined as:
The function handles any input dimension in my code, making it ideal for testing GA’s scalability.
IMPLEMENTATION DETAILS
GA Parameters
Population Size: 50 (balance between diversity and speed).
Crossover Rate: 80% (aggressive recombination).
Mutation Rate: 10% (prevents premature convergence).
Elitism: Enabled to retain the best solution.
ADVANTAGES OF GA
Global Search: Escapes local minima better than gradient-based methods.
Flexibility: Works with discrete, continuous, or mixed variables.
Parallelism: Naturally suited for distributed computing.
LIMITATIONS OF GA
Speed: Requires 100+ generations for complex problems.
Parameter Tuning: Sensitive to mutation/crossover rates.
No Guarantees: May miss the global optimum in limited time.
While PSO excels in continuous spaces, GA’s mutation operator prevents stagnation. In my next post, I’ll show how combining them leverages exploration (GA) and exploitation (PSO).
The full code for this post :
“https://github.com/abeenoch/PARTICLE-SWARM-OPTIMIZTION-GENETIC-ALGORITHM/blob/main/GA.py"






