Challenge

Week 4.1 - Genetic Algorithm

function [stalagmite] = stalagmite_func(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));
stalagmite = -(f1*f2*f3*f4);
end

STALAGMITE FUNCTION

A stslsgmite function is a function is a function which generating values over a stalagmite kind of surface for finding out the local and global maxima and minima of that particular given function

CODE EXPLANATION

function - This keyword is used to define a function.

end - This keyword is used to terminate the function.

clear all - To clear all the variables from the current workspace.

close all - It close all figures whose handles are visible.

clc - clc clears all the taxt from the command window.

Line 7 & 8 - Defning x and y arrays for generating surface to project the function.

[xx,yy]=meshgrid(x,y)- mesgrid return 2-D grid coordinates based on the coordinates contained in vectors x and y.

Line 15 to 19 - For loop starts. It is used to creats a array of containing values of stalagmite function 'f'. Inside for loop we call the stalagmite function.

Line 25 to 30 - Next for loop we is for creating arrays for x and y co-ordinate of minimum function and store it. we run 50 times. Inside the loop we use 'ga' function.

ga - It is inbuilt function in matlab used to find the minimum of function genetic algorithm. inside the 'ga' function wef call the 'stalagmite' function define the number of  variables. In this case there are 2 variables.

line 23 to 42 - 1st case. Here we did not define the boundary to values taken by the 'ga' function. 

figure() -  Ton creat figure windows.

subplot() - subplot (m,n,p) divides the curren figer into an m-by-n grid and creates axes in the position specified by p.

surfc() - surfc(X, Y, Z) creates a three-dimensional surface plot, which is a three-dimensional surface that has solid edge colors and solid face colors.

shading interp - The shading interp function control the color shading of surface and patch graphics object.

hold on -The hold function determines whether new graphics objects are added to the graph pr replace in the graph hold on retains the current plot and certain axes properties so that subsequent command add to the existing graph.

plot3() - plot3(x, y, z) plots coordinates in 3-D space. to plot a set of coordinates connected by line segments, specify x, y, and z as vectors of the some length .To plot multiple sets of coodenates on the set of axes, specify at least one of x, y, or z as a matrix and the others as vectors.

plot - plot is used to create a graphical representation of some date.

 xlabel - To add the label to the specified target object on the x-axis

ylabel - To add the label to the specified target object on the y-axis

 Line 44 to 63 - 2nd case. Here we define the boundary to values taken by the 'ga' function.

Line 65 to 87 - 3rd case. Here we increase the population size to 170 with boundary inputs.  

 optimoption - optimoption organizes option by solver, and it creates and modifies only the options that apply to a solver. Shown your option and choices and default values for a specific solver /algorithm

options - set option for ga by using optimoptions.

Populationsize - It is used to define the populationsize of ga. The default population size used by ga is 50 when the number of decision variables is less than 5, and 150 otherwise.

SOLUTION PROCEDURE

When we define the stalagmite function in the programe we put a negative sing in the equation of f(x,y). Then only we can find the global maxima of the function using 'ga' function 'ga' is on inbuilt function in matlab used to find minium of function using genetic algorithm

MATLAB CODE   

% Genetic Algorithm

clear all
close all
clc

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

% creating a 2Dimesional mesh
[xx, yy] =meshgrid(x,y);
%
% Evaluation the stalagmite funtion
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

% case 1 - statistical behaviour

for  i=1:num_cases

    [inputs, fopt(i)]= ga(@stalagmite_func,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end

figure(1)
subplot(2,1,1)
surfc(x, y, f)
shading interp
hold on 
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('Unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Itrations')
ylabel('function minimum')

% case 2 - Statistical Behaviour with upper and lower bounds

for  i=1:num_cases

    [inputs, fopt(i)]= ga(@stalagmite_func,2,[],[],[],[],[0;0],[1;1]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end

figure(2)
subplot(2,1,1)
surfc(xx,yy,f)
shading interp
hold on 
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Itrations')
ylabel('function minimum')

% case3 - Increased population 

options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',170);

for  i = 1:num_cases

    [inputs, fopt(i)]= ga(@stalagmite_func,2,[],[],[],[],[0,0],[1,1],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
end

figure(3)
subplot(2,1,1)
surfc(x,y,f)
shading interp
hold on 
plot3(xopt,yopt,fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('Bounded inputs - Population increased')
subplot(2,1,2)
plot(fopt)
xlabel('Itrations')
ylabel('function minimum')
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.