Challenge

Week 4.1 - Genetic Algorithm

1) Main function for Genetic Algorithm:

clear all
close all
clc

%% Providing  a search space for the stalagmite function

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

%Meshgrid command returns 2-D grid coordinates based on the coordinates contained in vectors x and y.

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

%% Creating the surface plot of stalagmite function

for i = 1:length(xx)
    
    for j = 1:length(yy)
        
        input_vec(1) = xx(i,j);
        input_vec(2) = yy(i,j);
        
        f(i,j) = stalagmite(input_vec); %Values of the function in the given limit
    end
end

pos_f = -1 * f;

% Creating a surface using surfc command
surfc(xx,yy,pos_f)

% To remove the lines produced by the mesh grid
shading interp

%% Introduction to Genetic Algorithm

% A Genetic algorithm is a search experiential that is stimulated by
% Charles Darwin's theory of natural evolution. This algorithm reproduces
% the process of natural selection where the appropriate individuals are
% selected for reproduction in order to produce offspring of the next
% generation.

% It is a technique for solving both constrained and unconstrained
% optimization problems that are based on natural selection, the process
% that drives biological evolution.

% Different Phases in GA:

% 1) Initialization of population
% 2) Fitness function
% 3) Selection
% 4) Reproduction (in two ways - Crossover, Mutation)

%% Study 1 (Without boundation)

% In this evaluating the optimum value of fitness function at each
% iteration and calculating it using the required syntax of GA

% [x,fval,exitflag,output,population,scores] = ga(func,nvars,A,B,Aeq,Beq,lb,ub,nonlcon,intcon,options)
% x = optimum solution
% exitflag = reason GA stopped
% output = information about optimiztion process
% population = final population
% Scores = final scores
% func = fitness function
% nvars = no. of variables
% A,B = Linear inequality constraints
% Aeq,Beq = Linear equality constraints
% ub, lb = upper and lower bounts (limits)
% nonlcon: Use for nonliner constraints
% intcon: to get solution in integer
% options - To set populationsize minimize with non-default options

% Evaluating the study time using Tic and Toc command, Where tic command
% starts the timer and toc command ends the timer

tic
for i = 1:50
    
% Calculating the optimezed value of the function at each iteration 
[input, fval(i)] = ga(@stalagmite,2);
x_in(i) = input(1);
y_in(i) = input(2);
end

% Displays the time taken for the evaluation
study1_time = toc

% Plotting the graph in a separate figure 
figure(1)

% Subplots are created for easier understanding
subplot(2,1,1)

% Creating a surface using surfc command
surfc(xx,yy,-f)
hold on
% To remove the lines produced by the mesh grid
shading interp

% For Ploting graphs in 3-D we use plot3 command 
plot3(x_in,y_in,-fval,'marker','o','markersize',5,'markerfacecolor','r')

% Providing the title, labels for the plots 
title('Unbounded Inputs')
xlabel('x')
ylabel('y')
grid on

subplot(2,1,2)

% The values of the function is plotted using the plot command
plot(-fval)
xlabel('iterations')
ylabel('function min value')

%% Study 2 (With Boundation)

% Evaluating the study time using Tic and Toc command, Where tic command
% starts the timer and toc command ends the timer

tic
for k = 1:50

% Calculating the optimezed value of the function at each iteration 
[input, fval(k)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
x_in(k) = input(1);
y_in(k) = input(2);

end

% Displays the time taken for the evaluation
study2_time = toc

% Plotting the graph in a separate figure 
figure(2)

subplot(2,1,1)

% Creating a surface using surfc command
surfc(xx,yy,-f)
hold on
% To remove the lines produced by the mesh grid
shading interp

% For Ploting graphs in 3-D we use plot3 command 
plot3(x_in,y_in,-fval,'marker','o','markersize',5,'markerfacecolor','r')

% Providing the title, labels for the plots 
title('Bounded Inputs')
xlabel('x')
ylabel('y')
grid on

subplot(2,1,2)

% The values of the function is plotted using the plot command
plot(-fval)
xlabel('iterations')
ylabel('function min value')

%% Study 3 (With the increase in the size of population)

% Increasing the size of the population using optimoptions function

opti_option = optimoptions('ga','populationsize',300)

% Evaluating the study time using Tic and Toc command, Where tic command
% starts the timer and toc command ends the timer

tic
for j = 1:50

% Calculating the optimezed value of the function at each iteration 
[input, fval(j)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],opti_option);
x_in(j) = input(1);
y_in(j) = input(2);
end

% Displays the time taken for the evaluation
study3_time = toc

% Plotting the graph in a separate figure 
figure(3)

subplot(2,1,1)

% Creating a surface using surfc command
surfc(xx,yy,-f)
hold on
% To remove the lines produced by the mesh grid
shading interp
% For Plotting graphs in 3-D we use plot3 command 
plot3(x_in,y_in,-fval,'marker','o','markersize',5,'markerfacecolor','r')

% Providing the title, labels for the plots 
title('Bounded Inputs with modified population size')
xlabel('x')
ylabel('y')
grid on

subplot(2,1,2)

% The values of the function is plotted using the plot command
plot(-fval)
xlabel('iterations')
ylabel('function min value')

%% Result

% In study 1 without any boundation the Genetic algorithm gave
% different values within the function. In this case, there is no correct
% value that can be obtained from the plot, which is pointless

% In study 2 with boundation the Genetic algorithm was optimized and
% gave some values of local maxima, including global maxima

% In the final study after setting the boundaries and increasing the
% population range we can obtain the value of Global maxima. 
% But the only draw back was the computing time took too long. 

2) Stalagmite Function:

%% Fitness Function (Stalagmite function)

function [value_stal] = stalagmite(input_vec)

f_1_x = (sin((5.1*pi*input_vec(1))+0.5)).^6
f_1_y = (sin((5.1*pi*input_vec(2))+0.5)).^6

f_2_x = exp(((input_vec(1)-0.0667).^2)/(0.64)*(-4*log(2)))
f_2_y = exp(((input_vec(2)-0.0667).^2)/(0.64)*(-4*log(2)))

value_stal = (-1) * f_1_x * f_1_y * f_2_x * f_2_y;
end

3) Errors faced and steps took for correcting them:

There was no major error faced while solving this problem

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.