Challenge

Week 4.1 - Genetic Algorithm

#program_for_global_maxima

clear all
close all
clc

%defining the search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
%creating a 2D mesh
[xx,yy]=meshgrid(x,y);
%evaluating the stalgamite 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)
xlabel('x value')
ylabel('y value')
title('GLOBAL MAXIMA')
shading interp
[inputs,fval]=ga(@stalagmite,2)

figure(1)
mkdir('global_maxima')
cd('global_maxima')

saveas(figure(1),'global_maxima')



# Stalagmite_function for local maxima
function [f]=stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1=(sin(5.1*pi*x+0.5))^6;
f2=(sin(5.1*pi*y+0.5))^6;
f3=exp(-4*log(2)*(x-0.0667)^2/0.64);
f4=exp(-4*log(2)*(y-0.0667)^2/0.64);
f=(f1*f2*f3*f4);
end



#program_for_global_minima

clear all
close all
clc
x=linspace(0,0.6,150)
y=linspace(0,0.6,150)
[X,Y]=meshgrid(x,y)
num_cases=50
for i = 1:length(X)
    for j =1:length (Y)
        input_vector(1) = X(i,j);
        input_vector(2) = Y(i,j);
        f(i,j) = get_stalagmite(input_vector);
    end
end
tic
%study 1 - statistical behavior
for i=1:num_cases
 [inputs,fopt(i)]=ga(@get_stalagmite,2)
 xopt(i)=inputs(1)
 yopt(i)=inputs(2)
end
study1_time=toc
%plotting1
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','g')
title('Unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('function minimum')
tic
%study 2 -statistical behaviour
for i=1:num_cases
 [inputs,fopt(i)]=ga(@get_stalagmite,2,[],[],[],[],[0;0],[0.6;0.6])
 xopt(i)=inputs(1)
 yopt(i)=inputs(2)
end
study2_time=toc
%plotting2
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',3,'markerfacecolor','g')
title('Bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('function minimum')

%increrasing ga iteration
options=optimoptions('ga')
options=optimoptions(options,'populationsize',250)
tic
for i=1:num_cases
    [inputs,fopt(i)]=ga(@get_stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options)
 xopt(i)=inputs(1)
 yopt(i)=inputs(2)
end
study3_time=toc
%plotting3
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','g')
title('Bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('function minimum')


# Get_Stalagmite_function for local minima

function [f]=get_stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);

f1=(sin(5.1*pi.*x+0.5)).^6;
f2=(sin(5.1*pi.*y+0.5)).^6;
f3=exp(-4*log(2).*(x-0.0667).^2/0.64);
f4=exp(-4*log(2).*(y-0.0667).^2/0.64);
f=-(f1.*f2.*f3.*f4);
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.

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.