Challenge

Week 4 - Genetic Algorithm

  • AIM:- OPTIMISE THE STALAGMITE FUNCTION AND FIND GLOBAL MAXIMA USING MATLAB
  • THEORY:- Optimisation is the process of finding out the optimal solution of any funtion i.e finding out the maximum or minimum value form all the solution.Here,for optimisation of stalagmite function we are gonna use genetic algorithms which is based on the Theory of natural selection and evolution given by Charls Darwin which say that how the population of all the living organism evaluate,survive and change their genetic structure according to environment condition and only the best fit genetic is mutate to the next generation. Similarly, using the same concept of genetic algorithms we will find the best optimum sulution among all population.Here,the term population refers to solutions of stalagmite function and the best fit population for mutation refers to optimal solution.
  • SYNTAX OF GA IN MATLAB:- The ga systax in matlab is used to find the minimum of the function using genetic algorithms.

Syntax:- x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intCon,options)

where x is variable which store the output of ga syntax

func is the calling the function for genetic algorithms

nvars is the dimention of function i.e no of design variables of function

A and b used when no linear inqualities exist

Aeq and beq used when no linear equality exist

lb and ub denotes the lower and uper boundary of variables

nonlcon specified an another fuction.It gives two array which contains constrains of function. 'c' contains values of equality constrains and 'Ceq' contains values inequality constrains

intCon specify the interger constraints

option used to minimzes with the default optimization parameters replaced by value in options.It create options using optimoptions.

  • FUNCTION CODING:- The stalagmite function of which we are going to optimise is

so the programming for stalagmite function will be

function [f] = stalagmite(input_vector)
x1 = input_vector(1)
y1 = input_vector(2)
f1x = (sin((5.1*pi*x1+0.5))^6;
f1y = (sin((5.1*pi*y1)+0.5))^6;
f2x = exp((-4*(log(2))*(x1-0.0667).^2/0.64);
f2y = exp((-4*(log(2))*(y1-0.0667).^2/0.64);

% (-) sign for finding global maxima

f = -((f1x)*(f2x)*(f1y)*(f2y); 
end
  • MAXIMIZATION CODING:- The syntax ga is only evaluate the minima of function but here we have to find the maxima so to find maxima we can find the minima of the same function with negative sign that will same maxima for that function.
% maximization of stalagmite function

clear all 
close all
clc

x = linspace(0,1,150);
y = linspace(0,1,150);
n_case = 50;

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

for i = 1:length(xx)
    for j = 1:length(yy)
      input(1) = xx(i,j);
      input(2) = yy(i,j);
      f(i,j) = stalagmite(input);

surfc(xx,yy,f);

shading intrp

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

end
end

RESULT OF THIS PROGRAMMING WILL BE

  • EXPLANATION OF PROGRAMMING:- We will start our progamming by using clear all,close all and clc command to clear the all area and for input variables will use linspace command and to represent different cases value giving the name n_case. After input the variables to create the 2D array of it will use meshgrid command.So, now to find the plot use surfc command the grid lines from the plot use shading interp and finally to find maxima of function using gentic algorithms usee ga command.

Now to optimize and study the statistical behaviour of stalagmite function by many of iteration using genetic algorithms programming will be

% maximization of stalagmite function

clear all 
close all
clc

x = linspace(0,1,150);
y = linspace(0,1,150);
num_case = 50;

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

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 behaviour 
for i = 1:num_case
    [inputs,fopt(i)] = ga(@stalagmite,2);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2)
end
study1_time = toc;

%plotting 
figure(1)
subplot(2,1,1)
hold on
surfc(x,y,-f)
hold on
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r');
title('unbounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')


tic
%study 2 - statistical behaviour with upper and lower bound
for i = 1:num_case
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1]);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2)
end
study2_time = toc;

%plotting 
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
hold on
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r');
title('bounded inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')



%study 3 - increasing ga iterations
options = optimoptions ('ga');
options = optimoptions(options,'populationsize',280);

tic
for i = 1:num_case
    [inputs,fopt(i)] = ga(@stalagmite,2,[],[],[],[],[0;0],[1;1],[],[],options);
    xopt(i) = inputs(1);
    yopt(i) = inputs(2)
end
study3_time = toc;

%plotting 
figure(3)
subplot(2,1,1)
hold on
surfc(x,y,-f)
hold on
shading interp
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r');
title('bounded inputs with modified population size')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')

Here, we can do lots of iteration as much as we want for best optimization. In above we taken the different different condition for itertation and optimization and to plot it clarly use subplot command also so in our first iteration we are doing simple optimisation without any bounded input, the first iteration almost take 98 sec and result of it is

Then again done second iteration by giving bounded input result of 2nd iteration will be

as a result of 2nd study we can see that value of maximum narrowing it comes betweeb 0.5 to 1 from 0 to 1.

Again in third study is done by modifying the population size with bounded inputs then iteration result we can see is

So, now we can see that the value of range of maxima after 3rd iteration is between 0.85 to 1 which looks far optimise than first iteration.

  • ERROR :- Only one errors come to coding the stalagmite function. I was doing mistake to using paranthesis so after getting popup by matlab corrected it
  • CONCLUSION:- We can do lots of iteration as much as we want by changing the inputs boundation to getting more accurate optimasation.
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.