Week 5 - Genetic Algorithm
MAIN CODE
1) optimization
close all
clear all
clc
% inputs parameters
x = linspace(0,0.6,160);
y = linspace(0,0.6,160);
num_case = 50;
% using for meshgrid command
[xx, yy] = meshgrid(x,y);
% evaluating the objecting function using nested for 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
surf(x,y,-f)
shading interp
2) performing genetic algorithm
% 1) unbounded study
tic
for i = 1:num_case;
[inputs1, fopt1(i)] = ga(@stalagmite,2);
xopt1(i) = inputs1(1);
yopt1(i) = inputs1(2);
end
study_time1 = toc
figure(2)
subplot(2,1,1)
hold on
surf(x,y,-f)
shading interp
plot3(xopt1,yopt1,-fopt1,'marker','o','MarkerSize',5,'MarkerFaceColor','g')
title('unbounded study')
% figure (2)
subplot(2,1,2)
plot(-fopt1)
xlabel('iterations')
ylabel('minimum functions')
% 2) bounded study
tic
for i = 1:num_case;
[inputs2, fopt2(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6]);
xopt2(i) = inputs2(1);
yopt2(i) = inputs2(2);
end
study_time2 = toc
figure(3)
subplot(3,1,1)
hold on
surf(x,y,-f)
shading interp
plot3(xopt2,yopt2,-fopt2,'marker','o','MarkerSize',5,'MarkerFaceColor','g')
title('bounded study')
% figure (3)
subplot(3,1,2)
plot(-fopt2)
xlabel('iterations')
ylabel('minimum functions')
% 3) bounded study with population size
options = optimoptions('ga');
options = optimoptions(options,'populationsize',500);
tic
for i = 1:num_case;
[inputs3, fopt3(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options);
xopt3(i) = inputs3(1);
yopt3(i) = inputs3(2);
end
study_time3 = toc
figure(4)
subplot(4,1,1)
hold on
surf(x,y,-f)
shading interp
plot3(xopt3,yopt3,-fopt3,'marker','o','MarkerSize',5,'MarkerFaceColor','g')
title('bounded study with population size')
% figure (4)
subplot(4,1,2)
plot(-fopt3)
xlabel('iterations')
ylabel('minimum functions')
FUNCTION CODE
function [f] = stalagmite(input_vector);
x = input_vector(1);
y = input_vector(2);
f1x = (sin((5.1*pi*x)+0.5))^6;
f1y = (sin((5.1*pi*y)+0.5))^6;
f2x = exp(-4*log(2)*(((x-0.0667)^2)/0.64));
f2y = exp(-4*log(2)*(((y-0.0667)^2)/0.64));
f = -(f1x*f1y*f2x*f2y);
end
EXPLANATION :
1) defining the x and y inputs
2) inputs are 1d arrays are converted 2d arrays using meshgrid command
3) and then scripting the function code
4) after that evaluating the function using nested for loop
5) next performing the genetic algorithm study
6) there are three type of genetic algorithm study
* unbounded study ( we are not defined upper and lower bounds)
* bounded study (we are defined lb and ub and better then point one)
* bounded study with populatin size (we are defined lb, ub and population size)
7) unbounded study is multiply optimize value
8) bounded study they are 2 or 3 optimize value
9) bounded study with populatin size they are single optimum value
10) this case we are not using
* linear inequality constrians (A,B)
* linear equality constrains (Aeq,Beq)
* nonlinear constrains (nonlcon)