Challenge

Week 4.1 - Genetic Algorithm

Concept of Genetic Algorithm:

Genetic Alogorithm is a term were two individual parent terms are taken to create a new offspring for the next generation. The individuals called the parent contributrs for the process. Crossover rules combines two parent to form a new offspring.

Stalagmite Function:

Stalagmite function is a mathematical model to generate local minima and maxima. Stalagmite function has a number of maxima and minima and a global maxima and minima.

Syntax for genetic algorithm is "ga"

x = ga(func,nvalues,A,B,Aeq,Beq,lu,up)

x = optimal solution

func = objective function

nvaslues = number of values (here we consider x,y therefore it is two)

A,B = linera inequalities constraints

Aeq,Beq = linera equalities constraints

Matlab program for Stalagmite function to find maxima :

function[f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
A = (sin((5.1*pi*x)+0.5))^6;
B = (sin((5.1*pi*y)+0.5))^6;
C = exp(-4*log(2)*((x-0.0667)^2)/0.64);
d = exp(-4*log(2)*((y-0.0667)^2)/0.64);
f= -(A*B*C*d);
end
The stalagmite function by default find the minima. To find the maxima ve- command is used.
clear all
close all
clc
% x = input_vector(1);
% y = input_vector(2);
%
% A = (sin((5.1*pi*x)+0.5))^6;
% B = (sin((5.1*pi*y)+0.5))^6;
% C = exp(-4*log(2)*((x-0.0667)^2)/0.64);
% d = exp(-4*log(2)*((y-0.0667)^2)/0.64);
%
% f= -(A*B*C*d);
%defining the search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
n = 50
%2d mesh
[xx,yy] = meshgrid(x,y);
%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
%studying the statistical behaviour - 1
tic
for i = 1:n
[inputs, foptimum(i)] = ga(@stalagmite,2);
xoptimum(i) = inputs(1);
yoptimum(i) = inputs(2);
end
study1_time = toc
figure(1)
subplot(2,1,1)
hold on
surf(xx,yy,-f)
shading interp
xlabel = ('X_axis')
ylabel = ('Y_axis')
title('studying the statistical behaviour unbound output')
plot3(xoptimum,yoptimum,-foptimum,'o', 'MarkerSize',2,'MarkerFaceColor','r')
subplot(2,1,2)
plot(-foptimum)
xlabel('no of iterations')
ylabel ('function maxima')
%studying statical behaviour with upper and lower bounds
for i = 1:num_case
[input,foptimum(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xoptimum(i) = inputs(1);
yoptimum(i) = inputs(2);
end
study2_time = toc
figure(2)
subplot(2,1,1)
hold on
surf(x,y,f)
shading interp
xlabel = ('X_axis')
ylabel = ('Y_axis')
title('bounded output')
plot3(xoptimum,yoptimum,foptimum,'o', 'MarkerSize',2,'MarkerFaceColor','r')
subplot(2,1,2)
plot(foptimum)
xlabel('iteration')
ylabel ('function minima')
%increasing the ga iterations study-4
tic
options = optimoptions('ga');
options = optimoptions(options,'populationsize',270);
for i = 1:num_case
[input,foptimum(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xoptimum(i) = inputs(1);
yoptimum(i) = inputs(2);
end
study3_time = toc
figure(3)
subplot(2,1,1)
hold on
surf(x,y,f)
shading interp
xlabel = ('X_axis')
ylabel = ('Y_axis')
title('bounded output')
plot3(xoptimum,yoptimum,foptimum,'o', 'MarkerSize',2,'MarkerFaceColor','r')
subplot(2,1,2)
plot(foptimum)
xlabel('iteration')
ylabel ('function minima')
The 'x' and 'y' array is defined using 'linspace' command to create value at 150 intervals.
2D mesh is created for x and y using meshgrid and numbwer of iteration is given by n = 50.
The input vector 'xx' and 'yy' are defined and stalagmite functionfor these input vector is defined.
To know the time that is taken to solve the loop for 50 iteraqtions 'tic' 'toc' command is used before the loop command.
Then the function created in the loop is plooted using the 'surfc' command.
Due to some error the plot number 2,3 is not plotted.
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.