Challenge

Week 4.1 - Genetic Algorithm

AIM: To write a code in MATLAB to optimize stalagmite function and find the global maxima using genetic algorithm.

DESCRIPTION:

GENETIC ALGORITHM: Genetic algorithm makes use of optimization along with tchniques available for optimization along with genetic algorithm syntax.

Optimization is a decision making process by which a optimum solution is obtained with the help of Genetic algorithm.For example like there are certain cases like for Air foil needs to provide proper lift at different angles, so for it be providing a proper lift the drag force should be minimum. If we go on changing the parameters manually and doing the calcualtions it becomes too difficult to calcualte and is time consuming which doesn't result a proper output. So, for this to outcome we make use of optimization which reduces the calculations and gives and optimum results. Genetic algorithm carries the process in three steps

  1. Selection rules: where the individuals are selected called as parents, who contibute to the next coming generation.
  2. Crossover rules: where two parents combine to form children for next generation
  3. Mutation rules: where random changes to individual parents to form children.

In matlab G.A is an inbuilt function which performs optimization for finding the minimum/optimum value.So, here the main objective is to modify the stalagmite function such that the genetic algorithm optimizes the data and gives the minimized/optimized value.

SYNTAX FOR GENETIC ALGORITHM:

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(fun,nvars) finds a local unconstrained minimum, x, to the objective function, fun. nvars is the dimension (number of design variables) of fun
  • x = ga(fun,nvars,A,b) finds a local minimum x to fun, subject to the linear inequalities A*x ≤ b. ga evaluates the matrix product A*x as if x is transposed (A*x').
  • x = ga(fun,nvars,A,b,Aeq,beq) finds a local minimum x to fun, subject to the linear equalities Aeq*x = beq and A*x ≤ b. (Set A=[] and b=[] if no linear inequalities exist.) ga evaluates the matrix product Aeq*x as if x is transposed (Aeq*x').
  • 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.)
  • x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon) subjects the minimization to the constraints defined in nonlcon. The function nonlcon accepts x and returns vectors C and Ceq, representing the nonlinear inequalities and equalities respectively. ga minimizes the fun such that C(x) ≤ 0 and Ceq(x) = 0. (Set lb=[] and ub=[] if no bounds exist.)
  • 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.
  • x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon) or x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options) requires that the variables listed in intcon take integer values.

Here,

x = local minimum point

fun = objective function

nvars = number of variables used

Aeq & Beq = They are used if the inequa;ity Aeq*x = Beq exists in the function

A & B = they are used if the inequality A*x<=B exists in the function

lb = It is lower bound limit of the function

ub = It is upper bound limit of the function

nonlcon = The function nonlcon accepts x and returns vectors C and Ceq, representing the non linear inequalities and equalities respectively

IntCon = Takes integer values

options = This argument allows add extra parameters like increasing population size. The options argument is created by the command 'otimoptions'

STALAGMITE FUNCTION :

In order to find the best global maximas of the function we have 3 study cases for the stalagmite function

1. Study case1 : Unbounded i.e inputs are not subjected to any limits

2. Study case2 : Bounded i.e a set of lower and upper bounds on design variables

3. Study case3 : Bounded i.e a set of lower and upper bounds on the design variables with increase in population size

Global Maxima = -(Fopt)

Global Minima = +(Fopt)

PROGRAM FOR STALAGMITE FUNCTION:

function [ f] = stalagmite(input_vector)
a = input_vector(1);
b = input_vector(2);
f1 = (sin((5.1*pi*a)+0.5))^6;
f2 = (sin((5.1*pi*b)+0.5))^6;
f3 = exp((-4*log(2)*((a-0.0667)^2))/(0.64));
f4 = exp((-4*log(2)*((b-0.0667)^2))/(0.64));
f = (f1*f2*f3*f4);
end
PROGRAM FOR GENETIC ALGORITHM ALONG WITH STALAGMITE FUNCTION:
clear all
close all
clc
% Defining Search space
x = linspace(0,0.6,150); % Giving suface area of plot in x direction
y = linspace(0,0.6,150); % Giving surface area of plot in y direction
num_cases = 50; % number of times the GA runs
% Creating 2D mesh
[xx,yy] = meshgrid(x,y);
% Evaluating the stalagmite function generates the plot
for i = 1:length(xx)
for j = 1:length(yy)
input_vector(1) = xx(i,j); %for each vector of x&y creating an input vector (1row,2col)
input_vector(2) = yy(i,j);
f(i,j) = stalagmite(input_vector); %function
end
end
%Plot 1
figure(1)
surf(xx,yy,f) %plot to vizualise the function
xlabel('X-axis')
ylabel('Y-axis')
shading interp % removes the grid lines on the plot
title('STALAGMITE FUNCTION');
%Study case1 i.e study of Genetic algorithm unbounded
tic
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2); % calling GA and passing inputs 2 i.e x&y
xopt(i) = inputs(1); %storing x values
yopt(i) = inputs(2); %storing y values
end
study_time = toc; %calculating how long does it take to study function
figure(1)
subplot(2,1,1) %creating sub-plots to get in one window
hold on
surf(xx,yy,f) % to create surface
shading interp
title('UNBOUNDED')
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'MarkerFaceColor','r');
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('function Minimum')
%Study case2 i.e study of Genetic algorithm with upper&lower bounds
tic
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]); % calling GA and passing inputs 2 i.e x&y
xopt(i) = inputs(1); %storing x values
yopt(i) = inputs(2); %storing y values
end
study_time = toc; %calculating how long does it take to study function
figure(2)
subplot(2,1,1) %creating sub-plots to get in one window
hold on
surf(xx,yy,f) % to create surface
shading interp
title('BOUNDED')
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'MarkerFaceColor','r');
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('function Minimum')
%Study case3 i.e increasing GA population
tic
for i = 1:num_cases
[inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[]); % calling GA and passing inputs 2 i.e x&y
xopt(i) = inputs(1); %storing x values
yopt(i) = inputs(2); %storing y values
end
study_time = toc; %calculating how long does it take to study function
figure(3)
subplot(2,1,1) %creating sub-plots to get in one window
hold on
surf(xx,yy,f) % to create surface
shading interp
title('BOUNDED')
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'MarkerFaceColor','r');
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('function Minimum')

OUTPUTS:

figure1

stalagmite plot with global maximas , global minimas, local maximas and local minimas are generated in the plot

figure2

for the 50 iterations the statistical behavior of GA is generated. The time taken is 12.006 seconds. But we are not satisfied with more peaks in the iterations vs Funstion minimum.

figure3

Here due to defining the upper and lower boundary the peaks are optimized to some extent but still we are not satisfied with the result.

figure4

Here increasing the population size and allocating the upper and lower bounds the peaks are optimized to a larger extent. Time taken here is 48.0065 seconds.

CONCLUSION

Hence, Code in MATLAB to optimize the stalagmite function and to find the Global maxima of function using a genetic Algorithm is generatted successfully.

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.