Challenge

Week 4.1 - Genetic Algorithm

Aim: To Write a code to optimise the stalagmite function and to find the global maxima of the function.

Genetic algorithms (GA)

*The genetic algorithm(GA) approach to optimisation is based upon the concept of survival of the fittest

*GA is also used to solve both constrained and unconstrained optimisation problems that is based on natural selection,the process that drives biological evolution.

*The Genetic Algorithm selects random individuals from the current population to be parents which is the fittest and uses them to produce children for the next generation and the unfit once are eliminated.

*Genetic Algorithm is also to find the maximium and minimium value for example maximium value of top dead centre and minimium value of bottom dead centre of an engine

*Genetic Algorithm works under 3 major rules

1)Selection - select individual known as parents which contribute to the population of the next generation

2)Crossover - generation created by combination (parent 1,parent2)=child

3)Mutation - only one parent is involed like cloning

*Problem Approch:

1)Defining input space

2)Scripting the objective function(stalagmite function)

3)Performing unbounded GA study 1

4)Performing bounded GA study 2

5)Performing bounded GA study 3 with altered Population Size

clear
close
clc
% stalagmite function calling

% input data
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);

% converting 1D array into 2D array

[xx yy] = meshgrid(x,y);
num_case = 50; % Runs 50 iteration
tic

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_time = toc

surfc(xx,yy,-f)
shading interp
xlabel('x value')
ylabel('y value')
title('Stalagmite Function')



% STUDY 1

% Unbounded Inputs

tic

for i = 1:num_case
    
    [inputs,fopt(i)] = ga(@stalagmite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
    
end

study_time1 = toc

figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded Inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')

% STUDY 2

%  Bounded inputs
tic

for i = 1:num_case
    
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
    
end

study_time2 = toc

figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r');
title('Bounded Inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')

% STUDY 3

% Bounded Inputs and Population Size

options = optimoptions(@ga)
options = optimoptions(options,'PopulationSize',270);


tic
for i=1:num_case
    
    [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2);
    
end


study_time3 = toc


figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r');
title('Bounded Input with Increased Population Size')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function Maximum')

>Genetic Algorithm coding procedure:

*Input data are defined (x,y) and number of iteration was defined (num_case)

*Stalagmite function is scripted based the equation

*Nested for loop is used to iterate each of the element and for loop is used for and the other study1,study2,study3

*Genetic Algorithm is used to optimize and determine the stalagmite function

*In the plot section the output that is stalagmite function come as a negative value due to the stalagmite function scripting, so the plot will be displayed upside down to avoid this negative('-') sign is included through out the studies, So as a result negative and negative become positive so the plot will be displayed properly

*plot and surfc command are used to display graph , shading interp command was used through out the studies to remove gridliness.

*The Three studies are as follows

1. Unbounded inputs

2. Bounded Inputs - restricted with upper and lower bounds

3. Bounded inputs with options - increasing no of iterations by giving population size.

*Combining all the step to the stalagmite function define the Global Maxima

* Stalagmite Function

clear
close
clc

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*f1y*f2x*f2y);

end

*The Global Maxima can be obtained as = 0.9999998

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.