Project

STALAGMITE FUNCTION - OPTIMIZATION AND GLOBAL MAXIMA

AIM:

To optimise Stalagmite Function and find Global Maxima of the Function using MATLAB

INTRODUCTION TO GENETIC ALGORITHM

A genetic algorithm (GA) is a search and optimization method which works by imitating the evolutionary principles and chromosomal processing in natural genetics. Genetic Algorithm begins its operation with a random set of solutions. Every solution is assigned a fitness which is directly related to the objective function of the search and optimization problem. Generally Genetic Algorithm minimises the function, hence it may be used to find out the set of solution which might lead to the least error in a system under consideration.
Thereafter, the population of solutions is modified to a new population by applying three operators similar to natural genetic operators-reproduction, crossover, and mutation. Genetic Algorithm works iteratively by successively applying these three operators in each generation till a termination criterion is satisfied.
Genetic Algorithm have been successfully applied to a wide variety of engineering problems, because of their simplicity, global perspective, and inherent processing etc.
The purpose of optimization is to achieve the best solution relative to a set of prioritized criteria or constraints. These include maximizing factors such as productivity, strength, reliability, longevity, efficiency, and utilization.
Steps Of Optimisation in Genetic Algorithm:
There are three main steps of optimisation in GA
1) Selection
2) Cross over
3) Mutation
After fitness assignment has been performed, the Selection operator chooses the individuals that will recombine for the next generation. The individuals most likely to survive are those more fitted to the environment. Therefore, the selection operator selects the individuals according to their fitness level.
Crossover and mutation perform two different roles.
Crossover (like selection) is a convergence operation which is intended to pull the population towards a local minimum/maximum. .
Mutation is a divergence operation. It is intended to occasionally break one or more members of a population out of a local minimum/maximum space and potentially discover a better minimum/maximum space.
Since the end goal is to bring the population to convergence, selection/crossover happen more frequently (typically every generation). Mutation, being a divergence operation, should happen less frequently, and typically only effects a few members of a population (if any) in any given generation.
STALAGMITE FUNCTION:
CODE:
1) Main Program
clear all
close all
clc
 a = linspace(0,0.8,150);
 b = linspace(0,0.8,150);
 %creating input matrix
 [aa,bb] = meshgrid(a,b);
 
%obtaining the output from the function
for i = 1: length(a)
    for j = 1:length(b)
        inputs(1) = aa(i,j);
        inputs(2) = bb(i,j);
        P(i,j) = STALAGMITE(inputs);
        %P(i.j) is a matrix of output from the STALAGMITE function
    end
end
figure(1)
surfc(aa,bb,-P)
shading interp
title(\'FUNCTION PLOT\')
colorbar
xlabel(\'X-Axis\')
ylabel(\'Y-Axis\')
zlabel(\'Z-Axis\')

%fixing total number of cases.
nc = 50;
%case 1 - unbounded condition
for i = 1: length(a)
    for j = 1:length(b)
        inputs(1) = aa(i,j);
        inputs(2) = bb(i,j);
        P(i,j) = STALAGMITE(inputs);
        %P(i.j) is a matrix of output from the STALAGMITE function
    end
end
tic
for k = 1:nc
    [inputsS1, fnS1(k)] = ga(@STALAGMITE,2);
    xnS1(k) = inputsS1(1);
    ynS1(k) = inputsS1(2);
end
Runtime_unbound = toc;
%ploting the surface plot and marking the maxima
figure(2)
subplot(2,1,1)
surfc(aa,bb,-P)
%-ve for P since ga gives the minima and hence inverting for maxima.
shading interp
colorbar
title(\' UN-BOUND OR UN-CONSTRAINED\')
hold on
plot3(xnS1,ynS1,-fnS1,\'o-\',\'Markerfacecolor\',\'r\',\'Markersize\',4)
%using -fnS1 since the we have to plot the maxima points
xlabel(\'X-Axis\')
ylabel(\'Y-Axis\')
zlabel(\'Z-Axis\')
subplot(2,1,2)
plot(-fnS1)
%using -fnS1 since the we have to plot the fitness points
xlabel(\'No. of iterations\')
ylabel(\'Function MAX\')

%CASE 2 x = ga(fun,2,A,b,Aeq,beq,lb,ub) - providing boundaries
for i = 1: length(a)
    for j = 1:length(b)
        inputs(1) = aa(i,j);
        inputs(2) = bb(i,j);
        P(i,j) = STALAGMITE(inputs);
        %(i.j) is a matrix of output from the STALAGMITE function
    end
end
tic
for o = 1:nc
    [inputsS2, fnS2(o)] = ga(@STALAGMITE,2,[],[],[],[],[0;0],[0.4;0.4]);
    xnS2(o) = inputsS2(1);
    ynS2(o) = inputsS2(2);
end
Runtime_bound = toc;

%ploting the surface plot and marking the maxima
figure(3)
subplot(2,1,1)
surfc(aa,bb,-P)
%-ve for P since ga gives the minima and hence inverting for maxima.
shading interp
colorbar
title(\' BOUND OR CONSTRAINED\')
hold on
plot3(xnS2,ynS2,-fnS2,\'o-\',\'Markerfacecolor\',\'r\',\'Markersize\',5)
xlabel(\'X-Axis\')
ylabel(\'Y-Axis\')
zlabel(\'Z-Axis\')
subplot(2,1,2)
plot(-fnS2)
%using -fnS2 since the we have to plot the fitness points
xlabel(\'No. of iterations\')
ylabel(\'Function MAX\')

%CASE 3 Bounded with fixed Population
for i = 1: length(a)
    for j = 1:length(b)
        inputs(1) = aa(i,j);
        inputs(2) = bb(i,j);
        P(i,j) = STALAGMITE(inputs);
        %(i.j) is a matrix of output from the STALAGMITE function
    end
end

tic
options = optimoptions(\'ga\')
options = optimoptions(options,\'populationsize\',250)
for o = 1:nc
    [inputsS3, fnS3(o)] = ga(@STALAGMITE,2,[],[],[],[],[0;0],[0.4;0.4],[],options);
    xnS3(o) = inputsS3(1);
    ynS3(o) = inputsS3(2);
end
Runtime_pop_bound = toc;

%ploting the surface plot and marking the maxima
figure(4)
subplot(2,1,1)
surfc(aa,bb,-P)
%-ve for P since ga gives the minima and hence inverting for maxima.
shading interp
colorbar
title(\' BOUND OR CONSTRAINED with Population\')
hold on
plot3(xnS3,ynS3,-fnS3,\'o-\',\'Markerfacecolor\',\'r\',\'Markersize\',6)
xlabel(\'X-Axis\')
ylabel(\'Y-Axis\')
zlabel(\'Z-Axis\')
subplot(2,1,2)
plot(-fnS3)
%using -fnS3 since the we have to plot the fitness points
xlabel(\'No. of iterations\')
ylabel(\'Function MAX\')

type = [{\'unbound\';\'Bounded\';\'Bounded with options\'}];
time = [Runtime_unbound;Runtime_bound;Runtime_pop_bound];
RESULTS = table(type,time,\'VariableNames\',{\'STUDY TYPE\',\'STUDY TIME\'})

2) Function Definition

%Defining the Function
function [OP] = STALAGMITE(inputs)
  %defining x and y
 x = inputs(1);
 y = inputs(2);
  
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));

%providing the output from the function
OP = -1*f1x*f2x*f1y*f2y;
 %-1 is multiplied to maximise the inputs or to obtain a max. value.
end

Code Explanation:

Line 4 - 7: Input array and matrix is defined and generated.

Line 10 - 17: using for loop to obtain output of the function for each value of the input matrix

Line 18 - 25 : Creating the plot of the given function using surfc command, -P is taken to plot the maximum values.

Line 28 : Fixing the numbe of iterstions required.

Line 30 - 37 : using for loop to obtain output of the function for each value of the input matrix

Line 39 - 43 : using genetic algorithm for the function to get maximised output. also tic - toc command used to find out the time taken for the current study.

Line 46 -53 : Creating the plot of the given function and condition using surfc command, -P is taken to plot the maximum values.

Line 54 - 63: plotting the points at which the function is having the maximum ( -fnS1 used to plot the maximum value, where fnS1 contains the minimum value)

Line 65 - 73 : using for loop to obtain output of the function for each value of the input matrix

Line 75 to 80 :using genetic algorithm for the function with boundaries to get maximised output. also tic - toc command used to find out the time taken for the current study.

Line 83 - 90 : Creating the plot of the given function and condition using surfc command, -P is taken to plot the maximum values.

Line 91 - 99 :plotting the points at which the function is having the maximum ( -fnS2 used to plot the maximum value, where fnS2 contains the minimum value)

Line 101 - 109 :using for loop to obtain output of the function for each value of the input matrix

Line 112-113 : defining options to get the global maxima value( defining the population size)

Line 114 - 119 :using genetic algorithm for the function with boundaries and population size to get maximised output. also tic - toc command used to find out the time taken for the current study.

Line 122 -129 : Creating the plot of the given function and condition using surfc command, -P is taken to plot the maximum values.

Line 130 - 139: plotting the points at which the function is having the maximum ( -fnS3 used to plot the maximum value, where fnS3 contains the minimum value)

Line 139 - 141: Taking the time taken to a table to display as output.

Function description:

initially x and y are defined as inputs to the functions respectively.

Then the definition of function is done.

The result of the function is assigned to a variable. The negative sign is required since Genetic algorithm minimises a function , so the negative of the function helps in creating the maximum values.

Syntax For GA :

Case 1

x = ga(fun,nvars)

produces minimum x for function \'fun\', having \'nvars\' number of design variables.

CASE 2

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

In this case, lower and upper boundary is specified where the solution lies in between the boundaries. Here since linear inequalities are not considered, replace A,b, Aeq, beq by [].

Case 3

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

\'nonlcon\' represents the non linear inequalities which is not considered,hence replaced by [] in the syntax. The options variable is used for optimisation within a specified size. options can be defined using optimoptions command.

RESULT:

1) Function Plot

2) Case 1 - UN-BOUNDED Condition

3) Case 2 - Function with Lower and Upper Boundaries

4) Case 3 - Function with Boundaries and Specified population size

5) Time To Study

Errors Faced:

1) Error : x and y value not defined for the function

Correction: x and y defined within the function

Screen Shot:

2) Error : x and y value wrongly defined for the function

Correction: x and y defined as per need of the function

Screen Shot:

3) Error : wrong description of command population size

Correction: \'size\' changed to \'populationsize\'

Screen Shot:

4) Error : synatx error while using the ga command with boundaries and options

Correction: missing value as per syntax was provided

Screen Shot:

CONCLUSION:

The maxima for the function was obtained by inversing the function output using Genetic Algorithm operation. Through the process of selection, cross-over and mutation, the Global Maxima for the function is produced. Time for each study reveals the amount of processing required for obtaining the Global Maxima starting from a set of Local Maximas.

REFERENCES:

https://link.springer.com/chapter/10.1007/978-3-540-39930-8_2

https://www.neuraldesigner.com/blog/genetic_algorithms_for_feature_selection

https://in.mathworks.com/discovery/genetic-algorithm.html

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.