Optimization Technique Through Genetic Algorithm by Matlab
Aim: In this study we are focusing the optimization of a function.Optimization of a function is a method to get optimum value or we can a best value for that function.Example in a Industry,the product depends on various manufacturing processes like cutting operation time, grinding time,packing time etc. so the company focuse on appropriate selection of minimum time process to complete finished prouct.This minimum time focussed is here optimization of product.There are various approches to solve optimization but here we will use genetic algorithm technique.In this study we will take a function for optimization and then by using genetic algorithm syntex we will solve hat function in matlab.We will also see the affecting factors like population size,search space etc on optimization value.
Introduction: There are many complex engineering problem that are vary difficult to solve by other conventional optimization techniques.In recent years, genetic algorithms have received considerable attention regarding their potential as a novel optimization technique. Based on their simplicity, ease of operation, minimal requirements and parallel and global perspective[1] .The genetic algorithm is a random-based classical evolutionary algorithm. Here Random means that genetic algorithm will do random changes in current values to get a optimum solution.Genetic algorithm concept is basically based on Darwin's theory of evolution[2].
There are Four main characterstics on which this genetic algorithm depends.
1.Population size: Under this, we have a size of set of current solutions value.The population size depends on the nature of the problem, but typically contains several hundreds or thousands of possible solutions. Often, the initial population is generated randomly.
2.Fitness oriented function: This function shows which current solution value give better result than other.
3.Selection:Now algorithm will select best best solution value corresponding to result of fitnes function.But these solution value give local optimization solution (maxima or minima requred of a funcion).
4.Number of Generation :To get globally optimization solution we will create new generations.This new generation is based on two type of process (a)Cross over (b) mutation.
(a)cross over: A standard representation of each candidate solution is as an array of bits.So in cross over the two or different kind of candidate solution array combined to each other at there array bit place and form a new candidate solution value.It is like a new generation baby contain the some feature of there father and mother ,these feature can be the body part look like simillar to their parents.
(b)mutation: Under this The new solution value formed by self randomly re-arranging the place of a bit in array of a solution value. It is naturally different changes in features of new baby from their parents. It is like that the child does not look like completely to their parents, like as comparing their faces.
So by creating a new generation it will be a new population for focussing the fitness fuction and then simillar way genetic algorithm choose best fitted solution value and these selected values goes again for new generation population solution values.By this way it creats global optimized solution value.
Our function f, for optimization is:
Here optimization is based by maximzation of the function but genetic algorithm solve function for minimization as default.So we put negative sighn in function for getting maximization by geneic algorithm. To create simplicity looking of program coding first we make function coding and then we will call that function coding in main matlab programing coding.We will use genetic algorithm syntex . which solve problem internally, by consisting above four characteristics of genetic algorithm.
Genetic algorithm syntex: ga(@fit function,nvars,A,B,C,D,L.B,U.B,,nonlinear,options)
fit function=it is that function for which solution value should be satisfied
nvars=number of designed variables, here these are two solution value which are X and Y
Ax =B, A and B are equality constrained. These can be we make in matrix form when two or more these type of constrained available for given problem.
Cx <=D, C and D are inequality constrained
L.B = lower boundary U.B =upper boundary , Example: a<=x<=b , here a is lower boundary and b is upper boundary.
nonlinear=nonlinear means non linear constrained example: x1+x2^3 <= 75
Options=options is used for calling the other parameters such as here this was for generation size.It is also used for showing or display parameter of specific solver(genetic algorithm or fminsearch etc) parameter option like cross over fraction , fitness limit etc.
NOTE: 1.If given problem has greater symbol inequality or equality or non linear constrained then converted those greater than symbol to less than symbol constrained as mentioned in above constrained . Example: if it is given that CX `>=`2 then converted into less than sighn convention by negative sighn as -CX <= 2. So here -C is inequality constrained value instead of C [3].
2.If objective function is not subjected to any constraind value than leave that constrained value with empty square bracket [].
Function coding:
function f = ch_stalagmite(inputvector)
X=inputvector(1);
Y=inputvector(2);
f1=[sin(5.1*pi*X+0.5)].^6;%dont use product sighn * between sin andits value
f2=[sin(5.1*pi*Y+0.5)].^6;
f3=exp(-4*log(2)*((X-0.0667).^2/0.64));%here we can not use [] bracket instead of () and dot use product sighn * between exp and its value
f4=exp(-4*log(2)*((Y-0.0667).^2/0.64));
f=-(f1*f2*f3*f4);%negative is used to maximise the fuction during genetic algorithm because otherwise genetic algorithm takes function to solve for minimization as default
end
Programing Coding:
clear all
close all
clc
x=linspace(0,0.6,50)%linspace is used here for putting 50 values between 0 to 0.6
y=linspace(0,0.6,50)
[xx,yy]=meshgrid(x,y)
%now we will set point position for each value of xx matrix and yy matrix
%both points can positioned at same place . they are formation of sheet and that can be value for finding
%z or function value
for i=1:length(xx)
for j=1:length(yy)
inputvector(1)=xx(i,j);
inputvector(2)=yy(i,j);
f(i,j)=ch_stalagmite(inputvector);
end
end
LB=[0 0];
UB=[0.6 0.6];
num_cases=50;
options=optimoptions('ga')%
'option' is to open any here to create optimum specific solver option like crossover fraction,fitness limit,initial population etc we can leave this option in coding.we can change solver name like fminsearch
options=optimoptions('ga','PopulationSize',200);
%here weare inputing population size by opening genetic algorithm option
tic% tic and toc command for time estimation
for i=1:num_cases
[inputs_value,function_val]= ga(@ch_stalagmite,2,[],[],[],[],LB,UB,[],options);
%syntex of genetic algorithm is ga(@function,number of inputs here these are x and y,inequality constraints A from AX<=B and here A and B can be in matrix form,inequality constraints B from AX<=B,equality constraints C from CX = D,equality constraints D from CX = D,lower boundry , upper boundry,nonlinear constraints (it can be inquality and equality constraints,options/ where we input some extra options in algorithm like here population size)
xopt(i)=inputs_value(1);
yopt(i)=inputs_value(2);
fopt(i)=function_val
end
toc
f=-f
%we are taking -ve because to make poositive of negative function value f=-(f1*f2*f3*f4)
fopt=-fopt %we are taking -ve here also to make poositive because this fopt value is also from ffunction value f=-(f1*f2*f3*f4)
figure(1)
subplot(2,2,1)
hold on
surfc(xx,yy,f);%here we can use x,y,f instead of xx,yy,f because meshgrid is already generated
shading interp
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
%plot3 command is used for 3 dimension like in direction x,y and z direction but plot command is only for x,y plane direction
title('unbounded inputs')
subplot(2,2,3)
plot(fopt)
xlabel('iteration')
ylabel('function maximum')
Plots:
Output Result screenshot:
Discussion and Results: 1.By solving this given function, We get optimum function value 0.999999 which is closed to 1.0 at input values of x= 0.0668 and y=0.0668 .
2.We also find that by increasing population size and number of generation, the accuracy of optimum finding value increased by genetic algorithm but computation time increased.
Error Facing in Programming:
1.Symbol problem of using square bracket [] instead of small bracket () in fitting function.
2. symbol problem, using of small bracket instead of square bracket [] in genetic algorithm syntex.
Refrences
[1]Sivanandam, S. N., and S. N. Deepa. "Genetic algorithm optimization problems." Introduction to Genetic Algorithms. Springer, Berlin, Heidelberg, 2008. 165-209.
[2]https://www.kdnuggets.com/2018/03/introduction-optimization-with-genetic-algorithm.html
[3]https://www.youtube.com/watch?v=wiQOsH8C-uQ&t=1340s