Challenge

Week 4.1 - Genetic Algorithm

clear all
close all
clc

%search space definition
x=linspace(0,0.7,150);
y=linspace(0,0.7,150);


%2D mesh diagram creating
[xx,yy]=meshgrid(x,y);

%stalagmate 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_fun(input_vector);
    end
end

surfc(xx,yy,-f)
shading interp
num_cases=50;
tic
for i = 1:(num_cases)
   [inputs,fval(i)] = ga(@stalagmite_fun,2);
   xval(i) = inputs(1);
   yval(i) = inputs(2);
end
time1 = toc
figure(1)
subplot(3,1,1)
surfc(xx,yy,-f)
hold on
shading interp
plot3(xval,yval,-fval,'marker','o','markersize',5,'markerfacecolor','g')
title('Unboud Inputs')
xlabel('x value')
ylabel('y value')
zlabel('z value')
grid on
subplot(3,1,3)
plot(-fval)
xlabel('Iterations')
ylabel('Maxima of the function')

%Case:2 Evaluate the statistical behaviour 
 
 tic
for i=1:num_cases

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

end
case_time_2=toc
figure(2);
subplot(2,1,1)
hold on;
surfc(xx,yy,-f)
xlabel('x')
ylabel('y')
title('case 2','color','k')
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('max Function Value')

%Case:3 bounding the input values
options=optimoptions('ga');
options=optimoptions(options,'populationsize',100)
  
 tic
for i=1:num_cases

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

end

 case_time_3=toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('x');
ylabel('y');
title('Case 3','color','k')
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')

 %Case:4 bounding all the input values and increase the population size
options=optimoptions('ga');
options=optimoptions(options,'populationsize',300)
  
 tic
for i=1:num_cases

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

end
 case_time_4=toc
figure(4)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('x');
ylabel('y');
title('Case 4','color','k')
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')
%Displaying the values of x & y for maximum value of function
display(input)


 
function [f]= stalagmite_fun(input_vector)

f1x= (sin((5.1*pi)*input_vector(1)+0.5)).^6;
f1y= (sin((5.1*pi)*input_vector(2)+0.5)).^6;

f2x= exp((((input_vector(1)-0.0667).^2)/0.64)*(-4*log(2)));
f2y= exp((((input_vector(2)-0.0667).^2)/0.64)*(-4*log(2)));
f=-(f1x*f1y*f2x*f2y);
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.