Optimization of the population using Genetic Algorithm
Aim : To optimize the stalagmite function and find the global maxima using genetic algorithm.
Genetic Algorithm (GA): It is method for soving and optimizing the problem based on natural selection as per darwin\'s proposed genetic algorithm, the GA repeatedly modifies the population based on induvidual solutions.It selects the induviduals at random from the current population to be parents and uses them to produce children for next generation so repeated successsive generations produces optimal solution based on the population and it can be used for any type of optimization problem.
GA performs three steps:
Selection: Selects the parent from the population
Cross Over:Combines two parents to form children for next generations
Mutation:Apply random changes to induvidual parents to form children
ga syntax performs these steps to optimize the problem.
[x,fval] = ga(func,nvars), fval returns, the value of the fitness function at x,the function handle is called in for the ga syntax with number of variables
The GA always perform the optimization and preducts minimum function and to find maximum the objective function is changed to negative, the procedure to find the maixima of objective function is shown below image.
The function for stalagmite is created with the given equations as shown below
clear all
close all
clc
%input array
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50;% defining number of iterations
%making a 2D array
[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)=stalg(input_vector);%stalg is function created and called
end
end
figure(1)
surfc(xx,yy,f)
shading interp
xlabel(\'X values\')
ylabel(\'Y values\')
title (\'stalagmite function\')
%study 1 un-bounded no lower and upper bounds
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalg,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study=toc
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
xlabel(\'X values\')
ylabel(\'Y values\')
title (\'unbounded iterations\')
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',4,\'markerfacecolor\',\'b\')
subplot(2,1,2)
xlabel(\'iterations\')
ylabel(\'function maximum\')
%study 2- bounded iterations with lower and upper bounds
tic
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalg,2,[],[],[],[],[0;0],[1;1]);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study_2=toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
xlabel(\'x values\')
ylabel(\'y values\')
title(\'bounded iterations\')
plot3(xopt,yopt,-fopt,\'marker\',\'o\',\'markersize\',4,\'markerfacecolor\',\'b\')
subplot(2,1,2)
plot(-fopt)
xlabel(\'itrs\')
ylabel(\'func max\')
%study 3 bounded iterations with increased population size
options=optimoptions(\'ga\')
options=optimoptions(options, \'PopulationSize\',400)
tic
for i=1:num_cases
[inputs, fopt(i)]=ga(@stalg,2,[],[],[],[],[0;0],[1;1],[],[],options);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
study3=toc
figure(4)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
xlabel(\'x values\')
ylabel(\'y values\')
title(\'bounded iterations_increased pop_size\')
plot3(xopt,yopt,fopt,\'marker\',\'o\',\'markersize\',4,\'markerfacecolor\',\'b\')
subplot(2,1,2)
plot(-fopt)
xlabel(\'itrs\')
ylabel(\'func max\')
function [S]=stalg(input_vector)
a=input_vector(1);
b=input_vector(2);
f1x=(sin((5.1*pi*input_vector(1))+0.5))^6;
f2x=(sin((5.1*pi*input_vector(2))+0.5))^6;
f1y=exp((-4*log(2)*((input_vector(1)-0.0667)^2))/0.64);
f2y=exp((-4*log(2)*((input_vector(2)-0.0667)^2))/0.64);
S=-(f1x*f2x*f1y*f2y);
The initial study is done just by calling the stalagmite function,Then the study is done without giving lower and upper bound, then next study is follwed by poviding lower and upper and upper bounds, the last study is done by providing the population size for e.g.say 400. The tic and toc command is inserted to find the running time of particular study
Initial plot for stalagmite function
Plot for unbounded iterations
Plot of bounded iterations
plot of bounded with increased population size