% GENETIC ALGORITHM : 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.
% GA procedure involves four stages (1) Initilization :generates its random population of defined size and from that population the code evaluates the fitness of the members of the population according to which it forms new population based on no. of iterations performed. (2) Selection :to select the fittest individuals and let them pass their genes to the next generation (3)
% Cross over :a random point is selected while mating a pair of parents to
% generate offspring.(4) Mutation : diversity among the population and stop premature convergence.
% Find minimum of function using genetic algorithmcollapse all in page
% Syntax
% x = ga(fun,nvars)
% x = ga(fun,nvars,A,b)
% x = ga(fun,nvars,A,b,Aeq,beq)
% x = ga(fun,nvars,A,b,Aeq,beq,lb,ub)
% x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon)
% x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options)
% x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon)
% x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options)
% x = ga(problem)
% [x,fval] = ga(___)
% [x,fval,exitflag,output] = ga(___)
% [x,fval,exitflag,output,population,scores] = ga(___)
% Creating function for stalgmite function
% STALGMITE 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.
% stalgmite function has four major equations by which a resultant is
% calculated [ equations will be attached under]
function f=stalagmite(input_vector)
x=input_vector(1);
y=input_vector(2);
f1=(sin(5.1*pi*x+0.5))^6;
f2=(sin(5.1*pi*y+0.5))^6;
f3=exp((-4*log(2)*(x-0.06677)^2)/0.64);
f4=exp((-4*log(2)*(y-0.06677)^2)/0.64);
f=-(f1*f2*f3*f4);
end
clear all
close all
clc
%defining scope of search
% creating the mesh from the two vectors which will be acting as a search space to find the maxima of the function.
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
num_cases=50;% it is the number of itreation for which the function will evaluate the fitness
% creating a 2D mesh
[xx yy]=meshgrid(x,y);
%Evaluating the stalgmite 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)=stalagmite(input_vector);
end
end
surf(xx,yy,f) % reating its surface through ‘surfc’ command which will also provide the contour of the generated surface.
xlabel('x value');
ylabel('y value');
%To remove the lines produced by the mesh grid we are using shading interp
shading interp
[inputs,fval]=ga(@stalagmite,2); % calculating the values of fitness function at each point of the mesh grid formed.
% CASE STUDY 1: For Unbounded conditions
% x = ga(fun,nvars) finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun.
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2);
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
subplot(2,1,1)
hold on
surfc(x,y,-f) % Comand used for generating surface
shading interp
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',6,'MarkerFaceColor','r')
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations');
ylabel('Function max');
% CASE STUDY 2: Appllying Statistical conditions
% implementation of upper and lower bounds
% x = ga(fun,nvars,A,b,Aeq,beq,lb,ub) defines a set of lower and upper bounds on the design variables, x, so that a solution is found in the range lb ≤ x ≤ ub. (Set Aeq=[] and beq=[] if no linear equalities exist.)
% what we are doing here is optimizing search operation on GA under
% specified limits or boundries unlike above first case where no boundary
% conditions are defined.
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
% This will calculate the optimized value of the function
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',6,'MarkerFaceColor','r')
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations');
ylabel('Function max');
% CASE STUDY 3: By increasing GA Iterations.
% x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,options) minimizes with the default optimization parameters replaced by values in options. (Set nonlcon=[] if no nonlinear constraints exist.) Create options using optimoptions.
options=optimoptions('ga') % ‘optimoptions’ command to increase the size of population.working on the large set of numbers and which will be led to increase the possibilities of finding the better fitness of the function.
options=optimoptions(options,'PopulationSize',500);
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options); % analysis will take time as we have increased the population size.
xopt(i)=inputs(1);
yopt(i)=inputs(2);
end
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
shading interp
plot3(xopt,yopt,-fopt,'marker','o','MarkerSize',6,'MarkerFaceColor','r')
title('bounded inputs with population size')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations');
ylabel('Function max');
% Errors
% Not enough input arguments : argumets provided to input vectors
% GA requires global optimization tool: Downloaded it and run program
% again.
% Optimization terminated: average change in the fitness value less than options.FunctionTolerance.
% References
% https://tech-qa.com/how-does-matlab-use-genetic-algorithm-for-optimization/
% https://in.mathworks.com/matlabcentral/answers/745867-how-to-use-genetic-algorithm-ga-in-matlab
% https://en.wikipedia.org/wiki/Genetic_algorithm
% https://stackoverflow.com/questions/8956533/find-the-minimum-of-y-xx-using-genetic-algorithm-in-matlab
% https://in.mathworks.com/help/gads/ga.html#d123e47582