Challenge

Week 5 - Genetic Algorithm

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Function Code%%%%%%%%%%%%%%%%%%%%%%%

function [f]= stalagmite_function(input)
%calcuating f1 x & Y
f1_x= (sin((5.1*pi).*input(1)+0.5)).^6;
f1_y= (sin((5.1*pi).*input(2)+0.5)).^6;
%calculating f2 x & Y
f2_x= exp((((input(1)-0.0667).^2)/0.64).*(-4*log(2)));
f2_y= exp((((input(2)-0.0667).^2)/0.64).*(-4*log(2)));
f=-(f1_x*f2_x*f1_y*f2_y); %minus sign for the global maxima
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Main - Code%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
close all
clear all
clc

x=linspace(0,0.6,100); % 1D array of X
y=linspace(0,0.6,100); %1D array of Y
iterations=100; %15 no of itertaions
[xx yy]=meshgrid(x,y); %converting 1D array to 2D array
%For loops to execute stalgmite_function

for i=1:length(xx)
    for j=1:length(yy)
        input(1)=xx(i,j); % accessing XX matrix (i,j)
        input(2)=yy(i,j); % accessing YY matrix (i,j)
        f(i,j)= stalagmite_function(input);
    end
end
%ploting the stalgmite representation
figure(1)
surfc(xx,yy,f)
xlabel('X-Value')
ylabel('Y-Value')
shading interp % to avoid mesh lines in the plot
[input,f_val] = ga(@stalagmite_function,2)

%study 1 - statistical behaviour - no bounds set
tic
for i=1:iterations

[input,fval(i)]=ga(@stalagmite_function,2);
x_op(i)=input(1);
y_op(i)=input(2);

end
case_time_1=toc

figure(2)
subplot(2,1,1)
surfc(xx,yy,f)
xlabel('x- Axis')
ylabel('y - Axis')
title('study 1 - No bounds set')
shading interp
hold on;
plot3(x_op,y_op,-fval,'marker','o','markersize',6,'markerfacecolor','g');
subplot(2,1,2)
plot(-fval); % iteration Vs function value
xlabel('No.Of Iterations')
ylabel('function Value')

%study 2 - setting up upper and lower bounds
tic
for i=1:iterations
% x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
[input,fval(i)]=ga(@stalagmite_function,2,[],[],[],[],[0;0],[0.6;0.6]);
% upper and lower bounds : 1 to 0.6
x_out(i)=input(1);
y_out(i)=input(2);

end
case_time_2=toc
figure(3);
subplot(2,1,1)
hold on;
surfc(xx,yy,-f)
xlabel('x - Axis')
ylabel('y - Axis')
title('study 2 - With upper and lower Bounds')
shading interp
plot3(x_out,y_out,-fval,'marker','o','markersize',6,'markerfacecolor','r');
subplot(2,1,2)
plot(-fval)
xlabel('No.Of Iterations')
ylabel('function Value')

%study 3 - increased population

options=optimoptions('ga');
options=optimoptions(options,'populationsize',300) %increased population
  
 tic
for i=1:iterations

[input,fval(i)]=ga(@stalagmite_function,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
x_out(i)=input(1);
y_out(i)=input(2);

end
 case_time_3=toc
figure(4)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('x');
ylabel('y');
title('Case 3 - increased population')
plot3(x_out,y_out,-fval,'marker','o','markersize',6,'markerfacecolor','r')
subplot(2,1,2)
plot(-fval)
xlabel('No Of Iterations')
ylabel('max Function Value')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% END %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.