Week 4.1 - Genetic Algorithm
AIM :- to calculate the global maxima of a stalagmite function in MATLAB.
THEORY:-
A genetic algorithm is a search heuristic that is inspired by Charles Darwin's theory of natural evolution. This algorithm reflects the process of natural selection where the fittest individuals are selected for reproduction in order to produce offspring of the next generation.
Advantages/Benefits of Genetic Algorithm
The concept is easy to understand.
GA search from a population of points, not a single point.
GA use payoff (objective function) information, not derivatives.
GA supports multi-objective optimization.GA use probabilistic transition rules, not deterministic rules.
The basic process for a genetic algorithm is:
Initialization - Create an initial population. ...
Evaluation - Each member of the population is then evaluated and we calculate a 'fitness' for that individual. ...
Selection - We want to be constantly improving our populations overall fitness.
SOLUTION STEPS:-
1) 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. 3. For this in Matlab we are using a method called 'Optimization'.
2) First two input variables (x,y)were created using 1D array of 150 equally spaced values between 0 and 0.6 using linspace command and then two dimensional matrix of these variables were created using meshgrid
3) the resultsfrom stalagimite function was stored in another variable and plotted as surface usinf surf command in MATLAB.
4)"GA" function was used to calculate the maximum value of stalagimite function, but since "ga" command gives minimum value of function
5) then our code wasb made to iterate 50 times using ''for loop" to study the statistical behaviour of genetic algorithm,
MATLAB CODE:-
function[f] = stalagmite(input_vector)
x = input_vector(1);
y = input_vector(2);
f_1_x = (sin(5.1*pi*x+0.5))^6;
f_1_y = (sin(5.1*pi*x+0.5))^6;
f_2_x = exp(-4*log(2)*((x-0.0667)^2/0.64));
f_2_y = exp(-4*log(2)*((x-0.0667)^2/0.64));
[f] = -((f_1_x)*(f_2_x)*(f_1_y)*(f_2_y));
end
CASE 1:-
clear all
close all
clc
%inputs for search space
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
for a=1:num_cases
[inputs,fopt_1(a)]=ga(@stalagmite,2);
xopt(a)=inputs(1);
yopt(a)=inputs(2);
end
study1_time = toc
%plotting our data
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
colorbar
plot3(xopt,yopt,-fopt_1,'MarkerSize','o','markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt_1)
xlabel('iterations')
ylabel('Function maximum')
case 2:-
clear all
close all
clc
x=linspace(0,0.6,50);
y=linspace(0,0.6,50);
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 2 statistical behaviour with upper and lower boundaries
for b=1:num_cases
[inputs,fopt_1(a)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(b)=inputs(1);
yopt(b)=inputs(2);
end
study1_time = toc
%plotting our data
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
colorbar
plot3(xopt,yopt,-fopt_1,'MarkerSize','o','markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt_1)
xlabel('iterations')
ylabel('function maximum')
clear all
close all
clc
x=linspace(0,0.6,50);
y=linspace(0,0.6,50);
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
%incresing genetic algorithm iterations
option_1 = optimoptions('ga');
option_1 = optimoptions(option_1,'PopulationSize',300);
tic
for b=1:num_cases
[inputs,fopt_1(a)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
xopt(b)=inputs(1);
yopt(b)=inputs(2);
end
study1_time = toc
%plotting our data
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
colorbar
plot3(xopt,yopt,-fopt_1,'MarkerSize','o','markerfacecolor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt_1)
xlabel('iterations')
ylabel('function maximum')
conclusion:-
the GA repetedly modifoes a population of individual solution.over succesive generations ,the population "envloves"toward an optimal solution.if the population size is too large the processing time taken by matlab also increases,