Challenge

Week 5 - Genetic Algorithm

% Genetic Algorithm
% 1D search space
clear all
close all
clc
% Defining search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
Num_of_cases = 50;
% Creating a 2D mesh
[xx,yy] = meshgrid(x,y);
% Evaluating the Stalagmite Function
tic
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) = Stalagamite_Function(input_vector);
end
end
figure(1)
surfc(xx,yy,-f)
title('Stalagamite Function Representation')
shading interp
xlabel('x value')
ylabel('y value')
study_time = toc;
fprintf('\n Study time : %0.4f \n\n',study_time)



% Case 1 - Statistical behavior (Unbounded)
tic
for i = 1:Num_of_cases
[inputs,f_output(i)] = ga(@Stalagamite_Function, 2);
x_output(i) = inputs(1);
y_output(i) = inputs(2);
end
figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp
plot3(x_output,y_output,-f_output,'marker','o','markersize',5,'markerfacecolor','r')
title('Statistical behavior (Unbounded)')
subplot(2,1,2)
plot(-f_output)
xlabel('No. of Iterations');
ylabel('Function Maximum');
study_time1 = toc;
fprintf('\n Study time for case 1: %0.4f \n\n',study_time1)



% Case 2 - Statistical behavior (Bounded - upper and lower bounds)
tic
for i = 1:Num_of_cases
[inputs,f_output(i)] = ga(@Stalagamite_Function, 2,[],[],[],[],[0;0],[0.6;0.6]);
x_output(i) = inputs(1);
y_output(i) = inputs(2);
end
figure(3)
subplot(2,1,1)
hold on
surfc (xx,yy,-f)
shading interp
plot3(x_output,y_output,-f_output,'marker','o','markersize',5,'markerfacecolor','r')
title('Statistical behavior (Bounded - upper and lower bounds)')
subplot(2,1,2)
plot(-f_output)
xlabel('No. of Iterations');
ylabel('Function Maximum');
study_time2 = toc;
fprintf('\n Study time for case 2: %0.4f \n\n',study_time2)



% Case 3 - Increasing GA iterations (Bounded - increasing population)
tic
options=optimoptions('ga');
options=optimoptions(options,'PopulationSize',500);
for i = 1:Num_of_cases
[inputs,f_output(i)] = ga(@Stalagamite_Function,2,[],[],[],[],[0;0],[1;1],[],[],options);
x_output(i) = inputs(1);
y_output(i) = inputs(2);
end
figure(4)
subplot(2,1,1)
hold on
surfc (xx,yy,-f)
shading interp
plot3(x_output,y_output,-f_output,'marker','o','markersize',5,'markerfacecolor','r')
title('Increasing GA iterations (Bounded - increasing population)')
subplot(2,1,2)
plot(-f_output)
xlabel('No. of Iterations');
ylabel('Function Maximum');
study_time3 = toc;
fprintf('\n Study time for case 3: %0.4f \n\n',study_time3)




% Stalagamite Structure/Function

function [f] = Stalagamite_Function(input_vector)
    
    f1x = (sin(((5.1*pi).*input_vector(1))+0.5)).^6;
    f1y = (sin(((5.1*pi).*input_vector(2))+0.5)).^6;

    f2x = exp((((input_vector(1)-0.0667).^2)/0.64).*(-4*log(2)));
    f2y = exp((((input_vector(2)-0.0667).^2)/0.64).*(-4*log(2)));

    f=-f1x*f1y*f2x*f2y;
end

REPORT

Aim :

  • Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
  • Explain the concept of Genetic Algorithm in and also explain the syntax for GA in MATLAB.
  • Plot graphs for all studies and for Function maximum vs No. of iterations.

Theory :

Theory of Evolution and Genetic Algorithm -

A genetic algorithm is like a computer program inspired by Charles Darwin's theory of evolution. Its main goal is to find the best individuals in a group. It does this by creating new generations of offspring who inherit traits from their parents. If the parents are strong and successful, their children are likely to be even better. This process repeats until a generation with the strongest individuals is discovered.

The syntax used in this program are as follows -

  • x = ga(fun,nvars)-
    finds a local unconstrained minimum.
  • 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. When there is no linear inequalities we set Aeq and Beq as null vector.
  • x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon) or x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options)
    requires all the variables listed.

Where,

x - local unconstrained minimum.

fun - objective function.

nvars - the dimension (number of design variables) of fun.

A and b - A=[] and b=[], if no linear inequalities.

lb and ub - lower bound and upper bound. Lower bounds, and upper bound specified as a real vector or array of doubles.

Nonlcon - is a function that accepts a vector or array.

Intcon- integer variable vectors of positive integers.

options- Optimization options, it helps in constraint tolerance by increasing the population size.

Stalagmite Function -

Stalagmite is a type of rock formation that rises from the floor of a cave due to the accumulation of material deposited on the floor from ceiling drippings. Stalagmite function is the mathematical function which generates similar shapes to stalagmites and is used for finding out the local and global maxima and minima of that particular function.

Code:

Main Code -

Stalagmite Function -

Case 1 -

Case 2 -

Case 3 -

Errors :

No complicated errors occurred while running the MATLAB program.

Outputs/ plotted graphs :

Stalgamite Function -

Case 1 -

Case 2 -

Case 3 -

Conclusion :

  • The ga solver is an inbuilt algorithm in MATLAB for optimization problems.
  • Using the solver with bounded inputs gets more accurate results.

References :

  1. https://in.mathworks.com/help/gads/what-is-the-genetic-algorithm.html
  2. https://in.mathworks.com/help/gads/ga.html
  3. https://towardsdatascience.com/introduction-to-genetic-algorithms-including-example-code-e396e98d8bf3
  4. https://skill-lync.com/knowledgebase/genetic-algorithm-explained
  5. https://en.wikipedia.org/wiki/Stalagmite
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.