Challenge

Optimization using Genetic Algorithm

AIM:

To write a code in MATLAB to optimise the stalagmite function and finding the global maxima

THEORY:

Genetic Algorithm GA:

It is the method for solving optimisation problem by the use of random sampling.It generates the population of points at each iteration. The best point in the population approches an optimal solution. The working of GA is as follows:

  • The alogorithm begins by creating a random initial array
  • Then at each iteration it performs a series of computation on the current array to generate new array.
  • The algorithm stops when one of the stopping conditions met.

Syntax used:

  • x= ga(function name, no of variables)
  • x = ga(fun,nvar,A,b,Aeq,beq,lb,ub) finds the min of function with linear inequalities of `A*x<=b` and linear equalities of Aeq*x=beq and with upper bounds and lower bounds
  • x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)

PROGRAM:

clear all
close all
clc

% Define the search space

x = linspace(0,0.6,150); % It generates a array of 500 values from 0 to 0.6  

y = linspace(0,0.6,150);

iterations = 50;

% Creating a 2D mesh

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

% Evaluating the Stalagmite function

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

% Surface plotting

figure(1)

surfc(xx,yy,f) % creates a surface plot 

shading interp % It will generate the shading by renoving the grid on the surface plot

[inputs, fmax] = ga(@stalagmite, 2)


tic

% Study 1 under unbound behaviour

for i = 1:iterations
    
    [inputs1, fopt1(i)] = ga(@stalagmite, 2);
    
    xopt1(i) = inputs1(1);
    
    yopt1(i) = inputs1(2);
    
end

study1_time =toc 

% Plotting

figure(2)

subplot(2,1,1)

hold on

surfc(x,y,f)

xlabel("X-axis")

ylabel("Y-axis")

shading interp

hold on

plot3(xopt1,yopt1,fopt1,'marker','o','markersize',5,'markerfacecolor','r')

xlabel("X optimum")

ylabel("Y optimum")

zlabel("F maximum")

title("Maximizing function with unbounded inputs")

subplot(2,1,2)

plot(fopt1)

xlabel("Iterations")

ylabel("F maximum")

title("Iterations vs F maximum")



% Study 2 with lower and upper bounds

tic

for i = 1:iterations
    
    [inputs2, fopt2(i)] = ga(@stalagmite, 2,[],[],[],[],[0;0],[0.6;0.6]);
    
    xopt2(i) = inputs2(1);
    
    yopt2(i) = inputs2(2);
    
end

study1_time =toc 

% Plotting

figure(3)

subplot(2,1,1)

hold on

surfc(x,y,f)

shading interp

hold on

plot3(xopt2,yopt2,fopt2,'marker','o','markersize',5,'markerfacecolor','r')

xlabel("X optimum")

ylabel("Y optimum")

zlabel("F maximum")

title("Maximizing function with bounded inputs")

subplot(2,1,2)

plot(fopt2)

xlabel("Iterations")

ylabel("F maximum")

title("Iterations vs F maximum")



% Study 3 with lower and upper bounds and also with optimised options

tic

options = optimoptions('ga','populationsize',300);

for i = 1:iterations
    
    [inputs3, fopt3(i)] = ga(@stalagmite, 2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    
    xopt3(i) = inputs3(1);
    
    yopt3(i) = inputs3(2);
    
end

study1_time =toc 

% Plotting

figure(4)

subplot(2,1,1)

hold on

surfc(x,y,f)

shading interp

hold on

plot3(xopt3,yopt3,fopt3,'marker','o','markersize',5,'markerfacecolor','r')

xlabel("X optimum")

ylabel("Y optimum")

zlabel("F maximum")

title("Maximizing function with defined population size")

subplot(2,1,2)

plot(fopt3)

xlabel("Iterations")

ylabel("F maximum")

title("Iterations vs F maximum")
   

STALAGMITE FUNCTION:

% Stalagmite Function

function Fmax = stalagmite(inputs)

        x = inputs(1); % Assigning the first element in first row to x
        
        y = inputs(2); % Assigning the second element in first row to y
        
        F1x = (sin((5.1*pi.*x)+0.5)).^6;
        
        F2x = exp((-4*log(2)).*((x-0.0667).^2)/0.64);
        
        F1y = (sin((5.1*pi.*y)+0.5)).^6;
        
        F2y = exp((-4*log(2)).*((y-0.0667).^2)/0.64);
        
        Fmax = -F1x.*F2x.*F1y.*F2y;
        
                  
end

PROCEDURE:

  1. Defining the search space
  2. Defining the stalagmite function
  3. Evaluating the function
  4. Finding the globsl msxima using ga
  5. As it not generates same solution on every execution of code we do 3 study to reduce it
  6. study-1 consists of finding the solution for unbounded inputs
  7. study 2 consists of finding the solution with upper and l;ower bounds
  8. study 3 consists of specifying the population and generates unquie solution.

PLOTS:

1.Surface plot

2. Study 1

3.Study-2

4. study-3

REFERENCES:

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.