Week 5 - Genetic Algorithm
AIM- write a code to optimize the stalagmite function and to calculate the global maxima of the functtion and explain the syntax of genetic algorithm (GA) in MATLAB
OBJECTIVE
- To write a code in MATLAB to optimize the stalagmite function and find the global maxima of the function.
- To explain the concept of genetic algorithm and syntax of GA used in programs.
- To plot the graphs of all stastical behaviour of GA and also of global maxima.
CONCEPT OF GA
GA is a way to find out the optimiation in which we study how to maximizeand minimize the partcular function and GA is a inbuilt function and it refers to basiacally minima of the particular function.
SYNTAX OF GA
EXPLANATION OF SYNTAX
x= ga( finds a local unconstrained minimum, fun,nvars)x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.
x = ga( finds a local minimum fun,nvars,A,b,Aeq,beq)x to fun, subject to the linear equalities Aeq*x = beq and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product Aeq*x as if x is transposed (Aeq*x').
subjects the minimization to the constraints defined in x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)nonlcon. The function nonlcon accepts x and returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and Ceq(x) = 0. (Set lb=[] and ub=[] if no bounds exist.)
or x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon) requires that the variables listed in x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)IntCon take integer values.
MATHEMATICAL EQUATION

%A program on Genetic Algorithm unbounded condition
clear all
close all
clc
%defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num = 150;
%creating 2D mesh
[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
% statistical behaviour of unbound condition input
tic
for i=1:num
[inputs,fopt(i)] = ga(@stalagmite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time = toc
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
title('Statistical behaviour of unbounded input')
plot3(xopt,yopt,-fopt,'marker','o','markersize',4,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('number of iterations')
ylabel('maximum points of the function')
% statistical behaviour of bound condition input
for i=1:num
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[1,1]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time = toc
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
title('Statistical behaviour of unbounded input')
plot3(xopt,yopt,-fopt,'marker','o','markersize',4,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('number of iterations')
ylabel('maximum points of the function')
%%statical condition poulation minima and maxima
options= optimoptions('ga');
options=optimoptions(options,'PopulationSize',500);
for i=1:num
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[1,1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time = toc
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
title('Statistical behaviour of unbounded input')
plot3(xopt,yopt,-fopt,'marker','o','markersize',4,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('number of iterations')
ylabel('maximum points of the function')