Week 4.1 - Genetic Algorithm
Aim-: Write a matlab code to optimise the stalagmite function and find the global maxima also to understand the concept of Ganetic Algorithm.
Theory & Introduction
Stalagmite Function: What is a Stalagmite? A stalagmite is a type of rock formation that rises from the floor of a cave due to the accumulation of material deposited on the floor from ceiling drippings. Stalagmites may be composed of lava, minerals, mud, peat, pitch, sand, sinter and amber
Ganetical Algorithm: In my word in Algorithm taking so many Random values like a ganes in body
that values undergo in algorithm that gives the global minima like best mutation of chromsome.Algorithm made by Dr. charles Darwin . Ganectical algorithm generate the population of a each point. Best point of the population aproches the best optimal solution for next generation
- They need the given components: A process of coding solutions to solve the problem of chromosomes. ...
- ANNs working principle: The working principle of a standard Genetic Algorithm is illustrated in the given figure. ...
- Difference between traditional and genetic approach: An algorithm is a progression of steps for solving a problem


Genetic operators:-
- Selection – selection of random values to check global minimas & maximas,
--Crossover- combine two good value and compare values between both to check minimas & maximas
---Mutation – probability of selection of values to be low for good result
Method of mutation-
1. Bitwise method
2.Swap method
3.Inversion mutation
Syntax for GA-
x=ga(function, Nvar);
x=ga(function, Nvar,A,B);
x=ga(function, Nvar,A,B,Aeq,Beq,lb,ub);
x=ga(function, Nvar,A,B,Aeq,Beq,lb,ub,lnconst,nlnconst,options);
[inputs,f3_out(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options)
Nvar-no.of input variable-2
A- [] A matrix of linear inequlity equation
B- [] B vectors of linear inequlity equation
Aeq-[] linear equality
Beq-[] linear equality
lb- lower bound
ub- upper bound
lnconst-[]linear constraints
nlnconst-[]non-linear constraints
Mathematical Equation of Stalagmite Function

Matlab code for this-:
% program for stalagmite funtion
function solution= stalagmite(input)
x=input(1);
y=input(2);
f1x=(sin(((5.1*pi.*x)+0.5))).^6;
f2x= exp((((x-0.0667).^2).*(-4*log(2)))./0.64);
f1y= (sin(((5.1*pi.*y)+0.5))).^6;
f2y= exp((((y-0.0667).^2).*(-4*log(2)))./0.64);
solution=-((f1x)*(f2x)*(f1y)*(f2y));
end
Final matlab code to optimise the global maxima & Three study of graph plot b/w F maximum & no. of iteration--
explanation of case study in matlab program -
close all
clear all
clc
% defining our serach space
x=linspace(0,0.6,150);
y= linspace(0,0.6,150);
% creating 2-dimention data
[xx,yy]=meshgrid(x,y);
% evaluating the 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
[inputs,fval]=ga(@stalagmite,2);
x_out=inputs(1);
y_out=inputs(2);
xlabel('x axis --')
ylabel('y axis--')
% case 1 study
% finding a GA for limited no.of iterations without any specific
% boundaries , As GA works for random values so itmay consider values
% which are outside the our concern area and run for many values and
% slow the compiling of program sp we are constraning the no.of run it as follow.
Num_itr=40;
tic % used for calculating the time loop takes complete compiling
for i=1:Num_itr
[inputs,f1_out(i)]=ga(@stalagmite,2); % funtion that solve the local mainimas
x1(i)=inputs(1);
y1(i)=inputs(2);
end
study_time1=toc % calculate compile time
figure(1)
subplot(2,1,1) % subplot is used to plot 2 representation in single image
hold on
surfc(xx,yy,-f)
shading interp % create a smooth surface instead of having grid line
plot3(x1,y1,-f1_out,'marker','o','MarkerSize',5,'MarkerFaceColor','r') % plot3 use for 3 axis point
title('Unbounded inputs')
subplot(2,1,2) % ploting 2nd image for same image
plot(-f1_out)
xlabel('No.of iteration')
ylabel('function maximas')
% case 2 study
% finding a GA for limited no.of iterations with
% boundaries , As GA works for random values
% which are inside the our concern area and run for many values and
% slow the compiling of program sp we are constraning the no.of run it as follow.
Num_itr=40;
tic % used for calculating the time loop takes complete compiling
for i=1:Num_itr
[inputs,f2_out(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]); % funtion that solve the local mainimas
x2(i)=inputs(1);
y2(i)=inputs(2);
end
study_time2=toc % calculate compile time
figure(2)
subplot(2,1,1) % subplot is used to plot 2 representation in single image
hold on
surfc(xx,yy,-f)
shading interp % create a smooth surface instead of having grid line
plot3(x2,y2,-f2_out,'marker','o','MarkerSize',5,'MarkerFaceColor','r') % plot3 use for 3 axis point
title('bounded inputs')
subplot(2,1,2) % ploting 2nd image for same image
plot(-f2_out)
xlabel('No.of iteration')
ylabel('function maximas')
% case 3 study
% finding a GA for limited no.of iterations with bounded values controlled
% population size, As GA works for random values which are inside the
% our concern area with controlled size and run for many values and
% slow the compiling of program sp we are constraning the no.of run it as follow.
Num_itr=40;
options=optimoptions('ga'); % read the GA program & gives only optimum values
options=optimoptions(options,'PopulationSize',200); %optimum values with controlled size
tic % used for calculating the time loop takes complete compiling
for i=1:Num_itr
[inputs,f3_out(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options); % funtion that solve the local mainimas
x3(i)=inputs(1);
y3(i)=inputs(2);
end
study_time3=toc % calculate compile time
figure(3)
subplot(2,1,1) % subplot is used to plot 2 representation in single image
hold on
surfc(xx,yy,-f)
shading interp % create a smooth surface instead of having grid line
plot3(x3,y3,-f3_out,'marker','o','MarkerSize',5,'MarkerFaceColor','r') % plot3 use for 3 axis point
title('bounded inputs with controlled population size')
subplot(2,1,2) % ploting 2nd image for same image
plot(-f3_out)
xlabel('No.of iteration')
ylabel('function maximas')
workspace data-
MATLAB Workspace Page 1
May 28, 2022 1:54:17 PM
Name Value
f 150x150 double
f1_out 1x40 double
f2_out 1x40 double
f3_out 1x40 double
fval -0.2061
i 40
input_vector [0.6000,0.6000]
inputs [0.0668,0.0668]
j 150
Num_itr 40
options 1x1 GaOptions
study_time1 9.9324
study_time2 15.1778
study_time3 39.4638
x 1x150 double
x1 1x40 double
x2 1x40 double
x3 1x40 double
x_out -0.2901
xx 150x150 double
y 1x150 double
y1 1x40 double
y2 1x40 double
y3 1x40 double
y_out 0.0712
yy 150x150 double
output of a code :-
study first - select a random values without a boundries to optimise the data to gain local maxima

study two- select a values for cross over & mutation with a boundry condition that is inside the stalagmite curve.

study three- : select a values for crossover & mutation to check global maxima in forty no. of iterations that are controlled

Error comes - becuase of using negative sign in function calling output -f1_out(i)
MATLAB Command Window Page 6
Warning: Unable to copy the figure to the clipboard. Increasing the Java Heap Size
setting in the MATLAB preferences may help;
otherwise try saving to a file or choose a different clipboard format.
Error while evaluating ButtonPushedFcn for axes toolbar.
>> stalagmite_model
Error: File: stalagmite_model.m Line: 31 Column: 14
Assigning the function output to this expression is not supported.
This Error overcome by Replacing negative sign from calling function output
[inputs,f1_out(i)]=ga(@stalagmite,2);
Result- find Global maxima of stalagmite function with use of ganetic Algorithm in matlab
Refrences
www.google com.