Challenge

Week 4 - Genetic Algorithm

%Program to calculate Global maxima of a Stalagmite function

clear all
close all 
clc

%Creating lines
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_cases=50;

% Creating Plots
[xx yy] = meshgrid(x,y);
for i = 1 : length(xx);
    for j = 1 : length(yy);
        input_value(1)= xx(i,j);
        input_value(2) = yy(i,j);
        f(i,j) = stalagmite_func(input_value);
    end
end

%For Calculating and Genrating the Plot 
tic
for i=1:num_cases;
    [inputs1,fopt1(i)]=ga(@stalagmite_func,2);
    xopt(i)=inputs1(1);
    yopt(i)=inputs1(2);
end
study1_time=toc;
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
zlabel('function value')
shading interp
plot3(xopt,yopt,-fopt1,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded Statistical Behvaiour')
subplot(2,1,2)
plot(-fopt1)
xlabel('iterations')
ylabel('function maximum')

%For Second Plot same function 
tic;
for i=1:num_cases;
    [inputs2,fopt2(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt(i)=inputs2(1);
    yopt(i)=inputs2(2);
end
study2_time=toc;
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
zlabel('function value')
shading interp
plot3(xopt,yopt,-fopt2,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded Statistical Behvaiour')
subplot(2,1,2)
plot(-fopt2)
xlabel('iterations')
ylabel('function maximum')

%Sucess Completed 

%Program to Define the Stalagmite Function
function [f] = stalagmite_func(input_vector)

%Inputs
x = input_vector(1);
y = input_vector(2);

term1 = (sin((5.1*pi*x )+ 0.5))^6;
term2 = (sin((5.1*pi*y) + 0.5))^6;
term3 = exp(-4*log(2)*((x-0.0667)^2)/0.64);
term4 = exp(-4*log(2)*((y-0.0667)^2)/0.64);

f = -(term1*term2*term3*term4);

end

%Completed
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.