Challenge

Week 4.1 - Genetic Algorithm

AIM:

To write the code to calculate the global maxima of the stalagmite function and to explain the syntax for genetic algoritham(ga) in MATLAB.

OBJECTIVE:

To write a code in MATLAB to optimize the stalagmite function and find global maxima of the function. To explain the concept of genetic algoritham and the syntax for ga in MATLAB.

To plot the graph of all three studies and also for global maxima.

THEORY:

Stalagmite

A stalagmite is a type of rock that rises from the floor of a cave due to the accumulation of materials decomposed on the floor from ceiling drippings.That is why we are using the stalagmite as the name for the program due to its shape.

Genetic Algoritham(ga)

A genetic algoritham is an algoritham that imitates the process of natural selection.They help solve optimisation and search problems.Genetic algoritham are part of the bigger classof evolutionary algorithams.Genetec algoritham imitates natural biological process such as inheritance , mutation , selection and crossover.The concept of algoritham is a search technique often used compute science to find complex non-obvious solution to algorithamic optimisation and search problems.

SYNTAX OF GA IN MATLAB

The syntax of GA in MATLAB is as follows:

x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,IntCon,options)

Let us take the example of the following function to understand the syntac of GA:

" f(x) = 100*((x1)^2 - (x2)^2) + (1 - x(1)^2)^2 ".

The function is subjected to the following constraints:

  1. Linear Constraints:

x(1) + x(2) > = 1.

x(2) - x(1) = 5.

  1. Non-linear Constraints:

x(1)*x(2) + x(1) - x(2) + 1.5 <= 0. 10 - x(1)*x(2) <= 0.

  1. Bounds:

0 <= x(1) <= 1.

0 <= x(2) <= 2.

'x' is the variable to store the output of the Genetic Algorithm. 'fun' is the function handle to call the function.

'A' and 'b' are used to represent the linear inequalities of the the function, if any. Otherwise, they are

set as empty, '[]'.

As per the above constraints:

A = [1 1];

b =1;

'Aeq' and 'beq' are used to represent the equality constraints. As per the above constraints:

Aeq = [-1 1];

beq = 5;

'lb' and 'ub' are used to specify the upper and the lower bounds of the variables of the function. As per the above values:

lb = [0,0];

ub = [1,1];

'nonlcon' is a function that is specified using a function handle. A separate function code must be written as per the constraints of the function.

This function gives two arrays 'c' and 'Ceq' as its outputs.

'c' stores the values of the equality constraints. 'Ceq' stores the values of the inequality constraints.

'IntCon' is used to specify the Integer Constraint, such that the value of the function is an integer. 'options' is used to provide the values of the default parameters using named parameters, if it is required to replace the default parameters. This is done by using the function 'optimoptions'.

MATHEMATICAL EQUATION:

To find global maxima

f = (f1 ⋅ f2 ⋅ f3 ⋅ f4)

To find global minima

f = − (f1 ⋅ f2 ⋅ f3 ⋅ f4)

where

f1 = [sin(5.1πx + 0.5)]6

f2 = [sin(5.1πy + 0.5)]6

f3 = exp[ − 4 log(2)((x − 0.0667)2 /0.64 )]

f4 = exp[ − 4 log(2)((y − 0.0667)2 /0.64 )]

PROGRAM:

For plotting global maxima

clear all 
close all
clc

%defining the search space 
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

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

%evaluating the stalgamite 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
surfc(xx,yy,-f)
xlabel('x value')
ylabel('y value') 
shading interp
[inputs,fval]=ga(@stalagmite,2)

Function

function [f]=stalagmite(input_vector)

x=input_vector(1);
y=input_vector(2);
f1=(sin(5.1*pi*x+0.5))^6;
f2=(sin(5.1*pi*y+0.5))^6;
f3=exp(-4*log(2)*(x-0.0667)^2/0.64);
f4=exp(-4*log(2)*(y-0.0667)^2/0.64);

f=(f1*f2*f3*f4);
end

Here for ploting global maxima and finding the minimum of the function ga is used

clear all 
close all
clc
x=linspace(0,0.6,150)
y=linspace(0,0.6,150)
[xx,yy]=meshgrid(x,y)
num_cases=50
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
        tic
                
%study 1 - statistical behavior for
for i=1:num_cases
[inputs,fopt(i)]=ga(@stalagmite,2)
xopt(i)=inputs(1)
yopt(i)=inputs(2)
end
fopt=-fopt
study1_time=toc

%plotting1
figure(1)
subplot(2,1,1) 
surfc(x,y,-f)
shading interp
hold on
plot3(xopt,yopt,fopt,'marker','o','markersize',5,'markerfacecolor','g') 
title('Unbounded inputs')
subplot(2,1,2) 
plot(fopt)
xlabel('Iterations')
ylabel('function minimum')
tic
%study 2 -statistical behaviour

for i=1:num_cases
[inputs2,fopt2(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[1;1])
xopt2(i)=inputs2(1)
yopt2(i)=inputs2(2)
end
fopt2=-fopt2
study2_time=toc

%plotting2 figure(2)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
hold on
plot3(xopt2,yopt2,fopt2,'marker','o','markersize',3,'markerfacecolor','g') 
title('Bounded inputs')
subplot(2,1,2)
plot(fopt2)
xlabel('Iterations')
ylabel('function minimum')

%increrasing ga iteration
options=optimoptions('ga')
options=optimoptions(options,'populationsize',500) 
tic
for i=1:num_cases
 [inputs3,fopt3(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options)
 xopt3(i)=inputs3(1)
yopt3(i)=inputs3(2)
end
fopt3=-fopt3
study3_time=toc
%plotting3 
figure(3)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
hold on
plot3(xopt3,yopt3,fopt3,'marker','o','markersize',5,'markerfacecolor','g')
title('Bounded inputs')
subplot(2,1,2)
plot(fopt3)
xlabel('Iterations')
ylabel('function minimum')

OUTPUT:

GLOBAL Maxima

STUDY 1

STUDY 2

STUDY 3:

ERRORS AND CORRECTION:

This error happened due to the mistake of executing the function stalgmite and then renamed it as stalagmite

RESULT:

  • The code for global maxima and stalagmite function is done.
  • the concept of genetic algoritham and the syntax is done.
  • The graph for all three studies and global maxima are plotted.
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.