Week 4 - Genetic Algorithm
the code is given below
clear all
close all
clc
%defining search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases = 50
f = get_stalagmite(x,y);
tic
% Study 1 - Statistical Behaviour
for i = 1:num_cases
[inputs , fopt(i)] = ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time = toc
%plot
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-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')
tic
% study 2 - 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,1)
hold on
surfc(x, y, -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 minimum')
% syudy 3 - increasing GA Iterations
options = gaoptimset('ga');
options = gaoptimset(options,'populationsize',170);
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(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs with modified population size')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function minimum')
Total_time = study1_time + study2_time + study3_time
% creating a 2D mesh
[xx yy] = meshgrid(x,y);
% evaluate 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)
shading interp
[x,fval] = ga(@stalagmite,2)
the formula of stalagmite function is given below .
The genetic Algorithm is inspired from the charles darwin thoery of natural selection.we are performing three studies here i.e. 1.) Unbound statistical behavior. 2.) bound statistical behavior. 3.) bound statistical behavior with optimisation.
the minnimum value can be easily obtained but to get maximum value a ,'-'ve sign is added in front of the function so that it automatically give maxima value.
Explanation of the code:-
very first, clear command are used to clear everything from comman , editor and workspace.
then defining search space and number cases equals to 50.then we used get_stalagmite function here.
now, the first study is going to be cionducted using the for loop and tic-toc command is used to calculate the time taken by the software to run the program.
plot3 command is used with the help of marker,the label and titles are also added.
finally,the value of global maxima,global maxima x and y values and time taken for program evaluation are obtained in the command window as result including total time.
Errors faced during programming:-
1.) the first error is shown in image(20) , i got a habit to run the function also which is to be improved so i call the function in command window and i got value.
2.) the second error that i was getting is of version problem of matlab.since i am using R2014b so in place of 'optimoptions' i used 'gaoptimset' and the output i got.