Challenge

Genetic Algorithm Optimization Using Stalagmite function in MatLab

Genetic Algorithm Optimization Using Stalagmite function in MatLab

Aim

  • To understand the theory of optimization and genetic algorithm.
  • Write code for stalagmite function.
  • Write code for global maxima function.
  • Plot the results on the graph.

Objective

  • Study about optimization and genetic algorithm.
  • Explain the function of genetic algorithm use in Matlab.
  • Explain the stalagmite function and write down the code for it.
  • Determine the point of global maxima
  • Code the program to plot the results of genetic algorithm and to label it.
  • Plot the Function maximum vs Iteration plot below the main plot.
  • Built up the report.

Optimization

Optimization is the process of study mathematical data, numbers and quantitative problemsby implementing various mathematical principles and methods in process or in programming.

Use of optimization to modify all the design parameters to see which combination is well suited for that specific situation or condition.

Optimization method use in many disciplines including physics biology, engineering, economics and business. The subject grew from a realization that quantitative problems in manifestly different disciplines have important mathematical elements in common. Because of this commonality, many problems can be formulated and solved by using the unified setof ideas and methods that make up the field of optimization

Analyzing the each data set could be very time consuming and confusing, which can lead to innumerable amount of error, therefore, optimization, using programming, is the best and fast method to do it.

Optimization is the area of study which deals with how to minimize or maximize the particular function which depends on the multiple parameters.

Global maxima and minima

In mathematical and statistical analysis, largest and smallest value of the function called the global or absolute maxima (extrema) and minima respectively.

Other large and small value or points, in their respective ways denoted as local maxima or minima. For better understanding figure shown below clear all the doubts.

Representation of maxima and minima

Stalagmite Function

Stalagmite function is the mathematical model that helps us to generate local minima and maxima. We are going to use this function to solve our genetic algorithmic model in this part program.

Basically, following is the equation for the stalagmite function - using its equation stalagmite function program will be created.

`f(x,y)=f1x xxf1y xxf2x xxf2y`

`f1x = [sin(5.1pix+0.5)}]^6`

`f1y = [sin(5.1piy+0.5)}]^6`

`f2x = exp[-4ln(2)frac{(x-0.0667)^2}{0.64}]`

`f2y = exp[-4ln(2)frac{(y-0.0667)^2}{0.64}]`

For archiving global maxima make sure we multiply the function with ‘-’ minus sign[2]

`f(x,y)=-(f1x xxf1y xxf2x xxf2y)`

By using above equation we can write the stalagmite function program in MatLab in following way

Stalagmite Function program

%% Stalagmite Fuction  for Genetic algorithm
function [F] = stalagmite(input_vector)

% mention input (1) and (2) as x and y respectively
x = input_vector(1);
y = input_vector(2);

% write down all equation code
f1 = (sin(5.1*pi*x+0.5))^6;

f2 = (sin(5.1*pi*y+0.5))^6;

f3 = exp(-4*log10(2)*(((x-0.0667)^2)/0.64));

f4 = exp(-4*log10(2)*(((y-0.0667)^2)/0.64));

F = -(f1.* f2.* f3.* f4);
end

Genetic Algorithm

Genetic Algorithm is based upon evolution theory that proposed on the concept of Darwin's theory of evolution.

it is an optimization technique, which tries to find out such values of input so that we get the best output values or results.

Let’s take a hypothetical situation where, you are head of a country, and in order to keep your city safe from bad things, you implement a policy like this.

  • You select all the good people, and ask them to extend their generation by having their children.
  • This repeats for a few generations.
  • You will notice that now you have an entire population of good people.

Now, that may not be entirely possible, but this example was just for basic understanding of the concept. So the basic idea was that we changed the input (i.e. population) such that we get better output (i.e. better country).

Beginning of coding for main program

Up to this section, we learn all the theory related to this program, so it right time to understand program. we start it just by using clear all, close all, clc command which basically clear all previous data, close all previously opened plots and figure. Lastly, clc clears the command window.

Then we provide the input array range of x and y. and also provide limit to the iteration calculation using codes mentioned down below.

%% Genetic Algorithm Optimization Using Stalagmite function in MatLab 
clear all       % Clear all Variables from memory
close all       % close all charts and figures
clc             % clear command window
 

%% Input arrays
% defining our search space, basicaly one dimentionam array
x = linspace(0,0.6,150);                 % 150 x arrays range from 0 to 0.6
y = linspace(0,0.6,150);                 % 150 y arrays range from 0 to 0.6
num_case = 50;                           % Number of cases to analyse

Above program generate one dimensional arrays of x and y by using their values we convert it into two dimensional arrays using below command.

%% Creating a 2Dimensional mesh
[xx yy] = meshgrid(x,y);

After generating 2 dimensional arrays put all the value of xx and yy as a input using for loop into the stalagmite function to evaluate it using following code

%% Evaluating the stalagmite function

% for loop to compute the range of values 
for i = 1:length(xx)                     % i is equal to 1 to length of xx range 
    for j = 1:length(yy)                 % j is equal to 1 to length of yy range
        input_vector(1)= xx(i,j);        % inpute(1) value for stalagmite function
        input_vector(2)= yy(i,j);        % inpute(2) value for stalagmite function
        % value for z direction using stalagmite function equation
        zz(i,j) = stalagmite(input_vector);
    end
end

The syntax for ga in MATLAB

ga searches for a minimum of a function using the genetic algorithm. For this example, use ga to minimize the function shufcn, a real valued function of two variables.

Pot shufcn over the range = [xx,yy,zz] by calling plotobjective.

surfc(xx,yy,zz)                          % Plot 3Dimensional surface plot
shading interp                           % Remove all mesh or grids from 3D surafce

To use the ga solver, provide at least two input arguments, function and the number of variables in the problem. The first two output arguments returned by ga are x, the best point found, and Fval, the function value at the best point. A third output argument, exitFlag tells you the reason why ga stopped. ga can also return a fourth argument, Output, which contains information about the performance of the solver.

`x = ga(fun,nvars)`

Where,

  • ‘x’ is a local maxima values,
  • ‘`fun`’ is to mention stalagmite function,
  • ‘`nvars`’ is the number of design variables.
[input, fval] = ga(@stalagmite,2)
% Label x & y axis
xlabel('x plot')                         
ylabel('y plot')

Example of ga to Minimize the function output plot

Now, like minima we can also find out maxima which we are going to study in three Study

  1. Unbounded Statistical behavior
  2. bounded Statistical behavior
  3. bounded values and increasing GA iterations Statistical behavior

Study (1) -Unbounded Statistical behavior

In first study to find (x) local maxima value we not going to provide any bounds in ga syntax

So, our coding line will be:-

`x = ga(fun,nvars)`

Where,

  • ‘`x`’ is a local maxima values,
  • ‘`fun`’ is to mention stalagmite function,
  • ‘`nvars`’ is the number of design variables.
%% [1] Study 1 - Unbounded Statistical behavior

tic                                      % tic--- string name = toc use to determine computation time
for i =  1:num_case                      % i is equal to 1 to 50 range 
    % ga is the inbuilt function in Matlab for solving genetic variables
    % stalagmite function for global maximize
    [inputs,zopt(i)]=ga(@stalagmite,2);  % Optimum value for z 
    xopt(i)=inputs(1);                   % Optimum value for x
    yopt(i)=inputs(2);                   % Optimum value for y
end
study_time_1 = toc;                      % tic--- string name = toc use to determine computation time

Coding for Output plot for first study

%% plot first figure
figure(1)                                % plot first figure
% subplot - To plot multiple plot in single figure 
% (10,10,[2 8 53 58]) 10,10 is 10x10 matrix
% [2 8 53 58] - position of plot in 10x10 matrix
subplot(10,10,[2 8 53 58])                
hold on                                  % use hold on to add another plot in same figure
surfc(xx,yy,zz)                          % Plot 3Dimensional surface plot
shading interp                           % Remove all mesh or grids from 3D surafce
% Label x & y axis
xlabel('x plot')                         
ylabel('y plot')
% plot 3 point x,y,z using plot 3 
% mark plot points, size it and colour it using following commands
plot3(xopt,yopt,zopt,'marker','o','markersize',5,'markerfacecolor','r')
% title the first subplot
title('Unbounded Input')
% subplot - To plot multiple plot in single figure 
% (10,10,[71 80 91 100]) 10,10 is 10x10 matrix
% [71 80 91 100] - position of plot in 10x10 matrix
subplot(10,10,[71 80 91 100])
% plot z points to compare it with Iterations
plot(zopt)
% Label x & y axis
xlabel('Iterations')
ylabel('Function Maximum')
% disable the holf on command
hold off

Output of study (1)

Study (2) - bounded Statistical behavior

In this study part, we going to set upper and lower bounds then we get the results of x maxima between that specified bound. So here ga syntax will be:-

`x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)`

where,

  • `fun` - the objective function.
  • `nvars`is the dimension (number of design variables) of fun.
  • `x` is subject to the linear inequalities `Axxx ≤ b`. ga evaluates the matrix product `Axxx` as if x is transposed (`Axxx`').
  • `x` is subject to the linear equalities `Aeqxxx = beq` and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product `Aeqxxx` as if `x` is transposed (`Aeqxxx`').
  • defines a set of lower and upper bounds on the design variables, `x`, so that a solution is found in the range lb ≤ `x` ≤ ub. (Set Aeq=[] and beq=[] if no linear equalities exist.)
%% [2] Study 2 - bounded Statistical behavior - set Upper and lower bounds

tic                                      % tic--- string name = toc use to determine computation time
for i=1:num_case                         % i is equal to 1 to 50 range 
    % ga is the inbuilt function in Matlab for solving genetic variables
    % stalagmite function for global maximize
    % x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
    % Set bounds lb and ub
    [inputs, zopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);% Optimum value for z 
    xopt(i)=inputs(1);                   % Optimum value for x
    yopt(i)=inputs(2);                   % Optimum value for y
end
study_time_2 = toc

Coding for Output plot for Second study

%% plot Second figure
figure(2)                                % plot first figure
% subplot - To plot multiple plot in single figure 
% (10,10,[2 8 53 58]) 10,10 is 10x10 matrix
% [2 8 53 58] - position of plot in 10x10 matrix
subplot(10,10,[2 8 53 58])                
hold on                                  % use hold on to add another plot in same figure
surfc(xx,yy,zz)                          % Plot 3Dimensional surface plot
shading interp                           % Remove all mesh or grids from 3D surafce
% Label x & y axis
xlabel('x plot')                         
ylabel('y plot')
% plot 3 point x,y,z using plot 3 
% mark plot points, size it and colour it using following commands
plot3(xopt,yopt,zopt,'marker','o','markersize',5,'markerfacecolor','r')
% title the first subplot
title('Bounded Input')
% subplot - To plot multiple plot in single figure 
% (10,10,[71 80 91 100]) 10,10 is 10x10 matrix
% [71 80 91 100] - position of plot in 10x10 matrix
subplot(10,10,[71 80 91 100])
% plot z points to compare it with Iterations
plot(zopt)
% Label x & y axis
xlabel('Iterations')
ylabel('Function Maximum')
% disable the holf on command
hold off

Output of study (2)

Study (3) bounded values and increasing GA iterations Statistical behavior

The Genetic Algorithm works on a population using a set of operators that are applied to the population. A population is a set of points in the design space. The initial population is generated randomly by default. The next generation of the population is computed using the fitness of the individuals in the current generation.

This example shows the effects of some options for the genetic algorithm function ga. You create and change options by using the optimoptions function.

Specify Population Size

ga creates a default initial population by using a uniform random number generator. The default population size used by ga is 50 when the number of decision variables is less than 5 and 200 otherwise. This size can be a poor one for some problems; a smaller population size can be sufficient for smaller problems. Since the current problem has only two variables, specify a population size of 170. Set the value of the option PopulationSize to 170 in the existing options, opts.

For this case ga syntax will be,

`x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,noncol,options)`

where,

  • `fun` - the objective function.
  • `nvars`is the dimension (number of design variables) of fun.
  • `x`is subject to the linear inequalities `Axxx ≤ b`. ga evaluates the matrix product `Axxx` as if x is transposed (`Axxx`').
  • `x` is subject to the linear equalities `Aeqxxx = beq` and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product `Aeqxxx` as if `x` is transposed (`Aeqxxx`').
  • defines a set of lower and upper bounds on the design variables, `x`, so that a solution is found in the range lb ≤ `x` ≤ ub. (Set Aeq=[] and beq=[] if no linear equalities exist.)
  • The function nonlcon accepts x and returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga maximizes the fun such that C(`x`) ≤ 0 and Ceq(`x`) = 0. (Set lb=[] and ub=[] if no bounds exist.)
  • '`x`' maximize with the default optimization parameters replaced by values in options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions.
%% [3] Study 3 - bounded values and increasing GA iterations Statistical behavior

options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',170);


tic                                      % tic--- string name = toc use to determine computation time
for i = 1:num_case                       % i is equal to 1 to 50 range 
    % ga is the inbuilt function in Matlab for solving genetic variables
    % stalagmite function for global maximize
    [inputs, zopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1,1],[],[],options);% Optimum value for z 
    xopt(i)=inputs(1);                   % Optimum value for x
    yopt(i)=inputs(2);                   % Optimum value for y
end
study3_time = toc

Coding for Output plot for Third study

%% plot Third figure
figure(3)                                % plot first figure
% subplot - To plot multiple plot in single figure 
% (10,10,[2 8 53 58]) 10,10 is 10x10 matrix
% [2 8 53 58] - position of plot in 10x10 matrix
subplot(10,10,[2 8 53 58])                
hold on                                  % use hold on to add another plot in same figure
surfc(xx,yy,zz)                          % Plot 3Dimensional surface plot
shading interp                           % Remove all mesh or grids from 3D surafce
% Label x & y axis
xlabel('x plot')                         
ylabel('y plot')
% plot 3 point x,y,z using plot 3 
% mark plot points, size it and colour it using following commands
plot3(xopt,yopt,zopt,'marker','o','markersize',5,'markerfacecolor','r')
% title the first subplot
title('Bounded Inputs with modified population size')
% subplot - To plot multiple plot in single figure 
% (10,10,[71 80 91 100]) 10,10 is 10x10 matrix
% [71 80 91 100] - position of plot in 10x10 matrix
subplot(10,10,[71 80 91 100])
% plot z points to compare it with Iterations
plot(zopt)
% Label x & y axis
xlabel('Iterations')
ylabel('Function Maximum')
% disable the holf on command
hold off

Output of study (3)

Error faced during programming

(1) Equatuion error 'ln' which has to be replace with `log10`

(2) Typo error- typed 'num_case' instade of "num_cases"

(3) Not mentioned plot3 for three axis

(4)Typo error

(5)function name that provided was incorrect

Conclusion:-

(1)Genetic algorithm is vital to solve optimization problem as it make process simple.

(2)Overview based on this experiment we get to solve optimization problem using ga function in Matlab which is function for Genetic algorithm. Implementing before-mentioned technique in any optimization problem one can easily find local and global maxima and minima for any type optimization problem.

(3)Also,on the basis of second study and its chart and figure,We and take control over function variation by providing upper and lower bounds which minimize the research area from function data-set

(4)Lastly, according to third study and its plot, adding optimoption command by limiting number of population and increasing ga iteration we can get a very precise range of results in function.

References:-

[1] Stephen J. Wright- Professor of computer science at the University of Wisconsin. Coauthor, with Jorge Nocedal, of Numerical Optimization - Optimization mathematics - https://www.britannica.com/science/optimization

[2] Benjamin J. Lynch-Optimizing with Genetic Algorithms- https://www.msi.umn.edu/sites/default/files/OptimizingWithGA.pdf

[3]Shubham Jain - Introduction to Genetic Algorithm & their application in data science -https://www.analyticsvidhya.com/blog/2017/07/introduction-to-genetic-algorithm/

[4]Mathworks Help center Retrieved from from-https://in.mathworks.com/help/gads/examples/genetic-algorithm-options.ht

[5]Mathworks Help center Retrieved from from-https://in.mathworks.com/help/gads/ga.html#d120e39200

Complete main program

%% Genetic Algorithm Optimization Using Stalagmite function in MatLab 
clear all       % Clear all Variables from memory
close all       % close all charts and figures
clc             % clear command window
 

%% Input arrays
% defining our search space, basicaly one dimentionam array
x = linspace(0,0.6,150);                 % 150 x arrays range from 0 to 0.6
y = linspace(0,0.6,150);                 % 150 y arrays range from 0 to 0.6
num_case = 50;                           % Number of cases to analyse

%% Creating a 2Dimensional mesh
[xx yy] = meshgrid(x,y);


%% Evaluating the stalagmite function

% for loop to compute the range of values 
for i = 1:length(xx)                     % i is equal to 1 to length of xx range 
    for j = 1:length(yy)                 % j is equal to 1 to length of yy range
        input_vector(1)= xx(i,j);        % inpute(1) value for stalagmite function
        input_vector(2)= yy(i,j);        % inpute(2) value for stalagmite function
        % value for z direction using stalagmite function equation
        zz(i,j) = stalagmite(input_vector);
    end
end
 

%% [1] Study 1 - Unbounded Statistical behavior

tic                                      % tic--- string name = toc use to determine computation time
for i =  1:num_case                      % i is equal to 1 to 50 range 
    % ga is the inbuilt function in Matlab for solving genetic variables
    % stalagmite function for global maximize
    [inputs,zopt(i)]=ga(@stalagmite,2);  % Optimum value for z 
    xopt(i)=inputs(1);                   % Optimum value for x
    yopt(i)=inputs(2);                   % Optimum value for y
end
study_time_1 = toc;                      % tic--- string name = toc use to determine computation time


%% plot first figure
figure(1)                                % plot first figure
% subplot - To plot multiple plot in single figure 
% (10,10,[2 8 53 58]) 10,10 is 10x10 matrix
% [2 8 53 58] - position of plot in 10x10 matrix
subplot(10,10,[2 8 53 58])                
hold on                                  % use hold on to add another plot in same figure
surfc(xx,yy,zz)                          % Plot 3Dimensional surface plot
shading interp                           % Remove all mesh or grids from 3D surafce
% Label x & y axis
xlabel('x plot')                         
ylabel('y plot')
% plot 3 point x,y,z using plot 3 
% mark plot points, size it and colour it using following commands
plot3(xopt,yopt,zopt,'marker','o','markersize',5,'markerfacecolor','r')
% title the first subplot
title('Unbounded Input')
% subplot - To plot multiple plot in single figure 
% (10,10,[71 80 91 100]) 10,10 is 10x10 matrix
% [71 80 91 100] - position of plot in 10x10 matrix
subplot(10,10,[71 80 91 100])
% plot z points to compare it with Iterations
plot(zopt)
% Label x & y axis
xlabel('Iterations')
ylabel('Function Maximum')
% disable the holf on command
hold off


%% [2] Study 2 - bounded Statistical behavior - set Upper and lower bounds

tic                                      % tic--- string name = toc use to determine computation time
for i=1:num_case                         % i is equal to 1 to 50 range 
    % ga is the inbuilt function in Matlab for solving genetic variables
    % stalagmite function for global maximize
    % x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
    % Set bounds lb and ub
    [inputs, zopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);% Optimum value for z 
    xopt(i)=inputs(1);                   % Optimum value for x
    yopt(i)=inputs(2);                   % Optimum value for y
end
study_time_2 = toc


%% plot Second figure
figure(2)                                % plot first figure
% subplot - To plot multiple plot in single figure 
% (10,10,[2 8 53 58]) 10,10 is 10x10 matrix
% [2 8 53 58] - position of plot in 10x10 matrix
subplot(10,10,[2 8 53 58])                
hold on                                  % use hold on to add another plot in same figure
surfc(xx,yy,zz)                          % Plot 3Dimensional surface plot
shading interp                           % Remove all mesh or grids from 3D surafce
% Label x & y axis
xlabel('x plot')                         
ylabel('y plot')
% plot 3 point x,y,z using plot 3 
% mark plot points, size it and colour it using following commands
plot3(xopt,yopt,zopt,'marker','o','markersize',5,'markerfacecolor','r')
% title the first subplot
title('Bounded Input')
% subplot - To plot multiple plot in single figure 
% (10,10,[71 80 91 100]) 10,10 is 10x10 matrix
% [71 80 91 100] - position of plot in 10x10 matrix
subplot(10,10,[71 80 91 100])
% plot z points to compare it with Iterations
plot(zopt)
% Label x & y axis
xlabel('Iterations')
ylabel('Function Maximum')
% disable the holf on command
hold off


%% [3] Study 3 - bounded values and increasing GA iterations Statistical behavior

options = optimoptions('ga');
options = optimoptions(options,'PopulationSize',170);


tic                                      % tic--- string name = toc use to determine computation time
for i = 1:num_case                       % i is equal to 1 to 50 range 
    % ga is the inbuilt function in Matlab for solving genetic variables
    % stalagmite function for global maximize
    [inputs, zopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1,1],[],[],options);% Optimum value for z 
    xopt(i)=inputs(1);                   % Optimum value for x
    yopt(i)=inputs(2);                   % Optimum value for y
end
study3_time = toc


%% plot Third figure
figure(3)                                % plot first figure
% subplot - To plot multiple plot in single figure 
% (10,10,[2 8 53 58]) 10,10 is 10x10 matrix
% [2 8 53 58] - position of plot in 10x10 matrix
subplot(10,10,[2 8 53 58])                
hold on                                  % use hold on to add another plot in same figure
surfc(xx,yy,zz)                          % Plot 3Dimensional surface plot
shading interp                           % Remove all mesh or grids from 3D surafce
% Label x & y axis
xlabel('x plot')                         
ylabel('y plot')
% plot 3 point x,y,z using plot 3 
% mark plot points, size it and colour it using following commands
plot3(xopt,yopt,zopt,'marker','o','markersize',5,'markerfacecolor','r')
% title the first subplot
title('Bounded Inputs with modified population size')
% subplot - To plot multiple plot in single figure 
% (10,10,[71 80 91 100]) 10,10 is 10x10 matrix
% [71 80 91 100] - position of plot in 10x10 matrix
subplot(10,10,[71 80 91 100])
% plot z points to compare it with Iterations
plot(zopt)
% Label x & y axis
xlabel('Iterations')
ylabel('Function Maximum')
% disable the holf on command
hold off

Function program

%% Stalagmite Fuction  for Genetic algorithm
function [F] = stalagmite(input_vector)

% mention input (1) and (2) as x and y respectively
x = input_vector(1);
y = input_vector(2);

% write down all equation code
f1 = (sin(5.1*pi*x+0.5))^6;

f2 = (sin(5.1*pi*y+0.5))^6;

f3 = exp(-4*log10(2)*(((x-0.0667)^2)/0.64));

f4 = exp(-4*log10(2)*(((y-0.0667)^2)/0.64));

F = -(f1.* f2.* f3.* f4);
end

plots study 1, 2 and 3 respective orderly

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.