Project

Genetic Algorithm

AIM- To Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.

INTRODUCTION-

Genetic Algorithms are optimization techniques used to solve non-linear and non-diffrential optimization problems they use concepts of evoluationary biology to search for a global minimum an optimization problem , the name genetic algorithm comes from the fact that mimicking from evolutionary biology techniques.

Genetic algorithms works by starting with initial generation of canditate solution that are tested against objective function.

Subsequent generations evolve from first generation through selection, crossover and mutation.

i.Selection means to retain the best performing parent from one generation to next .

Lets say we have parent1 and parent2 in the previous generation and these sre the variables in optimization problems,through selection then are used since they performed well in previous generation.

ii.Crossover in this we select comman similarities between diffrent parent variables and keep those same to create children vaiables in next generation.

iii. Mutation is a genetic operator used to maintain genetic diversity from one generation of a population of genetic algorithm to the next.

Software used- MatlabR2018a

Stalagmite function defined as follow-

SYNTAX OF "ga" IN MATLAB-

Study 1 - Statistical Behaviour :-

Syntax:-

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.

Study 2- Statistical Behaviour with upper an lower bounds :-

Syntax:-

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.)

Study 3 - increasing ga iterations :-

Syntax:-

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.

Stopping Conditions for the Algorithm-

The genetic algorithm uses the following conditions to determine when to stop:

  1. Generations — The algorithm stops when the number of generations reaches the value of Generations.

  2. Time limit — The algorithm stops after running for an amount of time in seconds equal to Time limit.

  3. Fitness limit — The algorithm stops when the value of the fitness function for the best point in the current population is less than or equal to Fitness limit.

  4. Stall generations — The algorithm stops when the average relative change in the fitness function value over Stall generations is less than Function tolerance.

  5. Stall time limit — The algorithm stops if there is no improvement in the objective function during an interval of time in seconds equal to Stall time limit.

  6. Stall test — The stall condition is either average change or geometric weighted. For geometric weighted, the weighting function is 1/2n, where n is the number of generations prior to the current. Both stall conditions apply to the relative change in the fitness function over Stall generations.

  7. Function tolerance — The algorithm runs until the average relative change in the fitness function value over Stall generations is less than Function tolerance.

  8. Constraint tolerance — The Constraint tolerance is not used as stopping criterion. It is used to determine the feasibility with respect to nonlinear constraints. Also, max(sqrt(eps),ConstraintTolerance) determines feasibility with respect to linear constraints.

The algorithm stops as soon as any one of these conditions is met.

Matlab Program-

Input main program

clear all
close all
clc 

x=linspace(0,0.6,150)
y=linspace(0,0.6,150)

%creating a 2 d array
[xx yy]=meshgrid(x,y)

% Getting outputs from the stalagmite function and storing them
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
%study 1
iterations = 50;
tic
for i=1:iterations
      [input1,f_val1(i)]=ga(@stalagmite,2);
      x1(i)=input1(1);
      y1(i)=input1(2);
end
time1 = toc;

% plotting unbound iteration case
figure(1);
subplot(2,1,1);
surfc(xx,yy,-f);
shading interp; %makes surface look more smoother
hold on
plot3(x1,y1,-f_val1,'marker','*');
title('Study1: unbound inputs values');
xlabel('x input');
ylabel('y input');
zlabel('Z Value');
subplot(2,1,2);
plot(-f_val1);
axis([0,50,0,1]);
xlabel('iterations');
ylabel('function minimum');
%study 2
tic
for i=1:iterations
      [input2,f_val2(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
      x2(i)=input2(1);
      y2(i)=input2(2);
end
time2 = toc;

% plotting bound iteration case
figure(2);
subplot(2,1,1);
surfc(xx,yy,-f);
shading interp; %makes the surface look more smoother
hold on;
plot3(x2,y2,-f_val2,'marker','*');
title('Study2: unbound inputs values');
xlabel('x input');
ylabel('y input');
zlabel('Z Value');
subplot(2,1,2);
plot(-f_val2);
axis([0,50,0,1]);
xlabel('iterations');
ylabel('z max 2');


% study 3 increasing the population size
% by doing so we are increasing the probability of the function to converge at global maximum
opti=optimoptions('ga');
opti=optimoptions(opti,'PopulationSize',350); %increasing the popolation size
tic;
for i=1:iterations
      [input3,f_val3(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],opti);
      x3(i)=input3(1);
      y3(i)=input3(2);
end
time3=toc;  %gives the time taken for study
time1;
time2;
time3;
x_optimum = x3(1);
y_optimum = y3(1);
z_optimum = -f_val3(1);
figure(3);
subplot(2,1,1);
surfc(xx,yy,-f); %creates a surface to visualise 3D
shading interp;
hold on;
plot3(x3,y3,-f_val3,'marker','*');
title('Study3: Bounded inputs with increased population size');
xlabel('x inputs');
ylabel('y inputs');
zlabel('Z Value');
subplot(2,1,2); plot(-f_val3) 
plot(-f_val3) %plotting -f vs number of iterations
xlabel('iterations')
ylabel('Z max 3');

Function program

function z =stalagmite(input)

x=input(1);
y=input(2);

fx_1 = (sin((5.1*pi.*x)+0.5)).^6;
fx_2 = exp((-4*log(2).*((x-0.0667).^2))./0.64);
fy_1 = (sin((5.1*pi.*y) + 0.5)).^6;
fy_2 = exp((-4*log(2).*((y-0.0667).^2))./0.64);

z =(-fx_1 .* fx_2 .* fy_1 .* fy_2);
end

Explanation

  1. Initially the function program named stalagmite is programed , the formulas for stalagmite function are mentioned above,inputs for the function x,y are obtained from the main program.
  2. Then the caliculated value of f(x,y) are stored in an array z, since ga caliculates only minima,-ve sign is given in order to get maxima of the function.
  3. Now in the main program initiall it is started with clear all, close all,clc which deletes all the previously assigned values and clears the screen.
  4. In order to find the values of x and y for stalagmite function we have definened from 0 to 0.6 using linspace command which searches for 150 random values between them,to run the program with some restricted search space we have given no of iterations as 50 .
  5. Since the values of x and y are stored in coloumn vectors by using linspace command , so inorder to conver them into a m x n form we used meshgrid command and for loop.
  6. The program is executed for three cases .
  7. In case 1 or study1 in which maximum value of the function is found out without any bounds and limits.
  8. Now in case 2 or study2 we are performing same as that of in study1 , but the only diffrence is we are restricting the outputs i.e bounding from 0 to 1 only which is mentioned in ga function.
  9. Time taken by each case is caliculated using the commands tic and toc which are mentioned at starting and ending of each process or case.
  10. shading interp command is used to obtain a smoth looking curve.
  11. Using subplot command two or more obtained graphs are arranged in a single figure.
  12. The title command the title of the plots are generated for each plot.
  13. X,Y,Z axis labels are generated using the xlable ,ylabel,zlabel commands.
  14. The limits of the axis are set using the command axix[].
  15. Using plot3 command the maxima points are plotted in 3D.
  16. Now for the case 3 or study3 we are increasing the population size which inturn increses the probablity of the function to find global maxima, the rest is same as that of other cases.

OUTPUTS-

fig1 plots illustrating unbounded input values case

fig 2 plots illustrating when upper and lower bound are given

fig 3 plot illustrating when the population size is increased

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.