Challenge

Week 4.1 - Genetic Algorithm

The Genetic Algorithm is an optimization technique used to slove non linear optimizationproblems. It uses to find the maxima or minima in the required equation.

Main Program

clear all
close all
clc

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


%Creating 2D array using meshgrid fuction
[xx yy]=meshgrid(x,y);

% Nested loop for array value input

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


% Study 1 - Statestical Behaviour
tic 
for i=1:limit
    [inputs,fval(i)]=ga(@stalagmite_fun,2);
    x_opt(i)=inputs(1);
    y_opt(i)=inputs(2);
end
study1_time = toc
figure(1)
subplot(2,1,1)
surfc(xx,yy,-f)
xlabel('X_Axis')
ylabel('Y_Axis')
title('Unbounded inputs','color','k')
shading interp
hold on;
plot3(x_opt,y_opt,-fval,'marker','o','markersize',6,'MarkerFaceColor','r');
subplot(2,1,2)
plot(-fval)
xlabel('No of Iterations')
ylabel('Max Function Value')

% Case : 2 InsertingBounding Box

tic

for i=1:limit
    [inputs, fval(i)]=ga(@stalagmite_fun,2,[],[],[],[],[0;0],[0.6;0.6]);
    x_opt(i)=inputs(1);
    y_opt(i)=inputs(2);
end
study2_time = toc

figure(2);
subplot(2,1,1)
hold on;
surfc(xx,yy,-f)
xlabel('X_Axis')
ylabel('Y_Axis')
title('Case 2','Color','k')
shading interp
plot3(x_opt,y_opt,-fval,'marker','o','markersize',6,'markerfacecolor','r')
subplot(2,1,2)
plot(-fval)
xlabel('No of Iterations')
ylabel('Max function Value')

% Study 3 Increasing GA iteration
options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',100);

tic
for i=1:limit
       [inputs, fval(i)]=ga(@stalagmite_fun,2,[],[],[],[],[0;0],[0.6,0.6],[],[],options);
    x_opt(i)=inputs(1);
    y_opt(i)=inputs(2);
end
study3_time=toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
xlabel('X_Axis')
ylabel('Y_Axis')
plot3(x_opt,y_opt,-fval,'marker','o','markersize',5,'MarkerFaceColor','r')
subplot(2,1,2)
plot(-fval)
xlabel('No of Iterations')
ylabel('Max Function Value')
% Displying the Value of x & y for max value of function 
display(inputs)

% End of Program

Stalagmite Function

function [f] = stalagmite_fun(input_vector)

f1x= (sin((5.1*pi).*input_vector(1)+0.5)).^6;
f1y= (sin((5.1*pi).*input_vector(2)+0.5)).^6;

f2x=exp((((input_vector(1)-0.0667).^2)/0.64).*(-4*log(2)));
f2y=exp((((input_vector(2)-0.0667).^2)/0.64).*(-4*log(2)));
f1=f1x*f1y*f2x*f2y;
f=-f1;
end

Error Found

Values of x & y for maximum value of function.

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.