Challenge

Week 4.1 - Genetic Algorithm

AIM : To Solve a Global Optimization Programme. (Stalagmite Function)

THEORY : A stalagmite is a type of rock formation that rises from the floor of a cave due to the accumulation of material deposited on the floor from ceiling drippings. Stalagmites are typically composed of calcium carbonate, but may consist of lava, mud, peat, pitch, sand, sinter, and amberat (crystallized urine of pack rats).

CODE :

clear all
close all
clc

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

% Creatinf a 2Dimentional mesh
[xx,yy] = meshgrid(x,y);

%evaluting the 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

surfc(xx,yy,-f)

shading interp
[x,fval] = ga(@stalagmite,2)
function f = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f1x = [sin(5.1*pi*x + 0.5)].^6;

f1y = [sin(5.1*pi*y + 0.5)].^6;
f2x = exp(-4.*log(2)*((x-0.0667).^2/0.64));
f2y = exp(-4.*log(2)*((y-0.0667).^2/0.64));
f = -(f1x.*f2x.*f1y.*f2y);
end


EXPLAINATION :

Creating a function stalagmite by passing the input array as the argument for the variables x and y. The output of the function is stored in the variable sol.

function sol = stalagmite(input)

First column of the input array is stored in the variable x

First column of the input array is stored in the variable y

x = input(1);

y = input(2);

Stalagmite function codes

fxone = (sin(((5.1*pi).*x)+0.5)).^6;

fxtwo = exp((((x-0.0667).^2).*(-4*log(2)))./0.64);

fyone = (sin(((5.1*pi).*y)+0.5)).^6;

fytwo = exp((((y-0.0667).^2).*(-4*log(2)))./0.64);

Stalagmite function values are calculated and stored in sol

sol = -((fxone)*(fxtwo)*(fyone)*(fytwo));

//Maximization of a Stalagmite Function using Genetic Algorithm//

The program starts with clear all close all & clc

Stores 150 values of x from 0 to 1 and 150 values of y from 0 to 1 using linspace command.And to represent cases a value of 50 is assigned to a variable n_cases

x = linspace(0,1,150);

y = linspace(0,1,150);

n_cases = 50;

To create a 2D array using the arrays x and y using meshgrid command.

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

For assigning values to variables input(1) and input(2), two for loops are used. Starting for loop from 1 to the length of array xx using the counter i.Then starting one more nested for loop from 1 to the length of array yy using the variable j. Saving the value of xx as per the sequence of iteration of the for loops, in the first column of array input. Saving the value of yy as per the sequence of iteration of the for loops, in the second column of array input. Calling the function stalagmite that contains the code for evaluates the value of the function using the array input.

for i = 1:length(xx)

for j = 1:length(yy)

input(1) = xx(i,j);

input(2) = yy(i,j);

f(i,j) = stalagmite(input);

end

end

For the study of the statistical behaviour of Genetic Algorithm, primarily a stopwatch timer is started. Starting for loop from 1 to the length of n_cases using the variable k. Calling the genetic algorithm solver by passing the function to solve the stalagmite function with two number of variables. Inputs is the array to store the optimum values if the variables of the stalagmite function as selected. Fopt is the array to store the optimum values of the stalagmite function for all the values of x and y. Input and Fopt are the outputs of the GA. Then saving the value of x selected by GA in the array xopt for every iteration of the for loop. And saving the value of y selected by GA in the array yopt for every iteration of the for loop.

tic

for k = 1:n_cases

[Inputs, Fopt(k)] = ga(@stalagmite,2);

xopt(k) = Inputs(1);

yopt(k) = Inputs(2);

end

For maximizing , converting the values of the stalagmite function stored in the array Fopt to their negative values. The minimum value of \'-f(x)\' is the maximum value of \'f(x). Then saving the time taken by program to run the first iteration of GA in the variable Time_Study1.

Fopt = -Fopt;

f = -f;

Time_Study1 = toc

For plotting, figure is generated using figure command.Creating a 2X1 subplot and selecting subplot 1. Plotting starts with using a hold on command which retains plots in the current axes so that new plots added to the axes do not delete existing plots. surfc command is used to create a surface plot of the values of the stalagmite function stored in the array f. A smooth surface plot is generated by shading interp command.A 3D plot is created using plot3 command and a title

is given

figure(1)

subplot(2,1,1)

hold on

surfc(x,y,f)

shading interp

plot3(xopt,yopt,Fopt,\'color\',\'b\',\'marker\',\'o\',\'markersize\',5,\'markerfacecolor\',\'r\');

title(\'Unbounded Inputs\');

Subplot2 is selected. Fplot is plotted using plot command by using specified color and linewidth. And the axis is labelled

subplot(2,1,2)

plot(Fopt,\'color\',\'b\',\'linewidth\',1)

xlabel(\'Iterations\');

ylabel(\'Function Maximum\');

For the study the Statistical behaviour of Genetic Algorithm with Upper and Lower Bounds, primarily a stopwatch timer is started. Starting for loop from 1 to the length of n_cases using the variable k. Calling the genetic algorithm solver by passing the function to solve the stalagmite function with two number of variables. Inputs2 is the array to store the optimum values if the variables of the stalagmite function as selected. Fopt2 is the array to store the optimum values of the stalagmite function for all the values of x and y. Input2 and Fopt2 are the outputs of the GA. Then saving the value of x selected by GA in the array xopt2 for every iteration of the for loop. And saving the value of y selected by GA in the array yopt2 for every iteration of the for loop.

tic

for k = 1:n_cases

[Inputs2, Fopt2(k)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);

xopt2(k) = Inputs2(1);

yopt2(k) = Inputs2(2);

end

To create a stalagmite function you have to use this code, then a plot will appear and the plot look like this.

PLOT :

this is stalagmite function.

Thank you

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.