Project

Genetic Algorithm (using MATLAB)

Aim: To write a code in MATLAB to optimise the stalagmite function and to compute the global maxima of the function.

Software used: Octave

Introduction:

Genetic Algorithms (GAs) fall into the category of evolutionary algorithms with working principle based on the mechanics of natural genetics. It is a method fo solving both constrained and unconstrained optimization problems which are based on a natural selection process that mimics biological evolution.

CODES:
Stalagmite function;
function[f]=stalagmite(input_vector)
    x=input_vector(1);
    y=input_vector(2);
    
    a=(sin((5.1*pi*x)+0.5))^6;
    b=(sin((5.1*pi*y)+0.5))^6;
    c=exp((-4*log(2)*((x-0.0667)^2))/(0.64));
    d=exp((-4*log(2)*((y-0.0667)^2))/(0.64));
    f=-(a*b*c*d);
end

Main program:

clear all
close all
clc

x=linspace(0,0.6,150);
y=linspace(0,0.6,150);

numcase=50;
f =stalagmite(x,y);

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

%Evaluating a stalagmite funcion
for i=1:length(xx)
    for j=length(yy)
        input_vector(1)=xx(i,j);
        input_vector(2)=yy(i,j);
        f(i,j)=stalagmite(input_vector);
    end
end

%Study 1- Statistical Behavior
tic
for i=1:numcase
    [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(xx,yy,-f)
shading interp
ylabel('Y Values')
xlabel('X Values')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Unbounded Inputs')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function Maximum')
saveas(figure(1),'ga1.jpg')

%Study 2-Statistical Behavior with upper and lower bounds
tic
for i=1:numcase
    [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(xx,yy,-f)
shading interp
ylabel('Y Values')
xlabel('X Values')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Statistical Behavior with upper and lower bounds')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function Maximum')
saveas(figure(2),'ga2.jpg')

%Study 3-Increasing GA Iterations
options=optimoptions(@ga);
options=optimoptions(options,'populationsize',300);
tic
for i=1:numcase
    [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(xx,yy,-f)
shading interp
ylabel('Y Values')
xlabel('X Values')
plot3(xopt,yopt,-fopt,'marker','o','markersize',5,'markerfacecolor','r')
title('Statistical Behavior with increasing GA iterations')
subplot(2,1,2)
plot(-fopt)
xlabel('Iterations')
ylabel('Function Maximum')
saveas(figure(3),'ga3.jpg')

RESULT:

The genetic algorithm program is executed and hence the output is achieved.

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.