Challenge

Week 4 - Genetic Algorithm

Aim: Code in MATLAB to optimize the stelgamite and find the Global Maxima of the function

Genetic Algorithm : A Gentic Algorithm(GA) is a method for solving both constrained and unconstrained optimization problems based on natural selection process that mimics biological evoluion.

You can apply the GA to solve problems that are not well suited for standard optimization algorthim, including problems in which the objective function is discontinous, nondifferentiable etc

Synatx for GA in MATLAB :

x = ga(fun,nvars)

x = ga(fun,nvars,A,b)

x = ga(fun,nvars,A,b,Aeq,beq)

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

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

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

x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon)

x = ga(fun,nvars,A,b,[],[],lb,ub,nonlcon,IntCon,options)

x = ga(problem)

[x,fval] = ga(___)

[x,fval,exitflag,output] = ga(___)

[x,fval,exitflag,output,population,scores] = ga(___)

Main Coding :

clear all
close all
clc

% Defining the search space OR range
x = linspace(0,0.6,150);
y = linspace(0,0.6,150);
%Total no. of Iterations
n = 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_func(input_vector);
    end
end

%Study 1: statistical behaviour
tic
for i=1:n
    [inputs,fopt(i)]=ga(@stalagmite,2);
    xopt(i)=inputs(1);
    yopt(i)=inputs(2);
end

study_1_time=toc
figure(1)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
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')

%Study 2: statistical behaviour with bounds

for k = 1:n
[inputs2 , fopt2(k)] = ga(@stalagmite_func, 2,[],[],[],[],[0;0],[1;1]);
xopt2(k) = inputs2(1);
yopt2(k) = inputs2(2);
end

figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
shading interp

plot3(xopt2,yopt2,-fopt2,'marker','o','markerfacecolor','r')
xlabel('X values')
ylabel('Y values')
title('Statistical Behaviour - with upper and lower bound')
figure(4)
subplot(2,1,2)
plot(-fopt2)
xlabel('Iterations')
ylabel('fopt')
title('Statistical Behaviour - with upper and lower bound')

%Study 3 increasing GA iterations

options= optimoptions('ga');
options= optimoptions(options,'PopulationSize',500);
tic

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

study3_time= toc;
figure(3)
subplot(2,1,1)
hold on
surfc(xx,yy,-f)
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')

Stalagamite function :

function [f] = stalagmite_func(input_vector)

         x = input_vector(1);
         y = input_vector(2);
         term1 = (sin((5.1*pi*x )+ 0.5))^6;
         term2 = (sin((5.1*pi*y) + 0.5))^6;
         term3 = exp(-4*log(2)*((x-0.0667)^2)/0.64);
         term4 = exp(-4*log(2)*((y-0.0667)^2)/0.64);
         f = -(term1*term2*term3*term4);

end

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.