Challenge

Week 5 - Genetic Algorithm

AIM- 1) Try to understand the concept of genetic algorithm by referring to varius sources.

2) information regarding stalagmite function and ways to optimise it.

GOVERNING EQUATION -

term1=(sin((5.1*pi*a)+0.5))^6;

term2=exp(-4*log(2)*((a-0.0667)^2)/0.64);

term3=(sin((5.1*pi*b)+0.5))^6;

term4=exp((-4)*log(2)*((b-0.0667)^2)/0.64);

OBJECTIVE -

  • Writeen a code in MATLAB to optimise the stalagmite function and find the global maxima off the function.
  • Clearly explain the concept of genetic algorithm in own words and also explain the syntax for ga in MATLAB in your report.
  • Code gives the same output, even if it is made to run several times.
  • Ploted 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.

BODY OF CONTENT -

What is Genetic Algorithm-

The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural selection, the process that drives biological evolution. The genetic algorithm repeatedly modifies a population of individual solutions. At each step, the genetic algorithm selects individuals from the current population to be parents and uses them to produce the children for the next generation. Over successive generations, the population "evolves" toward an optimal solution. You can apply the genetic algorithm to solve a variety of optimisation problems that are not well suited for standard optimisation algorithms, including problems in which the objective function is discontinuous, non-differentiable, stochastic, or highly nonlinear. The genetic algorithm can address problems of mixed integer programming, where some components are restricted to be integer-valued.

Syntax for defining ‘ga’ (Genetic Algorithm) in matlab –

The ‘ga’ syntax in matlab is used to find the minimum of function using genetic algorithm. There are several ways to define the ‘ga’ function depending on the problem being solved. The syntax used for this study is shown below.

  • x = ga(fun,nvars)

where,

x = optimal solution/variables;

fun = Objective function;

nvars = Number of variables;

Sometimes ‘[x,fval]’ is used instead of ‘x’ where ‘fval’ gives objective function value at the solution

  • x = ga(fun,nvars,A,B,Aeq,Beq,lb,ub)

where,

A, B = Linear inequality constraints

Aeq, Beq = Linear equality constraints

If there are no linear equalities and inequalities, then A, B, Aeq, Beq are set to ‘[ ]’.

  • x = ga(fun,nvars,A,B,Aeq,Beq,lb,ub,nonlcon,IntCon,options)

where,

nonlcon = Nonlinear constraints

IntCon = Integer Values

options = Optimization options

If there are no nonlinear constraints and integer values, the nonlcon and IntCon are set to ‘[ ]’.

CONCLUSON -

For Case 1 -

Code- Given Below

For Plot-

Figure 1 & Figure 2

 %Genetic algorithm
clear all
close all
clc
% independent variacles
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
% matrix
[xx,yy]=meshgrid(x,y);
%Number of iterations
num_cases=50;
% starting the loop
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
% creates a three dimensional surface
figure(1)
surfc(xx,yy,-f)
%makes the surface look more smoother
shading interp  
xlabel('x value')
ylabel('y value')
title('Stalagmite Function')

%Case 1

tic
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite,2);
    xopt(i)= inputs(1);
    yopt(i)= inputs(2);
end
study_1=toc;
figure(2)
subplot(2,1,1)
hold on
surfc(x,y,-f)
title('Unbounded inputs')
xlabel('x value')
ylabel('y value')

shading interp
hold on

plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')




For Case 2 -

The function with upper and lower bound

Plot-

Figure 1 & Figure 3

%Genetic algorithm
clear all
close all
clc
% independent variacles
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
% matrix
[xx,yy]=meshgrid(x,y);
%Number of iterations
num_cases=50;
% starting the loop
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
% creates a three dimensional surface
figure(1)
surfc(xx,yy,-f)
shading interp  %makes the surface look more smoother
xlabel('x value')
ylabel('y value')
title('Stalagmite Function')


% case second with upper and lower bounds


tic;
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6]);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
study_2_time=toc;
figure(3)
subplot(2,1,1)
surfc(x,y,-f)
shading interp
title('Bounded inputs')
xlabel('x value')
ylabel('y value')

hold on

plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
subplot(2,1,2)
plot(-fopt)
xlabel('iterations')
ylabel('function maximum')

For Case 3-

To increase the population size.

Plot-

Figure 1 & Figure 4

%Genetic algorithm

clear all
close all
clc
% independent variacles
x=linspace(0,0.6,150);
y=linspace(0,0.6,150);
% matrix
[xx,yy]=meshgrid(x,y);
%Number of iterations
num_cases=50;
% starting the loop
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
% creates a three dimensional surface
figure(1)
surfc(xx,yy,-f)
% makes the surface look more smoother
shading interp 
xlabel('x value')
ylabel('y value')
title('Stalagmite Function')


%study 3 incresing the population size
% by doing so we are increasing the probability of the function
% converge at global maximum
options=optimoptions(@ga);
% for increasing the population size
options=optimoptions(@ga,'PopulationSize',500);  

tic
for i=1:num_cases
    [inputs,fopt(i)]=ga(@stalagmite,2,[],[],[],[],[0;0],[0.6;0.6],[],[],options);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end
%gives the time taken for study
study_3_time=toc;
figure(4)
subplot(2,1,1)
% it creates a surface to visualise 3D
surfc(x,y,-f)  
shading interp
title('bounded inputs with increased population size')
xlabel('x value')
ylabel('y value')

hold on

plot3(xopt,yopt,-fopt,'marker','o','markersize',4,'markerfacecolor','r')
subplot(2,1,2)
%plotting -f vs number of iterations
plot(-fopt)  
xlabel('iterations')
ylabel('function maximum')

Function Code-

function [f] = stalagmite(input_vector)

a=input_vector(1);
b=input_vector(2);

term1=(sin((5.1*pi*a)+0.5))^6;
term2=exp(-4*log(2)*((a-0.0667)^2)/0.64);
term3=(sin((5.1*pi*b)+0.5))^6;
term4=exp((-4)*log(2)*((b-0.0667)^2)/0.64);

[f]=-(term1*term2*term3*term4);   %inverts the objective function 
end

CONCLUSION-

Found the conclusion and the plots for the all 3 cases, by using online sources and Matlab documents on genetic algorithm

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.