Challenge

Week 4.1 - Genetic Algorithm

%performing optimization on stalagmite function
close all
clear all
clc
%input
%initializing x and y by taking 200 values b/w 0 to 0.6
%considering number of iteration to be 100
x = linspace(0,0.6,200);
y = linspace(0,0.6,200);
num_space = 100;

%forming 2D array
%we have 2 input variables, so we need to create a 3D plot to get the maxima
%2D array of x and y using meshgrid is created and stored in input_vector
%no. of iterations => num_space (100 iterations are to be done)
[xx,yy] = meshgrid(x,y);
for i=1:length(x)
    for j=1:length(y)
        input_vector(1)=xx(i,j);
        input_vector(2)=yy(i,j);
        f(i,j)=stalagmite(input_vector);
    end
end
%%
%study 1-unbounded
%resuts => maxima
%fopt => value of x and y where maxima occurs
%ga is initiated with random population
tic
%tic => starting the timer
for i=1:num_space
    [results,fopt(i)]=ga(@stalagmite,2)
    xopt(i)=results(1)
    yopt(i)=results(2)
end
study_time1=toc
%toc => to get the time required for performing the iterations
%tic-toc commands used to record time
figure(1)
subplot(2,1,1)
%plotting x,y and -f(to get the maxima) to get the 3D plot
surf(x,y,-f)
%shading interp used to smoothen the curve and remove gridlines in 3D plot
shading interp
hold on
%plot3 used to plot 3D points connected by line segment
plot3(xopt,yopt,-fopt,'marker','o')
subplot(2,1,2)
%-fopt plotted to visualize optimum values at each iteration
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
%study time 5.6897 seconds
%%
%failed to reach global maxima in previous case as values of x and y
%went beyond our boundaries
%ga performed with boundaries applied
%study 2-bounded
tic
for i=1:num_space
    [results,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6])
    xopt(i)=results(1)
    yopt(i)=results(2)
end
study_time2=toc
figure(2)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
%study time 6.9073 seconds
%%
%optimum values are within bounds but still not able to reach global maxima
%study3- increased initial population
%increasing initial population to 250 from 100
tic
options=optimoptions('ga')
options=optimoptions(options,'populationsize',250)
for i=1:num_space
    [results,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options)
    xopt(i)=results(1)
    yopt(i)=results(2)
end
study_time3=toc
figure(3)
subplot(2,1,1)
surf(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o')
subplot(2,1,2)
plot(-fopt)
xlabel('iteration')
ylabel('function maximum')
%study time 23.0048 seconds


%%
%stalagmite function used for optimization
function A = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f1 = (sin(5.1*pi*x+0.5)).^6;
f2 = exp((-4*log(2)*((x-0.0667).^2))/0.64);
f3 = (sin(5.1*pi*y+0.5)).^6;
f4 = exp((-4*log(2)*((y-0.0667).^2))/0.64);
A = -(f1*f2*f3*f4);
end
%stalagmite function gives a minima 
%therefore to get maxima we have defined "A" in a way so that we get maxima
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.