Week 4.1 - Genetic Algorithm
clear all
close all
clc
% Defining our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
% creating 2Dimensional mesh
[xx,yy] = meshgrid(x,y);
%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
surfc (xx,yy,f)
[x, fval] = ga(@stalagmite,2)
function [f]=stalagmite(input_vector)
%function that creates a stalagmite function with base equations
x=input_vector (1);
y=input_vector (2);
%the equation for stalagmite function depends upon 2 variables x and y
%the formulae required for output is given below:
a=(sin(5.1*pi*x+0.5))^6;
b=(sin(5.1*pi*y+0.5))^6;
f2a=exp(-4*log(2)*((x-0.0667)^2)/0.64);
f2b=exp(-4*log(2)*((y-0.0667)^2)/0.64);
%this -ve sign is needed for the GA function which only find the minimum
%values it is also responsible for the inverted shape which actually
f=-a.*b.*f2a.*f2b;
end
clear all
close all
clc
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases = 50
f=get_stalagmite (x y);
% study1 =satistical behavior
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
figure (1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shding interp
plot3(xopt,yopt,'marker','o','markersize',5,'markerfacecolor')
title ('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')
tic
%study2-statistical behavior -with upper and lower bounds)
for i=1:num_cases
[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,2)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')
study3 increasing GA iterations
options=optimoptions('ga');
options=optimoptions(options,'populationsize',150);
tic
for i=1:num_cases
[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(1,2,2)
hold on
surfc(x,y,f)
shading interp
AIM- write a concept of genetic algorithm by reffering to varius sources and information regarding stalagmite function.
Optimisation- it is a decision making process,it helps to find the best results.under a set of prioritized criteria or constraints.
these include maximizing factors such as productivity,strength,reliability,efficiency,utilization etc.
Genetic Algorithm- as shown above in the flow chart,we need to choose an optimisation algorithm to perform the optimisation
process.genetic algorithm is one of the optimisation processes.
it is frequently used to slove optimization problems,in research and in machine learning.
Global Maxima- it is largest value of the function.
Local Maxima-it is a minimum value of the function.