## FINDING GLOBAL MAXIMA BY OPTIMISING THE STALAGMITE FUNCTION IN MATLAB

AIM:

To understand the concept of genetic algorithm and to use the acquired knowledge to optimise the stalagmite function and to point the global maxima of the function.

UNDERSTANDING THE CONCEPT OF GENETIC ALGORITHM:

GENETIC ALGORITHM:

• It is a search based optimization technique based on the principle of natural genetics and natural selection.
• It searches for the optimal solution in the solution space.
• It istarts with the random population of the solution and at each iteration, reproduction of new solution is done in order to get optimal solution.

What does optimization mean?

• Optimization is the process of making something better.

• Optimization refers to finding the values of inputs in such a way that we get the best output values

GENETIC ALGORITHM REPRODUCTION:

Reproduction in genetic algorithm is done by applying the GA sophisticated operators

• Selection
• Cross-over
• Mutation

SELECTION:

• From the set of solution, a number of fitter individuals are selected.
• Fitness is calculated using fitness function.

CROSS OVER:

• In this more than one parent is selected and one or more off-springs are produced using the genetic material of parents selected during the selection process.
• Example:-

MUTATION:

• Mutation is the part of GA which is related to the "exploration" of the search space.
• In natural genetics mutation happens when children have feature that are not present in their parents.

TERMINATION CONDITION OF GA:

• When there has been no improvements in the population of X iterations.
• When we reach the absolute no of generations.
• When the objective function value has reached a certain pre-defined value.

GA FLOW CHART:

OUR STALAGMITE FUNCTION:

PROGRAM TO FIND THE GLOBAL MAXIMA:

% Main program to find the global maxima
clear all
close all
clc

% Defining our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50;

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

% Calling stalagmite function
% Evaluating 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

tic
% Study 1 - Statistical Behaviour
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);

end
study1_time=toc;

%plotting
figure(1)
subplot(2,1,1)
hold on
surfc(x, y, -f)
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')

tic
% Study 2 - Statistical Behaviour - with upper and lower bounds
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1])
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study2_time=toc;

%plotting
figure(2)
subplot(2,1,1)
hold on
surfc(x, y, -f)
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')

% Study 3 - Increasing GA iterations
options = optimoptions('ga');
options = optimoptions(options,'populationsize',280);

tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study3_time=toc;

%plotting
figure(3)
subplot(2,1,1)
hold on
surfc(x, y, -f)
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs with modified population size')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')


INPUTS:

STEP1:

To calculate the values of x & y and to find the global maxima we have to code the above formula as a function and then call this function into our main program

% stalagmite function
function [f]=stalagmite(input_vector)

x1=input_vector(1);
y1=input_vector(2);

term1=(sin(5.1*pi*x1+0.5)).^6
term2=(sin(5.1*pi*y1+0.5)).^6
term3=exp(-4*log(2)*((x1-0.0667).^2/0.64))
term4=exp(-4*log(2)*((y1-0.0667).^2/0.64))
f=-(term1.*term2.*term3.*term4);
end


As the genetic algorithm always find the minimum value, but our aim is to find the maximum value, Hence here in the above program we have used the -(minus) sign to minimize the function f to calculate the maximum value

STEP2:

Calling the stalagmite function into our main program

% Main program to find the global maxima
clear all
close all
clc

% Defining our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50;

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

% Calling stalagmite function
% Evaluating 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


• In the above program we are starting by using the clear all, close all, clc commands to erase the previous program results
• Now, we are defining the values for x and y which act as a parent in this program, then we are determining the value for num_cases which defines the no. of. iterations.
• Now we are using the mesh grid command to create a 2dimensional mesh

i.e., xx=150*150

yy=150*150

• Now we are calling the stalagmite function into our main program using the for loop by defining the values of input_vector(1) & (2) to calculate the value of "f" which is also of size 150*150.

STEP3:

The 3rd step is to conduct study 1 to calculate the statistical behaviour of unbounded inputs and plot graph.

tic
% Study 1 - Statistical Behaviour
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);

end
study1_time=toc;

%plotting
figure(1)
subplot(2,1,1)
hold on
surfc(x, y, -f)
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')
• Here in the above program we have defined the for loop for a length of num_cases to get the values of xopt, yopt and fopt
• we have use the tic toc commands to calculate the time for study one
• we have used the subplot command to plot the 3D representation of xopt,yopt,fopt and the 2d graph of f opt in the same window
• As the genetic algorithm always find the minimum value, but our aim is to find the maximum value, Hence here in the above program we have used the -(minus) sign to minimize the function f  and fopt to calculate the maximum value

OUTPUT1:

CONCLUSION1:

In 1st study we find that the  maximum values are not clearly defined and a lot of minimum values have been defined. hence, we are going to proceed with study 2

STEP4:

Our 4th step is to conduct study 2 to calculate the statistical behaviour of bounded inputs and plot a graph.

tic
% Study 2 - Statistical Behaviour - with upper and lower bounds
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1])
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study2_time=toc;

%plotting
figure(2)
subplot(2,1,1)
hold on
surfc(x, y, -f)
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')
• Here in the above program we have defined the for loop for a length of num_cases to get the values of xopt, yopt and fopt
• we have use the tic toc commands to calculate the time for study one
• We are using the ga syntax to fix the upper and lower bound values i.e., [0;0] and [1;1]
• We have used the empty braces to calculate the value of linear equality and linear inequality
• we have used the subplot command to plot the 3D representation of xopt,yopt,fopt and the 2d graph of f opt in the same window
• As the genetic algorithm always find the minimum value, but our aim is to find the maximum value, Hence here in the above program we have used the -(minus) sign to minimize the function f  and fopt to calculate the maximum value

OUTPUT2:

CONCLUSION2:

From study 2 we results we are getting closer to global maxima and there is no trace of minimum values.

STEP5:

Our 5th step is to conduct study 3 to calculate the statistical behaviour of bounded inputs with increased population size and plot a graph.

% Study 3 - Increasing GA iterations
options = optimoptions('ga');
options = optimoptions(options,'populationsize',280);

tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study3_time=toc;

%plotting
figure(3)
subplot(2,1,1)
hold on
surfc(x, y, -f)
hold on
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs with modified population size')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')
• Here in the above program we have defined the for loop for a length of num_cases to get the values of xopt, yopt and fopt
• we have use the tic toc commands to calculate the time for study one
• Here in our syntax the first four empty braces are used to calculate the value of linear equality and linear inequality and then to fix the upper and lower bound values i.e., [0;0] and [1;1] and then the other two empty braces are used to calculate the nonlinear constraints and integer variables.
• we have used the subplot command to plot the 3D representation of xopt,yopt,fopt and the 2d graph of f opt in the same window.
• As the genetic algorithm always find the minimum value, but our aim is to find the maximum value, Hence here in the above program we have used the -(minus) sign to minimize the function f  and fopt to calculate the maximum value.

OUTPUT3:

CONCLUSION3:

From study 3 by increasing the population size we have got the global maxima point.

THE STUDY TIME FOR ALL THE THREE STUDIES:

ERRORS FACED WHILE PROGRAMMING:

1.Forget to use '.' while multiplying the stalagmite functions with power

2.An error is surface plot command by providing a 1Dimensional matrix

STEPS TAKEN TO OVERCOME THE ERRORS:

1.By looking at the red hints popped at the command window

2.By contacting the support engineer and clarifying the doubts

3.By recalling the instructions provided during the course

CONCLUSION:

Thus by refering to various references and understanding the concept of GA the code to find the global maxima by optimising the stalagmite function has been coded and and implemented successfully.

REFERENCES:

https://in.mathworks.com/help/optim/ug/optim.problemdef.optimizationproblem.optimoptions.html#d117e127932

https://www.researchgate.net/post/How_can_you_find_the_max_function_with_Genetic_Algorithm

### CAR CRASH SIMULATION USING ANSYS WORKBENCH Avinash A S · 2020-02-27 12:32:21

AIM:      To perform a car crash analysis using ansys workbench. PROBLEM STATEMENT: To perform a parametric study using 3 different values of thickness for the car body. Here the car body remains stationary but the wall displaces to a distance of 500 Read more

### COPPER ROLLING SIMULATION IN ANSYS WORKBENCH Avinash A S · 2020-02-25 12:15:10

AIM:     To perform structural analysis to simulate the rolling operation on a copper workpiece of length 90mm. STRUCTURAL ANALYSIS: Structural analysis is the process of calculating and determining the effects of loads and internal forces on a struc Read more

### EXPLICIT DYNAMICS SIMULATION OF A BULLET PENETRATING INTO A BUCKET Avinash A S · 2020-02-12 11:33:34

AIM:    To perform explicit dynamics simulation of a bullet penetrating into a bucket considering three different non-linear materials for the bucket and analyse their behaviour for the same bullet velocity. CASE SETUP:1 Assign the bucket material as Copp Read more

### EXPLICIT ANALYSIS OF THE MACHINING WITH PLANER Avinash A S · 2020-02-09 05:39:33

AIM:

To perform an explicit dynamics simulation to simulate the machining operation with using a planer and observe their behaviour at 2 different velocities.

### TENSION AND TORSION TEST ON STEEL SPECIMEN USING ANSYS WORK BENCH Avinash A S · 2020-02-08 07:01:58

AIM:    To perform the tension and torsion test on the specimen considering the case setups and find out the total deformation, equivalent stress and temperature of the specimen. TENSION TEST: A tensile test, also known as a tension test, is one Read more

### TRANSIENT STRUCTURAL ANALYSIS ON WORM GEAR ASSEMBLY USING ANSYS WORKBENCH Avinash A S · 2020-02-07 13:06:11

AIM:    To perform transient structural analysis on a worm gear assembly. TRANSIENT STRUCTURAL ANALYSIS: Transient structural analysis is the process of calculating and determining the effects of loads and internal forces that are a function of time on a Read more

### TRANSIENT STRUCTURAL ANALYSIS OF DOUBLE UNIVERSAL JOINT USING ANSYS WORKBENCH Avinash A S · 2020-02-07 11:24:22

AIM:     To perform a transient structural analysis on a double universal joint with a spring using three different materials and then compare the results. TRANSIENT STRUCTURAL ANALYSIS: Transient structural analysis is the process of calculating and Read more

### TRANSIENT STRUCTURAL ANALYSIS OF LONG PISTON WITH CAM USING ANSYS WORKBENCH Avinash A S · 2020-02-06 10:49:41

AIM:       To perform a transient structural analysis on a piston and cam mechanism model considering different case setups and then compare the results of the case setups. TRANSIENT STRUCTURAL ANALYSIS: Transient structural analysis is the process o Read more

### ANALYSIS OF SPHERE PRESSING ON PLATE USING ANSYS WORKBENCH Avinash A S · 2020-01-29 08:41:21

AIM:      To perform structural analysisto simulate a sphere pressing on plate considering the case setup and experience the plastic deformation on the plate. STRUCTURAL ANALYSIS: Structural analysis is the process of calculating and determining the Read more

### BENDING OF WIRE USING ANSYS WORKBENCH Avinash A S · 2020-01-28 09:18:24

AIM:    To perform an structural analysis to simulate the bending of a wire considering three different case setups and compare their results. STRUCTURAL ANALYSIS: Structural analysis is the process of calculating and determining the effects of loads and Read more