Week 5 - Genetic Algorithm
AIM-it is used to solving both constrained and unconstrained optimization problems.
EXPLANATION-A global optimum is the extrema (minimum or maximum) of the objective function for the entire input search space.
global optamization problem used enginner in
1. inter combustion engine
2. aircraft.
it is also used to minimize the drag value.
We can apply the genetic algorithm to solve problems in MATLAB ga calculates the minimum value of a function. So to find the global maximum of a function we can invert the function whose global maximum is to be found,find the minimum value of the inverted function, the negative of the minimum value will give the global maximum of the function.
program
clear all
close all
clc
% independent variacles
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
% matrix
[xx,yy]=meshgrid(x,y);
%Number of iterations
num_cases=50;
% starting the loop
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
% creates a three dimensional surface
figure(1)
surfc(xx,yy,-f)
shading interp %makes the surface look more smoother
xlabel('x value')
ylabel('y value')
title('Stalagmite Function')
%study 1
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)= inputs(1);
yopt(i)= inputs(2);
end
study_1=toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
title('Unbounded inputs')
xlabel('x value')
ylabel('y value')
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')
% case 2nd with upper and lower bounds
tic;
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_2_time=toc
figure(3)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
title('Bounded inputs')
xlabel('x value')
ylabel('y value')
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')
%study 3 incresing the population size
% by doing so we are increasing the probability of the function
% to converge at global maximum
options=optimoptions(@ga)
options=optimoptions(@ga,'PopulationSize',500) %increasing the population size
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_3_time=toc %gives the time taken for study
figure(4)
subplot(2,1,1)
surfc(x,y,-f) %creates a surface to visualise 3D
shading interp
title('bounded inputs with increased population size')
xlabel('x value')
ylabel('y value')
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',4,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt) %plotting -f vs number of iterations
xlabel('iterations')
ylabel('function maximum')
function code:
function [f] = stalagmite(input_vector)
a=input_vector(1);
b=input_vector(2);
term1=(sin((5.1*pi*a)+0.5))^6;
term2=exp(-4*log(2)*((a-0.0667)^2)/0.64);
term3=(sin((5.1*pi*b)+0.5))^6;
term4=exp((-4)*log(2)*((b-0.0667)^2)/0.64);
[f]=-(term1*term2*term3*term4); %inverts the objective function
end
Explanation of the above code: -
-
function[f] = stalagmite(input_vector) - function to perform the task of stalagmite function
-
%Intializing x and y input - comments to make a program user friendly
-
x=input_vector(1); -to initialize the first column
-
y=input_vector(2); - to initialize the second column
-
%term X1 is equation for f1_x and term Y1 is equation for f1_y - comments
-
termX1 = (sin(5.1*pi*x +0.5))^6; - initializing the term according to the equation
-
termY1 = (sin(5.1*pi*y + 0.5))^6; - initializing the term according to the equation
-
%%term X2 is equation for f2_x and term Y2 is equation for f2_y
-
termX2 = exp(-4*log(2)*((x-0.0667)^2/0.64));- initializing the term according to the equation
-
termY2 = exp(-4*log(2)*((y-0.0667)^2/0.64));- initializing the term according to the equation
-
f = termX1*termY1*termX2*termY2; - calculating the value according tho the stalagmite function
-
end - to terminate the function
Output of the above code: -


Output for the bounded inputs :-
