Week 4.1 - Genetic Algorithm
- AIM:- To Optimise the stalagmite function and find the global maxima of the function.
- OBJECTIVE:-
1) To study the gentic Alogrithm and stalagmite function.
2) To write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
3) To plot the graphs based on the studies for gentic Alogrithm .
- OPTIMISATION:- Optimisation is a desicion making process, it helps to find best results.

- What is Gentics Alogrithm:- The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution.

a) POPULATION:- This is the first phase of the process where an initial population is selected. The initial population is simply a set of individuals which are potential solution to the problem we want to solve. It could actually be made of of thousands or even hundreds of thousands of individuals that may have been randomly selected or selected based on some specified criteria.

b) SELECTION:- Selection is simple the process of selecting the portion of the population that would pass into the next generation. The selection process is based on the fitness criteria from the preceding phase.

c) CROSS-OVER:- It is a process of taking two parents producing the child from interchanging the genes,they are many ways to do cross-over opertaion.

d) MUTATION:- Mutation is a random change in the chromosome of the new offspring. This change occurs with a little probability. The benefit of this phase is that if tends to prevent the algorithm from converging in a local minimum.

- SYNTAX:-
x=ga(func,nvars,A,B,Aeq,Beq,LB,UB,nonlcon,intcon,options)
# func = objective function.
# nvars = number of variables.
# A & B = subject to inequalities of the form A*x<=B.
# Aeq & Beq = subject to linear equalities of the form A*x=B.
# LB = lower bond.
# UB = upper bond.
# nonlcon = non linear inequalities .
# intcon = integrated constraints.
# options = helps to add constraints tolerance by increasing population size.
- MATLAB PROGRAM CODE FOR STALAGMITE FUNCTION:-
function [f]=stalagmite(input_vector)
x=input_vector(1) ;
y=input_vector(2) ;
f1=(sin(5.1*pi*x+0.5))^6;
f2=(sin(5.1*pi*y+0.5))^6;
f3=exp(-4*log(2)*(x-0.0667)^2/0.64);
f4=exp(-4*log(2)*(y-0.0667)^2/0.64);
f=-(f1*f2*f3*f4);
end
- MATLAB PROGRAM CODE FOR GA:-
clear all
close all
clc
% defining our search space
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
% creating 2d diamesional mesh
[xx,yy]=meshgrid(x,y);
% evaluting 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
num_cases=50;
tic
% study 1- stastical behaviour
for i = 1:num_cases
[inputs, fopt1(i)] = ga(@stalagmite,2);
xopt1(i) = inputs(1);
yopt1(i) = inputs(2);
end
study1_time = toc
figure(1)
subplot(2,1,1)
hold on
surfc(x, y, -f)
shading interp
plot3(xopt1,yopt1,-fopt1,'color','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt1)
xlabel('Interations')
ylabel('function minimum')
% in this iteration behaviour of alogrithm studied by not defining the limits.
tic
%study 2 - stastical behaviour - with upper and lower bonds
for i = 1:num_cases
[inputs2, fopt2(i)] = ga(@stalagmite,2,[],[],[],[],[0:0],[1:1]);
xopt2(i) = inputs2(1);
yopt2(i) = inputs2(2);
end
study2_time = toc
figure(2)
subplot(2,1,1)
hold on
surfc(x, y, -f)
shading interp
plot3(xopt2, yopt2, -fopt2,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt2)
xlabel('Interations')
ylabel('function minimum')

% lower and upper bond are defined so as to know within global maxima lies.
% study 3 increasing GA iterations
options = optimoptions('ga');
options = optimoptions(options,'populationsize', 170)
tic
for i = 1:num_cases
[inputs3, fopt3(i)] = ga(@stalagmite,2,[],[],[],[],[0:0],[1:1],[],[],options);
xopt3(i) = inputs3(1);
yopt3(i) = inputs3(2);
end
study3_time = toc
figure(3)
subplot(2,1,1)
hold on
surfc(x, y, -f)
shading interp
plot3(xopt3,yopt3,-fopt3,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt3)
xlabel('Interations')
ylabel('function minimum')

% polpulation size increased so as to converge th global maxima.