Chapter 6 - Practical Examples of Genetic Algorithms
Chapter 6 - Practical Examples of Genetic Algorithms
Chapter 6
Practical Examples of
Genetic Algorithms
3
Contents
1. Why use Genetic Algorithms
2. Application areas of GA
1. Transport: Genetic algorithms are used in the traveling salesman problem to develop transport plans that
reduce the cost of travel and the time taken. They are also used to develop an efficient way of delivering
products.
2. DNA Analysis: They are used in DNA analysis to establish the DNA structure using spectrometric
information.
3. Multimodal Optimization: They are used to provide multiple optimum solutions in multimodal optimization
problems.
2. Application areas of GA
4. Aircraft Design: They are used to develop parametric aircraft designs. The parameters of the aircraft are
modified and upgraded to provide better designs.
5. Economics: They are used in economics to describe various models such as the game theory, cobweb
model, asset pricing, and schedule optimization.
3. Traveling Salesman Problem using Genetic Algorithm
• The problem says that a salesman is given a set of cities, he has to find the shortest route to visit
each city exactly once and return to the starting city.
• Approach: In the following implementation, cities are taken as genes, string generated using these
characters is called a chromosome, while a fitness score which is equal to the path length of all the
cities mentioned, is used to target a population.
3. Traveling Salesman Problem using Genetic Algorithm
• Fitness Score is defined as the length of the path described by the gene.
• The fittest of all the genes in the gene pool survive the population test and move to the next
iteration.
• The value of the cooling variable keeps on decreasing with each iteration and reaches a threshold
after a certain number of iterations.
3. Traveling Salesman Problem using Genetic Algorithm
Algorithm:
1. Initialize the population randomly.
2. Determine the fitness of the chromosome.
3. Until done repeat:
1. Select parents.
2. Perform crossover and mutation.
3. Calculate the fitness of the new population.
4. Append it to the gene pool.
3. Traveling Salesman Problem using Genetic Algorithm
Pseudo-code
Initialize procedure GA{
Set cooling parameter = 0;
Evaluate population P(t);
While( Not Done ){
Parents(t) = Select_Parents(P(t));
Offspring(t) = Procreate(P(t));
p(t+1) = Select_Survivors(P(t), Offspring(t));
t = t + 1;
}
}
3. Traveling Salesman Problem using Genetic Algorithm: Code
# Structure of a GNOME
# defines the path traversed
# by the salesman while the fitness value
# of the path is stored in an integer
class individual:
def __init__(self) -> None:
self.gnome = ""
self.fitness = 0
return False
3. Traveling Salesman Problem using Genetic Algorithm: Code
# Function to return a mutated GNOME
# Mutated GNOME is a string
# with a random interchange
# of two genes to create variation in species
def mutatedGene(gnome):
gnome = list(gnome)
while True:
r = rand_num(1, V)
r1 = rand_num(1, V)
if r1 != r:
temp = gnome[r]
gnome[r] = gnome[r1]
gnome[r1] = temp
break
return ''.join(gnome)
# Function to return a valid GNOME string
# required to create the population
def create_gnome():
gnome = "0"
while True:
if len(gnome) == V:
gnome += gnome[0]
break
3. Traveling Salesman Problem using Genetic Algorithm: Code
# Function to return a valid GNOME string
# required to create the population
def create_gnome():
gnome = "0"
while True:
if len(gnome) == V:
gnome += gnome[0]
break
temp = rand_num(1, V)
if not repeat(gnome, chr(temp + 48)):
gnome += chr(temp + 48)
return gnome
3. Traveling Salesman Problem using Genetic Algorithm: Code
# Function to return the fitness value of a gnome.
# The fitness value is the path length
# of the path represented by the GNOME.
def cal_fitness(gnome):
mp = [
[0, 2, INT_MAX, 12, 5],
[2, 0, 4, 8, INT_MAX],
[INT_MAX, 4, 0, 3, 3],
[12, 8, 3, 0, 10],
[5, INT_MAX, 3, 10, 0],
]
f=0
for i in range(len(gnome) - 1):
if mp[ord(gnome[i]) - 48][ord(gnome[i + 1]) - 48] == INT_MAX:
return INT_MAX
f += mp[ord(gnome[i]) - 48][ord(gnome[i + 1]) - 48]
return f
3. Traveling Salesman Problem using Genetic Algorithm: Code
# Function to return the updated value
# of the cooling element.
def cooldown(temp):
return (90 * temp) / 100
found = False
temperature = 10000
3. Traveling Salesman Problem using Genetic Algorithm: Code
# Iteration to perform
# population crossing and gene mutation.
while temperature > 1000 and gen <= gen_thres:
population.sort()
print("\nCurrent temp: ", temperature)
new_population = []
for i in range(POP_SIZE):
p1 = population[i]
while True:
new_g = mutatedGene(p1.gnome)
new_gnome = individual()
new_gnome.gnome = new_g
new_gnome.fitness = cal_fitness(new_gnome.gnome)
temperature = cooldown(temperature)
population = new_population
print("Generation", gen)
print("GNOME FITNESS VALUE")
3. Traveling Salesman Problem using Genetic Algorithm: Code
for i in range(POP_SIZE):
print(population[i].gnome, population[i].fitness)
gen += 1
if __name__ == "__main__":
mp = [
[0, 2, INT_MAX, 12, 5],
[2, 0, 4, 8, INT_MAX],
[INT_MAX, 4, 0, 3, 3],
[12, 8, 3, 0, 10],
[5, INT_MAX, 3, 10, 0],
]
TSPUtil(mp)
3. Traveling Salesman Problem using Genetic Algorithm: Output
3. Traveling Salesman Problem using Genetic Algorithm: Output
4. Applying Genetic Algorithms to define a trading system
• When talking about quantitative trading, there are a large number of indicators and operators we can
use as a buy/sell rule.
• But apart from deciding what indicator to follow, the most important part is in setting the correct
parameters.
• So, one method we can use to find adequate parameters without spending too much time in the
simulation of a lot of combinations would be using a Genetic Algorithm.
• A Genetic Algorithm is an optimization method inspired by the evolution of species and natural
selection. Although it does not strictly belong to the Machine Learning field, it can be a good base to
build a ML algorithm.
4. Applying Genetic Algorithms to define a trading system
1. Initialization: the algorithm starts with an initial population, which may be generated totally randomly.
Every possible solution, (i.e. every element in that population), is called a chromosome.
2. Iterative Process:
- Crossover: those chromosomes are combined, creating a new population – the offspring.
Every chromosome in this new generation is formed by mixing pieces – genes by biological analogy – of
their ‘ancestors’.
- Mutation: usually, a mutation factor is also introduced, to allow some variation in the genes
apart from the combination of the already existing characteristics.
- Evaluation: last, we have to calculate the fitness value of every new individual.
4. Applying Genetic Algorithms to define a trading system
• The idea in the crossover process is to create a generation bigger than the first, because only the
best qualified individuals would survive. That means we will select the chromosomes that achieve
the best results to be the parents for the following ones.
4. Applying Genetic Algorithms to define a trading system
3. Stop Conditions:
we can use two or three different criteria to stop the iteration process:
- Reaching a fixed number of generations.
- Getting a satisfactory fitness level.
- Convergence of the algorithm.
• Implementing a Genetic Algorithm is in fact an easy task; the most challenging part is how to
transform our problem into chromosomes.
• We need mutable variables that we can transform easily and that do not require a huge amount of
memory, so the algorithm can be efficient.
4. Applying Genetic Algorithms to define a trading system: Pseudo algorithm
The following code is an example of a simple implementation of a genetic algorithm using Python syntax,
where:
max_iter: is the maximum number of iterations allowed before the algorithm stops.
n_repeats: is the maximum number of iterations allowed whose best fitness is worse than the overall best
fitness achieved in general during all the process. This is an easy way to control the convergence of the
method. If we don’t use this, we can spend time executing the code when the algorithm is already stagnant
in local optima.
N: is the number of individuals selected in every iteration to become the parents of the following
generation.
4. Applying Genetic Algorithms to define a trading system: Pseudo algorithm
i=0, fitness = 0, counter = 0
while i < max_iter:
if counter > n_repeats:
STOP
Y=[]
fit = [ ]
pairs = randompairsfrom (X)
for j in pairs:
y = crossover ( j )
y = randommutation ( y )
Y. append (y)
fit.append (fitness(y))
Y, fit = ordermaxtomin (Y, fit )
bestfitness = fit[ 0 ]
if bestfitness < fitness:
counter = counter+1
else:
counter = 0
X = Y [0: N]
i=i+1
fitness = bestfitness
5. Using Genetic Algorithms for Load Balancing in Fog
• To achieve load balancing in fog environment, we should select the most appropriate server to perform
the incoming task.
• First: Determine the overall properties for each server as shown in the following table.
• A new characteristic (Adaptive Weight (AW)) will be calculated according to the mentioned parameters
by (1) as:
• The priority of selecting a specific Fog Server depends on the value of AW.
• A Genetic Algorithm (GA) is used to optimize the value of α which leads to the minimum response time
by assigning a specific task to achieve the best value of AW.
6. String Problem using Genetic Algorithms
• Given a target string, the goal is to produce target string starting from a random string of the same
length. In the following implementation, following analogies are made –
Characters A-Z, a-z, 0-9, and other special symbols are considered as genes.
A string generated by these characters is considered as chromosome/solution/Individual.
• Fitness score is the number of characters which differ from characters in target string at a particular
index. So individual having lower fitness value is given more preference.
6. String Problem using Genetic Algorithms: C++ code
// C++ program to create target string, starting from
// random string using Genetic Algorithm
#include <bits/stdc++.h>
using namespace std;
// Valid Genes
const string GENES = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOP"\
"QRSTUVWXYZ 1234567890, .-;:_!\"#%&/()=?@${[]}";
Individual::Individual(string chromosome)
{
this->chromosome = chromosome;
fitness = cal_fitness();
};
6. String Problem using Genetic Algorithms: C++ code
// Perform mating and produce new offspring
Individual Individual::mate(Individual par2)
{
// chromosome for offspring
string child_chromosome = "";
vector<Individual> population;
bool found = false;
// create initial population
for(int i = 0;i<POPULATION_SIZE;i++)
{
string gnome = create_gnome();
population.push_back(Individual(gnome));
}
6. String Problem using Genetic Algorithms: C++ code
while(! found)
{
// sort the population in increasing order of fitness score
sort(population.begin(), population.end());
// if the individual having lowest fitness score ie.
// 0 then we know that we have reached to the target
// and break the loop
if(population[0].fitness <= 0)
{
found = true;
break;
}
// Otherwise generate new offsprings for new generation
vector<Individual> new_generation;
45