Challenge

Week 4.1 - Genetic Algorithm

%stalagmite function
function  s = stalagmite(vector)

x= vector(1);
y= 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));
f2y =exp(-4*log(2)*((y-0.0667).^2));
s =-(f1x.*f1y.*f2x.*f2y);

end

%main code ga
clear all
close all
clc
%values for x and y
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

%number of cases
num_cases=40;

%1d to 2d mesh
[x,y]= meshgrid(x,y);

for m=1:length(x)
   for n=1:length(y)
    vector(1)= x(m,n);
    vector(2)= y(m,n);
    s(m,n)= stalagmite(vector);
   end
end

%case1
%time limit
tic
for m = 1:num_cases
%carring the function
    [results,sout(m)]=ga(@stalagmite,2);
    xout(m)=results(1);
    yout(m)=results(2);
end
case_time1 = toc;
figure(1)
subplot(2,1,1)
surf(x,y,-s)
shading interp
hold on
plot3(xout,yout,-sout,'marker','o')
subplot(2,1,2)
plot(-sout)
xlabel('iteration')
ylabel('maximum')


%case2
%time limit
tic
for m= 1:num_cases
%carring the function
    [results,sout(m)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6]);
    xout(m)= results(1);
    yout(m)= results(2);
end
case_time2 =toc;
figure(2)
subplot(2,2,1)
surf(x,y,-s)
shading interp
hold on
plot3(xout,yout,-sout,'marker','o')
subplot(2,1,2)
plot(-sout)
xlabel('iteration')
ylabel('maximum')

%case3
%time limit
tic
%increasing the population size
options = optimoptions('ga');
options = optimoptions('ga','populationsize',200);

for m= 1:num_cases
    [results,sout(m)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options);
    xout(m)= results(1);
    yout(m)= results(2);
end
case_time3 = toc;
figure(3)
subplot(2,1,2)
surf(x,y,-s)
shading interp
hold on
plot3(xout,yout,-sout,'marker','*')
subplot(2,2,1)
plot(-sout)
xlabel('iteration')
ylabel('maximum')
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.