Week 4.1 - Genetic Algorithm
GENETIC ALGORITHM:- A genetic algorithm is a method for solving both constrained and unconstrained optimization problems based on natural selection process that minics bilogical evalution. The algorithm reptedly modifies a population of oindividual solutions.
OPTIMIZATION TOOL BOX:- optimization tool box provides functions for finding parameters that minimize or maximize objectives while asatisfying constraints you can define your optimization problem with function matrices or by specifying varible expression.
GLOBAL MAXIMA:- A global maximum is a point where the function value is greater than at all the feasible points
local maxima:- A local maxima of a function is apoint where the function value is smaller than near by points.
STALGNITE FUNCTION:- A stalagmite function generally refers to the function with which we are generating values over a stalagmite kind of surface for finding out the local and global maxima and minima of that particular given function.
1. STALAGNITE CODE IN MATLAB:-
function f = stalagnite(a)
x = a(1);
y = a(2);
p= (sin(5.1*pi*x+0.5))^6;
q = (sin(5.1*pi*y+0.5))^6;
r = exp(-4*log(2)*(x-0.0667)^2/0.64);
s = exp(-4*log(2)*(y-0.0667)^2/0.64);
f = -p*q*r*s;
end
2. get_stalagnite code in mat lab:-
function f = get_stalagnite(x,y)
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) = stalagnite(input_vector);
end
end
3. stalagnimite function code in matlab:-
clear all
close all
clc
% defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
% creating a 2Dimensional 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) = stalagnite(input_vector);
end
end
surfc(xx,yy,f)
xlabel('x value')
ylabel('y value')
shading interp
[inputs,fval] = ga(@stalagnite,2);
1. general code screen shorts:-





In mat lab we have solving three study cases:-
main program to solve the three study cases:-
% program for a genetic algorithm
clear all
close all
clc
% defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
num_case = 60;
% creating a 2Dimensional 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) = stalagnite(input_vector);
end
end
% study1- statical behaviour
tic
for i = 1:num_case
[inputs,fopt(i)] = ga(@stalagnite,2);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study1_time = toc
figure(1)
subplot(2,1,1)
surfc(x,y,f)
shading interp
hold on
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('function maxima')
% study2- satistical behaviour with upper and lower bounds
tic
for i = 1:num_case
[inputs,fopt(i)]= ga(@stalagnite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study2_time = toc
figure(2)
subplot(2,1,1)
surfc(x,y,f)
shading interp
hold on
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('function maxima')
% study3 increasing GA Iterations
options = optimoptions('ga');
options = optimoptions(options,'populationsize',150);
tic
for i = 1:num_case
[inputs,fopt(i)] = ga(@stalagnite,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i) = inputs(1);
yopt(i) = inputs(2);
end
study3_time = toc
figure(3)
subplot(2,1,1)
surfc(x,y,f)
shading interp
hold on
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('function maxima')
main program code screen shorts:-



study case_2 screenshorts of mat lab:-


study3_time matlab code screen shorts:-



1.study_time for first case = 4.894
2. study_time for second case = 6.2872
3. study_time for third case = 11.72