Challenge

Week 4 - Genetic Algorithm

FINDING THE GLOBAL MAXIMA USING GENETIC ALGORITHM

AIM:

To find the global maxima of the function using Genetic Algorithm. Genetic algorithm is based on the evolution theory proposed by Charles Darwin. Genetic Algorithm provides the random inputs, and it will try to improve the results by creating the offspring from the parent values i.e inputs. Further the results will be improved by mutating the off springs.

CODE:

Main Program:

clear all
close all
clc

%Defining variables
x=linspace(0,1,100);
y=linspace(0,1,100);
num_cases=50;

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


%Evaluating the equation using X,Y values
for i=1:length(xx)
      for j=1:length(yy)
       inputs=[xx(i,j);yy(i,j)];
       fout(i,j)=stalagmite(inputs);
      end
end
    
%Genetic Algorithm
%Study 1
rng default
tic;
for k=1:num_cases
    [inputs, fopt_study1(k)]=ga(@stalagmite,2);
    xopt(k)=inputs(1);
    yopt(k)=inputs(2);
end
study1_time=toc;

figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,fout)
shading interp
plot3(xopt,yopt,fopt_study1,'marker','o','markersize',5,'markerfacecolor','r')
xlabel('X axis')
ylabel('Y axis')
zlabel('Z Axis')
subplot(2,1,2)
plot(fopt_study1)
xlabel('Iterations')
ylabel('Function Minimum')

%Study 2-with upper and lower bounds
rng default
tic
for k=1:num_cases
    [inputs, fopt_study2(k)]=ga(@stalagmite,2,[],[],[],[],[0.5,0.5],[1,1]);
    xopt(k)=inputs(1);
    yopt(k)=inputs(2);
end
study2_time=toc;

figure(2)
subplot(2,1,1)
hold on
surfc(xx,yy,fout)
shading interp
plot3(xopt,yopt,fopt_study2,'marker','o','markersize',5,'markerfacecolor','r')
xlabel('X axis')
ylabel('Y axis')
zlabel('Z Axis')
subplot(2,1,2)
plot(fopt_study2)
xlabel('Iterations')
ylabel('Function Minimum')
axis([0 50 -1.2 -0.5])

%Study 3-with population size
rng default
options=optimoptions('ga');
options=optimoptions(options,'populationsize',120);
tic
for k=1:num_cases
    [inputs, fopt_study3(k)]=ga(@stalagmite,2,[],[],[],[],[0.5,0.5],[1,1],[],[],options);
    xopt(k)=inputs(1);
    yopt(k)=inputs(2);
end
study3_time=toc;

figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,fout)
shading interp
plot3(xopt,yopt,fopt_study3,'marker','o','markersize',5,'markerfacecolor','r')
xlabel('X axis')
ylabel('Y axis')
zlabel('Z Axis')
subplot(2,1,2)
plot(fopt_study3)
xlabel('Iterations')
ylabel('Function Minimum')
axis([0 50 -1.2 0])

Sub-Program:

function [stalfunc]=stalagmite(inputs)
x=inputs(1);
y=inputs(2);
term1=(sin(5*pi*x+0.5)).^6;
term2=(sin(5*pi*y+0.5)).^6;
term3=exp(-4*log(2)*(((x-0.667).^2)/0.64));
term4=exp(-4*log(2)*(((y-0.667).^2)/0.64));
stalfunc=term1.*term2.*term3.*term4;
stalfunc=-stalfunc;
end

CODE EXPLANTION:

Main Program:

Line 1-3:

clear all

close all

clc

Resetting the MATLAB window.

Line 5-8:

%Defining variables

x=linspace(0,1,100);

y=linspace(0,1,100);

num_cases=50;

Defining variables for the stalagmite inputs.

Line 10-11:

%Creating a 2D mesh

[xx, yy]=meshgrid(x,y);

Creating a 2D mesh using the Column Matix and storing them in the variables xx and yy.

Line 14-20:

%Evaluating the equation using X,Y values

for i=1:length(xx)

for j=1:length(yy)

inputs=[xx(i,j);yy(i,j)];

fout(i,j)=stalagmite(inputs);

end

end

Inputs for the stalagmite function will be provided in using the two for loops shown above. Basically we’re passing all the values yy for each and every value of xx and storing the output of the function in variable fout.

Line 22-31:

%Genetic Algorithm

%Study 1

rng default

tic;

for k=1:num_cases

[inputs, fopt_study1(k)]=ga(@stalagmite,2);

xopt(k)=inputs(1);

yopt(k)=inputs(2);

end

study1_time=toc;

These set of commands were used to optimise global maxima of the stalagmite function which we have defined already. Syntax for the genetic algorithm is ga(@function,num_of_variables). Here our function is named as stalagmite function and the number of variables is 2. Once the ga is processed it returns the optimum value of the function and the corresponding x and y values. Here the x and y values will be provided by the Genetic algorithm itself. We have used rng default commands which tells the genetic algorithm to generate the same set of random numbers again and again. If rng default is not included in the program output of this same set of code will give different output. Here the tic and toc command is used to count the time taken for MATLAB to process the set of statements written between the tic and toc key words and the time is stored in the variable study_time1.

Line 33-45:

figure(1)

subplot(2,1,1)

hold on

surfc(xx,yy,fout)

shading interp

plot3(xopt,yopt,fopt_study1,'marker','o','markersize',5,'markerfacecolor','r')

xlabel('X axis')

ylabel('Y axis')

zlabel('Z Axis')

subplot(2,1,2)

plot(fopt_study1)

xlabel('Iterations')

ylabel('Function Minimum')

*Syntax Explanation:

Here we’re placing two plots in same window for that purpose we’re using the subplot command. subplot(m,n,p) this command divides a single plot window into a mxn window. ‘p’refers to the position of the plot in that figure window. Surf command is like the plot command instead of just plotting it will create a surface also. Shading interp will disable the mesh on the surface.

*Programming explanation:

We’re plotting 3 graphs using these lines.

i)Stalagmite function xx,yy and their results.

ii)Value of xopt, yopt, fopt_study1 in the same plot in which the stalagmite function is plotted. xopt, yopt, were the inputs for the stalagmite function provided by the genetic algorithm. Here we’re retrieving them as the output form the GA algorithm line to know the x,y values which the ga provided to the function.

iii)In the second plot we’re plotting the GA output against the number of iterations. To view how the output changes for every iteration.

Line 47-55:

%Study 2-with upper and lower bounds

rng default

tic

for k=1:num_cases

[inputs, fopt_study2(k)]=ga(@stalagmite,2,[],[],[],[],[0.5,0.5],[1,1]);

xopt(k)=inputs(1);

yopt(k)=inputs(2);

end

study2_time=toc;

Here the stalagmite function which we and used follows the syntax below. ga(@function,numberof variables,A,b,Aeq,beq,lb,ub)

A,b:-

Here A is the coefficient of the inequality equations in matrix form and b is the constrain limit.

For example we have two constrains as shown below

2x+4y<=5

x-3y>=2

First, we need to convert all the equation in such a way that the inequality equations have less than or equal to form.

2x+4y<=5

-x+3y<=-2

Now the A matrix is the coefficients of x and y values and b matrix is the constrain limits.

A= and b=[]. In MATLAB A will be given as [2 4;1 -3] and b as [5 -2].

Aeq,beq:-

It is similar to the A,b as said above but in this set of Linear constrains one inequality and another one will be equalities. Here the equality equation will be considered as the second equations and the Matrix input will be provided as said above.

lb,ub:

Here the lower bounds and upper bounds of the variables will be decided. Input will be provided in the format Lower bounds of x and y and the upper bounds of x and y. In this problem we have defined the lower bounds of the x and y values as 0.5 and the upper bounds of the x and y values as 1 which means the GA algorithms passes the x and y values between 0.5 to 1.

Line 57-69:

figure(2)

subplot(2,1,1)

hold on

surfc(xx,yy,fout)

shading interp

plot3(xopt,yopt,fopt_study2,'marker','o','markersize',5,'markerfacecolor','r')

xlabel('X axis')

ylabel('Y axis')

zlabel('Z Axis')

subplot(2,1,2)

plot(fopt_study2)

xlabel('Iterations')

ylabel('Function Minimum')

These set of commands were similar to the lines except that these lines will plot the graph for 2nd study which we have defined the ga algorithm with the lower and upper bounds.

Line 72-82:

%Study 3-with population size

rng default

options=optimoptions('ga');

options=optimoptions(options,'populationsize',120);

tic

for k=1:num_cases

[inputs, fopt_study3(k)]=ga(@stalagmite,2,[],[],[],[],[0.5,0.5],[1,1],[],[],options);

xopt(k)=inputs(1);

yopt(k)=inputs(2);

end

study3_time=toc;

The empty braces after the upper and lower bounds were to define the nonlinear constrains and the options will be defined for the GA algorithm.

In the options we have defined the population size as 120. Population is the set of the random inputs provided by the ga to the functions. By default, the initial population value will be 50 for the number of variables less than 5. We can be able to define the higher or the lower number of pop-ulations using the options command.

Line 84-96:

figure(3)

subplot(2,1,1)

hold on

surfc(xx,yy,fout)

shading interp

plot3(xopt,yopt,fopt_study3,'marker','o','markersize',5,'markerfacecolor','r')

xlabel('X axis')

ylabel('Y axis')

zlabel('Z Axis')

subplot(2,1,2)

plot(fopt_study3)

xlabel('Iterations')

ylabel('Function Minimum')

These set of statements were used to plot the function and the GA output.

Sub-Program:

function [stalfunc]=stalagmite(inputs)

x=inputs(1);

y=inputs(2);

term1=(sin(5*pi*x+0.5)).^6;

term2=(sin(5*pi*y+0.5)).^6;

term3=exp(-4*log(2)*(((x-0.667).^2)/0.64));

term4=exp(-4*log(2)*(((y-0.667).^2)/0.64));

stalfunc=term1.*term2.*term3.*term4;

stalfunc=-stalfunc;

end

In this function we’re evaluating the four terms and storing the product of the terms in the variable stalfunc. The genetic algorithm is intended to predict the least value of the function. Here the stalfunc is multiplied with with negative sign to invert the stalagmite function. So the peak is now has the least value on the plot so the ga can able to get the minimum value of the functions which is the peak value of the function.

Output:

1.Plot without any bounds and options:

2. Plot with bound of x and y values(0.5<x,y<1):

3.Plot with initial population size of 120:

ERRORS OCCURRED:

Not enough input arguments error. The command window is attached below.

Code popped out the error.

GA can be able to send only one input arguments but in the coding shown above I have provided the 2 input arguments so the GA popped out the error shown above.

REFRENCES:

* https://in.mathworks.com/help/gads/ga.html#d122e38381

* https://in.mathworks.com/help/gads/how-the-genetic-algorithm-works.html

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.