Challenge

Week 5 - Genetic Algorithm

Aim

The main aim is to optimize the Stalagmite function and to find the global maxima of the function by clearly explaining the concept of genetic algorithm and plotting the graph for the three studies i.e. unbounded inputs, bounded inputs, and bounded inputs with modified population size.

Stalagmites:

A Stalagmite is a type of rock formation that rises from the floor of a cave due to the accumulation of material deposited on the floor from ceiling drippings.

Genetic algorithm;

a genetic algorithm (GA) is a metaheuristic inspired by the process of natural selection that belongs to the larger class of evolutionary algorithms (EA). Genetic algorithms are commonly used to generate high-quality solutions to optimization and search problems by relying on biologically inspired operators such as mutation, crossover, and selection.

Working Principle:

  1. Initial population
  2. Fitness function
  3. Selection
  4. Crossover
  5. Mutation
  1. Initial Population:
  • The process begins with a set of individuals which is called a Population. Each individual is a solution to the problem you want to solve.
  • An individual is characterized by a set of parameters (variables) known as Genes. Genes are joined into a string to form a Chromosome (solution).
  • In a genetic algorithm, the set of genes of an individual is represented using a string, in terms of an alphabet. Usually, binary values are used (string of 1s and 0s). We say that we encode the genes in a chromosome.
  1. Fitness function:
  • The fitness function determines how fit an individual is (the ability of an individual to compete with other individuals). It gives a fitness score to each individual. The probability that an individual will be selected for reproduction is based on its fitness score.
  1. Selection:
  • The idea of the selection phase is to select the fittest individuals and let them pass their genes to the next generation.
  • Two pairs of individuals (parents) are selected based on their fitness scores. Individuals with high fitness have more chances to be selected for reproduction.
  1. Crossover:
  • Crossover is the most significant phase in a genetic algorithm. For each pair of parents to be mated, a crossover point is chosen at random from within the genes.
  1. Mutation:
  • In certain new offspring formed, some of their genes can be subjected to a mutation with a low random probability. This implies that some of the bits in the bit string can be flipped.

Using Equations :

The Function is for the Genetic Algorithm of the global minima but in order to attain the maxima value the ‘-’ is placed before the equation.

f(x,y)=f1.x×f2.x×f1.y×f2.y">f(x,y)=f1.x×f2.x×f1.y×f2.yf(x,y)=f1.x×f2.x×f1.y×f2.y

Where,

f1.x=[sin⁡(5.1πx+0.5)]6">f1.x=[sin(5.1πx+0.5)]6f1.x=[sin⁡(5.1πx+0.5)]6

f1.y=[sin⁡(5.1πy+0.5)]6">f1.y=[sin(5.1πy+0.5)]6f1.y=[sin⁡(5.1πy+0.5)]6

f2.x=exp⁡[-4ln⁡(2)×(x-0.0667)20.64]">f2.x=exp[−4ln(2)×(x−0.0667)20.64]f2.x=exp⁡[-4ln⁡(2)×(x-0.0667)20.64]

f2.y=exp⁡[-4ln⁡(2)×(y-0.0667)20.64]">f2.y=exp[−4ln(2)×(y−0.0667)20.64]f2.y=exp⁡[-4ln⁡(2)×(y-0.0667)20.64]

The Global maxima is as follows

f(x,y)=-(f1.x×f2.x×f1.y×f2.y)">f(x,y)=−(f1.x×f2.x×f1.y×f2.y)

f(x,y)=-(f1.x×f2.x×f1.y×f2.y)">Functions:

f(x,y)=-(f1.x×f2.x×f1.y×f2.y)">code,

function [f] = stalagmite(input_value)

f1x = (sin(5.1*pi*input_value(1) + 0.5)).^6;

f1y = (sin(5.1*pi*input_value(2) + 0.5)).^6;

f2x = exp(-4*log(2)*(((input_value(1) - 0.0667).^2)/(0.64)));

f2y = exp(-4*log(2)*(((input_value(2) - 0.0667).^2)/(0.64)));

f = -(f1x*f1y*f2x*f2y);

end

Main function For,

clear all 
close all
clc 
num_case= 100;
%difining the search space
x = linspace(0,0.5,100)
y = linspace(0,0.5,100)

%creating the 2 dimentional array

[xx yy] = meshgrid(x,y)

%evaluvating the stalagmate function 

for i = 1:length(xx)
    for j = 1:length(yy)
        input_vector(1) = xx(i,j);
        input_vector(2) = yy(i,j);
        f(i,j)= stalagmite(input_vector);
    end
end

surfc(xx,yy,f)

shading interp
 

 tic
 %study1 - stastical behaviour
 for i = 1:num_case
     [input,fopt(i)] = ga(@stalagmite , 2);
     xopt = input(1);
     yopt = input(2);
 end

 study1_time = toc

 figure(1)
 subplot(2,1,1)
 hold on
 surfc(x,y,-f)
 shading interp
 plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',3,'MarkerFaceColor','b')
 titel('unbounded inputs')
 subplot(2,1,2)
 plot(-fopt)
 xlabel('itteration')
 ylabel('function minimum')


  tic
 %study2 - statistical behavior - with upper and lower bounds
 for i = 1:num_case
     [input,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
     xopt = input(1);
     yopt = input(2);
 end
 
 study2_time = toc
 figure(2)
 subplot(2,1,1)
 hold on
 surfc(x,y,-f)
 shading interp
 plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',3,'MarkerFaceColor','b')
 titel('bounded inputs')
 subplot(2,1,2)
 plot(-fopt)
 xlabel('itteration')
 ylabel('function minimum')

 %study3 - increasing GA ireration
 options = optimoptions('ga');
 options = optimoptions('populationsize',500);


tic
 for i = 1:num_case
     [input,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
     xopt = input(1);
     yopt = input(2);
 end
 study3_time = toc
 figure(3)
 subplot(2,1,1)
 hold on
 surfc(x,y,-f)
 shading interp
 plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',3,'MarkerFaceColor','b')
 titel('bounded inputs')
 subplot(2,1,2)
 plot(-fopt)
 xlabel('itteration')
 ylabel('function minimum')

Error:

The Errors were made which were simple syntax errors that are rectified right away with the help of the instructions in the command window.

Result ;

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.