Challenge

Week 4.1 - Genetic Algorithm

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

Governing Equation-

f(x,y)=f1x*f1y*f2x*f2y

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

Objective-

  • Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function.
  • Clearly expalin the concept of genetic algorithm in your own words and also explain the syntax for ga in MATLAB in your report.
  • Make sure that your code gives the same output, even if it is made to run several times.
  • Plot graphs for all 3 studies and for F maximum vs no. of iterations. Title and axes labels are a must, legends could be shown if necessary.

Theory-

Genetic Algorithm :
Genetic Algorithm (GA) is a branch of field of study called evolutionary comutation. It is based on evolution theory by Darwin. GA is used to optimize a given function to find its maximum or minimum value.

Basic components of a GA are-

1. Fitness function : It is the function that GA is trying to optimize.
2. A population of chromosome : It is the numerical values that represent all the solutions to the function. The selection operator
chooses some of the chromosomes (with replacement) for reproduction based on probability distribution. fitter the chromosome
more likely it is to be selected.
3. Cross over : This operator swaps in random a subsequence of two of the chosen chromosomes to create two offspring. Usually
each chromosome is converted into binary code and then cross over is done from one parent chromosome into another.
4. Mutation : Mutation operator randomly flips individual bits in the new chromosomes.


The GA syntax in Matlab is given by :


x = ga(fun,nvars,A,B.Aeq.Beq,LB,UB.nonlcon,intcon,options)
Where,
fun = objective function
nvars = number of variables
A and B = subject to linear inequalities of the form A*x <=B
Aeq and Beq = subject to linear equalities of the form A*x = B
LB = lower bound
UB = upper bound
nonlcon = non linear inequalities
intcon = integer constrained
options = helps to add contraint tolerance by increasing the population size


optimization-Optimization is the process of making something better(such as a design, system, or decision) as fully perfect,
functional, or effective as possible, this also helps us to find the best possible results under a set of constraints. This process is
executed again and again till the final result or the output is satisfactory. This includes maximizing the product, strength, reliability,efficiency, and utilization.

Deterministic method: In this method, the output of the model is fully determined by the parameter of the initial conditions. This uses specific rules from moving from one solution to another. let us understand with the help of an example:
stress analysis of wing is determined as the output result(stress value) is totally dependent on the initial condition (loading condition) and the parameters like material and forces acting on the component.
Stochastic method: This method is used for minimizing and maximizing an objective function, this possesses some inherent
randomness, it is a probabilistic approach.

MATLAB CODE

1) STALAGMITE FUNCTION

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

2.MAIN BODY OF THE CODE

clear all
close all
clc
%Defining our search space
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
[xx,yy] = meshgrid(x,y);
num_case = 50 ;

%calling 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

figure (1)
hold on
grid on
surfc(xx,yy,-f)
shading interp
title('stalagmite plot')
xlabel ('X')
ylabel('Y')

%Study 1 - Statistical behavior
tic
for i=1:num_case
[inputs, fopt1(i)] = ga(@stalagmite,2);
xopt1(i) = inputs(1);
yopt1(i) = inputs(2);
end

study1_time = toc

figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
title('Unbound inputs')
xlabel('X values')
ylabel('Y values')
plot3(xopt1,yopt1,fopt1,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt1)
xlabel('Iterations')
ylabel('Function maximum')

%Study 2 - Statistical behavior with upper and lower bounds
tic
for i=1:num_case
[inputs, fopt2(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
xopt2(i) = inputs(1);
yopt2(i) = inputs(2);
end

study2_time = toc
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
title('Bounded inputs')
xlabel('X values')
ylabel('Y values')
plot3(xopt2,yopt2,fopt2,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt2)
xlabel('Iterations')
ylabel('Function maximum')

%Study 3 - Increasing GA iterations

options = optimoptions('ga','PopulationSize',150);

tic
for i=1:num_case
[inputs, fopt3(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
xopt3(i) = inputs(1);
yopt3(i) = inputs(2);
end
study3_time = toc
figure(4)
subplot(2,1,1)
hold on
surfc(xx,yy,f)
shading interp
xlabel('X values')
ylabel('Y values')
plot3(xopt3,yopt3,fopt3,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(fopt3)
xlabel('Iterations')
ylabel('Function maximum')

OUTPUT GRAPH

1.STALAGMITE PLOT

2. STUDY-1

3.STUDY-2

4.STUDY-3

STUDY TIME FOR 3-CASES-

  • study1_time =2.9245
  • study2_time =3.4259
  • study3_time =6.2508

ERROR

  • Continue loop running without output
  • function inputs
  • syntax error

RESULT- Succesfully we create a matlb code for stalgmite function and also gote the stdy time.

STUDY TIME FOR 3-CASES-

  • study1_time =2.9245
  • study2_time =3.4259
  • study3_time =6.2508

References

https://skill-lync.com/knowledgebase/genetic-algorithm-explained

Genetic algorithm - Wikipedia

Introduction to Genetic Algorithms — Including Example Code | by Vijini Mallawaarachchi | Towards Data Science

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.