Week 4.1 - Genetic Algorithm
Aim:
To write a Program in MATLAB to optimise the stalagmite function and find the global maxima of the function.
Program:
_% Stalagmite Function
function[f]=stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1=(sin((5.1*pi*x)+0.5))^6;
f2=(sin((5.1*pi*y)+0.5))^6;
f3=exp(-4*log(2)*((x-0.0667)^2)/0.64);
f4=exp(-4*log(2)*((y-0.0667)^2)/0.64);
f=-(f1*f2*f3*f4);
end
% Genetic Algotithm
clear all
close all
clc
% Creating a 1 dimensional array
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
% creating a 2 dimensional array
[xx,yy]=meshgrid(x,y);
%evaluating a 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
% statistical behaviour
tic
for i=1:n
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study1_time=toc;
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('x axis')
ylabel('y axis')
title('Statistical behaviour iwht unbound inputs')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('No. of Iterations')
ylabel('Function maximum')
% Statistical beh. with upper and lower bounds
tic
for i=1:n
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[0;0],[1;1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study2_time=toc;
figure(2)
subplot(2,1,1)
hold on
surf(xx,yy,-f)
shading interp
xlabel('x axis')
ylabel('y axis')
title('Statistical behaviour wiht upper and lower bounds')
plot3(xopt,yopt,-fopt,'markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('No. of Interations')
ylabel('Function maximum')
% Statistical Beh. with increasing interations of GA
options=optimoptions('ga')
options=optimoptions(options,'populationsize',300);
tic
for i=1:n
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i)=inputs(1);
yopt(I)=inputs(2);
end
study3_time=toc;
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('x axis')
ylabel('y axis')
title('Statistical behavior with increasing interations')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('No of iterations')
ylabel('function maximum')
Explaination:
- the program starts with clear all, close all, clc.
- defining the x and y using linspace to create 150 equal intervals
- using meshgrid command a 2d mehs of x and y array is created, n=50
- after preparing the inputs for the function , we need to obtain 3 cases of genetic algorithm.
- in first study, the max valuess of xx and yy are defined
- tic and toc command is used before and after the loop, for solving the loop in 50 iterations
- to neglect the gridlines on the surface and to obatin a smooth and clean polt we used shading interp and then plot
- the only difference between study1 and study2 is in the 'ga' syntax, the algorith is provided lower limits and upper limits as[0;0] and [1;1]
- for study 3the number of iterations are increased to 300. so for this we use options=optimoptions('SolverName,'Name','Value"), 'popultaions' 300.
Errors:
- invalid expression. use brackers instead of parenthese.
- [x,fval]=ga(@stlagmite(inputvector), f(i,j)=stalagmite(input_vector).
Results:
Thus, a Program in MATLAB to optimise the stalagmite function and find the global maxima of the function is done and the output is verified