Project

Optimizing Stalagmite function using Matlab program and Finding Global Maxima

Optimize Stalagmite Function using Matlab

Aim: To optimize Stalagmite function and find the Global maxima of the function using the Matlab program

Theory:

Genetic Algorithm:

The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution. The genetic algorithm repeatedly modifies a 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 the children for the next generation. Over successive generations, the population “evolves” towards an optimal solution. You can apply the genetic algorithm to solve a variety of optimization problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, non-differentiable stochastic or highly nonlinear. The genetic algorithm can address problems of mixed integer programming, where some components are restricted to be integer-valued.

The genetic algorithm uses three main types of rules at each steps to create the next generation from the current population

Selection rules select the individuals, called parents, that contribute to the population at the next generation.

Crossover rule combine two parents to form children for the next generation.

Mutation rule apply changes to individual parents to form children.

Syntax for GA in Matlab :

x = ga(fun,nvars,A,b,Aeq,Beq,lb,ub,nonlcon,options)

minimizes with the default optimization parameters replaced by values in options (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions

fun = Objective function

nvars = Number of variables,

A = Linear inequality constraints (real matrix)

b = Linear inequality constraints (real vectors)

Aeq = Linear equality constraints (real matrix)

beq = Linear equality constraints (real vectors)

lb = Lower bounds ([] default or real vector or matrix)

ub = Upper bounds ([] default or real vector or matrix)

nonlcon = Non linear constraints (Function handle, function name)

options = Optimization options (Output of optimoptions, Structures)

Program Description:

(1). We create the code for stalagmite function in a separate function which contains the following equations which by passing input array as input arguments

f(x,y) = f1,x f2,x f1,y f2,y

`f1,x = [sin(5.1π.x+0.5)]^6`

`f1,y = [sin(5.1π.y+0.5)]^6`

`f2,x = exp[-4ln(2)((x-0.0667)^2)/0.64]`

`f2,y = exp[-4ln(2)((y-0.0667)^2)/0.64]`

(2). In the main program, we create two input arrays and create 2D arrays using two input arrays by meshgrid commands.

(3). For the study of the statistical behaviour of Genetic Algorithm primarily a stopwatch timer is started. By using the genetic algorithm solver by passing the function to solve the stalagmite function with two number of variables. Inputs is the array to store the optimum values it the variables of the stalagmite function as selected. Fopt is the array to store the optimum values if the variables of the stalagmite function for all values of x and y. Input and fopt are the two outputs of GA. Then saving the value of x and y selected by GA in the array xopt and yopt respt. for each iteration.

(4). For maximizing, converting the values of the stalagmite function stored in the array fopt to their negative values. The minimum value of –f(x) is the maximum value of f(x). Then saving the time taken by program to run the first iteration of GA in study1.

(5). After that we plot the result.

(6). Now for the study of Statistical behaviour of Genetic Algorithm with Upper and Lower bounds, the GA solver is used by passing the function to solve the stalagmite func with two number of variables. We repeat the same process only change is in the input arguments of GA i.e. lower and upper bound values are given and other remaining arguments are passed as defaults [].

(7). For maximizing we do same process as first study. After that we plot the results.

(8). For the study of statistical behaviour of GA by increasing the number of iterations. Using the optimoptions function to return default options to ga solver. The default options of the ga solver with named parameter Population size with a value of 500. We repeat the same process as in above two studies except the input arguments of GA solver in which we make changes as mentioned above.

(9). For maximizing, we repeat the same process as in above studies and plot the result.

Matlab program (main file):

% A Program for Stalagmite function Optimization using genetic algorithm
clear all
close all
clc
% Creating input arrays for passing as variables for stalagmite function
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
[xx,yy]=meshgrid(x,y);    % creating 2d array mesh using meshgrid cmd
numcases=50;               % defining the no of iteration values
for i= 1:length(xx)
    for j=1:length(yy)
        inputvector(1)=xx(i,j);
        inputvector(2)=yy(i,j);
        f(i,j)=stalagmite(inputvector);    % calling stalagmite function
       
    end
end
% generating stalagmite function
surf(xx,yy,-f)                
shading interp         
[inputs,fval] = ga(@stalagmite,2)
xlabel('x value')
ylabel('y value')
title('Stalagmite function')

% Study Statistical Behaviour for GA with unbounded inputs
tic
for i=1:numcases
    [inputs,fopt(i)] = ga(@stalagmite,2);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study1 = toc          % time registered is stored in study1
figure(2)             % Plotting of result
subplot(2,1,1)
hold on
surfc(x,y,-f)            
title('UNBOUNDED INPUTS')
xlabel('x value')
ylabel('y value')
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')

% Study of Statistical Behavior for GA with Bounded inputs
tic;
for i=1:numcases
    [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study2 = toc               % time for study is stored in study2
figure(3)                  % Plotting the result
subplot(2,1,1)
surfc(x,y,-f)
shading interp
title('BOUNDED INPUTS')
xlabel('x value')
ylabel('y value')
 hold on
 
 plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
 subplot(2,1,2)
 plot(-fopt)
 xlabel('iterations')
 ylabel('function maximum')
 
 %Study of statistical behavior for GA with increased iteration or
 %population
 options = optimoptions(@ga)                     
 options = optimoptions(@ga,'populationsize',500)
 
 tic
 for i=1:numcases
      [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
      xopt(i)=inputs(1);
      yopt(i)=inputs(2);
 end
  study3time=toc            %time registered is stored here
  figure(4)                 %plotting of result
  subplot(2,1,1)
  surfc(x,y,-f)   
  shading interp
  title('BOUNDED INPUTS WITH INCREASED POPULATION SIZE')
  xlabel('x value')
  ylabel('y value')
  hold on
  plot3(xopt,yopt,-fopt,'marker','o','markersize',4,'markerfacecolor','r')
  subplot(2,1,2)
  plot(-fopt)
  xlabel('iterations')
  ylabel('function maximum')
  

Matlab program (function file):

% Separate program for stalagmite function
function [f]=stalagmite(inputvector)
a=inputvector(1);
b=inputvector(2);
f1x=(sin((5.1*pi*a)+0.5))^6;
f1y=(sin((5.1*pi*b)+0.5))^6;
f2x=exp(-4 *log(2)*((a-0.0667)^2)/0.64);
f2y=exp((-4 *log(2)*((b-0.0667)^2)/0.64));
f=-(f1x*f1y*f2x*f2y);
end

Result:

Plot for Stalagmite function :

For study of Statistical Behaviour of GA with Unbounded inputs:

For study of Statistical Behaviour of GA with Bounded inputs:

For study of Statistical Behaviout of GA for Increased population with bounded inputs:

After execution of program we get the study time and Workspace variables as shown in figure:

Conclusion:

  • From the plots we can see that the for the GA with unbounded inputs the most of the maximum values are between 1 and 0 and for GA with bounded input the maximum inputs are between 0.7 and 0.85. Hence we can say that Bounded inputs narrows down the maximum values but with increased study time.
  • Further for GA with Bounded inputs with increased population the maximum values are further narrowed with increase in study time.

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.