Challenge

Week 4.1 - Genetic Algorithm

clear all
close all
clc

% Aim
% Writing code in MATLAB to optimise stalagmite function and finding the gobal maxima for the function

% Concept of Genetic Algorithm
% Genetic algorithm (GA) is a metaheuristic inspired by the process of natural selection that belongs to the larger class of evolutionary algorithms (EA). Genetic algorithms are commonly used to generate high-quality solutions to optimization and search problems by relying on biologically inspired operators such as mutation, crossover and selection.
% IN matlab ga is an inbuilt solver  which gives global minima for a particular function.
% Here we are using ga to find global maxima of the function 
% Defining our search space

% Codes for finding global maxima for a function

% Defining our search space 
% creating one dimensional arrays
% ga didn't recognize this as the search space 
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

%  creating 2Dimensional array 
[xx yy]=meshgrid(x,y);

% Evaluating the stalagmite function

% by using the for loop we are creating an arrays for each value of x and y ,called as input_vector
% input_vector is having two columns 

for i=1:length(xx)
    for j=1:length(yy)
        input_vector(1)=xx(i,j);% storing the values of x
        input_vector(2)=yy(i,j);% storing the values of y
        f(i,j)=stalagmite_function(input_vector);% we have the function 'stalagmie_function'which takes in input vector and gives the value for f
    end
end

% ga EXPLANATION

% ga is an inbuilt solver in matlab which gives global minima
% In ga we are passing  the function that we have created and assigning varibales to it
% Here function is created in such a way that we get global maxima instead of global minima
% ga gives different results in every running so we have done three types of study 
% Different types study is for optimising the ga as well as to get accurate and smoother output

 
% STUDY-1 WITH UNBOUNDED INPUTS
 % In this study we have doing 50 iterations
 % Creating varibale and assigning the number to which iterations performed
num_cases=50;

tic % starting the timer 
    % we are performing the ga in for loop 
    
for i=1:num_cases
    [input,fopt(i)]=ga(@stalagmite_function,2);% calling ga by passing the function and assigning to variables
    xopt(i)=input(1);
    yopt(i)=input(2);
end

study_time_1=toc;
figure(1)
% creating subplot
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp

% we are using plot 3 to visualize x,y,and maximum value of function that the ga predicts over this 50 iterations
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded inputs')
% creating 2nd subplot for ploting directly the maximum value for the function that the ga predicts
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function maximum')
% study time is 2.5 secs
% From the plots we can see that the values of x and y goes beyond our search space
% Also we can see that for each iteration the value of function changes between 0 and -1
% the result is not good so we have to do next study


% STUDY-2 WITH UPPER AND LOWER BOUNDS

% In this study we are setting upper and lower limits such that the values x of and y is not goes beyond our search space
% In this study function defenition changes 

% performing the ga
% here we are setting the upper and lower limits for x and y
% starting the timer
tic
 for i=1:num_cases
    [input,fopt(i)]=ga(@stalagmite_function,2,[],[],[],[],[0;0],[1;1]);% calling ga by passing the function and assigning to variables with limits
    xopt(i)=input(1);
    yopt(i)=input(2);
 end
 study_time_2=toc;

% Ploting
figure(2)
% creating subplot
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp

% we are using plot 3 to visualize x,y,and maximum value of function that the ga predicts over this 50 iterations
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
% creating 2nd subplot for ploting directly the maximum value for the function that the ga predicts
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function maximum')

% study time is 2.8 secs
% In this study the graph is more cleaner than the first study 
% From the graph we can see that Function value  
% From this study ga preditcs the function between dash peaks
% Results are  much better but we have to do another study for better accuracy

% STUDY-3 WITH INCREASED POPULATION(ITERATIONS)

% In this study we are increasing the initial population(iterations)
% Initial population is 50 ,this gives the local maxima(local minima actually)
% By increasing initial population to 200 we get global maxima
% We are expecting an accurate output this time
% This study takes more time as it slow downs the ga by increasing population
% starting the timer

% Increasing population
options =optimoptions('ga');
options=optimoptions(options,'PopulationSize',200)
tic;
for i=1:num_cases
    [input,fopt(i)]=ga(@stalagmite_function,2,[],[],[],[],[0;0],[1;1],[],[],options); % calling ga by passing the function and assigning to variables with inncreased iterations
    xopt(i)=input(1);
    yopt(i)=input(2);
end

study_time_3=toc

figure(3)
% creating subplot
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp

% we are using plot 3 to visualize x,y,and maximum value of function that the ga predicts over this 50 iterations
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Increased iterations')
% creating 2nd subplot for ploting directly the maximum value for the function that the ga predicts
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function maximum')

% study time is 8.02 secs
% From the graph we get can see there is only one global maxima
% So we can say that this study is more accurate
% From this study we can conclude that ga performs well and gives the best output after increasing the population



Codes for stalagmite function

% we have the mathematical equation for function z 
% z= -f1x*f2x*f1y*f2y
% We are putting -ve sign in equation because ga gives only global minima
% Such that by giving -ve sign we are inversing it
% Thus global minima that the ga gives is actually becomes the global maxima

function [z]=stalagmite_function(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);
z= -f1x*f2x*f1y*f2y;
end
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.