Week 4.1 - Genetic Algorithm
clear all
close all
clc
%inputs
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases = 50;
%creating 2D mesh
[xx yy] = meshgrid(x,y)
%preparing stalagmite function
for i=1:length(xx)
for j=length(yy)
input_vector(1)=xx(i,j);
input_vector(2)=yy(i,j);
f(i,j)= stalagmite(input_vector);
end
end
%study 1 statistical behaviour
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(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('Function minimum')
%study 2 - statistical behaviour - with upper and lower bounds
tic
for k=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,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(-fopt2)
xlabel('Iterations')
ylabel('function minimun')
%study 3 increasing GA iterations
options=optimoptions('ga');
options=optimoptions(options,'Populationsize',170);
tic
for k=1:num_cases
[inputs, fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_time=toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('function minimun')
% stalagmite function
function [f] =stalagmite(input_vector)
a=input_vector(1);
b=input_vector(2);
f1x=(sin(5.1*pi*a+0.5))^6;
f1y=(sin(5.1*pi*b+0.5))^6;
f2x=exp(-4*log(2)*(a-0.0667)^2/0.64);
f2y=exp(-4*log(2)*(b-0.0667)^2/0.64);
f=-(f1x*f2x*f1y*f2y);
end
THE COCEPT OF GENETIC ALGORITHM
- A genetic algorithm is used to solve the optimization problems.These problems can either be constrained or unconstrained bsed on a natural selection process that mimics biological evolution.The algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents to produce the children for the next generation.