Week 4.1 - Genetic Algorithm
Aim :-
To write a code for optimise the stalagmite function and find the global maxima of the function.
Theory :-
Stalagmite Function :-
With the help of this function we are going to find out a mathematical model with the help of genetic algorithm
Genetic Algorithm :-
The genetic algorithm is a method for solving both constrained and unconstrained optimization problem that are baes on natural selection
Self Study:-


Given Stalagmite function :-

The 'ga' is use as to find local minima, but in our case we use 'ga' to find local maxima by using 'negative sign'
Function Defination program :-
function [f] =stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f1x = (sin((5.1*pi*x)+0.5))^6;
f1y = (sin((5.1*pi*y)+0.5))^6;
f2x = exp((-4*log(2)*(x-0.0667)^2)/0.64);
f2y = exp((-4*log(2)*(y-0.0667)^2)/0.64);
f = -(f1x*f1y*f2x*f2y);
end
Error Faced :-
1)Forget to add '*' in line No 9 & line No 100

Main Program to define stalagmite function and plot population :-
close all
clear all
clc
% No. of Irretaions
num_cases = 150;
x = linspace (0,0.6,150);
y = linspace (0,0.6,150);
[xx yy] = meshgrid (x,y);
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
surfc(x,y,-f)
shading interp
Plot No.1 :-

Study No.1 :- Study of 'ga'
syntax used and description :-
finds a local unconstrained minimum, x = ga(fun,nvars)x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.
Program for study No.1 :-
%study 1 Study of GA
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2)
xopt(i)=inputs(1)
yopt(i)=inputs(2)
end
%Plot graph
figure(1)
subplot(2,1,1);
surf(x,y,-f);
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('unbounded Input');
subplot(2,1,2);
plot(-fopt);
xlabel('iteration');
ylabel('function Minimum')
Plot No .2 :-

Observation :-
Value are unbounded and not getting an optimum value
Study No.2 :- Study of 'ga' with upper and lower bounds .
syntax used and description :-
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub) defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub. (Set Aeq=[] and beq=[] if no linear equalities exist.)
Program for study No.2 :-
%study 2 Study of GA with upper bonds and lower bounds in ga
tic
for i=1:num_cases
[input,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.4;0.4])
xopt(i)=input(1);
yopt(i)=input(2);
end
study_time2=toc
figure(2)
subplot(2,1,1);
surf(x,y,-f);
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('unbounded Input');
subplot(2,1,2);
plot(-fopt);
xlabel('iteration');
ylabel('function Minimum')
Plot No. :- 3

Observation :-
In second study it is observed that by using another syntax of 'ga' values are more optimized and in between upeer and lower bound
Study No. 3 :- study of 'ga' with incresing in pupulation size :-
syntax used and description :-
minimizes with the default optimization parameters replaced by values in x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions.
Program for Study No. 3 :-
%study 3 study of ga with incresing in pupulation size
options= optimoptions ('ga');
options=optimoptions(options,'PopulationSize',500);
tic
for i=1:num_cases
[inputs3, fopt3(i)]=ga(@stalagmite,2,[],[],[],[],[0,0],[0.6,0.6],[],[],options);
xopt(i)=input(1)
yopt(i)=input(2)
end
figure(3)
subplot(2,1,1);
surf(x,y,-f);
shading interp
hold on
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',5,'MarkerFaceColor','r')
title('unbounded Input');
subplot(2,1,2);
plot(-fopt);
xlabel('iteration');
ylabel('function Minimum')
Plot No. 4:-

Observation :-
Getting value of global Maxima.
Reference :-
1)https://www.youtube.com/watch?v=rZBH3Z8N1Dc
2)https://skill-lync.com/student-projects/Stalagmite-function-67841
3)https://skill-lync.com/student-projects/finding-the-global-maxima-of-a-stalagmite-function-and-optimizing-it-using-matlab-code