Challenge

Week 4 - Genetic Algorithm

GENETIC ALGORITHM

AIM: A code in MATLAB to optimize the stalagmite function and find the global maxima of the function.

OBJECTIVE:

  • Write a code in MATLAB to optimize the stalagmite function and find the global maxima of the function.
  • Clearly explain the concept of genetic algorithm in your own words and also explain the syntax for gain MATLAB in your report.
  • Make sure that your code gives the same output, even if it is made to run several times.
  • Plot graphs for all 3 studies and for F maximum vs no. of iterations. Title and axes labels are a must, legends could be shown if necessary.

THEORY: A genetic algorithm is the process that reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation

A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution.

Optimization of stalagmite function:

This is done be 3 difference ways:-

  • Study 1 Unbounded Statistical Behaviour

The range of the genetic algorithm is unbounded and hence the values of global maxima and the values of x and y can be out of the defined search space in inputs

Use Code [inputs fopt) ga(fun,nvars)

here, inputs represents the values of parameters x and y for which the functionhas the least value (fopt)

Fun - the function to be optimized, in this case Stalagmite

nvars - the number of design variables, in this case since only x and y are the variables, 2 is to be inputted

  • Study 2: Bounded Statistical Behaviour

The range of the genetic algorithm is Bounded by introducing values for lower and upper bounds and hence the values of global maxima and the values of x and y will be within the defined search space in input

Use Code : [inputs fopt] = ga(fur , nvars, A, Aeq, beq, lb, ub)

inpuut fopt, fun and mars are the same as Study 1

A Linear inequality constraints real matrox, if there are none, then

D-Linear inequality constraints real vector, if there are none then

Aeg - Linear equality constraints real matrix, if there are none, then

beq - Linear equality constraints real vector, if there are none, then

lb - Lower bounds, specified as a real vector or array

ub - Upper bounds, specified as a real vector or array

  • Study 3:Bounded Statistical Reha Your with Options Optimized

The range of the genetic algorithm is Bounded as in Study 2 but to improve the accuracy, the commands optimoptions is introduced to increase the Population Size

Use Code ga(fun nvars A b Aeq beq lb ub nonicon IntCon options)

inputs, fopt fun and nvars A b Aeq beq lb ub are the same as Study 1 and Study 2 respectively

nonicon-Nonilinear constraints, specified as a function handle or function name. If there are none, then

IntCon - integer variables, specified as a vector of positive integers, if there are none, then

Options Optimization options, specified as the output of optimoptions or a structure.

Determining the Maxima Using ga-function

The function ga in matlab determines only the global minima of the function.To determine the maxima, it is ideal to minimize the function to be optimized by introducing a (negative) sign infront of the function Therefore, when running the function go, it will determine the global minima of the function which is actually the global maxima

Plotting and Displaying the Maxima Value

While plotting the graphs using surfc (surface contour), plats (3 dimensional Plotting), plot and also while displaying result with fprintf, it is ideal to represent the value of the function with a (negative) sign, thereby making the plot or value to appear in positive direction as is the requirement. Also, since a surface contour plot and a regular plot are required to be shown in same figure window, Command subplot is to be used.

EXPLANATION OF CODE:

  • We start off the code by defining one dimensional space quntities.
  • Getting the number of cases, taking them as 50 cases.
  • And we make a separete code defining the two dimentional surface.
  • function [F] = get_stalagmite(x,y)
    
    % Creating 2D Surface
    
    [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
  • To show the cases for defining each instance using stalagmite funtion.
  • By calling the formulas using the below funcions.(making this stalagmite function)
    function [F] = stalagmite(input_vector)
    
    F_1_x = (sin(5.1*pi*input_vector(1) + 0.5))^6;
    F_2_x = exp(-4*log(2)*((input_vector(1) - 0.0667)^2/0.64));
    F_1_y = (sin(5.1*pi*input_vector(2) + 0.5))^6;
    F_2_y = exp(-4*log(2)*((input_vector(2) - 0.0667)^2/0.64));
    
    F = -(F_1_x*F_1_y*F_2_x*F_2_y);
    ​
  • Now as ber the above mentioned 3 different ways optimizing the stalagmite funcion.
  • Study one . doing by using the for loop and calling stalagmite. Then plotting and mentioning the labels.\\
  • Repeating the above process to the others as well.
  • Then adding all of them and getting the total.
close all 
clear all 
clc
% Defing our search space 
 x = linspace(0,0.6,150);
 y = linspace(0,0.6,150);
 num_cases = 50;
 F = get_stalagmite(x,y);

% Unbounded Statistical Behaviour

tic
for i = 1:num_cases
    [inputs1, Fopt1(i)] = ga(@stalagmite,2);
    xopt(i) = inputs1(1);
    yopt(i) = inputs1(2);
end

study_time = toc;

% Plotting

figure(1)
subplot (2,1,1)
hold on
surfc(x,y,-F)
xlabel('x-value')
ylabel('y-value')
zlabel('function-value')
shading interp
plot3(xopt, yopt,-Fopt1,'marker','o','markersize',5,'markerfacecolor','b')
title('Unbounded')
subplot (2,1,2)
plot (-Fopt1)
xlabel('Iterations')
ylabel('Function Maximum')

% Bounded Statistical Behaviour
tic
for i = 1:num_cases
    [inputs2, Fopt2(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
    xopt(i) = inputs2(1);
    yopt(i) = inputs2(2);
end

study2_time = toc;

%Plotting

figure(2)
subplot (2,1,1)
hold on

surfc(x,y,-F)
xlabel('x-value')
ylabel('y-value')
zlabel('Function-value')
shading interp

plot3(xopt, yopt, -Fopt2,'marker','o','markersize', 5,'markerfacecolor','b')
title(  'Bounded')
subplot (2,1,2)
plot(-Fopt2)
xlabel('Iterations') 
ylabel('Function Maximum')

% Bounded Statistical Behaviour with Optimization Options

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

tic
for i = 1:num_cases
    [inputs3, Fopt3(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    xopt(i) = inputs3(1);
    yopt(i) = inputs3(2);
end

study3_time = toc;

%Plotting

figure(3)
subplot (2,1,1)
hold on

surfc(x,y,-F)
xlabel('x-value')
ylabel('y-value')
zlabel('Function value')
shading interp
plot3(xopt, yopt,-Fopt3,'marker','o','markersize',5,'markerfacecolor','r')
title ('Bounded with Optimized Options')
subplot(2,1,2)
plot (-Fopt3) 
xlabel('Iterations')
ylabel('Function Maximum')

Total_time = study_time + study2_time + study3_time;

ERRORS FACED:

  • 'ga' function required global tool box : solved by installing add ons to the matlab | screenshot 113
  • Misspelling : solved by checking : screenshot 114
  • mistake in mentioning the quotes : cheacking | screenshoot 115

CONCLUTION: The varying values of maxima over different itteration is ploted below with repect to their clasification.

  1. For Unbounded Statistical Behaviour | UB.fig
  2. For Bounded Statistical Behaviour | B.fig
  3. For Bounded Statistical Behaviour with Optimization Options | BWOO.fig
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.