Challenge

Week 4 - Genetic Algorithm

Objective :

To Write a code in MATLAB to optimise the stalagmite function and to find the global maxima of the function.

THEORY :

The genetic algorithm GA is a method for solving both constrained and unconstrained optimisation problems that is based on natural selction,the process that drives biological evolution.

GA repeatedly modifies the population of individual solutions.

At each step, the genetic algorithm selects individuals at random from the current population to be parents and uses them to produce children for the next generation. Over successive generations, the popultation evolves towards an optimal solution.

GA can be applied to solve a variety of optimization problems that are not well suited for standard optimazation algorithms, including problems in which the objective function is discontinuous, non differentiable, stochastic, or highly non linear.

The GA uses 3 main types of rules at each step to create the next gerneration from the current population :

  • Selection rule selects the individuals, called parents, that contribute to the population at the next generation.
  • crossover rules combine two parents to form children for the next generation.
  • Mutation rules apply random changes to individual parents to form children.

PROCEDURE:

1. Initially stalagmite function is defined in a seperate script.

2. A negative sign is used in front of the function to obtain maxima of the function rather minima.

3. A nested for loop is utilized to iterate each of the elements .

4. GA is used to determine the optimal of the function.

5. Plot is generated for different cases.

The three cases considered are :

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.

MATLAB CODE :

function [f]=stalagmite(input_vector)

x = input_vector(1);
y = input_vector(2);


f1 = (sin(5.1*pi*x + 0.5)).^6 ;
f2 = exp(-4*log(2)*((x-0.0667).^2)./0.64) ;
f3 = (sin(5.1*pi*y + 0.5)).^6 ;
f4 = exp(-4*log(2)*((y-0.0667).^2)./0.64) ;

f = -(f1*f2*f3*f4);

end

Main Program:

clear all
close all
clc


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



% converting 1D array into 2D array

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

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

tic

for i = 1:rep
    
    [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

tic

for i = 1:rep
    
    [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('optimum solution')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')


%-----------------------------------------------------------------------
% STUDY 3

options = optimoptions(@ga)
options = optimoptions(options,'populationsize',170);


tic
for i=1:rep
    
    [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('optimum solution')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')

global_maxima = -fopt

RESULTS:

https://drive.google.com/file/d/1zVqk2FYaYa9_WI91d7sd3Z0FqnA2Vmzi/view?usp=

https://drive.google.com/file/d/1zSK7NAP3a4O8eTt9OZqyu2yvo9d-E6T3/view?usp=drivesdk

https://drive.google.com/file/d/1zE6Qb5I_8C32d8TFWy7b0d2sMkpSkjEa/view?usp=drivesdk

https://drive.google.com/file/d/1z9KYD9B57zkvwQlE4cS17UqeeTBDXeLI/view?usp=drivesdk

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.