Challenge

Week 4.1 - Genetic Algorithm

Aim :

To write a code in MATLAB to optimize the stalagmite function and to find the global maxima of the function. To explain genetic algorithm. To plot the graph for all the studies

Procedure:

The program is created for stalagmite function the genetic algorithm is used to find the minimum value. Through this command using for loop condition we founded the minimum value and using surf we created the plot

When this method found the global minima the inverse of it should be the maxima through this only changing the function symbol in surf leads to global maxima

With use of formula shown in the video we created the function and the function is called in the loop and for each value answer is founded and function is created for plot

To create plot and find the f value we need the one dimensional value so the meshgrid id used to create one dimensional

ga(@stalagmite,2)

calling the function and finding the fval the ga is genetic algorithm this finds the minimum value it does not have any restriction applied so takes any value as initial and calculate as per above then 2 stands for the position no of variables used

for i=1:num_cases

[inputs,fval_opti(i)] = ga(@stalagmite,2);

x_opti(i) = inputs(1);

y_opti(i) = inputs(2);

end

in the above statementnum cases value is 60 so the for loop is run 60 times here the restriction are not provided for ga this is used in study 1

for i=1:num_cases

[inputs,fval_opti(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);

x_opti(i) = inputs(1);

y_opti(i) = inputs(2);

end

here the lower and upper limits are provided with in the this the ga is with in the limits

options = optimoptions ('ga');

options = optimoptions (options,'populationsize',170)

for i=1:num_cases

[inputs,fval_opti(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);

x_opti(i) = inputs(1);

y_opti(i) = inputs(2);

end

here the lower and upper limits are provided and also option value is provide 170 so it gets the particular points so it get the requirements

program and plots are attached with this

Formula 

function f = stalagmite(input_vector)

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

f1 = (sin(5.1*pi*x + 0.5))^6;
f2 = (sin(5.1*pi*y + 0.5))^6;
f3 = exp(-4*log(2)*(x-0.0667)^2/0.64);
f4 = exp(-4*log(2)*(y-0.0667)^2/0.64);

f= -(f1*f2*f3*f4);
end

Main program

clear all
close all
clc

%Defining our search space 
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

%creating a 2D dimensional mesh
[xx,yy] = meshgrid (x,y);

%Evaluation of 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(input_vector);
    end
end
surf(x,y,-f)

shading interp

[input,fval] = ga(@stalagmite,2)

study1

clear all
close all
clc

%Defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

num_cases = 60

%creating a 2D dimensional mesh
[xx,yy] = meshgrid (x,y);

%Evaluation of 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(input_vector);
    end
end

%study 1- statistical behaviour without bonds
tic
for i=1:num_cases
    [inputs,fval_opti(i)] = ga(@stalagmite,2);
    x_opti(i) = inputs(1);
    y_opti(i) = inputs(2);
   
end

study1_time=toc

figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f);
xlabel('x-axis');
ylabel('y_axis');
zlabel('z_axis');
shading interp
plot3(x_opti,y_opti,-(fval_opti),'marker','o','markersize',5,'markerfacecolor','r');
title('bounded inputs');
subplot(2,1,2);
plot(-fval_opti);
xlabel('no of iterations');
ylabel('f maximum');

Study2

clear all
close all
clc

% inputs fore defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

num_cases = 60

%creating a 2D dimensional mesh
[xx,yy] = meshgrid (x,y);

%Evaluation of 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(input_vector);
    end
end

%study 2- statistical behaviour with upper and lower limit
tic
for i=1:num_cases
    [inputs,fval_opti(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    x_opti(i) = inputs(1);
    y_opti(i) = inputs(2);
   
end

study2_time=toc

figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f);
xlabel('x-axis');
ylabel('y_axis');
zlabel('z_axis');
shading interp
plot3(x_opti,y_opti,-(fval_opti),'marker','o','markersize',5,'markerfacecolor','r');
title('bounded inputs');
subplot(2,1,2);
plot(-fval_opti);
xlabel('no of iterations');
ylabel('f maximum');

study 3

clear all
close all
clc

% inputs fore defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

num_cases = 60

%creating a 2D dimensional mesh
[xx,yy] = meshgrid (x,y);

%Evaluation of 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(input_vector);
    end
end

%study 3- increasing ga iterations
options = optimoptions ('ga');
options = optimoptions (options,'populationsize',170)
tic
for i=1:num_cases
    [inputs,fval_opti(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    x_opti(i) = inputs(1);
    y_opti(i) = inputs(2);
   
end

study3_time=toc

figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f);
xlabel('x-axis');
ylabel('y_axis');
zlabel('z_axis');
shading interp
plot3(x_opti,y_opti,-(fval_opti),'marker','o','markersize',5,'markerfacecolor','r');
title('bounded inputs');
subplot(2,1,2);
plot(-fval_opti);
xlabel('no of iterations');
ylabel('f 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.