Challenge

Week 4.1 - Genetic Algorithm

  • Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.

first we will write the code for the given mathematical expression.

function [F] = stalagmite(input_vector)

x=input_vector(1);
y=input_vector(2);

f1_x= (sin(5.1*pi*x+0.5)).^6;

f1_y = (sin(5.1*pi*y+0.5)).^6;

f2_x = exp(-4*log(2)*((x-0.0667).^2)/0.64);

f2_y = exp(-4*log(2)*((y-0.0667).^2)/0.64);

[F] = -(f1_x.*f1_y.*f2_x.*f2_y);

end

% Step 1
% clearing workspace, command window & closing all plots

close all
clear all
clc
% Define our search space 

x=linspace(0,0.6,150);

y = linspace(0,0.6,150);
num_spaces=50;

% Step 3
% Calculating a 2Dimention1 mesh

[xx, yy] = meshgrid(x,y);

% Step 4
%Evaluating the stalagmite 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


% study 1 satistical behaviour
tic
 for i=1:num_spaces
     [results,fopt(i)]=ga(@stalagmite,2)
     xopt(i)=results(1)
     yopt(i)=results(2)
 end
 study_time1=toc
 figure(1)
 subplot(2,1,1)
 surf(x,y,-f)
 shading interp
 hold on
 plot3(xopt,yopt,-fopt,'marker','o')
 subplot(2,1,2)
 plot(-fopt)
 xlabel('iteration')
 ylabel('function maximum')
 
 
 %bounded conditions
 tic
  for i=1:num_spaces
     [results,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6])
     xopt(i)=results(1)
     yopt(i)=results(2)
 end
 study_time2=toc
 figure(2)
 subplot(2,1,1)
 surf(x,y,-f)
 shading interp
 hold on
 plot3(xopt,yopt,-fopt,'marker','o')
 subplot(2,1,2)
 plot(-fopt)
 xlabel('iteration')
 ylabel('function maximum')
 
% study three increased population
 tic
 options=optimoptions('ga')
 options=optimoptions(options,'populationsize',250)
  for i=1:num_spaces
     [results,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options)
     xopt(i)=results(1)
     yopt(i)=results(2)
 end
 study_time3=toc
 figure(3)
 subplot(2,1,1)
 surf(x,y,-f)
 shading interp
 hold on
 plot3(xopt,yopt,-fopt,'marker','o')
 subplot(2,1,2)
 plot(-fopt)
 xlabel('iteration')
 ylabel('function maximum')

what is genetic algorithym?

A genetic algorithm is a search heuristic that is inspired by Charles Darwin’s theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.

The process of natural selection starts with the selection of fittest individuals from a population. They produce offspring which inherit the characteristics of the parents and will be added to the next generation. If parents have better fitness, their offspring will be better than parents and have a better chance at surviving. This process keeps on iterating and at the end, a generation with the fittest individuals will be found.

This notion can be applied for a search problem. We consider a set of solutions for a problem and select the set of best ones out of them.

Five phases are considered in a genetic algorithm.

  1. Initial population
  2. Fitness function
  3. Selection
  4. Crossover
  5. Mutation

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.

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.

Selection

The idea of 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 chance to be selected for reproduction.

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.

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.

after this algorithm terminates if the population has converged (does not produce offspring which are significantly different from the previous generation). Then it is said that the genetic algorithm has provided a set of solutions to our problem.

syntax for ga algorithm

first use the commands

close all and clear all and clc

to start the script

assign the search space value for both x and y from (0 to 0.6)

and having 150 values betwwen them.

then we intialize x and y values upto 150 values between 0 to 0.6

we consider no of iterations upto 50(for ga)

now we will make use of the meshgrid command to convert those 2d array to 3d plot and are stored in the input vectors where later it can be called to the stalagmite function as an arugument.

on the other hand

we write the function code for the given mathematical expression and since we want the golbal maximum value we wil save the output in terms of negative value.

(by default ga function find theminimum value)

study 1 (statstical behaviour)

now we will study the statstical behaviour of the ga function

the iteration are made for 50 times and the outputs are saved in the results which as the maxima and fopt(the points where maxima is obtained)

in this case ga is intilazied with random population.

later the results are stored in xopt,yopt ,to plot 3d plot x ,y,-f plots are required.

plot 3 command is used to plot maximas

and to see the fopt values at each iteration

we will plot fopt aswell

and tic toc command is used to find how much did it take to evaluate the code.

study case 2

in the above case we have seen it gave the maxima beyound the boundaries which we did not want to happen.

so,

we will now put boundaries i.e., from 0 to 0.6

code goes like this

[results,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6])

similarly we store resluts into xopt,yopt and aswell we will plot xopt,yopt fopt values to get 3d surface plot.

plot 3 command is used to plot maximas

and to see the fopt values at each iteration

we will plot fopt aswell

and tic toc command is used to find how much did it take to evaluate the code.

study case 3

in the above case we have see that though it gave the output witin the limits yet it has many maximas so now we will increase the population size

this is done by using the commands

option=optimoptions('ga')

options=options(options,'populationsize',250)

and

similarly we store resluts into xopt,yopt and aswell we will plot xopt,yopt fopt values to get 3d surface plot.

plot 3 command is used to plot maximas

and to see the fopt values at each iteration

we will plot fopt aswell

and tic toc command is used to find how much did it take to evaluate the code.

plotting images

1. random population

2. bounded condition with population size 50

3. increased population

After many iteration the it gave the maximum value since we have increased the population time.

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.

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.