Challenge

Week 4.1 - Genetic Algorithm

% Genetic alogorithm Main Code Basic code.



clear all
close all
clc
% Defining our search space

x= linspace(0,0.6,150);
y= linspace(0,0.6,150);

% Generation of the 2D array

[xx,yy] = meshgrid (x,y);


% Evaluating Stalagmite 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_func(input_vector);
    end 
end 

surfc(xx,yy,-f)

xlabel('x value')
ylabel('y value')

shading interp

[inputs, fval] = ga(@stalagmite_func,2)
        


Stalagmite Function Calling Code:

function [f] = stalagmite_func(input_vector)

term1 = ((5.1*pi.*input_vector(1))+0.5);
term2 = ((5.1*pi.*input_vector(2))+0.5);
term3 = (4*log(2).*(input_vector(1)-0.0667).^2)./0.64;
term4 = (4*log(2).*(input_vector(2)-0.0667).^2)./0.64;

f1x = sin(term1)^6;
f1y = sin(term2)^6;
f2x = exp(-term3);
f2y = exp(-term4);
f=-(f1x*f1y*f2x*f2y);

end

Unbouded Region Code Case 1

close all
clear all
clc
% Defining Our search space.
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

iterations_limits = 30;

% Forming 2-D array from defined search space.

[xx,yy] = meshgrid(x,y);

% Plot for unbounded case.

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_func(input_vector);
    end
end

tic

for i=1:iterations_limits

    [input_vector,fval(i)] = ga(@stalagmite_func,2);

    x_out(i) = input_vector(1);
    y_out(i) = input_vector(2);

end
case_time_1 = toc;

figure(1)

subplot(2,1,1)
surfc(xx,yy,-f);
xlabel('x')
ylabel('y')
title('case_1_(unbounded)','color','k')
shading interp

hold on;

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 2 : Inserting Bounding Region

close all
clear all
clc
% Defining Our search space.
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

iterations_limits = 30;

% Forming 2-D array from defined search space.

[xx,yy] = meshgrid(x,y);

% Plot for Bounding box insertion case.

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_func(input_vector);
    end
end

tic

for i=1:iterations_limits

    [input_vector,fval(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[0.6;0.6]);

    x_out(i) = input_vector(1);
    y_out(i) = input_vector(2);

end
case_time_2 = toc;

figure(1)

subplot(2,1,1)
surfc(xx,yy,-f);
xlabel('x')
ylabel('y')
title('case_2_(inserting bounding box)','color','k')
shading interp

hold on;

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 Limit Optimisation.

close all
clear all
clc
% Defining Our search space.
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

iterations_limits = 30;

% Forming 2-D array from defined search space.

[xx,yy] = meshgrid(x,y);

% Plot for Bounding box optimization case.

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_func(input_vector);
    end
end


options = optimoptions('ga','populationsize',100);
tic

for i=1:iterations_limits

    [input_vector,fval(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);

    x_out(i) = input_vector(1);
    y_out(i) = input_vector(2);

end
case_time_2 = toc;

figure(1)

subplot(2,1,1)
surfc(xx,yy,-f);
xlabel('x')
ylabel('y')
title('case_3_(Bounding limits optimizations)','color','k')
shading interp

hold on;

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 Limit Optimisation More Precise

close all
clear all
clc
% Defining Our search space.
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

iterations_limits = 30;

% Forming 2-D array from defined search space.

[xx,yy] = meshgrid(x,y);

% Plot for Bounding box optimization case.

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_func(input_vector);
    end
end

options = optimoptions('ga');
options = optimoptions(options,'populationsize',300);
tic

for i=1:iterations_limits

    [input_vector,fval(i)] = ga(@stalagmite_func,2,[],[],[],[],[0;0],[0.1;0.1],[],[],options);

    x_out(i) = input_vector(1);
    y_out(i) = input_vector(2);

end
case_time_2 = toc;

figure(1)

subplot(2,1,1)
surfc(xx,yy,-f);
xlabel('x')
ylabel('y')
title('case_4_(Bounding limits optimizations precise)','color','k')
shading interp

hold on;

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')


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.