Week 4.1 - Genetic Algorithm
AIM:Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
Clearly expalin the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
Make sure that your code gives the same output, even if it is made to run several times.
Plot graphs for all 3 studies and for F maximum vs no. of iterations. Title and axes labels are a must, legends could be shown if necessary.
GOVERNING EUATIONS:

GENETIC ALGORITHM:
A genetic algorithm (GA) is a method for solving both constrained and unconstrained optimization problems based on a natural selection process that mimics biological evolution. The algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm randomly selects individuals from the current population and uses them as parents to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution.
You can apply the genetic algorithm to solve problems that are not well suited for standard optimization algorithms, including problems in which the objective function is discontinuous, nondifferentiable, stochastic, or highly nonlinear.
The genetic algorithm differs from a classical, derivative-based, optimization algorithm in two main ways, as summarized in the following table.
| Classical Algorithm | Genetic Algorithm |
|---|---|
| Generates a single point at each iteration. The sequence of points approaches an optimal solution. | Generates a population of points at each iteration. The best point in the population approaches an optimal solution. |
| Selects the next point in the sequence by a deterministic computation. | Selects the next population by computation which uses random number generators. |
SYNTAX (GA):
Syntax
Description
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,Aeq,beq,lb,ub,nonlcon,intcon) requires that the variables listed in x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options)intcon take integer values.
Note
When there are integer constraints, ga does not accept nonlinear equality constraints, only nonlinear inequality constraints.
BODY: function code
function [F] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
term1=(sin((5.1*pi*x)+0.5))^6;
term2=exp((-4)*log(2)*((x-0.0667).^2)/0.64);
term3=(sin((5.1*pi*y)+0.5))^6;
term4=exp((-4)*log(2)*((y-0.0667).^2)/0.64);
F = -(term1*term2*term3*term4);
end
MAIN CODE:
clear all
close all
clc
x= linspace(0,0.6,150);
y= linspace(0,0.6,150);
num_cases=50
[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
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
figure(1)
subplot(2,1,1);
hold on
surfc (x,y,-f)
shading interp
plot3 (xopt,yopt,-fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','r');
title('unbounded inputs');
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')
tic
% study 2 statistical behaviour wuth 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
figure(2)
subplot(2,1,1);
hold on
surfc (x,y,-f)
shading interp
plot3 (xopt,yopt,-fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','r');
title('bounded inputs');
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')
% study 3 increasing GA iterations
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',350);
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
figure(3)
subplot(2,1,1);
hold on
surfc (x,y,-f)
shading interp
plot3 (xopt,yopt,-fopt,'Marker','o','MarkerSize',5,'MarkerFaceColor','r');
title('unbounded inputs');
subplot(2,1,2)
plot(fopt)
xlabel('iterations')
ylabel('function minimum')
RESULTS:STUDY1:

STUDY2:

STUDY3:
workspace:
References:https://in.mathworks.com/discovery/genetic-algorithm.html
https://in.mathworks.com/help/gads/ga.html
https://skill-lync.com/student-projects/week-4-genetic-algorithm-401
Conclusion:Genetic algorithm code is done to find global maxima.