Challenge

Week 4.1 - Genetic Algorithm

Aim:

To create a code in MATLAB to optimise the stalagmite function and find the global maxima of the function, explain the concept of Genetic Algorithm and the syntax for GA in MATLAB.

Procedure:

  • Understand Genetic Algorithm, Optimization, Syntax of Genetic Algorithm
  • To derive Stalagmite expression using given data
  • Plot the graph for stalagmite function, unbounded or unconstrained condition & for constrained condition

Concept of Genetic Algorithm:

Genetic algorithm first discovered by John Holland, it is based on random sampling where the random input parameters are given to calculate the function based on the inputs provided It is based on Genetics and Natural Selection. Genetic algorithm is used to solve the optimal solutions to the problem, which is difficult to solve in real time. Genetic algorithm in MATLAB always minimize the function

Optimization:

Optimization refers to find the values of inputs to get the best output. Based on the different input parameters provided it can be analyse the minimize or maximize the particular function, it is executed iteratively untill the optimum solution is obtained.

Stalagmite Function:

It is the function where the model will generate the maxima & minima, that is maximum and minimum points, here the global maxima is the maximum point, which is our objective.To optimise the stalagmite function and to find the global maxima of function, the below coding is used in MATLAB.

clear all
close all
clc

% Defining search space
x = linspace(0,0.6,150); % Represents the x values
y = linspace(0,0.6,150); % Represents the y values
% Defining number of times to iterate
num_cases = 50;

% creating 2D mesh
[xx,yy] = meshgrid(x,y);

% Evaluating the stalagmite 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

% ga gives random values each time the code is executed
% the below plots are used to show how the random values are getting
% generated and we name it as study1

% study1: statistical behaviour
for i = 1:num_cases
   [inputs ,fopt(i)] = ga(@stalagmite,2);
   x_opt(i) = inputs(1);
   y_opt(i) = inputs(2);
end

%plotting the graphs
%using tic toc comments to calculate the time taken to simulate
tic
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(x_opt,y_opt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')

study1_time = toc

% study2: statistical behaviour with upper and lower bounds

for i = 1:num_cases
 [inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[1,1]);
x_opt(i) = inputs(1);
y_opt(i) = inputs(2);
end

%plotting the graphs
%using tic toc comments to calculate the time taken to simulate
tic
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(x_opt,y_opt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')

study2_time = toc

% study3: increasing the population
options = optimoptions('ga');
options = optimoptions(options,'populationsize',170);

for i = 1:num_cases
 [inputs, fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0,0],[1,1],[],[],options);
x_opt(i) = inputs(1);
y_opt(i) = inputs(2);
end

%plotting the graphs
%using tic toc comments to calculate the time taken to simulate
tic
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,f)
shading interp
plot3(x_opt,y_opt,fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Bounded inputs')
subplot(2,1,2)
plot(fopt)
xlabel('Iterations')
ylabel('Function minimum')

study3_time = toc

To get the stalagmite function, the below coding is used seperately in new tab as below.

function [f] = stalagmite(input_vector)

x = input_vector(1);
y = input_vector(2);
f1x = (sin(5.1*pi*x+0.5)).^6;
f1y = (sin(5.1*pi*y+0.5)).^6;
f2x = exp(-4*log(2)*(x-0.0667).^2/0.64);
f2y = exp(-4*log(2)*(y-0.0667).^2/0.64);

f = -f1x*f1y*f2x*f2y;

end

As we can see the above coding to get the results to optimise the stalagmite function & to get local maxima of function.

Study1 Result:

The output of study case1 shows the minimum and maximum values of stalagmite function that there is no bounds or constrains provided so the output values is different for each time we execute the program.

x = ga(fun,nvars) finds a local unconstrained minimum, fun,nvars is the dimension (number of design variables) of fun.

Study2 Result:

In the first case there was no bounds or constrains to the input parameters, to solve this issue study2 is used as the lower and upper values are bounded which is done by syntax as shown,

x = ga(fun,nvars,A,b,Aeq,beq)

which 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')

In the result file we can clearly see that the global maxima points has been shown in very few points when compared to the case study1.

Study3 Result:

The study case2 has few points of global maxima, hence in the study3 in addition to the bound initial population size is also given to get optimum maxima points. The default number of samplings has been initailly provided as 50, in study3 the population size is mentioned as 170 and the results as below

Result data for study3.

x = 0.068;

y = 0.072, this are the maxima values of stalagmite function.

Conclusion:

This is the procedure where we can clearly understand the Genetic Algorithm, Optimization, Function of stalagmite expression and use of upper & lower bounds to constrain the values, plotting the maxima values. Hence, Genetic Algorithm uses randam values and it helps to get the best possible output. Stalagmite function always gives the minima values but th epurpose of maxima values has been taken.

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.