Project

Optimization of anonymous function Stalagmite with the inbuilt solver 'Genetic Algorithm'(ga) function in MATLAB. To get the constant global maxima of the function.

Title :-

Optimization of anonymous function Stalagmite with the inbuilt solver 'Genetic Algorithm'(ga) function in MATLAB. To get the constant global maxima of the function.

Dilemma Declaration :-

  • To write a code in MATLAB to optimise the stalagmite function using the built-in genetic algorithm function and find the ‘global maxima of the function’.
  • Having statistical studies on the genetic algorithm (ga) for each variation to achieve the constant global maxima value for all iterations and adding bounds and options to population size in case studies strides.

Overview :-

Optimization :-

Optimization is used to achieve the best design relative to a set of prioritized criteria or constraints in the function. In simple , optimization consists of maximum or minimum of the function and select the input values from within the following set and compute the value of the function.In MATLAB , 'ga' algorithm always finds the minimum of the following function.

Genetic algorithms (GAs) :-

Genetic algorithms (GAs) is a method for solving both constrained and unconstrained optimization problems based on innate selection process that mimicking the process of evolution in nature, genetic algorithm repeatedly modify a population of individual solutions. At each stride,genetic algorithms randomly select individuals from the current population and use them as parents to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution.Each succeeding generation of any species must retain the good properties of the preceding generation while improving and altering them so that the quality of individuals in the population is continuously enhanced.So we get towards the optimal solution.

The above concept works is used in the execution of the 'ga' function in the program and optimise the stalagmite function. A random set of values are chosen from a scrum of values of the function for the purpose of attaining a higher value of generation,and the next set of values are chosen from the generation of values. A similar operation is performed until the (Tolerances and Stopping Criteria) threshold is achieved and the function returns the minimum function value and the corresponding to dependent variables.To get the optimal solution.

Which is most optimal 'Brute force' or Genetic algorithms (GAs) ?

The answer to the question is brute force requires checking every possible strategy to find the most optimal, there are cases where brute force approach is impossible where genetic algorithms can be useful.In a genetic algorithm the routing of the muscles and the control parameters are evolved .In brute force method you would have to test every possible muscle route and control value, and they are lots of floating point values.Instead a genetic algorithm allows you to approach functionality. The solution might not be optimal,but it's very good.

SYNTAX OF GAs IN MATLAB SOFTWARE :-

The syntax of the GAs in MATLAB is as followed :-

[x,fval]= ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options)

Here, the function is subjected to the following types of constraints:

Linear Constraints , Non- linear constraints & Bounds.

'x' is the variable which stores the minimum value of objective function.

‘fval’ is having the valve of the objective function at the solution.

‘fun’ is a fitness function. A fitness function is the function that takes a SAMPLING solution and analyzes it for the output.

‘nvars’ is for the number of design variables we have given to it.

'A' and 'b' are used to represent the linear inequalities of the function, if any. Otherwise, they are set as empty, '[]'.

'Aeq' and 'beq' are used to represent the equality constraints.

'lb' and 'ub' are used to specify the upper and the lower bounds of the variables of the function.

'nonlcon' is a function that is specified using a function handle. A separate function code must be written as per the constraints of the function.

This function gives two arrays 'c' and 'Ceq' as its outputs.

Where,

'c' stores in the values of the ‘equality’ constraints.

'Ceq' stores in the values of the ‘inequality’ constraints.

'IntCon' is used to specify the Integer Constraint, such that the value of the function is an integer.

'options' is used to provide the values of the default parameters using the named parameters, if it is required to replace the default parameters,and is done by using the function of 'optimoptions'.

Stalagmite :-

Stalagmite is a type of shape form in conelike. It rises from the ceilings of caves like icicles due to accumulation of material deposited on the floor from ceiling drippings.

Likewise we have a close identical geometrical shape that can be obtained by using the mathematical function. ‘The function applies two variables in two dimensions which yields the stalagmite shape for observation.

It is mathematically expressed as below.


Stalagmite function codes :-

fx_one=(sin((5.1*pi*x)+0.5))^6;

fx_two=exp(-4*log(2)*((x-0.0667)^2)/0.64);

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

fy_two=exp(-4*log(2)*((y-0.0667)^2)/0.64);

Stalagmite function values are calculated and stored in solution.

solution=-(fx_one)*(fx_two)*(fy_one)*(fy_two);

STEPWISE EXPLANATION OF THE MATLAB PROGRAM :-

This MATLAB program has a user defined Stalagmite Function (UDF) and a primary body. Three case studies are performed using the same function to obtain the global maxima.

Part 1:- Creating a user define Stalagmite function for GA.

To Create a function ‘stalagmite_fun’ by passing the input array as the argument for the variables x and y . The output of this user defined function is stored in the variable ‘solution’.

% to create a function of stalagmite by passing the input array as the argument for variables x
% & y. and getting the output of the function as it's stored in variable solution.


function solution =stalagmite_fun(input)

Valve of the input array is stored in the variable x

Valve of the input array is stored in the variable y

%valve of the input array stored in variables x and y.
x=input(1);
y=input(2);

Using the user define Stalagmite function codes :-

fx_one=(sin((5.1*pi*x)+0.5))^6;
fx_two=exp(-4*log(2)*((x-0.0667)^2)/0.64);
fy_one=(sin((5.1*pi*y)+0.5))^6;
fy_two=exp(-4*log(2)*((y-0.0667)^2)/0.64);

Stalagmite function values are calculated and stored in solution.

solution=-(fx_one)*(fx_two)*(fy_one)*(fy_two);

Here,the equations as shown in 'Stalagmite Equations' are entered to ‘solution’ is multiplied by -1 which generates a ‘downward stalagmite’.

Part 2:- Implementation of the User Defined Function and 'ga' in the primary body to get global maxima.

Firstally,maximization of Stalagmite Function using Genetic Algorithm (ga).

The program will starts with clear all close all & clc and then.

It stores 100 values of x from 0 to 0.6 and 100 values of y from 0 to 0.6 using ‘linspace’ command.And then to represent cases a value of 50 is assigned to variable ‘num_cases’.

x=linspace(0,0.6,100);
y=linspace(0,0.6,100);
num_cases = 50;

For creating 2D arrays using the arrays of x and y by ‘meshgrid’ command.

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

Now for assigning values to variables ‘input(1)’ and ‘input(2)’,we use two ‘for’ loops. Starting’ for’ loop from 1 to the length of array ‘xx’ using the counter ‘i’.Then starting one more nested ‘for’ loop from 1 to length of array ‘yy’ using the variable ‘j’. Saving the value of ‘xx’ as per the sequence of iteration of the ‘for’ loops, in the first column of array ‘input’. And secondly saving the value of ‘yy’ as per the sequence of iteration of the ‘for’ loops, in the second column of array ‘input’. Calling the function ‘stalagmite_fun’ that contains the code for evaluating the value of the function using the array input given .

for i=1:length(xx)
    for j=1:length(yy)
     
        input(1)=xx(i,j);
        input(2)=yy(i,j);
        F(i,j)=stalagmite_fun(input);
                     
    end
end

For studying the statistical behaviour of Genetic Algorithm (ga), we use a stopwatch timer. Starting ‘for’ loop from 1 to the length of ‘num_cases’ using the variable ‘v’. Calling the ‘genetic algorithm (ga)’ inbuilt solver by passing the function to solve the ‘stalagmite_fun’ function with two numbers of variables. ‘Inputs’ is the array to store the optimum values if the variables of the ‘stalagmite_fun’ function are selected. Fopt is the array to store the optimum values of the stalagmite function for all the values of ‘x’ and ‘y’. ‘Input’ and ‘Fopt’ are the outputs of the ga. Then saving the value of ‘x’ selected by ga in the array ‘xopt’ for every iteration of the ‘for’ loop. And saving the value of ‘y’ selected by ga in the array ’yopt’ for every iteration of the ‘for’ loop.

tic %starting stopwatch timer
for v = 1:num_cases


    [Inputs,Fopt(v)]=ga(@stalagmite_fun,2);
 
  
    xopt(v)=Inputs(1);
    yopt(v)=Inputs(2);
            
end

Now saving the time taken by the program to run the first iteration in the variable Case_Study1.

Case_Study1=toc %reading proceed time of the stopwatch  

Plotting for Case Study -1, a ‘figure’ (figure (1)) is generated using ‘figure’ command.Creating a (2,1) ‘subplot’ and selecting the subplot as 1 and plotting starts with a use of ‘hold on’ command which retains the plots in the current axes so that new plots adds to the axes and do not delete the existing plots. A ‘surfc’ command is used to create surface plot of the values of the stalagmite function stored in the array F. A smooth surface plot is generated by ‘shading interp’ command.For 3D plot we use ‘plot3’ command and a title to it.

figure(1)
subplot(2,1,1)
hold on
surfc(-xx,-yy,-F) %converts the contour plot (2-D) into surface plot (3-D)
shading interp %to vary the color in each line segment and face by the interpolating in the colormap index 
 
%plotting according to the genetic algorithm actions
%3-D point
plot3(-xopt,-yopt,-Fopt,'color','g','Marker','o','MarkerSize',4,'MarkerFaceColor','r')
title('unbounded inputs without modifying ga')

‘subplot2’ is selected and Fplot is plotted using ‘plot’ command by using specified color and linewidth. and the axis is labelled with x and y label and also using the colorbar.

 
%plotting to observe the variation with respect to optimal function valve against the iterations
subplot(2,1,2)
plot(-Fopt,'color','g','LineWidth',0.5)
xlabel('Iterations')
ylabel('Maximum Function')
colorbar
title ('Maxima vs Iterations')

INITIAL OUTPUT :-

Now in the Case Study -2 we study the Statistical behaviour of Genetic Algorithm with bounds (upper and lower bounds), here also a stopwatch timer is started. Starting ‘for’ loop from 1 to the length of ‘num_cases’ using the variable ‘v’. Calling the ‘genetic algorithm(ga)’ solver by passing the function to solve the given stalagmite function with two numbers of variables. Inputs2 is the array to store the optimum values if the variables of the stalagmite function are selected. Fopt2 is the array to store the optimum values of the stalagmite function for all the values of x and y. ‘Input2’ and ‘Fopt2’ are the outputs of the ga. Then saving the value of x selected by ga in the array ‘xopt2’ for every iteration of the ‘for’ loop. And saving the value of y selected by ga in the array ‘yopt2’ for every iteration of the ‘for’ loop.With respect to the syntax of ga.

tic %starting stopwatch timer
 
for v = 1:num_cases
    [Inputs2,Fopt2(v)]=ga(@stalagmite_fun,2,[],[],[],[],[0;0],[0.6;0.6]);
    
    xopt2(v)=Inputs2(1);
    yopt2(v)=Inputs2(2);
    
end

And now saving the time taken by the program to run the first iteration of ga in the variable Case_Study2.

Case_Study2=toc %reading proceed time of the stopwatch  

Plotting for Case Study -2, a ‘figure’ (figure (2)) is generated using ‘figure’ command.Creating a (2,1) ‘subplot’ and selecting the subplot as 1 and plotting starts with a use of ‘hold on’ command which retains the plots in the current axes so that new plots adds to the axes and do not delete the existing plots. A ‘surfc’ command is used to create surface plot of the values of the stalagmite function stored in the array F. A smooth surface plot is generated by ‘shading interp’ command.For 3D plot we use ‘plot3’ command and a title to it.

%plotting for Case Study -2  
 
figure(2)
subplot(2,1,1)
hold on
surfc(-xx,-yy,-F) %converts the contour plot (2-D) into surface plot (3-D)
shading interp %to vary the color in each line segment and face by the interpolating in the colormap index 
 
 
%plotting according to the genetic algorithm actions
%3-D point
plot3(-xopt2,-yopt2,-Fopt2,'color','g','Marker','o','MarkerSize',4,'MarkerFaceColor','r')
title('bounded inputs without modifying ga')

‘subplot2’ is selected and Fplot is plotted using ‘plot’ command by using specified color and linewidth. and the axis is labelled with x and y label and also using the colorbar.

%plotting to observe the variation with respect to optimal function valve against the iterations
%to observe in 2-D
 
 
subplot(2,1,2)
plot(-Fopt2,'color','g','LineWidth',0.5)
xlabel('Iterations')
ylabel('Maximum Function')
colorbar
title ('Maxima vs Iterations')

OUTPUT :-

Now in the Case Study -3, we study the Statistical behaviour of Genetic Algorithm by increasing the number of iterations. Using the ‘optimoptions’ function to return default options to the ‘ ga’ solver. Providing the default options of the ‘ga’ solver with named parameter ‘Population Size’ with a value of 200. Then a stopwatch timer is started. Starting ‘for’ loop from 1 to the length of num_cases using the variable v. Calling the ‘genetic algorithm’ solver by passing the function to solve the stalagmite function with two numbers of variables. Input3 is the array to store the ‘optimum’ values if the variables of the stalagmite function as selected. Fopt3 is the array to store the optimum values of the stalagmite function for all the values of x and y. Input2 and Fopt3 are the outputs of the ga. Then saving the value of x selected by ga in the array xopt3 for every iteration of the ‘for’ loop. And saving the value of y selected by ga in the array yopt3 for every iteration of the ’for’ loop.

% Case Study -3 :-  Statistical behaviour of Genetic Algorithm with 
%bounds (upper and lower bounds) and specification of population size.
 
 
%here we are going to use the 'options command' as we have to modify the
%population size we are using options so we can set up the field value options in 
%that is different from the default
 
options =optimoptions('ga'); %creating options using the function optimoptions.
options = optimoptions(options,'PopulationSize',500);
%creates options with all values set to defaults except for PopulationSize,which is set to 200.
 
 
tic %starting stopwatch timer
 
for v = 1:num_cases
    [Inputs3,Fopt3(v)]=ga(@stalagmite_fun,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    
    xopt3(v)=Inputs3(1);
    yopt3(v)=Inputs3(2);
        
end

And now saving the time taken by the program to run the first iteration of ga in the variable Case_Study3.

Case_Study3=toc %reading proceed time of the stopwatch  

Plotting for Case Study -3, a ‘figure’ (figure (3)) is generated using ‘figure’ command.Creating a (2,1) ‘subplot’ and selecting the subplot as 1 and plotting starts with a use of ‘hold on’ command which retains the plots in the current axes so that new plots adds to the axes and do not delete the existing plots. A ‘surfc’ command is used to create surface plot of the values of the stalagmite function stored in the array F. A smooth surface plot is generated by ‘shading interp’ command.For 3D plot we use ‘plot3’ command and a title to it.

%plotting for Case Study -3  
 
figure(3)
subplot(2,1,1)
hold on
surfc(-xx,-yy,-F) %converts the contour plot (2-D) into surface plot (3-D)
shading interp %to vary the color in each line segment and face by the interpolating in the colormap index 
 
 
%plotting according to the genetic algorithm actions
%3-D point
plot3(-xopt3,-yopt3,-Fopt3,'color','g','Marker','o','MarkerSize',4,'MarkerFaceColor','r')
title('bounded inputs & specifications to population size')

‘subplot2’ is selected and Fplot is plotted using ‘plot’ command by using specified color and linewidth. and the axis is labelled with x and y label and also using the colorbar.

%plotting to observe the variation with respect to optimal function valve against the iterations
%to observe in 2-D
 
 
subplot(2,1,2)
plot(-Fopt3,'color','g','LineWidth',0.5)
xlabel('Iterations')
ylabel('Maximum Function')
colorbar
title ('Maxima vs Iterations')

And we can use the ‘rng’ function to set the random number generator to its default state, to have reproducibility of the random numbers generated in the first run of ga,without reproducibility, ga produces different results on each run.

rng('default');

FINAL OUTPUT WITH GLOBAL MAXIMA :-

BODY OF THE PROGRAM :-

%getting the maximize of the stalagmite function, 
% using Gentic Algorithm. 

close all
clear all
clc


x=linspace(0,0.6,100);
y=linspace(0,0.6,100);
num_cases = 50;

%converting 1-D into 2-D by creating mesh 

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

%evaluating the stalagmite function,generating data for the plot

for i=1:length(xx)
    for j=1:length(yy)
        
        input(1)=xx(i,j);
        input(2)=yy(i,j);
        
        F(i,j)=stalagmite_fun(input);
        
                
    end
end
 


% Case Study -1 :- Statistical behaviour of Genetic Algorithm, 
% without providing boundary (bounds) or population size to the genetic algorithm

tic %starting stopwatch timer

for v = 1:num_cases
    [Inputs,Fopt(v)]=ga(@stalagmite_fun,2);
    
  
    xopt(v)=Inputs(1);
    yopt(v)=Inputs(2);
            
end

Case_Study1=toc %reading proceed time of the stopwatch  


%plotting for Case Study -1  

figure(1)
subplot(2,1,1)
hold on
surfc(-xx,-yy,-F) %converts the contour plot (2-D) into surface plot (3-D)
shading interp %to vary the color in each line segment and face by the interpolating in the colormap index 

%plotting according to the genetic algorithm actions
%3-D point
plot3(-xopt,-yopt,-Fopt,'color','g','Marker','o','MarkerSize',4,'MarkerFaceColor','r')
xlabel('x-axis');
ylabel('y-axis');
title('unbounded inputs without modifying ga')

%plotting to observe the variation with respect to optimal function valve against the iterations
subplot(2,1,2)

plot(-Fopt,'color','g','LineWidth',0.5)
xlabel('Iterations')
ylabel('Maximum Function')
colorbar
title ('Maxima vs Iterations')



% Case Study -2 :-  Statistical behaviour of Genetic Algorithm with 
%bounds (upper and lower bounds) and no specification of population size.


tic %starting stopwatch timer

for v = 1:num_cases
    [Inputs2,Fopt2(v)]=ga(@stalagmite_fun,2,[],[],[],[],[0;0],[0.6;0.6]);
    
    xopt2(v)=Inputs2(1);
    yopt2(v)=Inputs2(2);
    
    
end
    
    
Case_Study2=toc %reading proceed time of the stopwatch  

%plotting for Case Study -2  

figure(2)
subplot(2,1,1)
hold on
surfc(-xx,-yy,-F) %converts the contour plot (2-D) into surface plot (3-D)
shading interp %to vary the color in each line segment and face by the interpolating in the colormap index 


%plotting according to the genetic algorithm actions
%3-D point
plot3(-xopt2,-yopt2,-Fopt2,'color','g','Marker','o','MarkerSize',4,'MarkerFaceColor','r')
xlabel('x-axis');
ylabel('y-axis');
title('bounded inputs without modifying ga')

%plotting to observe the variation with respect to optimal function valve against the iterations
%to observe in 2-D


subplot(2,1,2)
plot(-Fopt2,'color','g','LineWidth',0.5)
xlabel('Iterations')
ylabel('Maximum Function')
colorbar
title ('Maxima vs Iterations')




% Case Study -3 :-  Statistical behaviour of Genetic Algorithm with 
%bounds (upper and lower bounds) and specification of population size.

%here we are going to use the 'options command' as we have to modify the
%population size we are usinf options so we can set up the field value optins in 
%that is different from the default


options =optimoptions('ga'); %creating options using the function optimoptions.
options = optimoptions(options,'PopulationSize',500);
%creates options with all values set to defaults except for PopulationSize,which is set to 200.


tic %starting stopwatch timer

for v = 1:num_cases
    [Inputs3,Fopt3(v)]=ga(@stalagmite_fun,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    
    xopt3(v)=Inputs3(1);
    yopt3(v)=Inputs3(2);
    
    
end

 Case_Study3=toc %reading proceed time of the stopwatch
 
 

%plotting for Case Study -3  

figure(3)
subplot(2,1,1)
hold on
surfc(-xx,-yy,-F) %converts the contour plot (2-D) into surface plot (3-D)
shading interp %to vary the color in each line segment and face by the interpolating in the colormap index 


%plotting according to the genetic algorithm actions
%3-D point
plot3(-xopt3,-yopt3,-Fopt3,'color','g','Marker','o','MarkerSize',4,'MarkerFaceColor','r')
xlabel('x-axis');
ylabel('y-axis');
title('bounded inputs & specifications to population size')
%plotting to observe the variation with respect to optimal function valve against the iterations
%to observe in 2-D


subplot(2,1,2)
plot(-Fopt3,'color','g','LineWidth',0.5)
xlabel('Iterations')
ylabel('Maximum Function')
colorbar
title ('Maxima vs Iterations')

%setting the random number generator to its default state
rng('default');







OBSERVATION :-

On having a look on 'workspace' the case study 1 shows that how algorithm behaves when no bounds are given. The lack of bounds results to the random sampling which is beyond and below the searchspace that we use to evaluating the stalagmite function which leads to random optimal function values for each iteration given.Whereas in case study 2 after inculding the upper and lower bounds, the level of randomness is given the optimal outputs of the respective function and also reduce the result of the defined search space to be,ultimately in case study 3 specification of population size on how many individuals there are in each respective generation.When with a large population size the genetic algorithm goes on to search the solution with searchspace more in depth as thereby it reducing chance of that algorithm returns a local minimum that is not a global minimum. Aa we also observe that with large population size it causes algorithm to run more slowly with increasing the population size.

RESULT :-

Hence ,the stalagmite function is being evaluated with which the global maxima is found using the inbulit genetic algorithm and output is also elaborated.

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.