Genetic Algorithm
GENETIC ALGORITHM
Introduction
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.
Genetic algorithm syntax
[inputs,output]=ga(@ff,nv,[A],[b],[Aeq],[beq],[lb],[ub],[ncf],[ivi],opt)
where
ff - fitness function
nv - number of variables
[A],[b] - Linear inequalities
[Aeq],[beq] - Linear equalities
[lb],[ub] - Lower bound and upper bound
[ncf] - Non linear constraint function
[ivi] - Integer variable indices
opt - Options
Code for genetic algorithm
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)
inputvector(1)=xx(i,j);
inputvector(2)=yy(i,j);
f(i,j)=stalagmitee(inputvector);
end
end
figure(1)
surf(xx,yy,-f)
shading interp
xlabel('X values')
ylabel('Y values')
zlabel('Z values')
title('Stalagmite Function')
%study1
tic
for i=1:num_cases
[xval_yval,fval(i)]=ga(@stalagmitee,2);
xval(i)=xval_yval(1);
yval(i)=xval_yval(2);
end
study1_time=toc
figure(2)
subplot(2,1,1)
hold on
surf(xx,yy,-f)
shading interp
xlabel('X values')
ylabel('Y values')
title('Unbounded inputs')
plot3(xval,yval,-fval,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fval)
xlabel('Iterations')
ylabel('Function max')
%study2
tic
for i=1:num_cases
[xval_yval,fval(i)]=ga(@stalagmitee,2,[],[],[],[],[0;0],[1;1]);
xval(i)=xval_yval(1);
yval(i)=xval_yval(2);
end
studytime2=toc
figure(3)
subplot(2,1,1)
hold on
surf(xx,yy,-f)
shading interp
xlabel('X values')
ylabel('Y values')
title('Bounded inputs')
plot3(xval,yval,-fval,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fval)
xlabel('Iterations')
ylabel('Function max')
%study3
options=optimoptions('ga')
options=optimoptions(options,'PopulationSize',450)
tic
for i=1:num_cases
[xval_yval,fval(i)]=ga(@stalagmitee,2,[],[],[],[],[0;0],[1;1],[],[],options);
xval(i)=xval_yval(1);
yval(i)=xval_yval(2);
end
studytime3=toc
figure(4)
subplot(2,1,1)
hold on
surf(xx,yy,-f)
shading interp
xlabel('X values')
ylabel('Y values')
title('Bounded inputs- with increase in Population Size')
plot3(xval,yval,-fval,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fval)
xlabel('Iterations')
ylabel('Function max')
gmax=-fval
Code for fitness function
function [o]=stalagmitee(inputvector)
k=inputvector(1);
l=inputvector(2);
a=(sin((5.1*pi*k)+0.5))^6;
b=exp(-4*log(2)*((k-.0667)^2/0.64));
c=(sin((5.1*pi*l)+0.5))^6;
d=exp(-4*log(2)*((l-.0667)^2/0.64));
o=-a*b*c*d;
end
Coding procedure
- Stalagmitee function is first created from given data.
- The output of stalagmitee function is made negative because we need to find global maxima.
- The stalagmitee function takes input from [xx yy] mesh grid.
- Using surf command graph is generated.
- Inorder to find global maxima we use the command ga.
- Study-1 yields global maxima value and the co-ordinates at which this maxima occurs. But the value of this maxima changes everytime we run the code. To avoid this we use iterations and we assign 50 to a variable called num_case.
- To make the accurate and consistent, we give upper and lower bounds of x and y co-ordinates. This is discussed in Study-2.
- To get the exact result, we use the command optimoptions and we increase the population to 450. This is showcased in Study-3.
Graphs and Result
- Stalagmite function
- Study-1 Genetic algorithm with unbounded inputs
- Study-2 Genetic algorithm with bounded inputs
- Study-3 Genetic algorithm with increase in population size
Mistakes made
1. Not included Negative sign in f value of study-1 as a result the graph was pointing in negative direction.
Inference
Thus after the last study the output (global maximum) has been made constant by increasing the number of iterations. The value of global maximum of stalagmite function has been found to be 1.
Reference